我正在开发一个专家系统,它将使用PyClips做出录取决定。但是,代码不断产生“内存不足”错误。我想我已经解决了我的问题。我的CLIPS文件有问题。希望有人能看到我在这里做错了什么。这是代码的最简单版本....我简化了以便调试它:所以这是我的模板,并且为了参数,只有一条规则:如果收到了成绩单,那么app-complete属性标记为TRUE。
; template for application facts
(deftemplate application "structure of an application"
(slot app-id (type INTEGER))
(slot app-complete (type SYMBOL))
(slot transcript-received (type SYMBOL))
)
(defrule complete "rule for app completeness"
?f <- (application
(transcript-received Yes)
)
=>
(modify ?f
(app-complete TRUE)
)
)
; end.
所以我做(assert (application (app-id 123) (transcript-received Yes)))
然后,我加上了。
当我点击运行时...... CLIPS中的窗口开始超载数以千计的事实... app-complete属性看起来被标记为TRUE,但是,事实只是保持循环和循环,不间断。当它变得太多事实时,我说的是100K或者其他什么......然后CLIPS才会退出....任何想法我在这里做错了什么?我的语法搞砸了?我的意图是,然后有一个sqlite数据库,以便能够从数据库中读取'事实',以便系统能够做出决定....但我无法理解这一点!
答案 0 :(得分:1)
要记住的重要一点是,当您修改deftemplate
事实时,事实会被撤回并且新的(已修改的)事实被断言。您的规则也匹配修改后的事实,然后再次修改和匹配等,从而产生无限循环。如果您在运行代码时观察事实和激活,您会看到如下内容:
<== f-1 (application (app-id 123) (app-complete nil) (transcript-received Yes))
==> f-2 (application (app-id 123) (app-complete TRUE) (transcript-received Yes))
==> Activation 0 complete: f-2
<== f-2 (application (app-id 123) (app-complete TRUE) (transcript-received Yes))
==> f-3 (application (app-id 123) (app-complete TRUE) (transcript-received Yes))
==> Activation 0 complete: f-3
<== f-3 (application (app-id 123) (app-complete TRUE) (transcript-received Yes))
==> f-4 (application (app-id 123) (app-complete TRUE) (transcript-received Yes))
==> Activation 0 complete: f-4
(etc.)
您可以通过仅匹配尚未完成的应用程序来防止这种情况。以下是代码的修改版本,它向FALSE
广告位添加了默认app-complete
值,仅匹配未完成的应用:
(deftemplate application "structure of an application"
(slot app-id (type INTEGER))
(slot app-complete (type SYMBOL) (default FALSE))
(slot transcript-received (type SYMBOL)))
(defrule complete "rule for app completeness"
?f <- (application (transcript-received Yes) (app-complete FALSE))
=>
(modify ?f (app-complete TRUE)))
现在,如果您观察事实和激活,当您断言并运行时,您应该看到以下内容:
CLIPS> (assert (application (app-id 123) (transcript-received Yes)))
==> f-0 (application (app-id 123) (app-complete FALSE) (transcript-received Yes))
==> Activation 0 complete: f-0
<Fact-0>
CLIPS> (run)
<== f-0 (application (app-id 123) (app-complete FALSE) (transcript-received Yes))
==> f-1 (application (app-id 123) (app-complete TRUE) (transcript-received Yes))
CLIPS>