我有一个知识库:
bottle(b1).
bottle(b2).
bottle(b3).
bottle(b4).
full(bottle(b1),100).
full(bottle(b2),150).
full(bottle(b3),300).
full(bottle(b4),400).
consume(bottle(X),Milliliter) :-
full(bottle(X),Y),
Milliliter=<Y,
Y-10.因此,我想使用消费谓词,我希望减少分配给full的值,就像使用的值一样多。它是否允许从静态值中减去,如果还没有消耗瓶子,我如何才能解决这个问题才能达到真值。
发布于 2016-01-07 11:27:56
如果要在调用“消费”时“更新”KB,则必须收回并断言事实,例如.
% Use this to add the initial facts (if you don;t have a clause to do this, prolog complains about modifying static clauses...)
addfacts :-
asserta(full(bottle(b1),100)),
asserta(full(bottle(b2),150)),
asserta(full(bottle(b3),300)),
asserta(full(bottle(b4),400)).
consume(bottle(X), Millis) :-
% Retract the current state of the bottle
retract(full(bottle(X), V)),
% Calculate the new Millis after consumption
Y is V - Millis,
% Check it was possible (there should be 0 or more millis left after)
Y >= 0,
% Add the new fact
asserta(full(bottle(X), Y)).现在在prolog,你可以.
1 ?- addfacts.
true.
2 ?- full(bottle(b1), X).
X = 100.
3 ?- consume(bottle(b1), 10).
true.
4 ?- full(bottle(b1), X).
X = 90 .https://stackoverflow.com/questions/34651937
复制相似问题