1

我有两个 Prolog 文件。条款和规则如下:

子句.pl

get(mary,milk).
go(sandra,kitchen,1).
get(john,football).
go(john,hallway,1).
go(mary,garden,1).
go(john,kitchen,2).

规则.pl

/* X = person Y=location T,T2= time 
This rule finds last location of a person */
isAt(X,Y) :- go(X, Y, T), \+ (go(X,_,T2), T2 > T).

/* This rule finds the last location of an object */
whereIs(Q,R) :- findall(R,(get(P,Q,I),go(P,R,_)),L), last(L,R),!.

当我通过以下方式创建查询以找出 John 在 Java 中的位置时:

//include the prolog file with clauses to test
        File clauseFile = new File ("clauses_qa2.pl");
        File ruleFile = new File ("rules.pl");


  String clausePath = clauseFile.getAbsolutePath();
    String rulePath = ruleFile.getAbsolutePath();
    System.out.println("Clause file path: " + clausePath);
    System.out.println("Rule file path: " + rulePath);
    String t1 = "consult('" + clausePath + "').";
    String t2 = "consult('" + rulePath + "').";
    jpl.JPL.init();
    Query q1 = new Query(t1);
    Query q2 = new Query(t2);

    Variable X = new Variable("_");
    Variable Y = new Variable();
    Query q = new Query("isAt",new Term[]{new Atom("john"),X,Y});
    while (q.hasMoreElements()) {
       Hashtable binding = (Hashtable) q.nextElement();
        Term t = (Term) binding.get(X);
        System.out.println(t); 
    }
    System.out.println(q.toString());

我收到以下错误:

Exception in thread "main" jpl.PrologException: PrologException: error(existence_error(procedure, /(isAt, 3)), context(:(system, /('$c_call_prolog', 0)), _1))
    at jpl.Query.get1(Query.java:336)
    at jpl.Query.hasMoreSolutions(Query.java:258)
    at jpl.Query.hasMoreElements(Query.java:472)

但是,如果我删除该 while 循环并简单地打印出查询,我会从 Prolog 得到以下响应:

Clause file path: G:\Natural Language Final Project\PrologTest\clauses_qa2.pl
Rule file path: G:\Natural Language Final Project\PrologTest\rules.pl
isAt( john, _, _0 )

所以我知道至少查询是从 Java 到 Prolog 的。关于可能导致错误的任何想法?

注意:原来我的文件路径不正确。更改代码以创建查询,如下所示:

static void
    test_1()
    {
        Variable X = new Variable();
    Term args[] = { 
        new Atom( "john" ),
        X
    };
    Query query = 
        new Query( 
            "isAt", 
            args );

        System.out.println( "iSAt(john, X) = " + query.query() );
    }
    public static void main(String[] args) throws IOException {

        //include the prolog file with clauses to test
        File clauseFile = new File ("G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl");
        File ruleFile = new File ("G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl");
        String clausePath = clauseFile.getAbsolutePath();
        String rulePath = ruleFile.getAbsolutePath();
        System.out.println("Clause file path: " + clausePath);
        System.out.println("Rule file path: " + rulePath);
        String t1 = "consult('" + "G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl"+"').";
        String t2 = "consult('" + "G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl"+"').";
        /*Scanner scan = new Scanner(ruleFile);
        while (scan.hasNextLine()){
            System.out.println(scan.nextLine());
        }*/


        jpl.JPL.init();
        Term consult_arg[] = { 
            new Atom( "G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl")
        };
        Query consult_query = 
            new Query( 
                "consult", 
                consult_arg );
        Term consult_arg2[] = { 
            new Atom( "G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl")
        };
        Query consult_query2 = 
            new Query( 
                "consult", 
                consult_arg2);

        boolean consulted = consult_query.query()&& consult_query2.query();

        if ( !consulted ){
            System.err.println( "Consult failed" );
            System.exit( 1 );
        }


        test_1();
        Variable X = new Variable("_");
        Variable Y = new Variable();
        Query q = new Query("isAt",new Term[]{new Atom("john"),X});

        while (q.hasMoreElements()) {
           Hashtable binding = (Hashtable) q.nextElement();
            Term t = (Term) binding.get(X);
            System.out.println(t); 
        }
        System.out.println(q.toString());

    }

产生以下输出:

    iSAt(john, X) = true
null
isAt( john, _ )

这比编译器错误要好,但答案应该是:

isAt(john,X)
X= kitchen
4

1 回答 1

2

我没有足够的声誉,否则我会将此作为评论...

我怀疑问题是 isAt() 的 arity 为 2,但查询使用 isAt() 和 arity 3:isAt(john, X, Y)。

于 2016-08-15T17:34:27.160 回答