We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › java-prolog problem
Page Index Toggle Pages: 1
java-prolog problem (Read 518 times)
java-prolog problem
Feb 26th, 2008, 4:40am
 
I am using the jpl (java prolog) library that is included with SWI-Prolog. It only works partially, and I wonder what could be wrong. Here is the code (basically, the standard code from the SWI web site). In this code, test.pl is a prolog source file with a small database.

<code begins>

import jpl.fli.*;
import jpl.*;
import java.util.Hashtable; //I guess this is superfluous...

Atom x = new Atom("test.pl" );

Term arg[] = { x } ;

Query q1 = new Query( "consult", arg );
println(q1);
println( "consult " + (q1.query() ));


Query q2 =
   new Query(
       "child_of",
       new Term[] {new Atom("joe"),new Atom("ralf")}
   );


println(
   "child_of(joe,ralf) is " +
   ( q2.query() ? "provable" : "not provable" )
);


//the code works well up to this point.

Variable X = new Variable();

Query q4 =
   new Query(
       "child_of",
       new Term[] {new Atom("joe"), X}
   );


Hashtable solution;
solution = q4.oneSolution();

println( "X = " + solution.get(X));

<code ends>

Here is the response from the command prompt:

consult( 'test.pl' )
% test.pl compiled 0.00 sec, 1,088 bytes
consult true
child_of(joe,ralf) is provable
X = null

The last statement is buffling. Why should X be null?

Any help would be greatly appreciated!!!!!!








Re: java-prolog problem
Reply #1 - Feb 26th, 2008, 5:07am
 
And here is my response to my own question. I have been struggling with this for days, and right after I decide to post, the solution strikes me.... It is really, really, really simple. Many apologies for wasting everybody's time, but here is the correct code. I have added comments to indicate the changes that made all the difference.... This might help people out, in case they want to try to do what I have done, since the online SWI-Prolog doc is wrong (!!!!).

<code begins>
import jpl.fli.*;
import jpl.*;

Atom x = new Atom("test.pl" );

Term arg[] = { x } ;

Query q1 = new Query( "consult", arg );
println(q1);
println( "consult " + (q1.query() ));


Query q2 =
   new Query(
       "child_of",
       new Term[] {new Atom("joe"),new Atom("ralf")}
   );
println(
   "child_of(joe,ralf) is " +
   ( q2.query() ? "provable" : "not provable" )
);


//The problem is the next statement. The code in the SWI web site is:
// ....new Variable(X).
//This is wrong: use double quotes instead!

Variable X = new Variable("X");

Query q4 =
   new Query(
       "child_of",
       new Term[] {new Atom("joe"), X}
   );

println(q4);

Hashtable solution;
solution = q4.oneSolution();


//again: use solution.get("X") --include the double quotes!
println( "X = " + solution.get("X"));

java.util.Hashtable[] solutions = q4.allSolutions();
for ( int i=0 ; i<solutions.length ; i++ ) {
//again, use get("X")...
   println( "X = " + solutions[i].get("X"));
}

<code ends>

If anybody wants to know all of the steps needed to make jpl into a library for prolog, I am happy to post them (as far as I can remember what I  have done...)









Page Index Toggle Pages: 1