1

CLIPS 对我来说很新——我已经尝试深入研究这种语言 2 天了。我想到了一个问题,即:如何(如果可能)动态创建/添加新规则?例如,我想做这样的事情:

(deftemplate action
 (slot prev)
 (slot curr)
)

(defrule test
   (action (prev ?p))
 =>
   (defrule test_inner
       (action (curr ?p))
     =>
       (printout t "Result of a newly created rule.")
    ) 
)

请不要特别注意这些规则的逻辑——这只是一个例子。调用上述命令后,我收到:

[EXPRNPSR3] Missing function declaration for defrule.

ERROR:
(defrule MAIN::test
   (action (prev ?p))
   =>
   (defrule

这个错误是命令语法问题还是我无法“动态”定义新规则?

4

1 回答 1

5

首先创建一个包含规则(或任何其他结构)的字符串,然后使用构建函数:

CLIPS> 
(deftemplate action
   (slot prev)
   (slot curr)
)
CLIPS> 
(defrule test
   (action (prev ?p))
=>
   (build (str-cat
            "(defrule test_inner
                (action (curr " ?p "))
                =>
                (printout t \"Result of a newly created rule.\")
             )"
          ) 
   )
)
CLIPS> (reset)
CLIPS> (assert (action (prev move)))
<Fact-1>
CLIPS> (agenda)
0      test: f-1
For a total of 1 activation.
CLIPS> (run)
CLIPS> (rules)
test
test_inner
For a total of 2 defrules.
CLIPS> (ppdefrule test_inner)
(defrule MAIN::test_inner
   (action (curr move))
   =>
   (printout t "Result of a newly created rule."))
CLIPS> 
于 2013-11-10T17:42:09.727 回答