Number equality test fails in CLIPS pattern matching?

后端 未结 3 2054
清歌不尽
清歌不尽 2021-01-25 10:02

I have this following rule in my CLIPS file:

(defrule check-final (declare (salience 12))
    ?scnt <- (set-count (value ?v) (class ?c))
    (test (= ?v ?*tot         


        
相关标签:
3条回答
  • 2021-01-25 10:25

    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.

    0 讨论(0)
  • 2021-01-25 10:37

    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> 
    
    0 讨论(0)
  • 2021-01-25 10:40

    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

    0 讨论(0)
提交回复
热议问题