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.