CLIPS 对我来说相当陌生 - 我已经尝试深入研究这门语言两天了。我想到了一个问题,即:如何(如果可能)动态创建/添加新规则?我想做这样的事情:
(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.")
)
)
Run Code Online (Sandbox Code Playgroud)
请不要特别注意这些规则的逻辑——这只是一个例子。调用上述命令后,我收到:
[EXPRNPSR3] Missing function declaration for defrule.
ERROR:
(defrule MAIN::test
(action (prev ?p))
=>
(defrule
Run Code Online (Sandbox Code Playgroud)
这个错误是命令语法的问题还是我无法“动态”定义新规则?
首先创建一个包含规则(或任何其他构造)的字符串,然后使用构建函数:
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>
Run Code Online (Sandbox Code Playgroud)