|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--CiaoJava.PLInterpreter
This class interprets the terms received from the Prolog
side and performs the actions requested from that side.
This class is used by almost every class of the CiaoJava
package because contains the object table needed to
look up the Java objects referred by the Prolog side.
Field Summary | |
private static java.lang.String |
ADD_LISTENER
|
private static java.lang.String |
CREATE_OBJECT
|
private static java.lang.String |
DELETE_OBJECT
|
private PLEventListener |
eventListener
|
private static float |
FACTOR
|
private static java.lang.String |
GET_VALUE
|
private static java.lang.String |
INVOKE_METHOD
|
private int |
objKey
|
private java.util.Hashtable |
objTable
|
private static java.lang.String |
QUIT
|
private static java.lang.String |
REMOVE_LISTENER
|
private static java.lang.String |
SET_VALUE
|
private static int |
STARTING_CAPACITY
|
private static java.lang.String |
WAIT_FOR_EVENTS
|
Constructor Summary | |
PLInterpreter(PLConnection pl)
Creates a new interpreter, with the table of objects managed by Prolog given as argument. |
Method Summary | |
private PLTerm |
addListener(PLStructure st)
Adds the Java object event to the listener predicate. |
private PLTerm |
createObject(PLStructure st)
Creates a Java object and adds it to the object table. |
private PLTerm |
deleteObject(PLStructure st)
Deletes a Java object and removes it from the object table. |
private java.lang.reflect.Constructor |
getConstructor(java.lang.Class cl,
java.lang.Class[] clsarg)
Gets the constructor of the class cl that matches the parameter type object array. |
private int |
getDistance(java.lang.Class assignedTo,
java.lang.Class assignedFrom)
Gets a measure of the 'distance' between two assignable classes. |
private java.lang.reflect.Method |
getMethod(java.lang.Class cl,
java.lang.String mtName,
java.lang.Class[] clsarg)
Gets the method of the class cl that matches the parameter type object array. |
java.lang.Object |
getObject(java.lang.Integer hashCode)
Gets the object from the object table given the hash code. |
private PLTerm |
getValue(PLStructure st)
Gets the value of a Java object field. |
PLTerm |
interpret(PLTerm t)
Interprets the request received as argument in a PLTerm
object. |
private java.lang.Object |
invoke(java.lang.reflect.Method mt,
java.lang.Object obj,
java.lang.Object[] arg)
Invokes the method received as first argument on the object received as second argument, with the arguments of the third argument. |
private PLTerm |
invokeMethod(PLStructure st)
Invokes a Java object method. |
static boolean |
isInterpretable(PLTerm t)
Returns true if the term received as argument can be interpreted by the Prolog term interpreter. |
private PLTerm |
java_object(int i)
Prolog representation of a Java object in the object table. |
private PLTerm |
prologRepr(java.lang.Object v)
Gets the Prolog representation of the object argument. |
private PLTerm |
removeListener(PLStructure st)
Removes the Java object event from the listener predicate. |
private PLTerm |
setValue(PLStructure st)
Sets the value of a Java object field. |
private java.lang.Object |
translateArg(java.lang.Object arg,
java.lang.Class type)
Argument translation for method/constructor invocation. |
Methods inherited from class java.lang.Object |
|
Field Detail |
private java.util.Hashtable objTable
private int objKey
private static final int STARTING_CAPACITY
private static final float FACTOR
private PLEventListener eventListener
private static final java.lang.String CREATE_OBJECT
private static final java.lang.String DELETE_OBJECT
private static final java.lang.String GET_VALUE
private static final java.lang.String SET_VALUE
private static final java.lang.String INVOKE_METHOD
private static final java.lang.String ADD_LISTENER
private static final java.lang.String REMOVE_LISTENER
private static final java.lang.String QUIT
private static final java.lang.String WAIT_FOR_EVENTS
Constructor Detail |
public PLInterpreter(PLConnection pl)
pl
- Object that represents the connection to the Prolog process.Method Detail |
public PLTerm interpret(PLTerm t)
PLTerm
object. The possible requests are the following (the arguments of
the Prolog terms described are marked as input or output arguments
with the plus or minus sign; and the output arguments are not
actually included in the terms, only show what requests send
back results to Prolog):
$java_create_object(+NAME, +ARGUMENTS, -OBJECT)
Creates a new Java instance of the class
given as NAME (full class name), using
the appropriate constructor to the arguments
given in ARGUMENTS, and returns to Prolog an object
identifier.
$java_delete_object(+OBJECT)
Deletes the object with identifier equal
to OBJECT.
$java_get_value(+OBJECT, +FIELD, -VALUE)
Gets the value of the field FIELD from the
object OBJECT, and returns to Prolog its value.
$java_set_value(+OBJECT, +FIELD, +VALUE)
Sets the value of the field FIELD of the
object identified with the OBJECT reference to the
value received as VALUE.
$java_invoke_method(+OBJECT, +METHOD, +ARGUMENTS, -RESULT)
Invokes the corresponding method of the object
referred as OBJECT using the given arguments,
and sends back the result to Prolog (if the
method returns a result).
$java_add_listener(+OBJECT, +EVENT, +PREDICATE, -RESULT)
Adds a goal to the list of Prolog listeners for a
given object event. The EVENT argument must
be the full class name of the Java event object
representing the event (e.g., java.awt.event.ActionListener).
$java_remove_listener(+OBJECT, +EVENT, +PREDICATE, -RESULT)
Removes a Prolog listener from the list
of event handlers for a given event and
object.
$quit
Closes the connection and terminates the Java process.t
- Prolog term to be interpreted. Only Are interpreted the
Prolog terms related above.java_exception
is
returned to be propagated to the Prolog side.private PLTerm getValue(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: object reference and field name.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm setValue(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: object reference, field name and field value.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm deleteObject(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: object reference.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm createObject(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: class name and constructor argument list.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm invokeMethod(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: object reference, method name and argument list.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm addListener(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: object reference, event class name and
exception handler goal.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm removeListener(PLStructure st)
st
- Prolog structure that contains the data needed for this
operation: object reference, event class name and
exception handler goal.PLStructure
representing a Java exception
to be sent back to Prolog if the Prolog request
does not succeed.private PLTerm prologRepr(java.lang.Object v)
v
- Java object from which a Prolog representation will
be obtained.private PLTerm java_object(int i)
i
- hash code in the object table.PLTerm
object that references
the object referenced by the hash code.private java.lang.reflect.Method getMethod(java.lang.Class cl, java.lang.String mtName, java.lang.Class[] clsarg)
isAssignableFrom()
method (of the Class
class).
The main concern about searching the method to be launched for
a given object, method name, and parameter types is to choose
from method candidates that are compatible with the parameter types.
In most of cases will be no possibility of discussion, only
one method fits the number and parameter types.
The problem raises when given the parameter type array, several
methods could be called, mainly because the type correspondence
between Java and Prolog is not perfect.
This method uses the concept of 'distance' to select the best
fit for a given parameter array.cl
- Class
object that represents the
object class.mtName
- Method name.clsarg
- Array of Class
objects that
represents the method argument list.Method
object that represents
the method found. If there is no method in the
inheritance tree, returns null.private java.lang.reflect.Constructor getConstructor(java.lang.Class cl, java.lang.Class[] clsarg)
isAssignableFrom()
method (of the Class
class).
The algorithm is exactly the same as the used in getMethod.
See this method to get detailed information.cl
- Class
object that represents the
object class.clsarg
- Array of Class
objects that
represents the constructor argument list.Constructor
object that represents
the constructor found. If there is no constructor in the
inheritance tree, returns null.private int getDistance(java.lang.Class assignedTo, java.lang.Class assignedFrom)
isAssignableFrom
method of the
Class
class.
Is supossed that the assignedFrom
parameter will not be
primitive.assignedTo
- Class
object that represents the
Java type to be assigned.assignedFrom
- Class
object that represents the
Java type from which will be made the
assignment.int
number representing the
'distance' between the classes received as
argument.private java.lang.Object invoke(java.lang.reflect.Method mt, java.lang.Object obj, java.lang.Object[] arg) throws java.lang.Exception
invoke
of the class
Method
cannot be used directly because the argument
types could not correspond exactly to the method ones. A kind
of conversion must be done with the primitive Java types.mt
- Method
to be invoked.obj
- Object on which the method will be invoked.arg
- List of argument values to invoke the method.Exception
- if there is any problem invoking
the method.private java.lang.Object translateArg(java.lang.Object arg, java.lang.Class type)
arg
- Argument value received for method/constructor invocation.type
- Primitive type needed by the method/constructor.null
if there is no possible conversion.public static boolean isInterpretable(PLTerm t)
t
- Term to be tested for interpretation.true
if the Prolog term received
as argument can be interpreted by the Prolog
command interpreter (method interpret above).public java.lang.Object getObject(java.lang.Integer hashCode)
hashCode
- Hash code that references an object
in the Java object table.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |