CLIPS 6.4.1专家系统程序问题:双“no”输入未触发预期输出
CLIPS专家系统规则无输出问题排查
我编写了一段用于构建专家系统的CLIPS代码,运行代码并对两个问题均回答“no”时,未得到任何输出,但预期程序应打印“You are likely a teen.”。使用的CLIPS版本为6.4.1。
原代码
(defrule ask-question-1 => (printout t "Do you have a full-time job or go to school full-time? (yes/no): ") (assert (full-time-occupation (read))) ) (defrule ask-question-2 => (printout t "Do you have a driver's license? (yes/no): ") (assert (has-drivers-license (read))) ) (defrule determine-age (and (full-time-occupation yes) (has-drivers-license yes)) => (printout t "You are likely an adult." crlf) ) (defrule determine-age (and (full-time-occupation no) (has-drivers-license no)) => (printout t "You are likely a teen." crlf) ) (defrule determine-age (and (full-time-occupation no) (has-drivers-license yes)) => (printout t "You are likely a teen." crlf) ) (defrule determine-age (and (full-time-occupation yes) (has-drivers-license no)) => (printout t "You are likely an adult." crlf) )
问题原因
问题出在重复定义了同名规则determine-age。在CLIPS中,多次定义同名规则时,后续的规则会直接覆盖之前的同名规则。最终只有最后一个determine-age规则(匹配full-time-occupation yes且has-drivers-license no的情况)会被保留,前面三个规则都被替换掉了。当你对两个问题都回答“no”时,没有匹配的规则触发,因此不会输出预期内容。
修复方案
有两种可行的修复方式:
方式1:给每个规则设置唯一名称
(defrule determine-age-adult-both-yes (and (full-time-occupation yes) (has-drivers-license yes)) => (printout t "You are likely an adult." crlf) ) (defrule determine-age-teen-both-no (and (full-time-occupation no) (has-drivers-license no)) => (printout t "You are likely a teen." crlf) ) (defrule determine-age-teen-license-yes (and (full-time-occupation no) (has-drivers-license yes)) => (printout t "You are likely a teen." crlf) ) (defrule determine-age-adult-license-no (and (full-time-occupation yes) (has-drivers-license no)) => (printout t "You are likely an adult." crlf) )
方式2:合并条件简化规则
将同类结果的条件合并到一个规则中,用or逻辑减少冗余:
(defrule determine-age-adult (or (and (full-time-occupation yes) (has-drivers-license yes)) (and (full-time-occupation yes) (has-drivers-license no)) ) => (printout t "You are likely an adult." crlf) ) (defrule determine-age-teen (or (and (full-time-occupation no) (has-drivers-license no)) (and (full-time-occupation no) (has-drivers-license yes)) ) => (printout t "You are likely a teen." crlf) )
内容的提问来源于stack exchange,提问作者Fatima A
相关产品推荐
相关产品推荐

