Prolog query with multiple results in JPL

I want to create a knowledge base for AI in Prolog. First I want to try and learn Prolog and use it by doing the toy example with elephants, giant ants, etc.

I am using: NetBeans 8.0.1, SWI-Prolog 6.6.6 and Windows 8.1. Everything is 64 bit and the environment variables are set correctly. I also linked to jpl.jar

in my NetBeans library.

My Prolog knowledge base looks like this:

bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).
bigger(monkey, ant).
bigger(monkey, dog).
bigger(giant_ant, elephant).

is_bigger(X, Y) :- bigger(X, Y).
is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).

      

Inspired by this one . The knowledge base works without errors in SWI-Prolog.

When trying to get multiple answers when X is larger than ant in Java, however, I ran into an error.

These are snippets of Java code that uses JPL.

Query q1 =
    new Query(
        "consult",
        new Term[] {new Atom("pathToFile\\bigger.pl")}
    );

System.out.println( "consult " + (q1.query() ? "succeeded" : "failed"));

Query q2 =
    new Query(
        "bigger",
        new Term[] {new Atom("giant_ant"),new Atom("elephant")}
    );
Boolean resp= q2.query();
System.out.println("bigger(elephant, horse) is " + resp.toString());

Query q3 =
    new Query(
        "is_bigger",
        new Term[] {new Atom("giant_ant"),new Atom("ant")}
    );

System.out.println(
    "is_bigger(giant_ant, ant) is " +
    ( q3.query() ? "provable" : "not provable" )
);

Query q4 = new Query("is_bigger(X, ant)");

java.util.Hashtable solution;

q4.query();

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

      

The Java code was mostly taken from here .

From what I can glean from the debugger in NetBeans, the error seems to happen when Java tries to determine the value solution.get("X")

at the end of the code snippet.

This is what my console output looks like:

consult succeeded
bigger(elephant, horse) is true
is_bigger(giant_ant, ant) is provable
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at jpl.Term.getTerm(Term.java:614)
at jpl.Term.getTerm(Term.java:652)
at jpl.Term.getTerm(Term.java:652)
at jpl.Term.getTerm(Term.java:652)
at jpl.Term.getTerm(Term.java:652)
at jpl.Query.get1(Query.java:334) (and many more like this.)

      

Any ideas on how to solve this? It was my pleasure to provide you with additional information.

+3


source to share


1 answer


I can't get it to run JPL on my machine anymore, but looking at the docs you can use an object Variable

and build your query like I did for q3

:



Variable X = new Variable("X");
Query   q4 = new Query("is_bigger", new Term[]{X, new Atom("ant")});

while ( q4.hasMoreElements() ) {
 java.util.Hashtable solution = (Hashtable) q4.nextElement();
 System.out.println( "X = " + (Term) solution.get("X"));
}

      

0


source







All Articles