1

I want to create an object in java:

MyObject obj = new MyObject ();

and I want to pass it to prolog with a jpl query.

How can I accomplish java to prolog object passing?

I know that I could use jpl_new in a prolog file like this:

execMethod :-
  jpl_new('my_package.MyObject', [], Object),
  jpl_call(Object, myMethod, [], _ ).

But, I want to avoid the jpl_new call and just use the jpl_call with the java object obj.

And converserly, How can I accomplish prolog to java object passing?

I mean passing to java, objects created with a jpl_new call.

In other words, I want to share an object state between java and prolog.

Kami
  • 1,079
  • 2
  • 13
  • 28

1 Answers1

0

To access a Prolog knowledge base from within Java, you can use JPL Queries. Let's look at a simple, trivial example below:

% Knowledge base (Prolog)
foo(x,bar).

all_foo(X,Y) :- foo(X,Y).

In java, we could then write:

String query = "all_foo(x,Y)";
System.out.println("First solution: " + Query.oneSolution(query).get("Y"));

which would return 'bar' as answer in Y.

Vice versa -as you showed in your question- JPL can be used when we want to access Java functionality from within a Prolog file.

Firstly, looking at the docs of jpl_call/4, we see that its first arguments can be:

  • a type, class object or classname (for static methods of the denoted class, or for static or instance methods of java.lang.Class)
  • a class instance or array (for static or instance methods)

So you are free in how to pass your class information to jpl_call/4 to execute certain methods.

Subsequently, you can access your Java model rather than executing logic by using jpl_get/3. An example below is shown where we bind the Prolog variable Colour to a reference of a field of a Java car object held in the static final .colour field of the example.class.car class.

jpl_get('example.class.car', colour, Colour)

More generally:

jpl_get(+Class_or_Object, +Field, -Datum)

Hope this helped.

Good luck!

SND
  • 1,552
  • 2
  • 16
  • 29