0
我是CLIPS的新手,回溯範式的確令我頭疼。如何消除CLIPS中的if-then-else以更好地優化代碼
我得到一個問題,我需要產生後續輸出。
Q: Is the patient's temperature more than 39? [yes/no]: yes
The patient has fever
Q: Does the patient sick over a month? [yes/no]: yes
We suspect the patient has bacterial infection
Q: How about sore throat? [yes/no]: yes
We believe the patient has strep throat
因此,根據需要,我定義了一些規則來使其工作。
(defrule rule_1
(THE-PATIENT-HAS-A-SORE-THROAT)
(WE-SUSPECT-A-BACTERIAL-INFECTION) =>
(assert(WE-BELIEVE-THE-PATIENT-HAS-STREP-THROAT))
(printout t "We believe the patient has strep throat" crlf))
(defrule rule_2
(THE-PATIENT-TEMPERATURE-IS-40C) =>
(assert(THE-PATIENT-HAS-A-FEVER)))
(defrule rule_3
(THE-PATIENT-HAS-BEEN-SICK-OVER-A-MONTH)
(THE-PATIENT-HAS-A-FEVER) =>
(assert(WE-SUSPECT-A-BACTERIAL-INFECTION)))
(defrule ask-sick
(start-question) =>
(printout t "Q: Is the patient's temperature more than 39? [yes/no]: ")
(bind ?input (readline))
(if (neq ?input "no")
then
(assert (THE-PATIENT-TEMPERATURE-IS-40C))
(printout t "The patient has fever" crlf)
(printout t "Q: Does the patient sick over a month? [yes/no]: ")
(bind ?input2 (readline))
(if (neq ?input2 "no")
then
(assert (THE-PATIENT-HAS-BEEN-SICK-OVER-A-MONTH))
(printout t "We suspect the patient has bacterial infection" crlf)
(printout t "Q: How about sore throat? [yes/no]: ")
(bind ?input3 (readline))
(if (neq ?input3 "no")
then
(assert (THE-PATIENT-HAS-A-SORE-THROAT))
)
)
)
)
(deffacts start
(start-question)
)
最終我確實讓節目過日子。
問題是,在規則問病的時候,我必須包含一堆if-then-else以使程序工作在不太符合回溯範式的地方。
有人能告訴我,我應該如何優化它在它知道從哪裏着手,規則1 - >規則2 /規則3等
哇!這真的很巧妙!謝謝! –