Search code examples
rulesclips

clips - same slot in pattern and action


When we are defining a rule , can we have same slot names in the in the pattern and in the action area ( where assert is) ? I'm giving example with code below :

(deftemplate b1
    (slot s1) 
    (slot s2))

(deftemplate b2
    (slot s1) 
    (slot s2)) 

(deftemplate b3
    (slot r1)
    (slot r2))

(deftemplate b4
    (slot r1)
    (slot r2))

; Facts
(deffacts F 
    (b1 (s1 2)(s2 5))
    (b2 (s1 7)(s2 9)))


; Rules
(defrule R1 
    (b1 (s1 ?x1)(s2 ?y1))
    (b2 (s1 ?x2)(s2 ?y2))
=>
    (assert (b4(r1 (* ?x1 ?x2))(r2 (* ?y1 ?y2))))

    (printout t)    
)

(defrule R2 
    (b2 (s1 ?x1)(s2 ?y1))
    (b3 (r1 ?x2)(r2 ?y2))
=>
    (assert (b3(r1 (* ?x1 ?x2))(r2 (* ?y1 ?y2))))

    (printout s)
)

The situation I described is located in the R2 rule of the code . When I run the program , I see that only rule R1 is executed . is this because of this line ?

    (b3 (r1 ?x2)(r2 ?y2))
=>
    (assert (b3(r1 (* ?x1 ?x2))(r2 (* ?y1 ?y2))))

because we have r1 used two times , like I said before .


Solution

  • Rule R2 will execute only if both a b2 and a b3 fact exist. The assertion of a b3 fact from the actions of rule R2 will only happen if the rule is activated by a different b3 fact. Using the same slot in both the conditions and actions of a rule does not prevent the activation of that rule.