如何列出具有特定原子的所有谓词? [英] How to list all predicates that has an specific atom?
问题描述
问这个问题的另一种方法是:
another way to ask the question is:
如何列出原子的所有属性?
例如:
movie(agora).
director(agora, 'Alejandro Amenabar')
duration(agora, '2h').
因此,我希望收到所有带有论证的谓词.在这种情况下,它将是:电影,导演,时长以及其他参数("Alejandro Amenabar","2h").
so, I will like to receive all the predicates that has agora for argument. In this case it will be: movie, director, duration, with the other parameters ('Alejandro Amenabar', '2h').
I found: this, and this questions, but I couldn't understand well.
如果PersonInvited不喜欢电影中的某些内容,我希望在变量答案"中具有false值.
I want to have value of false in the "variable Answer" if PersonInvited doesn't like something about the movie.
我的查询将是:
answer(Answer, personInvited, personWhoMadeInvitation, Movie)
答案:我不喜欢这位导演
answer(false, personInvited, personWhoMadeInvitation, Movie):-
director(Movie, DirectorName),not(like(personInvited,DirectorName)).
例如,任何类型的属性都会发生同样的事情.
The same thing will happen with any property like genre, for example.
答案:我不喜欢这种类型
answer(false, personInvited, personWhoMadeInvitation, Movie):-
genre(Movie, Genre), not(like(personInvited,Genre)).
因此,我想对此情况进行概括,而不是重复编写每个对象的每个功能.
So, I want to generalize this situation, instead of write repeatedly every feature of every object.
推荐答案
从我的角度来看,我发现了两个解决方案比较干净的解决方案,但是它们是不同的.
I found two solutions the 2nd is cleaner from my point of view, but they are different.
参数:
- PredName :谓词的名称.
- Arity :谓词的Arity.
- ParamValue :如果我想按一个特定参数进行过滤.
- PosParam :这是参数在谓词中的位置.
- ListParam :posibles values参数的所有值(必须始终为变量).
- PredName: Name of the predicate.
- Arity: The Arity of the Predicate.
- ParamValue: If I want to filter by one specific parameter.
- PosParam: Which is the position of the parameter in the predicate.
- ListParam: All the value of the posibles values parameters (mustbe a Variable all the time).
解决方案1 :
filter_predicate(PredName, Arity, ParamValue,PosParam, ListParam):-
current_predicate(PredName/Arity),
Arity >= PosParam,
nth(PosParam, ListParam, ParamValue),
append([PredName], ListParam, PredList),
GlobalArity is Arity + 1,
length(PredList, GlobalArity),
Predicate =.. PredList,
Predicate.
查询
filter_predicate(PredName, Arity, agora, 1, Pm).
输出
Arity = 2
Pm = [agora,'Alejandro Amenabar']
PredName = director ?
yes
解决方案2 :
filter_predicate(PredName, Arity, ParamList):-
current_predicate(PredName/Arity),
append([PredName], ParamList, PredList),
GlobalArity is Arity + 1,
length(PredList, GlobalArity),
Predicate =.. PredList,
Predicate.
查询1:
filter_predicate(PredName, Arity, [agora, X]).
输出
Arity = 2
PredName = director
X = 'Alejandro Amenabar' ?
查询2:
filter_predicate(PredName, Arity, [X, 'Alejandro Amenabar']).
输出
Arity = 2
PredName = director
X = agora ?
这篇关于如何列出具有特定原子的所有谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!