Docstoc

fc

Document Sample
fc Powered By Docstoc
					Forward
Chaining
in Prolog
% add(P) adds P to DB and triggers forward chaining rules.
add(P) :- clause(P,true), !.
add(P) :-                                      FC Core
 dbug("Adding ~p.~n",[P]),
 assert(P),
 foreach(ifAdded(P,Actions), call(Actions)).

% remove(P) removes P from DB, triggering ifRemoved
  rules.
remove(P) :-
 dbug("Removing ~p.~n",[P]),
 retract(P),
 foreach(ifRemoved(P,Actions), call(Actions)).
         Mapping rules into triggers
% A=>B adds forward chaining rule to execute B whenever the facts
% in A have all been added to the DB.
% LHS is a conjunction
((P1,P2)=>Q) :- !, (P1=>(P2=>Q)).
% LHS is a disjunction
((P1;P2)=>Q) :- !, (P1=>Q), (P2=>Q).
% LHS is atomic. trigger already in DB?
(P=>Q) :- ifAdded(P,Q), !.
% LHS is atomic. Add trigger to DB
(P=>Q) :-
 assert(ifAdded(P,Q)),
 foreach(clause(P,true),fcDo(Q)).
               If-removed rules
% A=/>B adds ifRemoved rule to execute B whenever the
% assertions in A have all been removed from the database.


((P1,P2)=/>Q) :- !, (P1=/>(P2=/>Q)).

((P1;P2)=/>Q) :- !, (P1=/>Q), (P2=/>Q).

(P=/>Q) :- assert(ifRemoved(P,Q)).
(=>X) :- add(X).
% FCTEST                               A test   file
:- spouse(P1,P2) => add(spouse(P2,P1)).
:- spouse(P1,P2) =/> remove(spouse(P2,P1)).
:- => spouse(adam,eve)).
:- spouse(X,Y), female(X) => add(wife(X,Y)).
:- => female(eve).
:- female(X), male(X) =>
   add(contradiction(female(X), male(X))).
:- contradiction(P,Q) =>
   format(“CONTRADICTION: ~w and ~w.\n”, [P,Q]).
| ?- [fctest].
Adding spouse(adam,eve).
Adding spouse(eve,adam).
                                                          Fc in action
yes
                                          | ?- add(c(1)).
| ?- listing(ifAdded).
                                          Adding c(1).
ifAdded(spouse(A,B), add(spouse(B,A))).
                                          Adding d(1).
ifAdded(a(A), (b(A),c(A)=>add(d(A)))).    Yes
yes                                       | ?- b(X) => add(foo(X)), add(bar(X)).
| ?- add(b(1)).                           Adding foo(1).
Adding b(1).                              Adding bar(1).
yes                                       yes
| ?- add(a(1)).                           | ?- listing(ifAdded).
Adding a(1).                              ifAdded(spouse(A,B), add(spouse(B,A))).
yes                                       ifAdded(a(A), (b(A),c(A)=>add(d(A)))).
                                          ifAdded(b(1), (c(1)=>add(d(1)))).
| ?- listing(ifAdded).
                                          ifAdded(c(1), add(d(1))).
ifAdded(spouse(A,B), add(spouse(B,A))).
                                          ifAdded(b(A), (add(foo(A)),add(bar(A)))).
ifAdded(a(A), (b(A),c(A)=>add(d(A)))).
                                          yes
ifAdded(b(1), (c(1)=>add(d(1)))).
ifAdded(c(1), add(d(1))).
yes

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:8/30/2012
language:English
pages:6