;;;====================================================== ;;; Automotive Expert System ;;; ;;; This expert system diagnoses some simple ;;; problems with a car. ;;; ;;; CLIPS Version 6.0 Example ;;; ;;; Modified to run under JESS by Michael Stiber ;;; September 2002 ;;; ;;; To execute, merely load, reset and run. ;;;====================================================== ;;**************** ;;* DEFFUNCTIONS * ;;**************** (deffunction ask-question (?question $?allowed-values) (printout t ?question) (bind ?answer (read)) (if (lexemep ?answer) then (bind ?answer (lowcase ?answer))) (while (not (member$ ?answer ?allowed-values)) do (printout t ?question) (bind ?answer (read)) (if (lexemep ?answer) then (bind ?answer (lowcase ?answer)))) ?answer) (deffunction yes-or-no-p (?question) (bind ?response (ask-question ?question "yes" "no" "y" "n")) (if (or (eq ?response "yes") (eq ?response "y")) then TRUE else FALSE)) ;;*********************** ;;* DEFTEMPLATES * ;;*********************** (deftemplate question (slot ident) (slot text)) (deftemplate answer (slot ident) (slot value)) (do-backward-chaining answer) ;;****************** ;;* QUESTIONS * ;;****************** (deffacts all-questions (question (text "Does the engine start (yes/no)? ") (ident engine-starts)) (question (text "Does the engine run normally (yes/no)? ") (ident engine-works-satisfactorily)) (question (text "Does the engine rotate (yes/no)? ") (ident engine-rotates)) (question (text "Is the engine sluggish (yes/no)? ") (ident running-state-sluggish)) (question (text "Does the engine misfire (yes/no)? ") (ident firing-state-misfires)) (question (text "Is the battery charged (yes/no)? ") (ident battery-charged)) (question (text "Does the tank have any gas in it (yes/no)? ") (ident has-gas)) (question (text "Is the battery charged (yes/no)? ") (ident charge-state-battery)) (question (text "Is the conductivity test for the ignition coil positive (yes/no)? ") (ident coil-conductivity-positive)) (question (text "Does the engine knock (yes/no)? ") (ident engine-knocks)) (question (text "Does the engine have low output (yes/no)? ") (ident engine-low-output)) (question (text "Do the points look burned (yes/no)? ") (ident points-burned)) (question (text "Do the points look contaminated (yes/no)? ") (ident points-contaminated)) ) ;;**************************** ;;* BACKWARDS CHAINING RULE * ;;**************************** (defrule ask-question (need-answer (ident ?id)) (question (ident ?id) (text ?q)) => (assert (answer (ident ?id) (value (yes-or-no-p ?q))))) ;;;********************** ;;;* ENGINE STATE RULES * ;;;********************** (defrule normal-engine-state-conclusions "" (declare (salience 10)) (answer (ident engine-works-satisfactorily) (value TRUE)) => (assert (repair "No repair needed.")) (assert (spark-state engine normal)) (assert (answer (ident battery-charged) (value TRUE))) (assert (answer (ident engine-rotates) (value TRUE)))) (defrule unsatisfactory-engine-state-conclusions "" (declare (salience 10)) (answer (ident engine-works-satisfactorily) (value FALSE)) => (assert (answer (ident battery-charged) (value TRUE))) (assert (answer (ident engine-rotates) (value TRUE)))) (defrule determine-irregular-spark "" (answer (ident engine-starts) (value FALSE)) (answer (ident engine-rotates) (value TRUE)) (not (repair ?)) => (assert (spark-state engine irregular-spark))) (defrule determine-no-spark "" (answer (ident engine-starts) (value FALSE)) (answer (ident engine-rotates) (value FALSE)) (not (repair ?)) => (assert (spark-state engine does-not-spark))) ;;;********************** ;;;* REPAIR RULES * ;;;********************** (defrule sluggish "" (answer (ident engine-works-satisfactorily) (value FALSE)) (answer (ident running-state-sluggish) (value TRUE)) (not (repair ?)) => (assert (repair "Clean the fuel line."))) (defrule misfiring "" (answer (ident engine-works-satisfactorily) (value FALSE)) (answer (ident firing-state-misfires) (value TRUE)) (not (repair ?)) => (assert (spark-state engine irregular-spark)) (assert (repair "Adjust the point gaps."))) (defrule knocking "" (answer (ident engine-works-satisfactorily) (value FALSE)) (answer (ident engine-knocks)(value TRUE)) (not (repair ?)) => (assert (repair "Timing Adjustment"))) (defrule no-gas "" (answer (ident engine-starts) (value FALSE)) (answer (ident engine-rotates) (value TRUE)) (answer (ident has-gas) (value FALSE)) => (assert (repair "Add gas."))) (defrule battery-discharged "" (answer (ident engine-rotates) (value FALSE)) (answer (ident battery-charged) (value FALSE)) => (assert (repair "Charge the battery.")) ) (defrule burned-points "" (or (and (answer (ident engine-starts) (value FALSE)) (spark-state engine irregular-spark)) (answer (ident engine-low-output) (value TRUE))) (answer (ident points-burned) (value TRUE)) => (assert (repair "Replace the points."))) (defrule contaminated-points "" (or (and (answer (ident engine-starts) (value FALSE)) (spark-state engine irregular-spark)) (answer (ident engine-low-output) (value TRUE))) (answer (ident points-contaminated) (value TRUE)) => (assert (repair "Clean the points."))) (defrule repair-lead-wire (answer (ident engine-starts) (value FALSE)) (spark-state engine does-not-spark) (answer (ident battery-charged) (value TRUE)) (answer (ident coil-conductivity) (value TRUE)) => (assert (repair "Repair the distributor lead wire."))) (defrule repair-ignition-coil (answer (ident engine-starts) (value FALSE)) (spark-state engine does-not-spark) (answer (ident battery-charged) (value TRUE)) (answer (ident coil-conductivity) (value FALSE)) => (assert (repair "Replace ignition coil"))) (defrule no-repairs "" (declare (salience -10)) (not (repair ?)) => (assert (repair "Take your car to a mechanic."))) ;;;**************************** ;;;* STARTUP AND REPORT RULES * ;;;**************************** (defrule system-banner "" (declare (salience 10)) => (printout t crlf crlf) (printout t "The Engine Diagnosis Expert System") (printout t crlf crlf)) (defrule print-repair "" (declare (salience 10)) (repair ?item) => (printout t crlf crlf) (printout t "Suggested Repair:") (printout t crlf crlf) (format t " %s%n%n%n" ?item) (halt))