规则不执行

问题描述 投票:0回答:1

我目前正在创建一个专家系统来询问用户问题,哪些答案将决定动物是什么?例如动物大吗?如果用户说是-继续相关问题-脖子长吗-如果是-这是长颈鹿。当我运行代码时,我只会得到第一个问题:“动物是大还是小?”当我回答“是/否”时,当我希望出现下一个相关问题时,什么也没有出现。这是我已经包含的代码/规则。任何指导将不胜感激。

(deffacts startup (animal mouse) (animal squirrel) (animal hamster) (animal giraffe) (animal elephant) (animal hippo) (animal rhino))
=> 
(defrule is-animal-big (not (animal-big ?)) => (printout t "Is the animal big (yes or no)?") (assert (animal-big (read))))
(defrule yes-big 
(big yes) 
=>
(defrule is-neck-long (not (neck-long ?)) => (printout t "Is the animals neck long (yes or no)?") (assert neck-long (read))))
(defrule neck-long-yes
(neck-long yes) 
=>
(printout t "your animal is a giraffe" crlf)
(bind ?animal giraffe)
(assert (animal-is ? animal)))
;;;===================================
(defrule neck-long-no
(neck-long no) 
=> 
(defrule is-nose-long (not (nose-long ?)) => (printout t "Is the animals nose long (yes or no)?") (assert (nose-long (read))))
(defrule nose-long-yes
(nose-long yes)
=> 
(printout t "your animal is an elephant" crlf)
(bind ?animal elephant) 
(assert (animal-is ?animal)))
;;;==============================
(defrule nose-long-no
(nose-long no)
=> (defrule does-swim-alot (not (swim-alot ?)) => (printout t "Does the animal swim a lot (yes or no)?") (assert (swim-alot (read))))
(defrule swim-alot-yes
(swim-alot yes)
=> 
(printout t "your animal is a hippo" crlf)
(bind ?animal hippo)
(assert (animal-is ?animal)))
;;;===============================
(defrule swim-alot-no
(swim-alot no)
=>
(printout t "your animal is a rhino" crlf)
(bind ?animal rhino)
(assert (animal-is ?animal)))
;;;===============================
;;;*******************************
(defrule big-no
(big no)
=>
(defrule does-animal-squeak (not (animal-squeak ?)) => (printout t "Does the animal squeak (yes or no)?") (assert (animal-squeak (read))))
(defrule animal-squeak-yes
(animal-squeak yes)
=> 
(printout t "your animal is a mouse" crlf)
(bind ?animal mouse)
(assert (animal-is ?animal)))
;;;==============================
(defrule animal-squeak-no
(animal-squeak no)
=>
(defrule animal-tail-bushy (not (tail-bushy ?)) => (printout t "Is the animals tail bushy (yes or no)?") (assert (tail-bushy (read))))
(defrule (tail-bushy-yes
(tail-bushy yes)
=> 
(printout t "your animal is a squirrel" crlf)
(bind ?animal squirrel) 
(assert (animal-is ?animal)))
;;;==============================
(defrule (tail-bushy-no
(tail-bushy no)
=>
(printout t "your animal is a hamster" crlf)
(bind ?animal hamster)
(assert (animal-is ?animal)))
;;;=============================
;;;*****************************
rules clips expert-system
1个回答
1
投票

我想,您希望在回答第一个问题后执行大或大规则。但是,回答第一个问题将断言(animal-big yes)或(animal-big no)。与此相反,big-no和big-yes的前提是检查(big?)而不是(animal-big?)。因此,这些规则永远不会触发。

另外,我建议您重构代码,以便更清楚地区分不同的规则。判断您发布的代码,它将无法正常工作。您是否尝试在另一个规则的RHS中创建新规则?例如:

(defrule yes-big 
(big yes) 
=>
(defrule is-neck-long (not (neck-long ?)) => (printout t "Is the animals neck long (yes or no)?") (assert neck-long (read))))
(defrule neck-long-yes
(neck-long yes) 
=>
(printout t "your animal is a giraffe" crlf)
(bind ?animal giraffe)
(assert (animal-is ? animal)))

我会改写为

(defrule yes-big-check-neck 
   (animal-big yes)
   (not (neck-long ?)) 
=>
   (printout t "Is the animals neck long (yes or no)?") 
   (assert neck-long (read))
)

(defrule neck-long-yes
  (neck-long yes) 
=>
  (printout t "your animal is a giraffe" crlf)
  (bind ?animal giraffe)
  (assert (animal-is ? animal))
)
© www.soinside.com 2019 - 2024. All rights reserved.