使用JPL查询Prolog变量 [英] Querying Prolog variables with JPL
问题描述
我想通过JPL进行查询以在Java中使用Prolog,我阅读了文档(http://www.swi-prolog.org/packages/jpl/java_api/getting_started.html) 序言谓词是:
I want to make a query to use Prolog in java through JPL, I read the documentation (http://www.swi-prolog.org/packages/jpl/java_api/getting_started.html) The prolog predicates are these:
child_of(joe, ralf).
child_of(mary, joe).
child_of(steve, joe).
child_of(steve, ralf).
descendent_of(X, Y) :-
child_of(X, Y).
descendent_of(X, Y) :-
child_of(Z, Y),
descendent_of(X, Z).
我的代码如下
Variable X = new Variable();
Query q4 =
new Query(
"descendent_of",
new Term[] {X,new Atom("joe")}
);
java.util.Hashtable solution;
while ( q4.hasMoreSolutions() ){
solution = q4.nextSolution();
System.out.println( "X = " + solution.get(X));
}
根据我的序言谓词,我的Java代码应该检索"mary"和"steve",但是我得到了:
According to my prolog predicates, my java code should retrieve 'mary' and 'steve', but I get this:
X = null
X = null
我做错了什么?预先感谢
What I'm doing wrong? thanks in advance
编辑:这是我的全部测试
Query q1 =
new Query(
"consult",
new Term[] {new Atom("C:\\Users\\cardozo\\Documents\\fer\\info2\\lore\\test.pl")}
);
return q1;
System.out.println( "consult " + (q.query() ? "succeeded" : "failed"));
Query q2 =
new Query(
"child_of",
new Term[] {new Atom("joe"),new Atom("X")}
);
Boolean resp= q2.query();
System.out.println("child_of(joe,X) is " + resp.toString()
);
Query q3 =
new Query(
"descendent_of",
new Term[] {new Atom("steve"),new Atom("ralf")}
);
System.out.println(
"descendent_of(joe,ralf) is " +
( q3.query() ? "provable" : "not provable" )
);
Variable X = new Variable();
Query q4 =
new Query(
"descendent_of",
new Term[] {X,new Atom("joe")}
);
java.util.Hashtable solution;
q4.query();
while ( q4.hasMoreSolutions() ){
solution = q4.nextSolution();
System.out.println( "X = " + solution.get("X"));
}
这就是我在Java控制台中得到的结果
And this is what I get in my java console as result
run:
% C:\Users\cardozo\Documents\fer\info2\lore\test.pl compiled 0.00 sec, 8 clauses
consult succeeded
child_of(joe,X) is false
descendent_of(joe,ralf) is provable
X = null
X = null
BUILD SUCCESSFUL (total time: 0 seconds)
推荐答案
我会尝试通过名称获取变量:
I would try to get variable by name:
solution.get("X")
修改
带有文字查询,例如
查询q4 =新查询("descendent_of(X,joe)")
Query q4 = new Query("descendent_of(X,joe)")
这篇关于使用JPL查询Prolog变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!