Retrieve constrained list from knowledge base

笑着哭i 提交于 2020-12-13 05:46:10

问题


step('pancakes', 1, 'mix butter and sugar in a bowl', [butter, sugar], ['bowl']). 
step('pancakes', 2, 'add eggs', [eggs], []). 
step('pancakes', 3, 'mix flour and bakingpowder', [flour, baking_powder], []).

steps(R,Y,P):-
     findall(A,step(R,_,A,_,P),Z),
     distinctSteps(Z,Y).

distinctSteps(Z,Y) :-
     list_to_set(Z,Y).

So I have this knowledge base and a rule that should retrieve the step with the certain utensil I give as a parameter. So for example steps('pancakes', bowl, I). should return I = ['mix butter and sugar in a bowl']. However, it just gives false. If I reduce it to just R,Y it works, but that's not what I want. What am I doing wrong?


回答1:


So there might be a misunderstanding. Is your ingredients term an atom (bowl) or a list ([bowl])? I'm asking because this is the knowledge base I came up with, but I'm not sure if this meets your requirements. So I assume you use a list of utensils. Please note that the following knowledge base differs from yours (and as a result the questions as well):

step(pancakes, 1, 'mix butter and sugar', [butter, sugar], [bowl, mixer]). 
step(pancakes, 2, 'add eggs', [eggs], []). 
step(pancakes, 3, 'mix flour and bakingpowder', [flour, bakingpowder], [bowl]). 

hasUtensil(Dish, Step, Instr, Ingr, Utensil):-
    step(Dish, Step, Instr, Ingr, U),
    member(Utensil, U).

stepsUtensil(Dish,Utensil,Instructs):-
    findall(A, hasUtensil(Dish,_,A,_,Utensil), Instructs).

?- stepsUtensil(pancakes, bowl, I).
I = ['mix butter and sugar', 'mix flour and bakingpowder']

stepsUtensil/5 does simply unlist all utensils from the utensil list from step/5. It does it by just asking for all members in the utensil list. For example for the first step:

?- hasUtensil(_,1,_,_,U).
U = bowl ;
U = mixer ;
false.

If you would write hasUtensil/5 as a fact it would look as follows:

step(pancakes, 1, 'mix butter and sugar', [butter, sugar], bowl). 
step(pancakes, 1, 'mix butter and sugar', [butter, sugar], mixer). 
step(pancakes, 3, 'mix flour and bakingpowder', [flour, bakingpowder], bowl). 


来源:https://stackoverflow.com/questions/64926530/retrieve-constrained-list-from-knowledge-base

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!