/* * Filename: applicance.pro * * Adapted from: Artificial Intelligence Through Prolog, Neil C. Rowe * * Date of creation: March 17, 1991 * * Revised: December 9, 1999 to run under yap. * * Description: * * This program is a simple diagnostic expert system for household * applicances. Start it with the goal diagnosis(Problem). It will ask * series of questions and attempt to diagnose the problem. Once you get * a diagnosis, you may be able to get a second diagnosis by pressing ; * on the keyboard. */ :- dynamic asked/2. diagnosis('Fuse blown') :- power_problem, askif('lights out'). diagnosis('Fuse blown') :- power_problem, askifhear('pop'). diagnosis('Break in cord') :- power_problem, askif('cord frayed'). diagnosis('Short in cord') :- diagnosis('Fuse blown'), askif('cord frayed'). diagnosis('Device not turned on') :- power_problem, klutz_user, askifhas('an on-off switch or control'), askifnot('device on'). diagnosis('Cord not in socket properly') :- power_problem, klutz_user, askif('just plugged'), askifnot('in socket'). diagnosis('Foreign matter caught on heating element') :- heating_element, not(power_problem), askif('smell smoke'). diagnosis('Applicance wet (so dry it out and try again)') :- power_problem, klutz_user, askif('liquids'). diagnosis('Controls adjusted improperly') :- klutz_user, askifhas('knobs or switches'). diagnosis('Kick it, then try it again') :- mechanical_problem. diagnosis('Throw it out and get a new one'). power_problem :- askif('device dead'). power_problem :- askifhas('knobs or switches'), askifnot('knobs do something'). power_problem :- askif('smell smoke'), not(heating_element). klutz_user :- askifnot('handyperson'). klutz_user :- askifnot('familiar appliance'). mechanical_problem :- askifhear('weird noise'), askifhas('moving parts'). heating_element :- askif('heats'). heating_element :- askif('powerful'). /* if it draws a lot of power, it's likely to contain a heating element */ questioncode('device dead', 'Does the device refuse to do anything'). questioncode('knobs do something', 'Does changing the switch positions or turning the knobs change anything'). questioncode('lights out', 'Do all the lights in the house seem to be off'). questioncode('cord frayed', 'Does the outer covering of the cord appear to be coming apart'). questioncode('handyperson', 'Are you good at fixing things'). questioncode('familiar appliance', 'Are you familiar with how this appliance works'). questioncode('device on', 'Is the ON/OFF switch set to ON'). questioncode('just plugged', 'Did you just plug the appliance in'). questioncode('in socket', 'Is the cord firmly plugged into the socket'). questioncode('smell smoke', 'Do you smell smoke'). questioncode('liquids', 'Have any liquids spilled on the appliance just now'). questioncode('heats', 'Does the appliance heat things'). questioncode('powerful', 'Does the appliance require a lot of power'). affirmative(yes). affirmative(y). affirmative(ye). affirmative(right). affirmative(ok). affirmative(definitely). negative(no). negative(n). negative(never). negative(impossible). askif(Question) :- ask(Question, Answer), positive_answer(Question, Answer). askifhas(Item) :- askhas(Item, Answer), positive_answer(Item, Answer). askifhear(Item) :- askhear(Item, Answer), positive_answer(Item, Answer). positive_answer(_, Answer) :- affirmative(Answer), !. positive_answer(Question, Answer) :- not(affirmative(Answer)), not(negative(Answer)), write('Please answer yes or no: '), readln(Answer2), retract(asked(Question, Answer)), asserta(asked(Question, Answer2)), affirmative(Answer2). ask(Question, Answer) :- asked(Question, Answer). ask(Question, Answer) :- not(asked(Question, _)), questioncode(Question, CompleteQuestion), write(CompleteQuestion), write('? '), read(Answer), asserta(asked(Question, Answer)). askhas(Item, Answer) :- asked(Item, Answer). askhas(Item, Answer) :- not(asked(Item, _)), write('Does this appliance have '), write(Item), write('? '), read(Answer), asserta(asked(Item, Answer)). askhear(Item, Answer) :- asked(Item, Answer). askhear(Item, Answer) :- not(asked(Item, _)), write('Did you hear a '), write(Item), write('? '), read(Answer), asserta(asked(Item, Answer)). askifnot(Question) :- not(askif(Question)).