% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % File CAR.PL % Simple automotive expert system % % Main control procedures % start :- javaMessage('javax.swing.JOptionPane', showMessageDialog(null,string('This program diagnoses why a car won''t start.'))), clear_stored_answers, try_all_possibilities. try_all_possibilities :- % Backtrack through all possibilities... defect_may_be(D), explain(D), fail. try_all_possibilities. % ...then succeed with no further action. % % Diagnostic knowledge base % (conditions under which to give each diagnosis) % defect_may_be(drained_battery) :- user_says(starter_was_ok,yes), user_says(starter_is_ok,no). defect_may_be(wrong_gear) :- user_says(starter_was_ok,no). defect_may_be(starting_system) :- user_says(starter_was_ok,no). defect_may_be(fuel_system) :- user_says(starter_was_ok,yes), user_says(fuel_is_ok,no). defect_may_be(ignition_system) :- user_says(starter_was_ok,yes), user_says(fuel_is_ok,yes). % % Case knowledge base % (information supplied by the user during the consultation) % :- dynamic(stored_answer/2). % (Clauses get added as user answers questions.) % % Procedure to get rid of the stored answers % without abolishing the dynamic declaration % clear_stored_answers :- retract(stored_answer(_,_)),fail. clear_stored_answers. % % Procedure to retrieve the user's answer to each question when needed, % or ask the question if it has not already been asked % user_says(Q,A) :- stored_answer(Q,A). user_says(Q,A) :- \+ stored_answer(Q,_), nl,nl, ask_question(Q,Response), asserta(stored_answer(Q,Response)), Response = A. % % Texts of the questions % ask_question(starter_was_ok,R) :- javaMessage('javax.swing.JOptionPane',B, showConfirmDialog(null,string(' When you first started trying to start the car, \n did the starter crank the engine normally?'))), ipObjectSpec('java.lang.Integer',B,[A],_), ( A == 0 -> R = 'yes' ; R = 'no' ) . ask_question(starter_is_ok,R) :- javaMessage('javax.swing.JOptionPane',B, showConfirmDialog(null,string(' Does the starter crank the engine normally now? '))), ipObjectSpec('java.lang.Integer',B,[A],_), ( A==0 -> R='yes' ; R='no' ). ask_question(fuel_is_ok,R) :- javaMessage('javax.swing.JOptionPane',B, showConfirmDialog(null,string(' Look in the carburetor. Can you see or smell gasoline?'))), ipObjectSpec('java.lang.Integer',B,[A],_), ( A==0 -> R='yes' ; R='no' ). % % Explanations for the various diagnoses % explain(wrong_gear) :- javaMessage('javax.swing.JOptionPane', showMessageDialog(null,string(' Check that the gearshift is set to Park or Neutral.\n Try jiggling the gearshift lever.'))). explain(starting_system) :- javaMessage('javax.swing.JOptionPane', showMessageDialog(null,string(' Check for a defective battery, voltage \n regulator, or alternator; if any of these is \n the problem, charging the battery or jump- \n starting may get the car going temporarily. \n Or the starter itself may be defective.'))). explain(drained_battery) :- javaMessage('javax.swing.JOptionPane', showMessageDialog(null,string(' Your attempts to start the car have run down the battery. \n Recharging or jump-starting will be necessary. \n But there is probably nothing wrong with the battery itself. \n'))). explain(fuel_system) :- javaMessage('javax.swing.JOptionPane', showMessageDialog(null,string(' Check whether there is fuel in the tank. \n If so, check for a clogged fuel line or filter \n or a defective fuel pump.'))). explain(ignition_system) :- javaMessage('javax.swing.JOptionPane', showMessageDialog(null,string(' Check the spark plugs, cables, distributor, \n coil, and other parts of the ignition system. \n If any of these are visibly defective or long \n overdue for replacement, replace them; if this \n does not solve the problem, consult a mechanic.'))).