CLIPS程序故障求助:西瓜细菌性果斑病症状检测无输出问题
CLIPS程序无输出问题排查与修复
问题根源
- 规则条件区错误执行动作:第一个规则将
printout输出语句放在了规则左部(LHS,仅支持事实模式匹配),CLIPS不会执行条件区的动作代码,导致提问提示无法显示,后续也无法获取用户输入。 - 大小写不匹配:断言的事实为大写标识(如
(OWS ...)),但检查规则中匹配的是小写标识(如(ows ?OWS)),CLIPS对符号大小写敏感,导致规则无法匹配到已断言的事实。 - 语法不完整:第二个规则的
if语句缺少闭合的endif,且代码括号未正确闭合,规则定义不完整,无法被CLIPS正常加载。 - 冗余交互标识:代码中混入了
CLIPS>交互提示符,属于无效代码,需删除。
修复后的完整代码
; 规则1:获取用户输入的症状信息 (defrule bacterial-fruit-blotch-of-watermelon (initial-fact) => (printout t "is the plant showing a symptom of Oily and water-soaked cotyledons? (yes/no) " crlf) (assert (OWS (read))) (printout t "is the plant showing a symptom of Yellow halo paralleling veins? (yes/no) " crlf) (assert (YHP (read))) (printout t "is the plant showing a symptom of Small dark and angled lesion on leaves? (yes/no) " crlf) (assert (SDA (read))) (printout t "is the plant showing a symptom of Dark green blotch on the upper surface of developing fruit? (yes/no) " crlf) (assert (DGB (read)))) ; 规则2:检查症状并输出诊断结果 (defrule check (OWS ?OWS) (YHP ?YHP) (SDA ?SDA) (DGB ?DGB) => (if (and (eq ?OWS yes) (eq ?YHP yes) (eq ?SDA yes) (eq ?DGB yes)) then (printout t "The plant is showing symptoms of bacterial fruit blotch of watermelon disease." crlf) else (printout t "The plant does not match all symptoms of bacterial fruit blotch of watermelon disease." crlf)) (reset)) ; 可选:重置环境,方便下次运行
运行说明
- 将上述代码复制到CLIPS交互环境中执行;
- 输入
(run)触发规则,依次回答每个症状的yes或no; - 系统会根据输入的症状组合输出诊断结果。
内容的提问来源于stack exchange,提问作者Ahmad Rumah
相关产品推荐
相关产品推荐

