Number equality test fails in CLIPS pattern matching?

六眼飞鱼酱① 提交于 2019-12-02 12:32:44

问题


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 ????


回答1:


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.




回答2:


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




回答3:


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> 


来源:https://stackoverflow.com/questions/15842144/number-equality-test-fails-in-clips-pattern-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!