CiaoJava
Class PLTerm

java.lang.Object
  |
  +--CiaoJava.PLTerm
Direct Known Subclasses:
PLAtom, PLFloat, PLInteger, PLList, PLString, PLStructure, PLVariable

public abstract class PLTerm
extends java.lang.Object

Java representation of Prolog terms. Abstract class for representing generic Prolog terms. The rest of the term representation classes are childs of this one. This class contains data and code that are common to every term. It includes also the conversion mechanism between the Java representation of Prolog terms and the serialized representation received from Prolog.


Field Summary
(package private) static int ATOM
           
private static java.lang.String[] AtomTable
           
private static char currentVersion
           
static PLTerm fail
          Fail representation.
(package private) static int FLOAT
           
(package private) static int INTEGER
           
private static java.lang.String INTERPRETER_ERROR
           
private static java.lang.String INTERPRETER_SUCCESS
           
(package private) static java.lang.String JAVA_OBJECT
           
(package private) static int LIST
           
private static int MAX_READ_BUFFER
           
static PLTerm nil
          Empty list representation.
private static char PFX_ATOM
           
private static char PFX_FLOAT
           
private static char PFX_LIST_WITH_NIL_TAIL
           
private static char PFX_LIST_WITH_NONIL_TAIL
           
private static char PFX_LONG_INT
           
private static char PFX_MULTI_REF_ATOM
           
private static char PFX_MULTI_REF_STRUCT
           
private static char PFX_MULTI_REF_VAR
           
private static char PFX_SHORT_INT
           
private static char PFX_SINGLE_REF_VAR
           
private static char PFX_STRUCT
           
private static char PFXC_ATOM
           
private static char PFXC_FLOAT
           
private static char PFXC_INTEGER
           
private static char PFXC_LIST
           
private static char PFXC_NIL
           
private static char PFXC_STRING
           
private static char PFXC_STRUCT
           
private static char PFXC_VARIABLE
           
(package private) static java.lang.String PROLOG_EXCEPTION
           
(package private) static java.lang.String PROLOG_FAIL
           
(package private) static java.lang.String PROLOG_QUERY_ID
           
(package private) static java.lang.String PROLOG_SUCCESS
           
(package private) static int STRING
           
private static int STRING_BUFFER_SIZE
           
(package private) static int STRUCTURE
           
static PLTerm success
          Success representation.
 int Type
           
private static int VarCounter
           
(package private) static int VARIABLE
           
private static int VarNumber
           
static char VERSION_A
           
static char VERSION_C
           
 
Constructor Summary
PLTerm()
           
 
Method Summary
 void backtrack(PLTerm term)
          Undo the unification made on this PLTerm object using as pattern the term received as argument.
abstract  PLTerm copy()
          Duplication of Prolog terms.
abstract  boolean equals(PLTerm t)
          Comparison between Prolog terms.
protected static PLTerm fastRead(java.io.BufferedReader in)
          Conversion from Prolog low level format to a Java object.
 java.lang.String fastWrite()
          Translation from a Java object to Prolog low level representation.
private  java.lang.StringBuffer genPrefix(PLTerm t)
          Prefix generation in $fast format.
private  java.lang.StringBuffer genTerm(PLTerm t)
          Term generation in $fast format.
private static char getChar(java.io.BufferedReader in)
          Gets one char from the reader received as argument.
private static float getFloat(java.io.BufferedReader in)
          Gets a float from the buffer until '\0' is found.
private static int getInt(java.io.BufferedReader in)
          Gets an integer from the buffer until '\0' is found.
private static void getPrefix(java.io.BufferedReader in)
          Gets the prefix of a Prolog low level fast format term.
private static java.lang.String getString(java.io.BufferedReader in)
          Gets a substring from the buffer until '\0' is found.
private static PLTerm getTerm(java.io.BufferedReader in)
          gets a term from the buffer in 'fast' format.
protected  boolean isException()
          Prolog exception test.
 boolean isList()
          List test.
 boolean isNil()
          Nil test.
protected  boolean isPrologFail()
          Prolog fail test.
protected  boolean isPrologSuccess()
          Prolog success test.
protected  boolean isQueryId()
          Prolog query id test.
abstract  boolean isRunnable()
          Execution test for Prolog objects.
protected  boolean isSolution()
          Prolog solution test.
 boolean isString()
          String test.
 boolean isStructure()
          Structure test.
 boolean isVariable()
          Variable test.
abstract  java.lang.Object javaRepr(PLInterpreter i)
          Java representation of the Prolog term.
 void launchGoal(java.io.PrintWriter out)
          Execution of a Prolog goal.
protected abstract  int numberOfCells()
          Number of cells on the Prolog WAM heap for this term.
abstract  java.lang.String toString()
          String representation of the Prolog term.
 boolean unify(PLTerm term)
          Term unification.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

Type

public int Type

INTEGER

static final int INTEGER

FLOAT

static final int FLOAT

ATOM

static final int ATOM

LIST

static final int LIST

STRUCTURE

static final int STRUCTURE

VARIABLE

static final int VARIABLE

STRING

static final int STRING

INTERPRETER_ERROR

private static final java.lang.String INTERPRETER_ERROR

INTERPRETER_SUCCESS

private static final java.lang.String INTERPRETER_SUCCESS

nil

public static final PLTerm nil
Empty list representation. This PLTerm constant represents the empty list.

success

public static final PLTerm success
Success representation. This PLAtom constant is used to send back to Prolog if a request with no return value has been successfully completed.

fail

public static final PLTerm fail
Fail representation. This PLAtom constant is used to send back to Prolog if a request has failed.

JAVA_OBJECT

static final java.lang.String JAVA_OBJECT

PROLOG_EXCEPTION

static final java.lang.String PROLOG_EXCEPTION

PROLOG_FAIL

static final java.lang.String PROLOG_FAIL

PROLOG_SUCCESS

static final java.lang.String PROLOG_SUCCESS

PROLOG_QUERY_ID

static final java.lang.String PROLOG_QUERY_ID

AtomTable

private static java.lang.String[] AtomTable

VarNumber

private static int VarNumber

VarCounter

private static int VarCounter

STRING_BUFFER_SIZE

private static final int STRING_BUFFER_SIZE

MAX_READ_BUFFER

private static final int MAX_READ_BUFFER

VERSION_A

public static final char VERSION_A

PFX_LIST_WITH_NIL_TAIL

private static final char PFX_LIST_WITH_NIL_TAIL

PFX_LIST_WITH_NONIL_TAIL

private static final char PFX_LIST_WITH_NONIL_TAIL

PFX_SHORT_INT

private static final char PFX_SHORT_INT

PFX_LONG_INT

private static final char PFX_LONG_INT

PFX_FLOAT

private static final char PFX_FLOAT

PFX_MULTI_REF_VAR

private static final char PFX_MULTI_REF_VAR

PFX_SINGLE_REF_VAR

private static final char PFX_SINGLE_REF_VAR

PFX_ATOM

private static final char PFX_ATOM

PFX_MULTI_REF_ATOM

private static final char PFX_MULTI_REF_ATOM

PFX_STRUCT

private static final char PFX_STRUCT

PFX_MULTI_REF_STRUCT

private static final char PFX_MULTI_REF_STRUCT

VERSION_C

public static final char VERSION_C

PFXC_LIST

private static final char PFXC_LIST

PFXC_STRING

private static final char PFXC_STRING

PFXC_INTEGER

private static final char PFXC_INTEGER

PFXC_FLOAT

private static final char PFXC_FLOAT

PFXC_VARIABLE

private static final char PFXC_VARIABLE

PFXC_ATOM

private static final char PFXC_ATOM

PFXC_STRUCT

private static final char PFXC_STRUCT

PFXC_NIL

private static final char PFXC_NIL

currentVersion

private static final char currentVersion
Constructor Detail

PLTerm

public PLTerm()
Method Detail

toString

public abstract java.lang.String toString()
String representation of the Prolog term.
Returns:
a String representing this term.
Overrides:
toString in class java.lang.Object

javaRepr

public abstract java.lang.Object javaRepr(PLInterpreter i)
Java representation of the Prolog term. Is used to obtain the equivalent Java object.
Parameters:
i - PLInterpreter object used to interpret the Prolog representation of Java objects.
Returns:
a Java object representation of the Prolog term.

isRunnable

public abstract boolean isRunnable()
Execution test for Prolog objects.
Returns:
true if this Prolog term can be evaluated in the Prolog side as a goal; false otherwise.

equals

public abstract boolean equals(PLTerm t)
Comparison between Prolog terms.
Parameters:
t - Term to be compared to.
Returns:
true if this Prolog term is equal to the term received as argument; false otherwise.

numberOfCells

protected abstract int numberOfCells()
Number of cells on the Prolog WAM heap for this term. Not used currently.

copy

public abstract PLTerm copy()
Duplication of Prolog terms. Performs an in depth term duplication, so that will be no shared elements between this term and the copy returned. The implementation of this method in the PLList, PLStructure and PLVariable clases perform the duplication recursively.
Returns:
a copy of this Prolog term.

launchGoal

public void launchGoal(java.io.PrintWriter out)
                throws PLGoalException
Execution of a Prolog goal. This method is combined with nextSolution to obtain the set of solutions given by a query on a goal. Only are executed those terms where isRunnable returns true.
Parameters:
out - socket descriptor for sending the goal description to Prolog.
Returns:
Prolog goal identifier, if the goal is accepted as a valid goal in the Prolog side; Prolog Exception term, if a problem has been found translating the term to a Prolog goal.
Throws:
PLGoalException - if this term cannot be a Prolog goal.

unify

public boolean unify(PLTerm term)
Term unification. Unifies this Prolog term with the term received as argument.

Important: The unification is 'two sided': the variables found in the term received as argument could be bound in order to unify the complete terms. In the same way, the variables found in this term could be bound to unify both terms.

Parameters:
term - Term to unify with.
Returns:
true if the unification is successful: false otherwise.

backtrack

public void backtrack(PLTerm term)
               throws PLException
Undo the unification made on this PLTerm object using as pattern the term received as argument. This is the most general code. For PLList, PLStructure and PLVariable there are specific code that overrides this method.
Parameters:
term - Term to be used as pattern.
Throws:
PLException - if this term cannot be backtracked.

isVariable

public boolean isVariable()
Variable test.
Returns:
true if this Prolog term is a variable; false otherwise.

isStructure

public boolean isStructure()
Structure test.
Returns:
true if this Prolog term is a structure; false otherwise.

isList

public boolean isList()
List test.
Returns:
true if this Prolog term is a list; false otherwise.

isString

public boolean isString()
String test.
Returns:
true if this Prolog term is a string; false otherwise.

isNil

public boolean isNil()
Nil test.
Returns:
true if this Prolog term is nil; false otherwise.

isException

protected boolean isException()
Prolog exception test.
Returns:
true if the term represents a Prolog exception; false otherwise.

isPrologFail

protected boolean isPrologFail()
Prolog fail test.
Returns:
true if the term represents the valid Prolog atom used to indicate goal fail; false otherwise.

isPrologSuccess

protected boolean isPrologSuccess()
Prolog success test.
Returns:
true if the term represents the valid Prolog atom used to indicate Prolog success; false otherwise.

isQueryId

protected boolean isQueryId()
Prolog query id test.
Returns:
true if the term represents the valid Prolog structure used to indicate a query id; false otherwise.

isSolution

protected boolean isSolution()
Prolog solution test.
Returns:
true if the term represents the valid Prolog structure used to indicate a query solution; false otherwise.

fastRead

protected static PLTerm fastRead(java.io.BufferedReader in)
                          throws PLException
Conversion from Prolog low level format to a Java object. Receives a Prolog term in low level format from a stream reader, and builds a PLTerm representation of the term received.
Parameters:
in - Reader from which the term will be read.
Returns:
a Prolog term object with the term read.
Throws:
PLException - if raises any error reading the term.

getPrefix

private static void getPrefix(java.io.BufferedReader in)
                       throws PLException
Gets the prefix of a Prolog low level fast format term.
Parameters:
in - Reader from which the term will be read.

getInt

private static int getInt(java.io.BufferedReader in)
                   throws PLException
Gets an integer from the buffer until '\0' is found.
Parameters:
in - Reader from which the term will be read.
Returns:
The integer read.
Throws:
PLException - if the integer received is not a valid Java integer.

getFloat

private static float getFloat(java.io.BufferedReader in)
                       throws PLException
Gets a float from the buffer until '\0' is found.
Parameters:
in - Reader from which the term will be read.
Returns:
The float read.
Throws:
PLException - if the float received is not a valid Java float.

getChar

private static char getChar(java.io.BufferedReader in)
                     throws PLException
Gets one char from the reader received as argument.
Parameters:
in - Reader from which the term will be read.
Returns:
The character read.
Throws:
PLException - if the BufferedReader does not allow more read operations.

getString

private static java.lang.String getString(java.io.BufferedReader in)
                                   throws PLException
Gets a substring from the buffer until '\0' is found.
Parameters:
in - Reader from which the term will be read.
Returns:
The string read.
Throws:
PLException - if the BufferedReader does not allow more read operations, and '\0' is missing.

getTerm

private static PLTerm getTerm(java.io.BufferedReader in)
                       throws PLException
gets a term from the buffer in 'fast' format.
Parameters:
in - Reader from which the term will be read.
Returns:
The term read.
Throws:
PLException - if there is any problem during the term parsing.

fastWrite

public java.lang.String fastWrite()
Translation from a Java object to Prolog low level representation.
Returns:
a string that contains the low level representation of this Prolog term.

genPrefix

private java.lang.StringBuffer genPrefix(PLTerm t)
Prefix generation in $fast format.
Parameters:
t - Prolog term for which build a prefix in low level format.
Returns:
a StringBuffer object that contains the prefix.

genTerm

private java.lang.StringBuffer genTerm(PLTerm t)
Term generation in $fast format.
Parameters:
t - Prolog term to translate to low level format
Returns:
a StringBuffer object that contains the low level term representation.