backtracking - How to eliminate if-then-else in CLIPS to better optimise code -


i new clips , backtracking paradigm make head pain.

i given question need produce follow output.

q: patient's temperature more 39? [yes/no]: yes     patient has fever q: patient sick on month? [yes/no]: yes     suspect patient has bacterial infection q: how sore throat? [yes/no]: yes     believe patient has strep throat 

so required defined few rules working.

(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 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: patient's temperature more 39? [yes/no]: ")   (bind ?input (readline))   (if (neq ?input "no")           (assert (the-patient-temperature-is-40c))       (printout t "the patient has fever" crlf)       (printout t "q: patient sick on month? [yes/no]: ")       (bind ?input2 (readline))       (if (neq ?input2 "no")                   (assert (the-patient-has-been-sick-over-a-month))           (printout t "we suspect patient has bacterial infection" crlf)           (printout t "q: how sore throat? [yes/no]: ")           (bind ?input3 (readline))           (if (neq ?input3 "no")                           (assert (the-patient-has-a-sore-throat))           )       )   ) )  (deffacts start   (start-question) ) 

eventually did make program come live.

the problem is, @ rule ask-sick, had incorporate bunch of if-then-else program working not comply backtracking paradigm.

can tell me how should optimise in knows proceed, rule1 -> rule2 / rule3, etc.

here's 1 approach:

(deffunction ask-question (?question $?allowed-values)    (printout t "q: " ?question " [" (implode$ ?allowed-values) "] " )    (bind ?answer (read))    (if (lexemep ?answer)         (bind ?answer (lowcase ?answer)))    (while (not (member ?answer ?allowed-values))       (printout t "q: " ?question " [" (implode$ ?allowed-values) "] ")       (bind ?answer (read))       (if (lexemep ?answer)            (bind ?answer (lowcase ?answer))))    ?answer)  (deffunction yes-or-no (?question)    (ask-question ?question yes no))  (defrule ask-temperature   =>   (bind ?input (yes-or-no "is patient's temperature more 39?"))   (assert (the-patient-temperature-is-40c ?input)))  (defrule ask-sick-for-over-a-month    (the-patient-has-a-fever yes)    =>      (bind ?input (yes-or-no "does patient sick on month?"))    (assert (the-patient-has-been-sick-over-a-month ?input)))  (defrule ask-sore-throat    (we-suspect-a-bacterial-infection yes)    =>    (bind ?input (yes-or-no "how sore throat?"))    (assert (the-patient-has-a-sore-throat ?input)))  (defrule conclude-fever   (the-patient-temperature-is-40c yes)    =>   (assert (the-patient-has-a-fever yes))   (printout t "the patient has fever" crlf))  (defrule conclude-bacterial-infection   (the-patient-has-been-sick-over-a-month yes)   (the-patient-has-a-fever yes)    =>    (assert (we-suspect-a-bacterial-infection yes))   (printout t "we suspect patient has bacterial infection" crlf))  (defrule conclude-strep-throat   (the-patient-has-a-sore-throat yes)   (we-suspect-a-bacterial-infection yes)    =>   (assert (we-believe-the-patient-has-strep-throat yes))   (printout t "we believe patient has strep throat" crlf)) 

and another:

(deffunction ask-question (?question $?allowed-values)    (printout t "q: " ?question " [" (implode$ ?allowed-values) "] " )    (bind ?answer (read))    (if (lexemep ?answer)         (bind ?answer (lowcase ?answer)))    (while (not (member ?answer ?allowed-values))       (printout t "q: " ?question " [" (implode$ ?allowed-values) "] ")       (bind ?answer (read))       (if (lexemep ?answer)            (bind ?answer (lowcase ?answer))))    ?answer)  (deffunction yes-or-no (?question)    (ask-question ?question yes no))  (deftemplate attribute    (slot name)    (slot value))  (deftemplate question    (slot text)    (slot attribute)    (multislot precursors))  (deftemplate conclusion    (slot text)    (slot attribute)    (multislot precursors))  (deffacts questions    (question       (text "is patient's temperature more 39?")       (attribute the-patient-temperature-is-40c))    (question       (text "does patient sick on month?")       (attribute the-patient-has-been-sick-over-a-month)       (precursors the-patient-has-a-fever))    (question       (text "how sore throat?")       (attribute the-patient-has-a-sore-throat)       (precursors we-suspect-a-bacterial-infection)))  (deffacts conclusions    (conclusion       (text "the patient has fever")       (attribute the-patient-has-a-fever)       (precursors the-patient-temperature-is-40c))    (conclusion       (text "we suspect patient has bacterial infection")       (attribute we-suspect-a-bacterial-infection)       (precursors the-patient-has-been-sick-over-a-month the-patient-has-a-fever))    (conclusion       (text "we believe patient has strep throat")       (attribute we-believe-the-patient-has-strep-throat)       (precursors the-patient-has-a-sore-throat we-suspect-a-bacterial-infection)))  (defrule ask-question        (question (text ?text)               (attribute ?attr))     (forall (question (attribute ?attr)                       (precursors $? ?pre $?))             (attribute (name ?pre)                        (value yes)))     =>     (bind ?input (yes-or-no ?text))     (assert (attribute (name ?attr) (value ?input))))  (defrule conclude     (conclusion (text ?text)                 (attribute ?attr))     (forall (conclusion (attribute ?attr)                         (precursors $? ?pre $?))             (attribute (name ?pre)                        (value yes)))     =>     (assert (attribute (name ?attr) (value yes)))     (printout t ?text crlf)) 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -