剪辑defrule不起作用

时间:2014-08-02 15:02:10

标签: clips expert-system

我对包含url本身和整数计数器的url进行了deftemplate:

(deftemplate url_t
(slot counter
(type INTEGER)
(default 0))
(slot url
(type STRING)
(default "")))

我试图定义规则来打印url字符串,如果计数器值> 0

(defrule print-url
    (url_t (counter ?counter) (url ?url))
    (test (> ?counter 0))
=>
(printout t "url is " ?url crlf)
)

但是当我尝试更改url_t的值时: (断言url_t(计数器2)(url“hello hello”)) 该规则未执行。哪里是我的错误?

1 个答案:

答案 0 :(得分:1)

如果描述中的断言语句与您输入的完全相同,那么这就是您的问题,因为语法无效。除此之外,你的deftemplate或defrule的语法没有错:

CLIPS> (clear)
CLIPS> (unwatch all)
CLIPS> 
(deftemplate url_t
   (slot counter
      (type INTEGER)
      (default 0))
   (slot url
      (type STRING)
      (default "")))
CLIPS> 
(defrule print-url
   (url_t (counter ?counter) (url ?url))
   (test (> ?counter 0))
   =>
   (printout t "url is " ?url crlf))
CLIPS> (assert (url_t (counter 2) (url "hello hello")))
<Fact-1>
CLIPS> (facts)
f-0     (initial-fact)
f-1     (url_t (counter 2) (url "hello hello"))
For a total of 2 facts.
CLIPS> (agenda)
0      print-url: f-1
For a total of 1 activation.
CLIPS> (run)
url is hello hello
CLIPS> (modify 1 (counter 3) (url "goodbye goodbye"))
<Fact-2>
CLIPS> (agenda)
0      print-url: f-2
For a total of 1 activation.
CLIPS> (run 1)
url is goodbye goodbye
CLIPS> (modify 2 (counter 0) (url "won't fire"))
<Fact-3>
CLIPS> (agenda)
CLIPS>