SWI-Prolog - 从动态数据库中检索信息

时间:2018-02-17 23:42:08

标签: prolog

我正在试验SWI-Prolog,并且还没有弄清楚如何使用assertz检索保存在动态数据库中的信息。在下面的代码中,我创建了一个简单的问答专家系统,可以缩小无功能灯的来源。

在驱动程序(go)中,我尝试记住用户输入对电源是否正常工作的响应。如果电源不起作用,则应采取某些措施。我没有正确检索和定义变量' Response'。

动态变量powerstatus旨在保持用户的响应(即使它不是)。动态变量remember仅用于保持用户的响应。后者出现了问题,因为回答“不”。到了#34;电源不工作"不更新数据库。如果用户回答是,则仅填充列表(powerstatus)。

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').

更新我正在尝试新的事情 - 如果用户回答否,请设置问题(问题)功能的新规则。问题是规则(问题(对象)和mat_issue(对象)越来越短,并没有遍历所有规则。

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').

1 个答案:

答案 0 :(得分:1)

为了好玩,我已经对您的代码进行了一些更改,关于积极的否定知识,因为我觉得两者都有同样的问题积极和消极的形式是应该避免的。看看它们是否符合您的需求。

issue(fuse) :-
  is_false('Fuse is working').
issue(circuit) :-
  is_true('Fuse is working'),
  is_true('Circuit breaker is tripped').
issue(failure) :-
  is_true('Fuse is working'),
  is_false('Circuit breaker is tripped'),
  is_true('Power network failure').

mat_issue(bulb) :-
  is_false('Bulb is working').
mat_issue(powerswitch) :-
  is_true('Bulb is working'),
  is_false('Power switch is working').
mat_issue(powerswitch) :-
  is_true('Bulb is working'),
  is_true('Power switch is working'),
  is_false('Plug is working').
mat_issue(cord) :-
  is_true('Bulb is working'),
  is_true('Power switch is working'),
  is_true('Plug is working'),
  is_false('Cord is 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_false('Outlet is working').

is_true(X) :- remember(X,A),!,yes(A).
is_true(X) :- ask(X,A),yes(A).
is_false(X) :- \+is_true(X).

yes("yes").
yes("y").

:-dynamic remember/2.

ask(Question,Response) :-
  write(Question), write('? '),
  read_string(user_input,"\n","\r",_,Response),
  assertz(remember(Question,Response)).

go:-
  retractall(remember(_,_)),
  ( is_false('Power is working')
  ->    mat_issue(Object)
  ;     issue(Object)
  ),
  write('I guess that the problem is: '),
  write(Object), nl.
go:-
  write('I cannot recognize your issue').

示例会话

?- go.
Power is working? y
Fuse is working? y
Circuit breaker is tripped? n
Power network failure? y
I guess that the problem is: failure