【问题标题】:How to compare a global variable to a string in Clips?如何将全局变量与 Clips 中的字符串进行比较?
【发布时间】:2015-05-20 00:29:27
【问题描述】:

在我的系统中,用户输入 Y 或 N 来回答简单的问题。我在每个问题之后调用这个规则来增加一个计数器。我的代码存在一些一般性问题,但我看不到哪里

(defrule QPain
         (initial-fact)
         =>
         (printout t "Are You In Pain? " crlf) 
         (bind ?*Answer* (read)) 
)
(defrule IncSym
     (test(=(str-compare (?*Answer*) "y")0))
      =>
     (bind ?*symcount* (+ ?*symcount* 1))
) 

谢谢

【问题讨论】:

  • 不需要在没有其他条件的情况下将初始事实添加到规则中;它会在 6.3 版之前的 CLIPS 版本中自动添加。最初的事实功能在 6.3 版本中已被弃用;它仍然由复位断言,但没有条件的规则不再依赖它。在 6.4 版本中,initial-fact 不再被断言,因此明确匹配该事实的规则将不再被激活。

标签: clips


【解决方案1】:

语法错误可以按如下方式纠正:

CLIPS> (clear)
CLIPS> (defglobal ?*Answer* = nil)
CLIPS> (defglobal ?*symcount* = 0)
CLIPS> 
(defrule QPain
   =>
   (printout t "Are you in pain? ") 
   (bind ?*Answer* (read)))
CLIPS>    
(defrule IncSym
   (test (eq ?*Answer* y))
   =>
   (bind ?*symcount* (+ ?*symcount* 1))) 
CLIPS> (reset)
CLIPS> (run)
Are you in pain? y
CLIPS> (show-defglobals)
?*Answer* = y
?*symcount* = 0
CLIPS> 

但是,这不会产生您期望的行为,因为 ?*symcount* 不会增加。之前已经讨论过全局变量的行为以及为什么不应该以您尝试的方式使用它们:

How exactly (refresh) works in the clips?
CLIPS: forcing a rule to re-evaluate the value of a global variable?
Number equality test fails in CLIPS pattern matching?
CLIPS constant compiler directive
How can I run the clips with out reset the fact when using CLIPS

您应该使用事实或实例,而不是使用全局变量来跟踪反应和症状。这是一种方法:

CLIPS> (clear)
CLIPS> 
(deftemplate symptom
   (slot id)
   (slot response))
CLIPS> 
(deftemplate symptom-list
   (multislot values))
CLIPS> 
(deffacts initial
   (symptom-list))
CLIPS>    
(defrule QPain
   =>
   (printout t "Are you in pain? ")
   (assert (symptom (id in-pain) (response (read)))))
CLIPS>   
(defrule IncSym
   (symptom (id ?id) (response y))
   ?f <- (symptom-list (values $?list))
   (test (not (member$ ?id ?list)))
   =>
   (modify ?f (values ?list ?id)))
CLIPS>    
(defrule symptoms-found
   (declare (salience -10))
   (symptom-list (values $?list))
   =>
   (printout t "Symptom count: " (length$ ?list) crlf))
CLIPS> (reset)
CLIPS> (run)
Are you in pain? y
Symptom count: 1
CLIPS> (reset)
CLIPS> (run)
Are you in pain? n
Symptom count: 0
CLIPS>

还有一个:

CLIPS> (clear)
CLIPS> 
(deftemplate symptom
   (slot id)
   (slot response))
CLIPS>    
(defrule QPain
   =>
   (printout t "Are you in pain? ")
   (assert (symptom (id in-pain) (response (read)))))
CLIPS>   
(defrule symptoms-found
   (declare (salience -10))
   =>
   (bind ?count (find-all-facts ((?f symptom)) (eq ?f:response y)))
   (printout t "Symptom count: " (length$ ?count) crlf))
CLIPS> (reset)
CLIPS> (run)
Are you in pain? y
Symptom count: 1
CLIPS> (reset)
CLIPS> (run)
Are you in pain? n
Symptom count: 0
CLIPS> 

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 2015-10-03
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多