java – 使用JPL查询Prolog变量

我想通过JPL在java中使用Prolog进行查询,我阅读了文档(http://www.swi-prolog.org/packages/jpl/java_api/getting_started.html)
序言谓词是:

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));
        }

根据我的prolog谓词,我的java代码应该检索’mary’和’steve’,但我明白了:

X = null
X = null

我做错了什么?提前致谢

编辑:这是我的整个测试

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控制台中获得的结果

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)

解决方法:

我会尝试按名称变量:

solution.get("X")

编辑

用像这样的文字查询

查询q4 =新查询(“descendent_of(X,joe)”)

上一篇:如何在java中使用prolog?


下一篇:在Java EE Web应用程序中使用JPL(Java Prolog)