1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
;;;======================================================
;;; Automotive Expert System
;;;
;;; This expert system diagnoses some simple
;;; problems with a car.
;;;
;;; CLIPS Version 6.0 Example
;;;
;;; 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))
;;;**********************
;;;* ENGINE STATE RULES *
;;;**********************
(defrule normal-engine-state-conclusions ""
(declare (salience 10))
(working-state engine normal)
=>
(assert (repair "No repair needed."))
(assert (spark-state engine normal))
(assert (charge-state battery charged))
(assert (rotation-state engine rotates)))
(defrule unsatisfactory-engine-state-conclusions ""
(declare (salience 10))
(working-state engine unsatisfactory)
=>
(assert (charge-state battery charged))
(assert (rotation-state engine rotates)))
;;;***************
;;;* QUERY RULES *
;;;***************
(defrule determine-engine-state ""
(not (working-state engine ?))
(not (repair ?))
=>
(if (yes-or-no-p "Does the engine start (yes/no)? ")
then
(if (yes-or-no-p "Does the engine run normally (yes/no)? ")
then (assert (working-state engine normal))
else (assert (working-state engine unsatisfactory)))
else
(assert (working-state engine does-not-start))))
(defrule determine-rotation-state ""
(working-state engine does-not-start)
(not (rotation-state engine ?))
(not (repair ?))
=>
(if (yes-or-no-p "Does the engine rotate (yes/no)? ")
then
(assert (rotation-state engine rotates))
(assert (spark-state engine irregular-spark))
else
(assert (rotation-state engine does-not-rotate))
(assert (spark-state engine does-not-spark))))
(defrule determine-sluggishness ""
(working-state engine unsatisfactory)
(not (repair ?))
=>
(if (yes-or-no-p "Is the engine sluggish (yes/no)? ")
then (assert (repair "Clean the fuel line."))))
(defrule determine-misfiring ""
(working-state engine unsatisfactory)
(not (repair ?))
=>
(if (yes-or-no-p "Does the engine misfire (yes/no)? ")
then
(assert (repair "Point gap adjustment."))
(assert (spark-state engine irregular-spark))))
(defrule determine-knocking ""
(working-state engine unsatisfactory)
(not (repair ?))
=>
(if (yes-or-no-p "Does the engine knock (yes/no)? ")
then
(assert (repair "Timing adjustment."))))
(defrule determine-low-output ""
(working-state engine unsatisfactory)
(not (symptom engine low-output | not-low-output))
(not (repair ?))
=>
(if (yes-or-no-p "Is the output of the engine low (yes/no)? ")
then
(assert (symptom engine low-output))
else
(assert (symptom engine not-low-output))))
(defrule determine-gas-level ""
(working-state engine does-not-start)
(rotation-state engine rotates)
(not (repair ?))
=>
(if (not (yes-or-no-p "Does the tank have any gas in it (yes/no)? "))
then
(assert (repair "Add gas."))))
(defrule determine-battery-state ""
(rotation-state engine does-not-rotate)
(not (charge-state battery ?))
(not (repair ?))
=>
(if (yes-or-no-p "Is the battery charged (yes/no)? ")
then
(assert (charge-state battery charged))
else
(assert (repair "Charge the battery."))
(assert (charge-state battery dead))))
(defrule determine-point-surface-state ""
(or (and (working-state engine does-not-start)
(spark-state engine irregular-spark))
(symptom engine low-output))
(not (repair ?))
=>
(bind ?response
(ask-question "What is the surface state of the points (normal/burned/contaminated)? "
normal burned contaminated))
(if (eq ?response burned)
then
(assert (repair "Replace the points."))
else (if (eq ?response contaminated)
then (assert (repair "Clean the points.")))))
(defrule determine-conductivity-test ""
(working-state engine does-not-start)
(spark-state engine does-not-spark)
(charge-state battery charged)
(not (repair ?))
=>
(if (yes-or-no-p "Is the conductivity test for the ignition coil positive (yes/no)? ")
then
(assert (repair "Repair the distributor lead wire."))
else
(assert (repair "Replace the ignition coil."))))
(defrule no-repairs ""
(declare (salience -10))
(not (repair ?))
=>
(assert (repair "Take your car to a mechanic.")))
;;;****************************
;;;* STARTUP AND REPAIR 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))
|