Search code examples
clipsexpert-system

Forward chaining LIFO


For forward chaining (LIFO) it was mentioned that

Depth strategy: Newly activated rules are placed above all rules of the same salience : for example if fact-a activates rule-1 and rule-2 and fact-b activates rule-3 and rule-4, then if fact-a is asserted before fact b, rule-3 and rule-4 will be above rule-1 and rule-2 on the agenda. However the position of rule3 relative to rule-4 will be arbitrary

so if I have the following facts and rules

(deffacts my_facts
(B)
(C))

(defrule r1
(B)(D)(E)=> (assert (F)))

(defrule r2
(C)(D) => (assert (A)))

(defrule r3
(C)(F)=>(assert (A)))

(defrule r4
(B) => (assert (X)))

(defrule r5
(D) => (assert (D)))

(defrule r6
(X)(A) => (assert(H))
(printout t 'H' is reached crlf)
(halt))

(defrule r7
(C)
=> (assert (D)))

(defrule r8
(X)
(C)
=> (assert (A)))

(defrule r9
(X)
(B)
=> (assert (D)))

then

(reset)
(watch all)
(run)

The idea is that, it seems that r2 is always fired before r5, but I thought that:

the position of rule-2 relative to rule-5 will be arbitrary, so why do we have R2 fired before R5 every time ?

this is the image which represents the case :

enter image description here


Solution

  • It's arbitrary as in "based on or determined by individual preference or convenience," not as in "existing or coming about seemingly at random." In the Basic Programming Guide at the beginning of section 5.3, Conflict Resolution Strategies, it's mentioned that arbitrary as used does not mean random.

    If a rule is activated (along with several other rules) by the same assertion or retraction of a fact, and steps a and b are unable to specify an ordering, then the rule is arbitrarily (not randomly) ordered in relation to the other rules with which it was activated. Note, in this respect, the order in which rules are defined has an arbitrary effect on conflict resolution (which is highly dependent upon the current underlying implementation of rules). Do not depend upon this arbitrary ordering for the proper execution of your rules.