Professional Documents
Culture Documents
Rule Based Programming: 3rd Year, 2nd Semester
Rule Based Programming: 3rd Year, 2nd Semester
Alpha network
f1 = one f1 = two
(defrule first
(one ?x 1 ? ?)
f3 = 1 f2 = f3 f3 = 1
(two ?x ?x)
=>
… f2 = 2
)
(defrule second
(one 2 1 ?x ?y)
(two ?y 1)
=>
…
)
Root node
Beta network
f1 = one f1 = two
(defrule first
(one ?x 1 ? ?)
f3 = 1 f2 = f3 f3 = 1
(two ?x ?x)
=>
… f2 = 2
)
(defrule second
(one 2 1 ?x ?y)
left f2 = right f2 left f5 = right f2
(two ?y 1)
=>
… “first” activated “second”
) activated
Root node
Working memory n1 n2
f1 = one f1 = two
Fact 1: (one 1 1 2 2)
Fact 2: (one 1 1 2 1)
Fact 3: (one 2 1 3 2) n3 n4 n5
Fact 4: (one 3 1 3 2) f3 = 1 f2 = f3 f3 = 1
Fact 5: (two 1 1)
Fact 6: (two 2 2)
n6
f2 = 2
n1: Facts 1, 2, 3, 4
n2: Facts 5, 6
n3: Facts 1, 2, 3, 4
n4: Facts 5, 6 n7 n8
n5: Fact 5 left f2 = right f2 left f5 = right f2
n6: Fact 3
n7: Facts (1,5), (2,5), (3, 5), (4, 5)
n8: - “first” activated “second”
stored: 4, 6, 10, 12, 13, 14, 4x2 activated
Consideration for writing rules
● Place stricter patterns first (less chance to get to check the others)
● Place patterns over fewer facts first (fewer checks to get to the others)
● Place patterns over emerging facts last (adding new facts will trigger less checks)
(defrule good
(alpha ?x ?y&:(> ?y ?x) 5)
(beta ? $?)
)
(defrule bad
(beta ? $?)
(alpha ?x ?y&:(> ?y ?x) 5)
)
The Agenda
Agenda window OR (agenda)
0 first: f-1
0 second: f-2
The list of all activations (rule name : facts satisfying their patterns). The same rule can
appear multiple times in the agenda if it’s conditions are satisfied in multiple ways (by the
same or other facts).
What happens when we (run)?
How are the two activations executed?
● Sequential? What happens if the first rule removes facts needed by the second?
● Parallel? How do you solve the possible conflicts between the merging facts?
● What about this case:
Important CLIPS tip: the agenda is computed again after each rule execution.
Important CLIPS tip: once executed, an activation will never be added again.
Use the other activations for debugging. Is the rule you expected to run activated? What
activations are before it?
To run just one execution, use (run 1).
Rule saliences (I)
(defrule first 0 first: ^
(not (apple)) => (printout t “missing”))) 0 second: ^
(defrule second
(not (apple)) => (assert (apple)))
Is “missing” printed?
(defrule first
(not (apple)) => (printout t “missing”))) 10 second: ^
(defrule second (declare (salience 10)) 0 first: ^
(not (apple)) => (assert (apple)))
Rule saliences (II)
Use salience values in multiples of 10 or 100. You may decide later to insert
another salience between existing ones.
Using a different salience for each rule skips most of the inference engine (which is
bad!).
(defrule add_1_to_project
?a<-(student ?x1 ?x2 ?x3 ?x4 ?x5 ?x6 ?x7 ?x8 ?x $?x9)
=>
(retract ?a)
(assert (student ?x1 ?x2 ?x3 ?x4 ?x5 ?x6 ?x7 ?x8 (+ ?x 1)
$?x9)
)
CLIPS templates (I)
(deftemplate student
(slot name)
(multislot labs)
(slot project)
(slot exam)
(slot total)
)
Caution! Both example rules will enter an infinite loop. Make sure that the newly created
fact doesn’t fulfill the LHS conditions by using flags in templates or by removing facts
checked in the LHS.
Forall and exists equivalents
(forall (list $? ?x $?) (numbers $? ?x $?))
https://www.csie.ntu.edu.tw/~sylee/courses/clips/toc.htm
http://www.cs.umanitoba.ca/~comp4200/Notes.html
https://www.youtube.com/watch?v=WwGU6AU3f-A
https://kcir.pwr.edu.pl/~witold/ai/CLIPS_tutorial/