home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / clips_2 / CLIPS / Examples / Automotive < prev    next >
Encoding:
Text File  |  1993-06-02  |  5.7 KB  |  193 lines

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