有一个文件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))
我加载文件并:
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>
议程没有显示任何有效规则。我希望值的变化(modify-instance)为8将触发模式匹配并且规则&#34;高于阈值&#34;将被选中进行运行并列入议程。我错过了什么?
答案 0 :(得分:1)
基本编程指南的5.4.1.7节“与对象模式匹配”:
创建或删除实例时,所有模式都适用于 该对象已更新。但是,当一个插槽改变时,只有那些 在该插槽上明确匹配的模式会受到影响。
因此,修改规则以明确匹配要触发模式匹配的插槽:
(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))