剪辑 - 动态添加新规则

时间:2013-11-10 09:17:07

标签: clips

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

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

1 个答案:

答案 0 :(得分:4)

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

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>