Jalog 1.4


io.github.JalogTeam.jalog
Class Pro_Term

java.lang.Object
  extended by io.github.JalogTeam.jalog.Pro_Term
Direct Known Subclasses:
Jalog.Term

public class Pro_Term

All data and programs are represented as Pro_Term objects. Every Pro_Term object has a pointer to a Pro_TermData object that contains the associated data.

A Pro_Term object representing a program clause points to an object of type Pro_TermData_Compound that contains the head and the body of the clause.

A Pro_Term object representing an open variable either points to an object of type Pro_TermData_Unified that points to another open Pro_Term object or does not point to any data.

A Pro_Term object representing a bound variable either points to an object of type Pro_TermData_Unified that points to another bound Pro_Term object or points to an object of type Pro_TermData that contains the data.

The usual way to create a term is to use one of the methods whose name starts with "m_". The usual way to assign a value to an existing open term is to use one of the unify methods.

According to Prolog rules a value of a term can be changed only if it is open or in backtrack.


 

Field Summary
static Pro_Term EMPTY_LIST
           The empty list.
public long Id
           Unique identification number of this object.
static long lastId
           To assign unique identification number for each term. Updated by constructors.
static long lastPrintId
           To assign unique display number for each displayed open term. Updated by toString method.
public long printId
           Unique display number of this object.
 
Constructor Summary
Pro_Term()
           Constructs an open Pro_Term object.
Pro_Term(Pro_TermData data)
           Constructs a unified or bound Pro_Term object.
 
Method Summary
 void clearData()
           Removes data and makes this term open.
 void compval(Pro_Term term)
           Calculates the value of the expression in term and stores the result in this object.
Pro_Term copy()
           Returns an independent copy of this term.
Pro_Term copy(Hashtable variable_map)
           Returns an independent copy this of term using variable_map.
static long eval_integer(Pro_Term p)
           Calculates the value of the expression in p and returns it if it is an integer otherwise 0.
 Pro_TermData getData()
           Returns the data associated to this term, or null if this term is open.
 Pro_Term getRealNode()
           If this object is linked to another Pro_Term object (as a result of unification) then finds the actual node otherwise returns this object.
 String getType()
           Returns the type of this term.
String image()
           Returns human readable string representation of this term.
 static Pro_Term m_char(char iniVal)
           Creates a new Pro_Term object with a character value.
 static Pro_Term m_compound(String iniName, Pro_Term[] iniSubterm)
           Creates a new Pro_Term object with a compound value.
 static Pro_Term m_integer(long iniVal)
           Creates a new Pro_Term object with an integer value.
 static Pro_Term m_list(Pro_Term[] iniVal, Pro_Term tail)
           Creates a new Pro_Term object with a list value.
 static Pro_Term m_open()
           Creates a new open Pro_Term object.
 static Pro_Term m_real(double iniVal)
           Creates a new Pro_Term object with a real value.
 static Pro_Term m_string(String iniVal)
           Creates a new Pro_Term object with a string value.
 static Pro_Term m_string_concat(Pro_TermData_String left, Pro_TermData_String right)
           Creates a new Pro_Term object with a string value.
 static Pro_Term m_string_substring(Pro_TermData_String base_string, long req_start, long req_len)
           Creates a new Pro_Term object with a string value.
 static Pro_Term m_unified(Pro_Term iniVal)
           Creates a new Pro_Term object unified with another.
 boolean match(Pro_Term pn2)
           Checks wheter this term can be unified with another term but does not unify them.
String toString()
           Returns Jalog syntax representation of this term.
 boolean unify(Pro_Term pn2, Pro_Trail pBack)
           Unifies this term with another term.
 boolean unify(Pro_Term pn2, Pro_Trail pBack, Pro_TrailMark Mark)
           Unifies this term with another term.
 

Field Detail

EMPTY_LIST

public static Pro_Term EMPTY_LIST
The empty list. This is the last item of all closed lists.


Id

public long Id
Unique identification number of this object.


lastId

public static long lastId
The latest Id assigned to a term by a constructor. Before creating a term a constructor increases this by one.


lastPrintId

public static long lastPrintId
The latest printId assigned to an open term by a toString method. The purpose of this is to assign small integer printId for each separate open variable to be displayed.


printId

public long printId
Unique display number for an open variable for printout. Zero in those objects that are not assigned a display number.


Constructor Detail

Pro_Term

public Pro_Term()
Constructs an open Pro_Term object.


Pro_Term

public Pro_Term(Pro_TermData data)
Constructs a unified or bound Pro_Term object.

Parameters:
data - the data to be associated to this term.

Method Detail

clearData

public void clearData()
Removes data and makes this term open. Usually not needed as Jalog system automatically clears variables on backtrack, and they should not be cleared otherwise. Can be used in backtrack if Jalog system does not know that the data should be cleared.


compval

void compval(Pro_Term term)
Calculates the value of the expression in term and stores the result in this object. If there is no calculable expression value of this object is set to the same as the value of term. If term is open this and term are unified.

This method should only be used if this term is open because according to Prolog rules a value of a term can be changed only if it is open or in backtrack.


copy

public Pro_Term copy()
Makes a full copy of this term without linkages to other data. Open terms are changed to new open terms. Repeated open terms are changed to repeated open terms. Used in database asserting and fetching.

Returns:
an independent copy this term.

copy

public Pro_Term copy(Hashtable variable_map)
Returns a copy this term using variable_map. Hashtable is used to map repeated open terms to repeated open terms. Hashtable is updated for new open terms. Used in database asserting and fetching.

Parameters:
variable_map - contains open variable mapping for this term.
Returns:
a copy this term using variable_map.

eval_integer

public static long eval_integer(Pro_Term p)
Calculates the value of the expression in p and returns it if it is an integer, otherwise returns 0. Useful for indexing.

Parameters:
p - the expression as a Pro_Term.
Returns:
the the value of the expression if it is an integer otherwise 0.

getData

public Pro_TermData getData()
Returns the data associated to this term, or null if this term is open. If this term is unified to another term the data of the other term is returned, or null if the other term is open.

Returns:
the data associated to this term, or null if this term is open.

getRealNode

public Pro_Term getRealNode()
If this object is linked to another Pro_Term object (as a result of unification) then finds the actual node otherwise returns this object.

Returns:
the actual node

getType

public String getType()
Gets the type of this term. Type is one of the fields of the class Jalog

Returns:
the type of this term.

image

public String image()
Returns human readable string representation of this term. Used by built-in predicate write.


m_char

public static Pro_Term m_char(char iniVal)
Creates a new Pro_Term object with a character value.

Parameters:
iniVal - value for the new term.
Returns:
the created term object

m_compound

public static Pro_Term m_compound(String iniName, Pro_Term[] iniSubterm)
Creates a new Pro_Term object with a compound value. If the iniSubterm array is empty the type of the created term is SYMBOL, otherwise COMPOUND.

Parameters:
iniName - functor.
iniSubterm - subterms in an array.
Returns:
the created term object

m_integer

public static Pro_Term m_integer(long iniVal)
Creates a new Pro_Term object with an integer value.

Parameters:
iniVal - value for the new term.
Returns:
the created term object

m_list

public static Pro_Term m_list(Pro_Term[] iniVal, Pro_Term tail)
Creates a new Pro_Term object with a list value.

Parameters:
iniVal - list items in an array.
tail - tail of the list. Can be the Pro_Term.EMPTY_LIST if all items are listed in iniVal. For an open list this must be an open term.
Returns:
the created term object

m_open

public static Pro_Term m_open()
Creates a new open Pro_Term object.

Returns:
the created term object

m_real

public static Pro_Term m_real(double iniVal)
Creates a new Pro_Term object with a real value.

Parameters:
iniVal - value for the new term.
Returns:
the created term object

m_string

public static Pro_Term m_string(String iniVal)
Creates a new Pro_Term object with a string value.

Parameters:
iniVal - value for the new term.
Returns:
the created term object

m_string_concat

public static Pro_Term m_string_concat(Pro_TermData_String left,
      Pro_TermData_String right)
Creates a new Pro_Term object with a string value.

Parameters:
left - left part for the new term.
right - right part for the new term.
Returns:
the created term object

m_string_substring

public static Pro_Term m_string_substring(Pro_TermData_String base_string,
      long req_start, long req_len)
Creates a new Pro_Term object with a string value. The resulting string contains a part of the base_string. First req_start characters are not included. If req_len is less than the remaining length of the base_string then req_len characters are included, otherwise the rest of the base_string is included. If req_start is greater than the length of the base_string then an empty string is created.

Parameters:
base_string - a string containing the required result as a substring.
req_start - number of characters skipped from the beginning of the base_string.
req_len - number of characters to be included in the result.
Returns:
the created term object

m_unified

public static Pro_Term m_unified(Pro_Term iniVal)
Creates a new Pro_Term object unified with iniVal.

Parameters:
iniVal - the term to be unified with.
Returns:
the created term object

match

public boolean match(Pro_Term pn2)
Checks wheter this term can be unified with another term but does not unify them.

Parameters:
pn2 - compared term.
Returns:
true if the paramater can be unified with this, false othewise.

toString

public String toString()
Returns Jalog syntax representation of this term.

Returns:
Jalog syntax representation of this term.

unify

public boolean unify(Pro_Term pn2, Pro_Trail pBack)
Unifies this term with another term. Tries to modify this term and the argument term so that they are equivalent. Open subterms are replaced with corresponding subterms in the other term, or computed values of corresponding subterms if they can be computed. If the corresponding subterm is open, the subterms are unified. If unification does not succeed neither this nor pn2 is updated.

Parameters:
pn2 - compared term.
pBack - use Pred.trail.
Returns:
true if unification succeeded, false otherwise.

unify

public boolean unify(Pro_Term pn2, Pro_Trail pBack, Pro_TrailMark Mark)
Unifies this term with another term. Tries to modify this term and the argument term so that they are equivalent. Open subterms are replaced with corresponding subterms in the other term, or computed values of corresponding subterms if they can be computed. If the corresponding subterm is open, the subterms are unified. If unification does not succeed neither this nor pn2 is updated.
If Mark is not null then before unification sets Mark.marked to point to the top of the trail.

Parameters:
pn2 - compared term.
pBack - use Pred.trail.
Mark - trail mark for backtracking.
Returns:
true if unification succeeded, false otherwise.

Authors: Mikko Levanto, Ari Okkonen