how easy is it and can you easily go back and forth

PROLOG PORTION FOLLOWS



% This library is all we need to call Java from Prolog

:- ensure_loaded(library(jpl)).



main

:-

% Create an object

jpl_new(class([],['Test']), [], X),



% Printing objects is like printing object ids

write(X),nl,



% Access a field of the object; happens to be static

jpl_get(X, state, Y),



% Prints whatever the state's value is

write(Y),nl.



JAVA PORTION FOLLOWS



// This jar is all we need to call Prolog from Java

import jpl.*;



public class Test {



public static int state = 0;



public static void main(String[] args) {



// Consult a Prolog file

Query q1 = new Query("consult", new Term[] { new Atom("Test.pro") });



// Prints boolean success/failure value

System.out.println(q1.query());



// Affect static field.

// This way we can see whether Prolog sees the same JVM instance.

Test.state = 42;



// Invoke a predicate

Query q2 = new Query("main");



// Prints boolean success/failure value

System.out.println(q2.query());

}

}



BUILDING and RUNNING BOTH





SO WHAT?

I would like to mix Java and Prolog code on a number of occasions. Together with a student of mine (Joachim Pehl), we just convinced ourselves that the JPL library of SWI-Prolog is really cool. Basically JPL allows you to call Prolog from Java and Java from Prolog.But the question is, of course,. For instance, can we call Prolog from Java and call back Java from Prolog and share Java state throughout? Sure it works!javac -cp $CLASSPATH:/opt/local/lib/swipl-5.6.62/lib/jpl.jar:. Test.javajava -cp $CLASSPATH:/opt/local/lib/swipl-5.6.62/lib/jpl.jar:. -Djava.library.path="/opt/local/lib/swipl-5.6.62/lib/i386-darwin9.5.0" Testtrue@(J#00000000000016809476)42trueHence, we can start a Java app, have it start a Prolog engine and consult the Prolog portion of our app, then delegate from Java to Prolog, where we in turn call back on Java in the same JVM and can access some state that we left off in Java before we were calling Prolog.PF