0
votes

I have the below CLIPS script. I am trying to get values of p1, p2, p3 The last rule get-p2-2 should be activated if p2 is unknown and p3 is known.

(defrule main
(initial-fact) 
=>
(assert(fact (read))) ; user enters 1
(assert(p1 unknown))
(assert(p2 unknown))
(assert(p3 unknown))
)

;;;=====================================================
(defrule get-p1
(fact 1)
(p1 unknown)
=>
(printout t"p1 known"crlf)
(assert (p1 known)))

;;;======================================================
(defrule get-p2
(fact 1)
(p1 known)
(p2 unknown)
=>
(printout t "p2 known"crlf)
(assert (p2 known))
(assert (fact 2)))



;;;======================================================
(defrule get-p3
(fact 2)
(p3 unknown)
=>
(printout t"p3 known"crlf)
(assert (p3 known)))

;;;======================================================
(defrule get-p2-2
(fact 2)
(p2 unknown)
(p3 known)
=>
(printout t "p2 known"crlf)
(assert (p2 known)))

But p2 becomes known in rule get-p2. So the rule get-p2-2 should have never been activated. But it does gets activated and I get ouput

 p1 known
 p2 known
 p3 known
 p2 known ; this should not be here

Why is get-p2-2 activated?

1
It is not necessary to add the initial-fact to a rule with no other conditions; it is added automatically in versions of CLIPS prior to version 6.3. The initial-fact functionality was deprecated in the 6.3 release; it is still asserted by a reset, but rules without conditions no longer rely on it. In the 6.4 release, the initial-fact is no longer asserted, so rules that explicitly match this fact will no longer be activated. - Gary Riley

1 Answers

1
votes

You don't retract any of the unknown facts, so p1, p2, and p3 are both known and unknown which allows get-p2-2 to be activated.

CLIPS> (reset)
CLIPS> (run)
1
p1 known
p2 known
p3 known
p2 known
CLIPS> (facts)
f-0     (initial-fact)
f-1     (fact 1)
f-2     (p1 unknown)
f-3     (p2 unknown)
f-4     (p3 unknown)
f-5     (p1 known)
f-6     (p2 known)
f-7     (fact 2)
f-8     (p3 known)
For a total of 9 facts.
CLIPS>

Retract the unknown facts in get-p1, get-p2, and get-p3 and you'll get the results you want.

(defrule get-p1
  (fact 1)
  ?f <- (p1 unknown)
   =>
   (retract ?f)
   (printout t "p1 known" crlf)
   (assert (p1 known)))