所以,在这个prolog程序中我有这个“水果数据库”,这个“人员数据库”和“吃了数据库”......
%fruit(FId, Name, Type, Price).
fruit(1, 'apple' , sweet , 5).
fruit(2, 'greenApple' , bitter , 5).
fruit(3, 'grape' , sweet , 10).
fruit(4, 'peach' , sweet , 20).
fruit(5, 'orange' , citric , 5).
fruit(6, 'tangerine' , citric , 7).
fruit(7, 'banana' , sweet , 3).
fruit(8, 'lemon' , citric , 6).
fruit(9, 'bitterMelons', citric , 12).
fruit(10,'grapefruit' , citric , 8).
%. (don't mind these dots, they're meant for a better visual experience)
%person(PId, P).
person(1, 'ana').
person(2, 'john').
%.
%ate(PId, FId).
ate(1, [1, 2, 3, 4]).
ate(2, [1, 3, 5, 8]).
我也有这个规则,所以我可以查询有人吃的水果,水果类型或水果的价格。
person_ate(P, F, T, M) :-
person(PId, P),
ate(PId, FruitIds),
member(FId, FruitIds),
fruit(FId, F, T, M).
%.
person_ate_fruits(P,F, _, _) :-
findall(F,person_ate(P,F, _, _),F).
%.
person_ate_types(P, _,T, _) :-
findall(T,person_ate(P, _,T, _),T).
%.
person_ate_prices(P, _, _,M) :-
findall(M,person_ate(P, _, _,M),M).
我也有这三条规则告诉我水果便宜或昂贵,或者是“中等价位”。
cheap_fruit(FId, F, T, M) :- fruit(FId, F, T, M), M =< 6.
expensive_fruit(FId, F, T, M) :- fruit(FId, F, T, M), M > 15.
middleprice_fruit(FId, F, T, M) :- fruit(FId, F, T, M), M > 6, M =< 12.
所以,我的目标是根据有人吃的水果类型或价格来推荐水果。
例如:正如我们所见,当我查询
时person_ate_fruits('ana',F, _, _).
我可以看到Ana吃了
F = [apple, greenApple, grape, peach]
并查询
person_ate_types('ana', _,T, _).
和她吃的水果类型
T = [sweet, bitter, sweet, sweet]
好的,很漂亮,现在有不好的部分
recomend(P, R) :-
person_ate(P, F, T, M),
TcounterBitter is 0, %supposed to be a Type Counter for the Bitter Type and so are the others.
TcounterSweet is 0,
TcounterCitric is 0,
(T = sweet -> TcounterBitter is +1 ; ), %supposed to everytime i call person_ate_types('ana', _,T, _). count how many Bitter fruits the person ate and so are the others.
(T = sweet -> TcounterSweet is +1 ; ),
(T = sweet -> TcounterCitric is +1 ; ).
%.
(TcounterBitter => 3 -> recomend_fruits(P, _,T, _) ; ). %supposed to when one of the counters gets 3 or more (if one person ate more the 3 bitters, sweet or citric fruits), recommend fruits.
(TcounterSweet => 3 -> recomend_fruits(P, _,T, _) ; ).
(TcounterCitric => 3 -> recomend_fruits(P, _,T, _) ; ).
%.
recomend_fruits(P, Fruit) :-
person_ate(P, F, T, M),
fruit(FId, Fruit, T, M),
findall(Fruit,fruits(P,Fruit, _, _), Fruit /= person_ate_fruits(F)). %supposed to go through the fruit list and recommend fruits that are on the Type of fruits most ate and that the person didn't ate yet.
我很抱歉我的英语不好,这不是我的母语,我希望这是“可以理解的”。