% 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 % Menu that obtains 'yes' or 'no' answer question(Result,T):- jpl_call('javax.swing.JOptionPane','showConfirmDialog',[(@null),T,'Question',0],X), change(X,Result). info(X):- jpl_call('javax.swing.JOptionPane','showMessageDialog',[(@null),X],F). get_yes_or_no(Q,Result) :- ask_question(Q,X), question(Result,X). change(0,yes). change(1,no). % 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 :- info('This program diagnoses why a car won''t start. \n Answer all questions with Y for yes or N for no.'), 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) % % (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,_), get_yes_or_no(Q,Response), asserta(stored_answer(Q,Response)), Response = A. % % Texts of the questions % ask_question(starter_was_ok,'When you first started trying to start the car, \n did the starter crank the engine normally? '). ask_question(starter_is_ok,'Does the starter crank the engine normally now? '). ask_question(fuel_is_ok,'Look in the carburetor. Can you see or smell gasoline?'). % % Explanations for the various diagnoses % explain(wrong_gear) :- info('Check that the gearshift is set to Park or Neutral. \n Try jiggling the gearshift lever.'). explain(starting_system) :- info('Check for a defective battery, voltage regulator, or alternator; if any of these is the problem, charging the battery or jump- starting may get the car going temporarily. Or the starter itself may be defective.'). explain(drained_battery) :- info('Your attempts to start the car have run down the battery. Recharging or jump-starting will be necessary. But there is probably nothing wrong with the battery itself'). explain(fuel_system) :- info('Check whether there is fuel in the tank. If so, check for a clogged fuel line or filter or a defective fuel pump.'). explain(ignition_system) :- info('Check the spark plugs, cables, distributor, coil, and other parts of the ignition system. If any of these are visibly defective or long overdue for replacement, replace them; if this does not solve the problem, consult a mechanic.'). :- dynamic(stored_answer/2). nth_char('234',X).