2012-03-05 104 views
0

我需要在三天内完成这个任务,所以我别无选择,只能请教其他程序员,我是一个学习速度慢的人,而且有很多代码让我感到困惑...... :(prolog专家系统很难

好吧我正在使用这个专家系统来排除使用prolog的摩托车故障,并且我正在使用AMZI prolog作为SDK,到目前为止一切顺利,直到我的老师要求我在问问题时添加一条帮助命令,这个帮助菜单的目的是指导用户如何在故障排除期间执行一些测试,如火花测试,但是用户是否想要使用帮助,另一个是我应该给出解决方案在故障排除中说明的问题,以清楚地说明这里是我的代码的示例运行:

Welcome to Honda XRM/Wave motorcycle trouble shooting 
Please do not forget to type all your answers with a period(.) in the end 

What is the problem? 
1 : Engine does not start or is hard to start 
2 : Engine lacks power 
3 : Poor performance at low speed 
4 : Poor performance at high speed 
5 : Poor handling 
6 : Battery is damage or weak 
7 : Starter motor does not turn 
Enter the number of choice> 1. 


>> check fuel flow to carburetaor << 
question_1:Are there no fuel reaching the carburetor? (yes or no): no. 

>> remove and inspect spark plug << 
question_2:is the spark plug wet? (yes or no): yes. 

The problem(s) is/are: 
flooded carburetor 
throttle valve open 
dirty air cleaner 
improperly adjusted air screw 

正如你所看到的那样,有给出的选择,用户必须用数字或是或否来回答。我的问题是除了是或否回答外,应该为用户提供帮助答案,这意味着它应该看起来像这样。

What is the problem? 
1 : Engine does not start or is hard to start 
2 : Engine lacks power 
3 : Poor performance at low speed 
4 : Poor performance at high speed 
5 : Poor handling 
6 : Battery is damage or weak 
7 : Starter motor does not turn 
Enter the number of choice> 1. 


>> check fuel flow to carburetaor << 
question_1:Are there no fuel reaching the carburetor? (yes or no):help. 

>>then some help tip on how to perform some test, like a spark test or something<< 
>>then it should return to the previous question after the user enters done.<< 
Enter done. to return to the previous question> done. 

question_1:Are there no fuel reaching the carburetor? (yes or no): 

这里是我的全部源代码:

main :- identify. 

identify :- 
    write('Welcome to Honda XRM/Wave motorcycle troubleshooting'), 
    nl, 
    write('Please do not forget to type all your answers with a period(.) in the end'), 
    nl, 
    retractall(known(_,_,_)),   % clear stored information 
    diagnosis(X), 
    nl, 
    write('The problem(s) is/are: '), 
    nl, 
    writeListByLine(X).     
identify :- 
    nl, 
    write('I can''t identify the problem'). 

writeListByLine([]). 
writeListByLine([H|T]) :-    % write answers in newline 
    write(H), 
    nl, 
    writeListByLine(T). 

%%%%%%%%%%%%%%%%%%%%%%%% Engine does not start or is hard to start %%%%%%%%%%%%%%%%%% 

diagnosis(['clogged fuel line or strainer', 
    'clogged fuel tank cap', 
    'sticking float valve', 
    'clogged fuel filter']) :- 
    problem('Engine does not start or is hard to start'), 
    nl, 
    write('>> check fuel flow to carburetaor <<'), 
    nl, 
    question_1('Are there no fuel reaching the carburetor'). 

diagnosis(['flooded carburetor', 'throttle valve open', 
    'dirty air cleaner', 
    'improperly adjusted air screw']) :- 
    problem('Engine does not start or is hard to start'), 
    nl, 
    write('>> remove and inspect spark plug <<'), 
    nl, 
    question_2('is the spark plug wet'). 

diagnosis(['faulty spark plug', 
    'fouled spark plug', 
    'loose or disconnected ignition system', 
    'broken or shortened spark plug wire', 
    'faulty ignition pulse generator', 
    'faulty ignition coil', 
    'faulty ignition control module(ICM)', 
    'faulty engine stop switch']) :- 
    problem('Engine does not start or is hard to start'), 
    nl, 
    write('>> perform spark test <<'), 
    nl, 
    question_3('is there weak or no spark'). 

diagnosis(['improper choke operation', 
    'incorrectly adjusted carburetor', 
    'leaking carburetor insulator or intake manifold', 
    'improper ignition timing(faulty ICM or ignition pulse generator', 
    'contaminated fuel']) :- 
    problem('Engine does not start or is hard to start'), 
    nl, 
    write('>> start by following normal procedure <<'), 
    nl, 
    question_4('does the engine starts then stops'). 

diagnosis(['valve struck open' , 
    'worn cylinder and piston rings', 
    'damaged cylinder head gasket', 
    'seized valve', 
    'improper valve timing', 
    'valve clearance too small']) :- 
    problem('Engine does not start or is hard to start'), 
    nl, 
    write('>> test cylinder compression <<'), 
    nl, 
    question_5('is the compression low'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Engine lacks power %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

diagnosis(['brake dragging', 'worn or damage wheel bearings', 'bent axle']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> raise wheel off the ground and spin it by hand <<'), 
    nl, 
    question_1('does the wheel spin freely'). 

diagnosis(['faulty tire valve', 'punctured tire']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> check tire pressure <<'), 
    nl, 
    question_2('are the tire pressure low'). 

diagnosis(['clutch slipping', 
    'worn clutch discs/plates', 
    'warped clutch discs/plates', 
    'weak clutch spring', 
    'additive in engine oil', 
    'faulty clutch weight', 
    'faulty clutch lining', 
    'incorrect clutch adjustment']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> accelerate rapidly from low to second <<'), 
    nl, 
    question_3('does the engine speed do not change accordingly when the gearshift pedal is applied'). 

diagnosis(['improper choke operation', 
    'dirty air cleaner', 
    'restricted fuel flow', 
    'clogge exhaust system', 
    'clogged fuel tank cap']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> accelerate lightly <<'), 
    nl, 
    question_4('is the engine speed not increasing'). 

diagnosis(['plugs not serviced frequently enough', 
    'incorrent spark plug heat range', 'incorrent spark plug gap']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> remove and inspect spark plug <<'), 
    nl, 
    question_5('is the spark plug not fouled or discolored'). 

diagnosis(['oil level to high', 'oil level to low', 'contaminated oil']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> check oil level an condition <<'), 
    nl, 
    question_6('is there incorrect level and bad condition'). 

diagnosis(['faulty ignition control module', 'faulty ignition pulse generator']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> check ignition timing <<'), 
    nl, 
    question_7('is the ignition timing wrong'). 

diagnosis(['valve clearance to small', 
    'valve stuck open', 'worn cylinder and piston rings', 
    'damage cylinder head gasket', 'improper valve timing', 
    'seized valve']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> test cylinder compresor <<'), 
    nl, 
    question_8('is the compression low'). 

diagnosis(['carburetor not serviced frequently enough']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> check carburetor for glogging <<'), 
    nl, 
    question_9('is the carburetor clogged'). 

diagnosis(['clogged oil passage', 'clogged oil orifice', 
    'oil strainer screen not serviced frequently enough', '']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> remove cylinder head cover and inspect lubrication <<'), 
    nl, 
    question_10('is the valve train not properly lubircanted'). 

diagnosis(['excessive carbon build-up in combution chamber', 
    'use of poor quality fuel', 'wrong type of fuel', 'clutch slipping', 'lean fuel mixture']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> check for engine overheating <<'), 
    nl, 
    question_11('is the engine overheating'). 

diagnosis(['worn piston and cylinder', 'wrong type of fuel', 
    'excessive carbon buil-up in combustion chamber', 
    'ignition timing too advance(faulty ICM or ignition pulse generator', 'lean fuel mixture']) :- 
    problem('Engine lacks power'), 
    nl, 
    write('>> accelerate or run at high speed <<'), 
    nl, 
    question_12('is the engine knocking'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%% Poor performance at low speed %%%%%%%%%%%%%%%%%%%%%%%% 

diagnosis(['damaged insulator', 'faulty O-ring', 'loose carburator']) :- 
    problem('Poor performance at low speed'), 
    nl, 
    write('>> check for leaking carburetor insulator and intake manifold <<'), 
    nl, 
    question_1('is there leaking'). 

diagnosis(['faulty spark plug', 'fouled spark plug', 
    'loose or disconnected ignition system wires', 'faulty ignition pulse generator', 
    'faulty ignition coil', 'faulty ignition cotrol modue (ICM)']) :- 
    problem('Poor performance at low speed'), 
    nl, 
    write('>> perform spark test <<'), 
    nl, 
    question_2('is there weak or intermittent spark'). 

diagnosis(['faulty ignition control module (ICM)', 'faulty ignition pulse generator']) :- 
    problem('Poor performance at low speed'), 
    nl, 
    write('>> check ignition timing <<'), 
    nl, 
    question_3('is the ignition timing incorrect'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%% Poor performance at high speed %%%%%%%%%%%%%%%%%%%%%%%%%%%% 

diagnosis(['restricted fuel line and strainer', 'restricted fuel tank cap', 'restricted fuel filter']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> disconnect fuel line at carburetor <<'), 
    nl, 
    question_1('does fuel not flow freely'). 

diagnosis(['plug not service frequently enough', 'incorrect spark plug heat range', 
    'incorrect spark plug gap', 'air cleaner dirty', 'improper choke operation']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> remove and inspect the spart plug <<'), 
    nl, 
    question_2('is the spark plug in bad condition'). 

diagnosis(['carburetor not service frequently enough']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> check carburetor for clogging <<'), 
    nl, 
    question_3('is the carburetor clogged'). 

diagnosis(['faulty ignition control module(ICM)', 'faulty ignition pulse generator']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> check ignition timing <<'), 
    nl, 
    question_4('is the ignition timing incorrect'). 

diagnosis(['cam sprockets not installed properly']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> check valve timing <<'), 
    nl, 
    question_5('is the valve timing incorrect'). 

diagnosis(['faulty valve spring']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> check valve spring <<'), 
    nl, 
    question_6('is the valve spring free length not within specification'). 

diagnosis(['faulty camshaft']) :- 
    problem('Poor performance at high speed'), 
    nl, 
    write('>> remove and inspect camshaft <<'), 
    nl, 
    question_7('is the cam lobe height not within specification'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Poor handling %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

diagnosis(['steering top thread to tight', 'damage steering head bearings', 'low tire pressure']) :- 
    problem('Poor handling'), 
    question_1('is steering heavy'). 

diagnosis(['excecive wheel bearing play', 'bent rim', 'improperly installed wheel hub', 
    'excessively worn swingarn pivot bushing', 'bent frame']) :- 
    problem('Poor handling'), 
    question_2('either wheel is wobbling'). 

diagnosis(['front and rear wheels not aligned', 'bent fork', 'bent swingram', 'bent axle', 
    'bent frame', 'axle alignment (chain adjustment not equal on both sides)']) :- 
    problem('Poor handling'), 
    question_3('motorcycle pulled to one side'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%% Battery is damage or weak %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

diagnosis(['Faulty battery']) :- 
    problem('Battery is damage or weak'), 
    nl, 
    write('>> remove battery <<'), 
    nl, 
    write('>> check the battery condition using the recommended battery tester <<'), 
    nl, 
    write('>> RECOMMENDED BATTERY TESTER: BM-210 or BATTERY MATE or equivalent <<'), 
    nl, 
    question_1('is the battery in bad condition'). 

diagnosis(['faulty charging oil']) :- 
    problem('Battery is damage or weak'), 
    nl, 
    write('>> check the alternator charging coil <<'), 
    nl, 
    question_2('is the alternator charging coil resistence within 0.2 - 1.0 ohms (20 degree celcius/68 degree fahrenheit'). 

diagnosis(['faulty battery']) :- 
    problem('Battery is damage or weak'), 
    nl, 
    write('>> measure and record the battery voltage using a digital multimeter <<'), 
    nl, 
    write('>> start the engine <<'), 
    nl, 
    write('>> measure the charging voltage <<'), 
    nl, 
    write('>> compare the measurement to result of the following calculation <<'), 
    nl, 
    write('>> STANDARD: measure battery voltage < measure charging voltage < 15.0V <<'), 
    nl, 
    question_3('is the measure charging voltage within the standard voltage'). 

diagnosis(['faulty regulator/rectifier']) :- 
    problem('Battery is damage or weak'), 
    nl, 
    write('>> check the voltage and resistance at the regulator/rectifier connectors <<'), 
    nl, 
    question_4('are the results of the checked voltage and resistence correct?'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%% Starter motor does not turn %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

diagnosis(['replace the fuse']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    write('>> check for blown fuse <<'), 
    nl, 
    question_1('is the fuse blown'). 

diagnosis(['Recharge or replace the battery']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    write('>> make sure the battery is fully charge and in good condition <<'), 
    nl, 
    question_2('is the battery in bad condition'). 

diagnosis(['loose or poor contract connector', 'open circuit in wire harness', 
      'faulty starter switch']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    write('>> disconnect the starter relay switch connector and check for <<'), 
    nl, 
    write('>> continuity between yellow/red wire(ground line) and ground <<'), 
    nl, 
    question_3('is there no continuity while pushing the starter'). 

diagnosis(['faulty ignition switch', 'loose or poor contract connector', 
       'open circuit in wire harness']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    write('>> with the ignition switch turned ON, measure the voltage at the <<'), 
    nl, 
    write('>> starter relay switch connector(between black(+) and body ground(-) <<'), 
    nl, 
    question_4('is there no battery voltage'). 

diagnosis(['low battery voltage', 'poorly connected battery terminal(positve) cable', 
      'poorly connected starter motor cable', 'faulty starter motor', 
       'poorly connected battery ground(negative) cable']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    question_5('does the started motor turn slowly'). 

diagnosis(['Starter motor running backwards (case assembled improperly and/or terminals connecter improperly)', 
      'faulty starter clucth', 
      'damage or faulty starter idle gear and/or reduction gear']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    question_6('does starter motor turns, but engine does not turn'). 

diagnosis(['crankshaft does not turn due to engine problems']) :- 
    problem('Starter motor does not turn'), 
    nl, 
    question_7('does the starter relay switch "Clicks" but engine does not turn on'). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%% This is where the asking starts &&&&&&&&&&&&&&&&&&& 
problem(X):- menuask(problem,X,['Engine does not start or is hard to start', 
       'Engine lacks power', 'Poor performance at low and idle speed', 
       'Poor performance at high speed','Poor handling', 
       'Battery is damage or weak', 'Starter motor does not turn']).   % shows menu 
question_1(X):- ask(question_1,X). 
question_2(X):- ask(question_2,X). 
question_3(X):- ask(question_3,X). 
question_4(X):- ask(question_4,X). 
question_5(X):- ask(question_5,X). 
question_6(X):- ask(question_6,X). 
question_7(X):- ask(question_7,X). 
question_8(X):- ask(question_8,X). 
question_9(X):- ask(question_9,X). 
question_10(X):- ask(question_10,X). 
question_11(X):- ask(question_11,X). 
question_12(X):- ask(question_12,X). 

% "ask" only deals with simple yes or no answers. a "yes" is the only 
% yes value. any other response is considered a "no". 

ask(Attribute,Value):- 
    known(yes,Attribute,Value),  % succeed if we know its true 
    !.        % and dont look any further 
ask(Attribute,Value):- 
    known(_,Attribute,Value),   % fail if we know its false 
    !, fail. 

ask(Attribute,_):- 
    (known(yes,Attribute,_)),   % fail if we know its some other value. 
    !, fail.       % the cut in clause #1 ensures that if 
            % we get here the value is wrong. 
ask(A,V):- 
    write(A:V),      % if we get here, we need to ask. 
    write('? (yes or no): '), 
    read(Y),       % get the answer 
    asserta(known(Y,A,V)),   % remember it so we dont ask again. 
    Y = yes.       % succeed or fail based on answer. 

% "menuask" is like ask, only it gives the user a menu to to choose 
% from rather than a yes on no answer. 

menuask(Attribute,Value,_) :- 
    known(yes,Attribute,Value),  % succeed if we know 
    !. 
menuask(Attribute,_,_) :- 
    known(yes,Attribute,_),   % fail if its some other value 
    !, fail. 

menuask(Attribute,AskValue,Menu):- 
    nl,write('What is the '),write(Attribute),write('?'),nl, 
    display_menu(Menu), 
    write('Enter the number of choice> '), 
    read(Num),nl, 
    pick_menu(Num,AnswerValue,Menu), 
    asserta(known(yes,Attribute,AnswerValue)), 
    AskValue = AnswerValue.   % succeed or fail based on answer 

display_menu(Menu) :- 
    disp_menu(1,Menu), !.    % make sure we fail on backtracking 

disp_menu(_,[]). 
disp_menu(N,[Item | Rest]) :-  % recursively write the head of 
    write(N),write(' : '),write(Item),nl, % the list and disp_menu the tail 
    NN is N + 1, 
    disp_menu(NN,Rest). 

pick_menu(N,Val,Menu) :- 
    integer(N),      % make sure they gave a number 
    pic_menu(1,N,Val,Menu), !.  % start at one 
    pick_menu(Val,Val,_).    % if they didn't enter a number, use 
            % what they entered as the value 

pic_menu(_,_,none_of_the_above,[]). % if we've exhausted the list 
pic_menu(N,N, Item, [Item|_]).  % the counter matches the number 
pic_menu(Ctr,N, Val, [_|Rest]) :- 
    NextCtr is Ctr + 1,    % try the next one 
    pic_menu(NextCtr, N, Val, Rest). 
+4

问题是什么? – svick 2012-03-05 10:21:34

+0

我应该如何为用户插入一个帮助选项,因为这个程序接受的唯一答案是一个是或否,我的大脑已经在流血,截止日期快到了,所以我不能想得很好 – philip 2012-03-05 10:45:32

回答

2

只需调整你的ask/2处理也是帮助:

ask(A,V):- 
    write(A:V),      % if we get here, we need to ask. 
    write('? (yes, help or no): '), 
    read(Y),       % get the answer 
    ( Y = help 
    -> help_some(A,V), ask(A,V) 
    ; 
    asserta(known(Y,A,V)),   % remember it so we dont ask again. 
    Y = yes 
). 
+0

我应该在哪里放置help_some谓词?在诊断谓词里面?或者我应该在外面做一个?我应该如何申报? help_some('发动机不启动或难以启动', '有没有燃料到达化油器): - 写入('打印此处的帮助')。 这一个似乎并没有工作 – philip 2012-03-05 15:23:47

+0

我finaly得到它。谢谢... :) – philip 2012-03-05 15:34:27