问题
Having a file test.clp:
(defclass TestClass (is-a USER)
(role concrete)
(pattern-match reactive)
(slot value)
(slot threshold))
(definstances TestObjects
(Test of TestClass
(value 0)
(threshold 3)))
(defrule above-threshold
?test <- (object (is-a TestClass))
(test (> (send ?test get-value) (send ?test get-threshold)))
=>
(printout t "*** Above threshold!! ***" crlf)
(refresh below-threshold))
(defrule below-threshold
?test <- (object (is-a TestClass))
(test (> (send ?test get-value) (send ?test get-threshold)))
=>
(printout t "*** Back to normal ***" crlf)
(refresh above-threshold))
I load the file and:
CLIPS> (reset)
CLIPS> (agenda)
0 below-threshold: [Test]
For a total of 1 activation.
CLIPS> (run)
*** Back to normal ***
CLIPS> (modify-instance [Test] (value 8))
TRUE
CLIPS> (agenda)
CLIPS>
The agenda does not show any active rule. I would expect the change (modify-instance) for value to 8 would trigger pattern matching and rule "above-threshold" would be selected for running and put in the agenda. What am I missing?
回答1:
From section 5.4.1.7, Pattern-Matching with Object Patterns, of the Basic Programming Guide:
When an instance is created or deleted, all patterns applicable to that object are updated. However, when a slot is changed, only those patterns that explicitly match on that slot are affected.
So modify the rules to explicitly match the slots you want to trigger pattern matching:
(defrule above-threshold
?test <- (object (is-a TestClass)
(value ?value)
(threshold ?threshold))
(test (> ?value ?threshold))
=>
(printout t "*** Above threshold!! ***" crlf)
(refresh below-threshold))
(defrule below-threshold
?test <- (object (is-a TestClass)
(value ?value)
(threshold ?threshold))
(test (< ?value ?threshold))
=>
(printout t "*** Back to normal ***" crlf)
(refresh above-threshold))
来源:https://stackoverflow.com/questions/38699495/clips-modify-instance-does-not-trigger-pattern-matching