Question

I have this following rule in my CLIPS file:

(defrule check-final (declare (salience 12))
    ?scnt <- (set-count (value ?v) (class ?c))
    (test (= ?v ?*total*))
    =>
    (printout T ?*total* " == " ?v crlf)
)

And I get the following strange output:

CLIPS>(run)
14 == 9
5 == 2

How is this possible ????

Was it helpful?

Solution

Pattern matching for this rule occurs whenever the fact set-count is asserted or modified. The rule is fired some time afterwards, during the call to run. These two processes can be widely separated in time. The value of ?*v* can of course change during that long period of time.

The key is to realize that he printed results will reflect the value of ?v from the the epoch during which pattern matching happened, while ?*total* will be the value when the results are printed. Since ?*total* may have seen arbitrary changes since the pattern matching, there's no guarantee that it will be equal to ?v when the rule actually fires.

OTHER TIPS

Found part of the problem: I'm using the global ?*total* and according to the CLIPS Manual

Global variables can be accessed as part of the pattern‑matching process, but changing them does not invoke the pattern‑matching process.

But this does not explain the equality test failure

The most likely explanation is that at some point the equality test is being satisfied and then the value of the global is changed before the rule executes.

CLIPS> (deftemplate set-count (slot value) (slot class))
CLIPS> 
(defglobal ?*total* = 0)
CLIPS> 
(defrule check-final (declare (salience 12))
    ?scnt <- (set-count (value ?v) (class ?c))
    (test (= ?v ?*total*))
    =>
    (printout T ?*total* " == " ?v crlf)
)
CLIPS> (bind ?*total* 9)
9
CLIPS> (assert (set-count (value 9) (class a)))
<Fact-1>
CLIPS> (bind ?*total* 14)
14
CLIPS> (run)
14 == 9
CLIPS> (bind ?*total* 2)
2
CLIPS> (assert (set-count (value 2) (class b)))
<Fact-2>
CLIPS> (bind ?*total* 5)
5
CLIPS> (run)
5 == 2
CLIPS> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top