% Изкуствен интелект, Факултет по математика и информатика, СУ "св. Кл. Охридски"

%--------------------------------------------------------------%

%   Matching semantic nets                                     %

%--------------------------------------------------------------%



% is_a hierarchy



isa(square,rectangle).

isa(rectangle,4-side).

isa(trapezoid,4-side).

isa(parallelogram,4-side).

isa(circle,oval).

isa(ellipse,oval).

isa(triangle,polygon).

isa(4-side,polygon).

isa(polygon,figure).

isa(oval,figure).



% Objects represented as facts in the format:

% object(Name,Parts,IsaRelations,MustRelations,MustNotRelations



object(arch,[a,b,c],

            [isa(a,4-side),isa(b,4-side),isa(c,polygon)],

            [support(a,c),support(b,c)],

            [touch(a,b)]).



% Test patterns



test1([X,Y,Z],

      [isa(X,rectangle),isa(Y,rectangle),isa(Z,triangle)],

      [support(X,Z),support(Y,Z)]).



test2([X,Y,Z],

      [isa(X,rectangle),isa(Y,rectangle),isa(Z,triangle)],

      [support(X,Z),support(Y,Z),touch(X,Y)]).





% Sample runs

% ?- match([X,Y,Z],

%          [isa(X,rectangle),isa(Y,rectangle),isa(Z,triangle)],

%          [support(X,Z),support(Y,Z)],

%          Name).

% Name=arch



% ?- test1(Parts,Isa,Rels), match(Parts,Isa,Rels,Name).

% Name=arch



%---------------------------------------------------------------

match(Parts,Isa1,Rels,Name) :-

    object(Name,Parts,Isa2,Must,MustNot),

    match_isa(Isa1,Isa2),

    subset(Must,Rels),

    \+ intersect(Rels,MustNot).



match_isa([],[]).

match_isa([isa(X,P)|T],[isa(X,Q)|V]) :-

    parent(P,Q),

    match_isa(T,V).



subset([],_).

subset([X|T],Y) :-

    member(X,Y),

    subset(T,Y).



intersect(L1,L2) :-

    member(X,L1),

    member(X,L2).



member(X,[X|_]).

member(X,[_|T]) :-

    member(X,T).



parent(X,X).

parent(X,Z) :-

    isa(X,Y),

    parent(Y,Z).

