首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clips -动态添加新规则

Clips -动态添加新规则
EN

Stack Overflow用户
提问于 2013-11-10 09:17:07
回答 1查看 1.4K关注 0票数 1

剪辑对我来说是很新的--我已经试着深入挖掘这门语言2天了。一个问题浮现在我的脑海中,即:如何(如果可能的话)动态地创建/添加新规则?例如,我想做这样的事情:

代码语言:javascript
复制
(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.")
    ) 
)

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

代码语言:javascript
复制
[EXPRNPSR3] Missing function declaration for defrule.

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-10 17:42:09

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

代码语言:javascript
复制
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> 
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19888074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档