【发布时间】:2018-02-20 00:47:35
【问题描述】:
我正在尝试使用 SWI-Prolog,但还没有弄清楚如何使用 assertz 检索保存在动态数据库中的信息。在下面的代码中,我正在创建一个简单的问答专家系统,以缩小故障灯的来源。
在驱动程序(go)中,我尝试记住用户对电源是否正常工作的输入响应。如果电源不工作,它应该采取某些措施。我没有正确检索和定义变量“响应”。
动态变量 powerstatus 旨在保持用户的响应(即使它是否)。动态变量 remember 旨在仅在用户回答是时才保留用户的响应。后者出了点问题,因为对“电源不工作”回答“否”不会更新数据库。仅当用户回答“是”时才会填充列表(电源状态)。
status(power) :-
is_true('Power is not working').
issue(fuse) :-
is_true('Fuse is not working').
issue(circuit) :-
is_true('Fuse is working'),
is_true('Circuit breaker is tripped').
issue(failure) :-
is_true('Fuse is working'),
is_true('Circuit breaker is not tripped'),
is_true('Power network failure').
mat_issue(bulb) :-
is_true('Bulb is not working').
mat_issue(powerswitch) :-
is_true('Bulb is working'),
is_true('Power switch is not working').
mat_issue(powerswitch) :-
is_true('Bulb is working'),
is_true('Power switch is working'),
is_true('Plug is not working').
mat_issue(cord) :-
is_true('Bulb is working'),
is_true('Power switch is working'),
is_true('Plug is working'),
is_true('Cord is not working').
mat_issue(cord) :-
is_true('Bulb is working'),
is_true('Power switch is working'),
is_true('Plug is working'),
is_true('Cord is working'),
is_true('Outlet is not working. Replace outlet.').
is_true(X) :- remember(X,"yes"),!.
is_true(X) :- ask(X).
:-dynamic remember/2.
:-dynamic powerstatus/2.
ask(Question) :-
write(Question), write('? '),
read_string(user_input,"\n","\r",_,Response), Response == "yes",
assertz(remember(Question, Response)),
assertz(powerstatus(Question, Response)).
go:-
retractall(remember(_,_)),
retractall(powerstatus(_,_)),
status(Power),
powerstatus('Power is not working', Response),
format('Response is ~w', Response).
(Response = "no" ->
mat_issue(Object)
;issue(Object)),
write('I guess that the problematic object is: '),
write(Object), nl.
go:-
write('I cannot recognize your issue').
更新我正在尝试一些新的东西——如果用户回答“否”,则为 ask(Question) 函数设置新规则。问题是规则 (issue(object) 和 mat_issue(object) 被缩短并且没有遍历所有规则。
status(power) :-
is_true('Power is not working').
issue(fuse) :-
is_true('Fuse is not working').
issue(circuit) :-
is_true('Fuse is working'),
is_true('Circuit breaker is tripped').
issue(failure) :-
is_true('Fuse is working'),
is_true('Circuit breaker is not tripped'),
is_true('Power network failure').
mat_issue(bulb) :-
is_true('Bulb is not working').
mat_issue(powerswitch) :-
is_true('Bulb is working'),
is_true('Power switch is not working').
mat_issue(powerswitch) :-
is_true('Bulb is working'),
is_true('Power switch is working'),
is_true('Plug is not working').
mat_issue(cord) :-
is_true('Bulb is working'),
is_true('Power switch is working'),
is_true('Plug is working'),
is_true('Cord is not working').
mat_issue(cord) :-
is_true('Bulb is working'),
is_true('Power switch is working'),
is_true('Plug is working'),
is_true('Cord is working'),
is_true('Outlet is not working. Replace outlet.').
is_true(X) :- remember(X,"yes"),!.
is_true(X) :- ask(X).
:-dynamic remember/2.
ask(Question) :-
write(Question), write('? '),
read_string(user_input,"\n","\r",_,Response), Response == "yes",!,
assertz(remember(Question, Response)).
ask(Question) :-
assertz(remember(Question, "no")).
go:-
retractall(remember(_,_)),
status(Power),
remember('Power is not working', Response),
(Response = "no" ->
mat_issue(Object);
issue(Object)),
write('I guess that the problem is: '),
write(Object), nl.
go:-
write('I cannot recognize your issue').
【问题讨论】:
-
@TomasBy 我希望检索的问题是“电源无法正常工作”...不是电源开关。
-
@TomasBy 我愿意不使用该字符串,但我看不出它还能如何?
-
@TomasBy status(power) 没有这样做。
标签: prolog