3

I want to build a knowledge base for an AI in Prolog. First I want to try to learn Prolog and it's uses by doing a 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-bits and the environment variables are correctly setup. I have also linked to the 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. The knowledge base works without any errors in SWI-Prolog.

When trying to get the multiple answers when X is bigger than ant in Java, however, I run into a stack overflow error.

This is snippets from my Java code where JPL is being used.

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 get out from the debugger in NetBeans the stack overflow error seems to happen when Java is trying to determine the value of 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? I'll gladly provide more information if needed.

Community
  • 1
  • 1
m0rch
  • 31
  • 4

1 Answers1

0

I can't get it to run JPL on my machine anymore, but from looking at the docs you might want to use a Variable object and construct your query like your 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"));
}
Patrick J. S.
  • 2,885
  • 19
  • 26
  • Doing this seems to hide the error. Instead of having the stack overflow error in the console I get five instances (the correct amount of solutions!) of X = null. However if I use break points and try to "manually" see what the solution stored is I get the same stack overflow error after a while in the box where the value should be displayed. I also tried using Compound like in the second solution in the thread I linked to in my question, same error. – m0rch Oct 08 '14 at 15:13
  • Sorry, I somehow lost attention of this answer. I edited in an possible solution. JPL sill isn't running in my machine. But you'll have to name the variable for sure. – Patrick J. S. Jan 03 '15 at 12:18