java - 使用 JPL 查询 Prolog 变量

标签 java prolog swi-prolog jpl

我想查询通过 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)

最佳答案

我找到了解决方案,我必须像这样使用 Compound 类(包含在 jpl 中)

Query q4 = new Query(new Compound("descendent_of", new Term[] { new Variable("X"), new Atom("joe")}));

while ( q4.hasMoreSolutions() ){
            solution = q4.nextSolution();
            System.out.println( "X = " + solution.get("X"));
        }

我得到了解决方案

X = mary
X = steve

关于java - 使用 JPL 查询 Prolog 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11494747/

相关文章:

java - 使用滑动加载图像并根据图像宽度更改 ImageView 宽度

Prolog:缺少功能?

prolog - 计算机功能必须满足哪些要求才能被视为 "monotonic"?

带有变量的 Prolog DCG

macros - 在 SWI-Prolog 中编写宏

html - 如何在 SWI-Prolog 中处理 POST 请求?

prolog - 使用 Prolog CLPFD 实现 32 位数字的 XOR 功能

java - 从 java webstart 应用程序中的 jar 获取资源

java - 构建时找不到slf4j LoggerBinder

java - Apache httpd 无法提供 Tomcat 生成的文件