% 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 % % write('This program diagnoses why a car won''t start.'),nl, % write('Answer all questions with Y for yes or N for no.'),nl, 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(_,_)). 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,_), ask_question(Q,Response), asserta(stored_answer(Q,Response)), Response = A. % Texts of the questions % ask_question(starter_was_ok,Res) :- show_yes_no_dialog('When you first started trying to start the car\n did the starter crank the engine normally?',Res). ask_question(starter_was_ok,Res) :- show_yes_no_dialog('Does the starter crank the engine normally now? ',Res). ask_question(starter_was_ok,Res) :- show_yes_no_dialog('Look in the carburetor. Can you see or smell gasoline?',Res). show_yes_no_dialog(Tekst,Response) :- jpl_new(array(class([java,lang],['String'])),['yes','no'], ArrayRef), jpl_get(ArrayRef,0,ArrayPosRef), jpl_get('javax.swing.JOptionPane', 'YES_NO_OPTION', YesNoRef), jpl_get('javax.swing.JOptionPane', 'QUESTION_MESSAGE', QuestionRef), jpl_call('javax.swing.JOptionPane', 'showOptionDialog', [@('null'), Tekst, 'Question', YesNoRef, QuestionRef, @(null), ArrayRef, ArrayPosRef], RetVal), interpret(RetVal,Response). interpret(1,no). % ASCII 89 = 'Y' interpret(0,yes). % ASCII 121 = 'y' show_explain_dialog(Tekst) :- jpl_call('javax.swing.JOptionPane', 'showMessageDialog', [@('null'),Tekst],_). % % Explanations for the various diagnoses % explain(wrong_gear) :- show_explain_dialog('Check that the gearshift is set to Park or Neutral.Try jiggling the gearshift lever.'). explain(starting_system) :- show_explain_dialog('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) :- show_explain_dialog('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) :- show_explain_dialog('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) :- show_explain_dialog('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.').