prolog专家系统很难实现

prolog专家系统很难实现,prolog,Prolog,我需要在三天内完成这项工作,所以我别无选择,只能咨询其他程序员。你知道,我学习速度很慢,而且有太多的代码让我感到困惑…:( 好的,我正在使用prolog开发一个摩托车故障诊断专家系统,我正在使用AMZI prolog作为SDK,到目前为止一切都很顺利,直到我的老师要求我在提问时添加一个帮助命令,这个帮助菜单的目的是指导用户如何在故障诊断期间执行一些测试,例如spark测试,但这取决于用户是否希望使用帮助。另一个问题是,我应该给出故障排除中所述问题的解决方案,以澄清问题。下面是我的代码运行示例:

我需要在三天内完成这项工作,所以我别无选择,只能咨询其他程序员。你知道,我学习速度很慢,而且有太多的代码让我感到困惑…:(

好的,我正在使用prolog开发一个摩托车故障诊断专家系统,我正在使用AMZI prolog作为SDK,到目前为止一切都很顺利,直到我的老师要求我在提问时添加一个帮助命令,这个帮助菜单的目的是指导用户如何在故障诊断期间执行一些测试,例如spark测试,但这取决于用户是否希望使用帮助。另一个问题是,我应该给出故障排除中所述问题的解决方案,以澄清问题。下面是我的代码运行示例:

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
欢迎来到本田XRM/Wave摩托车故障排除
请不要忘记在最后键入带句号(.)的所有答案
有什么问题?
1:发动机不起动或起动困难
2:发动机动力不足
3:低速时性能差
4:高速时性能差
5:操控性差
6:蓄电池损坏或电量不足
7:起动马达不转动
输入选项数>1。
>>检查流向化油器的燃油流量>拆下并检查火花塞1。
>>检查到化油器的燃油流量>然后提供一些有关如何执行某些测试的帮助提示,如火花测试或其他>然后在用户输入done.done后应返回到上一个问题。
问题1:是否没有燃油到达化油器?(是或否):
以下是我的完整源代码:

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).
main:-识别。
确定:-
写信(“欢迎来到本田XRM/Wave摩托车故障排除”),
荷兰,
写下(‘请不要忘记在最后键入带句号的所有答案’),
荷兰,
收回所有(已知(,,,)),%清除存储的信息
诊断(X),
荷兰,
写入('问题是/是:'),
荷兰,
writeListByLine(X)。
确定:-
荷兰,
写下(‘我无法识别问题’)。
writeListByLine([])。
writeListByLine([H | T]):-%用换行符写出答案
写(H),
荷兰,
书面署名(T)。
%%%%%%%%%%%%%%%%%%%%%%%%发动机无法启动或难以启动:%
诊断([‘燃油管路或滤网堵塞’,
“燃油箱盖堵塞”,
“浮阀卡滞”,
“燃油滤清器堵塞”]):-
问题(“发动机无法启动或难以启动”),
荷兰,

写('>>检查流向化油器的燃油流量拆下并检查火花塞按照正常程序执行火花测试启动测试气缸压缩将车轮抬离地面并用手转动检查轮胎压力从低加速到秒加速轻轻拆下并检查火花塞检查油位状况检查ig起动正时测试气缸压缩机检查化油器有无堵塞拆下缸盖罩并检查润滑检查发动机是否过热加速或高速运转检查化油器绝缘体和进气歧管是否泄漏执行火花测试检查点火正时断开化油器处的燃油管路拆卸并检查发动机零件插头检查化油器是否堵塞检查点火正时检查气门正时检查气门弹簧拆下并检查凸轮轴拆下蓄电池使用推荐的蓄电池测试仪检查蓄电池状况推荐的蓄电池测试仪:BM-210或battery MATE或同等产品检查交流发电机充电线圈测量并记录蓄电池电压使用数字式万用表起动发动机测量充电电压将测量值与以下计算标准的结果进行比较:测量蓄电池电压<测量充电电压<15.0V检查调节器/整流器接头处的电压和电阻检查保险丝是否熔断确保蓄电池充满电在点火开关打开的情况下,断开起动机继电器开关接头并检查黄色/红色导线(接地线)和接地之间的导通性,测量起动机继电器开关接头(黑色(+)和车身接地(-)之间的电压只需调整
询问/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
  ). 

我应该如何为用户插入一个帮助选项,因为这个程序接受的唯一答案是“是”或“否”,我的大脑已经在流血,最后期限很快就要到了,所以我不能很好地思考应该将“帮助”谓词放在哪里?在诊断谓词内部?还是在外部?我应该如何声明它?帮助一些(‘发动机无法启动或难以启动’,‘化油器是否没有燃油):-写(‘在此处打印帮助’)。这个似乎不起作用
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
  ).