How to share an object state between java and prolog with JPL? - java

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.

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!

Related

When are dot operators required?

When calling a method, I get that you have to use instanceName.method() or className.method(). However, in some cases the instanceName or className is omitted in the code and just method() is written.
Programming language is Java. Just covering this for the AP Computer Science test and I have relatively limited knowledge of coding outside of the parameters of the course so a easy to understand explanation would be greatly appreciated.
My book says something about client programs but I'm not exactly sure what it means (both in general and about client programs specifically).
I'll put my explanation as simply as possible - Usually you would use instanceName.method() when trying to effect the variables within a class. For example a "Cat" object, you could make a cat - Cat catOne = new Cat() and then use its methods catOne.setName("Kitty");. This will set this objects name to "Kitty", leaving all other cat objects with the ability to have their own unique name.
Using className.method() is done when using a static method within a class, eg public static int method(), and then using it in another class. This does not require you to instantiate an object for that class, and can use them willingly. For example, having a class called MathConstants and using something like MathConstants.getPi() ( Sorry for the crude example ).
When methods are called like methodName() , this means that the method is located within the class itself. Usually we use this , as in this.methodName(), but just using methodName() is okay.
Hope that is easy to understand

Create an object from the contents of a variable java

How do I Create an object from the contents of a variable in Java?
For example: if the string variable "name" has a value of "Margaret"
and I apply this constructor
Name nx= new Name();
I want Java to know that I´m refering to the content of name. Therefore Java will know that I'm refering to Margaret
What I want is to create Dynamic objects without the name fixed.
I can do this in PHP but I'm new in Java and don't know if is it possible.
Thanks
Dynamic instantiation of Java classes is far more complicated than doing the same thing in PHP. In PHP you'd just use eval(), i guess.
You can get an instance of the Class object by using Class.forName. Afterwards, you would have to instantiate the object by using the Class.newInstance:
Class cls = Class.forName(name);
Object obj = cls.newInstance();
This is the simplest case. If the constructor needs parameters, you would have to get the constructor method and call it.

How to cast variables in python with jcc

In java it is possible to cast an object onto a class.
An good example is found here
Object aSentenceObject = "This is just a regular sentence";
String aSentenceString = (String)aSentenceObject;
I have a program that needs to integrate some java with python. I am trying to do this via the JCC library. The problem that I am encountering is that with JCC, all of the java classes are loaded into the imported library that I created with JCC. So I can create an instance of the base class by passing the necessary argument to the constructor of the java class.
obj = javaLibrary.BaseClass('foo')
However, in my code I need to be able to cast this object onto a a more “specific” type of Object.
How can I accomplish this in python with JCC? It seems like it may be impossible because python is dynamically typed, but that is why I am asking this question.
All comments above valid, but to be specific for your case:
casted_obj = Object.cast_(obj)

Rhino - Passing a javascript object to java

Im quite new to Rhino and my question is around how to achieve the following,
Say I have a javascript object that follows something like the following that I can consume within java.
var myObject = new Object();
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.int1 = 1;
But how do I consume this within java if its dynamic. For .e.g. if you decide to add few more members dynamically to this object within javascript. Is this doable ? My guess is the class defined within java will have to take all the possible members into account to do this ?
Hopefully I have explained what im trying to achieve correctly.
JavaScript objects, when you access them in Java, are all essentially the same class: ScriptableObject which implements the Scriptable interface (GitHub source). There are a few other classes for functions and other specialized objects.
The Scriptable interface includes methods like get, has, and put that correspond roughly to myObject.string1, myObject.hasOwnProperty("string1"), and myObject.string1 = "Hello" in JavaScript. The ScriptableObject class adds some other useful methods for defining properties, etc.
Consider using a library like GSON for converting a javascript object to JAVA.
https://code.google.com/p/google-gson/
you can convert a javascript object to JSON using JSON.stringify
and then use GSON or another such library to generate a Java object.

Getting a variable using a variable

How would I get a variable in another class using a variable in the current class? For example: The variable "userclass" Can either be human or alien. Inside my other class ("Cv.java") there are two variables (human, alien) How would I get one of the two variables in Cv.java, while using the "userclass" variable to get it.
Example 2:
userclass = alien
Cv.????
How would I get Cv.alien whilst using "userclass"
You can use reflection to do it, although it might hurt the performance.
A better way is to use a Enum to represent the userclass instead of using a String.
how about using class.forName :
Object o = Class.forName(userclass)
this assuming your userclass is a string.

Categories

Resources