Please download the following files containing simple knowledge bases about a family:
Both files have corresponding content:
In addition, files contain a single rule defining the child relation.
After downloading the files, they have to be loaded into corresponding Knowledge Bases (KB):
(load family_en.clp)
If everything was loaded correctly, you should a feedback similar to:
Defining deffacts: initial-facts Defining defrule: child +j TRUE
The TRUE is the most important part of this feedback. If FALSE appears, it means that loading failed.
[family].
The succesful loading should display something similar to:
% family compiled 0.01 sec, 9,232 bytes true.
Notice the true again.
To view the contents of the KB, enter:
(reset) (facts)
listing.
CLIPS uses the forward-chaining. The inference is implemented as a loop, that checks what rules can be executed based on the current state of the KB. Then the found rules are executed and the cycle repeats. The loop ends when it fails to find a rule to execute. The input of the forward-chaining algorithm is the set of facts in the knowledge base.
(agenda)
(run)
In Prolog we use backward-chaining and the process is inverted. The input of this algorithm is target — fact that we want to infer. We may say that this a goal-oriented approach. Let's ask:
woman(X).
With the ;
key, we can continue to list all the possible answers of the inference. If you want to interrupt, press „enter”.
mother(Mom, Child).
father(dariusz, Child).
father(Father, _).
father(dariusz, Son), man(Son).
Both files contain a rule defines who/what is a child.
CLIPS failed to start this rule (previous point). In Prolog, we can query using this rule:
child(X, Y).
In order to run the child rule, we have to define:
(defrule parent (or (mother ?x ?y) (father ?x ?y)) => (assert (parent ?x ?y)) )
parent(X, Y) :- father(X, Y). parent(X, Y) :- mother(X, Y).
It is worth noting that in the case of Prolog, we define two rules. One can assume that there is a conjuction (OR) between these two rules.
(clear)
to clear the fact base)We can try now to supplement the current KB with new rules, that define other relationships:
Knowing that the concepts brother is defined as follow:
(defrule brother (siblings ?x ?y) (man ?x) => (assert (brother ?x ?y)) )
brother(X, Y) :- siblings(X, Y), man(X).
Define the following rules:
cousins(X,Y), parent(R,X), parent(R, Y)
, it should return false
(defrule widower ... => (assert (widower ?x)) (printout t ?x " is widower" crlf) )
. Do you get the expected result?
Since CLIPS doesn't correctly find the widowers, it is necessary to track the execution of the program:
Allows you to track operations on facts: add, delete, change of the value.
(watch rules widower)
(reset)
(run)
(facts)
Allows tracking of rules: firing, activation.
(unwatch all) (watch rules marriage widow parent)
(reset)
(run)
Prolog allows you to follow the program. To do so, you set the so-called. trace point.
Trace points:
trace/0
- sets the trace point on all predicates, eg:?- trace, woman(K), parent(K, _).
trace(something/1, -all)
trace(something, call +)
debugging/0
lists all the set trace points.debug/0
predicate activates debug mode.nodebug/0
disactivates debug mode.Check following code:
?- trace(parent). [debug] ?- trace(mother). [debug] ?- trace(father) [debug] ?- parent(dariusz, tomasz). [debug] ?- nodebug.