首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JPL查询Prolog变量

用JPL查询Prolog变量
EN

Stack Overflow用户
提问于 2012-07-15 19:05:38
回答 2查看 4.3K关注 0票数 3

我想通过JPL查询在java中使用Prolog,我阅读了文档(started.html) -- prolog谓词如下:

代码语言:javascript
复制
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).

我的代码如下所示

代码语言:javascript
复制
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',但我理解如下:

代码语言:javascript
复制
X = null
X = null

我做错什么了?提前感谢

编辑:这是我的全部测试

代码语言:javascript
复制
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控制台中得到的结果。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-15 20:00:19

我试着用名字来获取变量:

代码语言:javascript
复制
solution.get("X")

编辑

使用如下的文字查询:

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

票数 2
EN

Stack Overflow用户

发布于 2012-07-15 21:33:13

我找到了解决方案,我必须像这样使用类化合物(包括在jpl中)

代码语言:javascript
复制
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"));
        }

我得到了解决办法

代码语言:javascript
复制
X = mary
X = steve
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11494747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档