我有一个类似于的数据库:
item(coin, [gold, metal, round]).
....
item(ball, [sphere, bouncy, -red]). % notice the negated property
我的目标是找到与给定属性匹配的项目。
问题所在的一个例子:
?- findall(I, (item(I, P), properties_true(P, [gold], [], match)), Items).
Items = [coin, ball]
findall/3
正在返回所有项目。在这种情况下,目标是只取回黄金物品。
我怀疑这是因为item/2
实际上是返回所有项目,因此将其添加到项目中。我的假设是这properties_true/4
将是错误的ball
,因此它不会被添加到Items
.
有人能指出我正确的方向吗?
以下是我正在使用的规则:
%% properties_true(Prop, True, False, SearchType)
%
% Prop = the items to search
% True = List of properties that must be in Prop
% False = List of items that mustn't be in Prop
% SearchType = match|solve
% * solve = Return true if item has all of True and none of False
% * match = Return true if item has 0 or more of true and none of False
%
%
properties_true([-Property|Rest], True, False, SearchType) :-
!, property_true(-Property, True, False, SearchType),
properties_true(Rest, True, [Property|False], SearchType), !.
properties_true([Property|Rest], True, False, SearchType) :-
property_true(Property, True, False, SearchType),
properties_true(Rest, [Property|True], False, SearchType), !.
%% property_true/4
%
properties_true([], _, _, _) :- !.
property_true(-Property, _, False, solve) :-
!, member(Property, False).
property_true(-Property, True, _, match) :-
!, not(member(Property, True)).
property_true(Property, True, _, solve) :-
!, member(Property, True).
property_true(Property, _, False, _) :-
!, not(member(Property, False)).
property_true(_ , [], [], solve) :- !. % dont think i really need this
示例用法:
?- properties_true([sphere, bouncy, -red], [], [], match).
true.
?- properties_true([sphere, bouncy, -red], [], [], solve). % solve needs to match all properties
false.
?- properties_true([sphere, bouncy, -red], [sphere, bouncy], [red], solve).
true.