ubc.cs.JLog.Terms
Class jCompoundTerm

java.lang.Object
  extended by ubc.cs.JLog.Foundation.jType
      extended by ubc.cs.JLog.Terms.jTerm
          extended by ubc.cs.JLog.Terms.jCompoundTerm
All Implemented Interfaces:
iType, iUnifiable, iConsultable, iMakeUnmake, iName, iTerm, iValue
Direct Known Subclasses:
jPredicateTerms

public class jCompoundTerm
extends jTerm
implements iMakeUnmake

This class represents a collection of terms.

Author:
Glendon Holst

Field Summary
protected  java.util.Vector terms
           
 
Fields inherited from class ubc.cs.JLog.Foundation.jType
type
 
Fields inherited from interface ubc.cs.JLog.Terms.iTerm
EQUAL, GREATER_THAN, LESS_THAN
 
Fields inherited from interface ubc.cs.JLog.Foundation.iType
TYPE_ARITHMETIC, TYPE_ATOM, TYPE_BUILTINPREDICATE, TYPE_COMMAND, TYPE_COMPARE, TYPE_COMPOUND, TYPE_CONS, TYPE_IF, TYPE_INTEGER, TYPE_LIST, TYPE_NULLLIST, TYPE_NUMERICCOMPARE, TYPE_OBJECT, TYPE_OPERATOR, TYPE_OR, TYPE_ORPREDICATE, TYPE_PREDICATE, TYPE_PREDICATETERMS, TYPE_REAL, TYPE_TYPE, TYPE_UNARYARITHMETIC, TYPE_UNARYOPERATOR, TYPE_UNDEFINED, TYPE_VARIABLE
 
Constructor Summary
jCompoundTerm()
           
jCompoundTerm(int initialCapacity)
           
jCompoundTerm(java.util.Vector t)
           
 
Method Summary
 void addTerm(jTerm t)
           
 int compare(jTerm term, boolean first_call, boolean var_equal)
          The private member function for term comparision.
 void consult(jKnowledgeBase kb)
          Consult the given jKnowledgeBase and cache any invariant values (those which would not change until the next time the jKnowledgeBase changes).
 void consultReset()
          Called when the jKnowledgeBase changes.
 jTerm copy(jVariableRegistry vars)
          Internal member function which creates a copy of this term.
 void copyCompoundTerm(jCompoundTerm ct)
          Makes this a copy of the provided jCompoundTerm.
 jTerm duplicate(jVariable[] vars)
          Creates a complete and entirely independant duplicate of this term.
 jTerm elementAt(int index)
           
 void enumerateVariables(jVariableVector v, boolean all)
          Adds variables belonging to this term (or belonging to any sub-part of this term) to the jVariableVector
 java.util.Enumeration enumTerms()
           
 boolean equivalence(jTerm term, jEquivalenceMapping v)
          The public interface for evaluating term equivalence (i.e., structural similarity).
protected  java.lang.String getEndingSymbol()
           
protected  java.lang.String getStartingSymbol()
           
 boolean hasTerm(jTerm t)
           
 java.util.Vector internal_copy(jVariableRegistry vars)
           
protected  java.util.Vector internal_duplicate(jVariable[] vars)
           
 void intersectionCompoundTerm(jCompoundTerm ct)
          Adds all jTerms in given jCompoundTerm to this.
 boolean isEmpty()
           
protected  boolean isHigherPriorityOperator(jTerm t)
          Determines if provided jTerm is of higher priority than ','.
 void make(jTerm t)
          Makes this a representation of the provided jTerm.
 void makeCompoundTerm(jTerm t)
          Add jCons separated jTerms to this jCompoundTerm.
 void mutateElementAt(int index, jTerm term)
          Mutates a term element in the compount term.
 void registerUnboundVariables(jUnifiedVector v)
          Adds any unbound variables belonging to this term (or belonging to any sub-part of this term) to the jUnifiedVector
 void registerVariables(jVariableVector v)
          Adds all variables belonging to this term (or belonging to any sub-part of this term) to the jUnifiedVector Should be called during the consultation phase by rules for their owned terms both head and base.
 void removeAllTerms()
           
 void removeTerm(jTerm t)
           
 boolean requiresCompleteVariableState()
          Call to determine if enclosing rule should record all variable states.
 int size()
           
 void subtractCompoundTerm(jCompoundTerm ct)
          Removes all jTerms in given jCompoundTerm from this.
 java.lang.String toString(boolean usename)
          Produces a string identifying this term, suitable for display to the console.
 boolean unify(jTerm term, jUnifiedVector v)
          Determines if this instance and term unify.
 void unionCompoundTerm(jCompoundTerm ct)
          Removes all jTerms not in given jCompoundTerm from this.
 jTerm unmake()
          Creates a jCons separated jTerms to represent this jCompoundTerm.
 jTerm unmakeCompoundTerm()
          Creates a jCons separated jTerms to represent this jCompoundTerm.
 
Methods inherited from class ubc.cs.JLog.Terms.jTerm
compare, copy, getName, getTerm, getValue, isConsultNeeded, toString
 
Methods inherited from class ubc.cs.JLog.Foundation.jType
getType, objectToString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

terms

protected java.util.Vector terms
Constructor Detail

jCompoundTerm

public jCompoundTerm()

jCompoundTerm

public jCompoundTerm(int initialCapacity)

jCompoundTerm

public jCompoundTerm(java.util.Vector t)
Method Detail

compare

public int compare(jTerm term,
                   boolean first_call,
                   boolean var_equal)
Description copied from class: jTerm
The private member function for term comparision. Compares this instance to the provided jTerm. sub-classes must override.

Specified by:
compare in class jTerm
Parameters:
term - the jTerm to compare with this instance of jTerm. if term is an unknown type then, if first_call is true, call term.compare(this,false) and return the negative result, otherwise return EQUAL.
first_call - true if just invoked by public compare member function. false otherwise.
var_equal - if var_equal is true, then unbound variables are considered equal. var_equal = false is standard prolog behavior.
Returns:
LESS_THAN if this instance is less than term, EQUAL if the two terms are equal, and GREATER_THAN if this instance is greater than term.

hasTerm

public final boolean hasTerm(jTerm t)

addTerm

public void addTerm(jTerm t)

removeTerm

public void removeTerm(jTerm t)

removeAllTerms

public void removeAllTerms()

enumTerms

public final java.util.Enumeration enumTerms()

size

public final int size()

elementAt

public final jTerm elementAt(int index)

mutateElementAt

public void mutateElementAt(int index,
                            jTerm term)
Mutates a term element in the compount term. NOTE: Do not use this method, except in an exceptional circumstance, since it violates the logical consistency of standard Prolog.

Parameters:
index - The zero-based index of the element to mutate.
term - The term to make the new element at index.

isEmpty

public final boolean isEmpty()

requiresCompleteVariableState

public boolean requiresCompleteVariableState()
Description copied from class: jTerm
Call to determine if enclosing rule should record all variable states. A rules goal only tracks variables which were unified, and relies on stack unwinding to unbind all variables. Terms such as cut, which jump back to the containing rules goal require that all variables are restored.

Specified by:
requiresCompleteVariableState in interface iTerm
Overrides:
requiresCompleteVariableState in class jTerm
Returns:
true if all variable bindings must be recorded by the rule containing this term. false otherwise. Normally returns false

registerUnboundVariables

public void registerUnboundVariables(jUnifiedVector v)
Description copied from class: jTerm
Adds any unbound variables belonging to this term (or belonging to any sub-part of this term) to the jUnifiedVector

Specified by:
registerUnboundVariables in interface iTerm
Overrides:
registerUnboundVariables in class jTerm
Parameters:
v - The jUnifiedVector where unbound variables are added to. This parameter is used to as output to the caller, not as input.

equivalence

public boolean equivalence(jTerm term,
                           jEquivalenceMapping v)
Description copied from interface: iTerm
The public interface for evaluating term equivalence (i.e., structural similarity).

Specified by:
equivalence in interface iTerm
Specified by:
equivalence in class jTerm
Parameters:
term - the jTerm to compare with this instance of jTerm.
v - v is the collection of equivalent variable pairs found. It should be empty when first calling this function.
Returns:
true if this instance is equivalent to term, false otherwise.

unify

public boolean unify(jTerm term,
                     jUnifiedVector v)
Description copied from interface: iUnifiable
Determines if this instance and term unify. Every variable which was previously unbound, but which becomes bound during the unification must register itself with the jUnifiedVector. It is the callers responsibility to deal with the unified vector.

Specified by:
unify in interface iUnifiable
Specified by:
unify in interface iTerm
Specified by:
unify in class jTerm
Parameters:
term - the term to unify with this instance.
v - the vector of variables bound during unification. Even if unification fails, this vector may contain recently bound vectors. This parameter is used to as output to the caller, not as input for unify.
Returns:
true if unification succeeded, false otherwise.

registerVariables

public void registerVariables(jVariableVector v)
Description copied from class: jTerm
Adds all variables belonging to this term (or belonging to any sub-part of this term) to the jUnifiedVector Should be called during the consultation phase by rules for their owned terms both head and base. all variables encountered should be unbound. After call, this term should not be modified in any way.

Specified by:
registerVariables in interface iTerm
Specified by:
registerVariables in class jTerm
Parameters:
v - The jUnifiedVector where variables are added to. This parameter is used to as output to the caller, not as input.

enumerateVariables

public void enumerateVariables(jVariableVector v,
                               boolean all)
Description copied from class: jTerm
Adds variables belonging to this term (or belonging to any sub-part of this term) to the jVariableVector

Specified by:
enumerateVariables in interface iTerm
Specified by:
enumerateVariables in class jTerm
Parameters:
v - The jVariabeVector where variables are added to. This parameter is used to as output to the caller, not as input.
all - If true, then all variables should register, otherwise only add non-existentially qualified variables.

duplicate

public jTerm duplicate(jVariable[] vars)
Description copied from class: jTerm
Creates a complete and entirely independant duplicate of this term. User should call only for terms for which registerVariables has already been invoked. Within duplicate, any other calls to duplicate should pass along the same vars array produced by registerVariables since the same duplication path previously taken by registerVariables should be taken by duplicate. This call is designed only for terms which belong to rules and are templates for instantiation. Any variables in the term should be unbound. As implied by the modification restrictions on registerVariables, terms and their children cannot change (especially during call!)

Specified by:
duplicate in interface iTerm
Specified by:
duplicate in class jTerm
Parameters:
vars - The user passes in a duplicate of the variable vector produced from the previous call to registerVariables. vars is produced from the jVariableVector by creating a single duplicate variable for each variable. Since this is created in the same order as the registerVariables, it is now efficient for jVariables to return their unique duplicate.
Returns:
jTerm which is an instantiated duplicate of this term.

internal_duplicate

protected java.util.Vector internal_duplicate(jVariable[] vars)

copy

public jTerm copy(jVariableRegistry vars)
Description copied from class: jTerm
Internal member function which creates a copy of this term. This member function is designed to duplicate a term, but without the restrictions of the duplicate function. Should only be invoked by copy() or other copy(vars). Should only invoke other copy(vars) functions. Bound variables should return a copy of the bound term.

Specified by:
copy in interface iTerm
Specified by:
copy in class jTerm
Parameters:
vars - The registry of variables and their duplicates. Initially this is empty. As variables generate copies, they add themselves and their copy to the jVariableRegistry, and this is output from the function call. Any further calls with the same vars ensures that the same variable (in a different term) returns the same copy.
Returns:
jTerm which is an instantiated copy of this term.

internal_copy

public java.util.Vector internal_copy(jVariableRegistry vars)

copyCompoundTerm

public void copyCompoundTerm(jCompoundTerm ct)
Makes this a copy of the provided jCompoundTerm.

Parameters:
ct - jCompoundTerm to duplicate.

subtractCompoundTerm

public void subtractCompoundTerm(jCompoundTerm ct)
Removes all jTerms in given jCompoundTerm from this.

Parameters:
ct - jCompoundTerm with terms to remove.

unionCompoundTerm

public void unionCompoundTerm(jCompoundTerm ct)
Removes all jTerms not in given jCompoundTerm from this.

Parameters:
ct - jCompoundTerm with terms to keep.

intersectionCompoundTerm

public void intersectionCompoundTerm(jCompoundTerm ct)
Adds all jTerms in given jCompoundTerm to this.

Parameters:
ct - jCompoundTerm with terms to add.

make

public void make(jTerm t)
Makes this a representation of the provided jTerm. Invokes makeCompoundTerm.

Specified by:
make in interface iMakeUnmake
Parameters:
t - jTerm using jCons to separate terms.

makeCompoundTerm

public void makeCompoundTerm(jTerm t)
Add jCons separated jTerms to this jCompoundTerm.

Parameters:
t - jTerm using jCons to separate terms.

unmake

public jTerm unmake()
Creates a jCons separated jTerms to represent this jCompoundTerm. Invokes unmakeCompoundTerm to perform work.

Specified by:
unmake in interface iMakeUnmake
Returns:
jTerm using jCons to separate terms. Duplicates this jCompoundTerm

unmakeCompoundTerm

public jTerm unmakeCompoundTerm()
Creates a jCons separated jTerms to represent this jCompoundTerm.

Returns:
jTerm using jCons to separate terms. Duplicates this jCompoundTerm

consult

public void consult(jKnowledgeBase kb)
Description copied from interface: iConsultable
Consult the given jKnowledgeBase and cache any invariant values (those which would not change until the next time the jKnowledgeBase changes). Where possible, consultable objects should minimize computation and assume that any cached values are still accurate.

Specified by:
consult in interface iConsultable
Specified by:
consult in interface iTerm
Overrides:
consult in class jTerm
Parameters:
kb - the knowledge base which attempted proofs are based upon.

consultReset

public void consultReset()
Description copied from interface: iConsultable
Called when the jKnowledgeBase changes. Should set all cached values to dirty so that a following call to consult would perform a full lookup as required.

Specified by:
consultReset in interface iConsultable
Specified by:
consultReset in interface iTerm
Overrides:
consultReset in class jTerm

toString

public java.lang.String toString(boolean usename)
Description copied from class: jTerm
Produces a string identifying this term, suitable for display to the console. param usename determines whether to display variables by name or identity. false is the default for displaying the term, true for displaying this term in a user query.

Specified by:
toString in interface iTerm
Specified by:
toString in class jTerm
Returns:
String which is a textual representation of this term.

getStartingSymbol

protected java.lang.String getStartingSymbol()

getEndingSymbol

protected java.lang.String getEndingSymbol()

isHigherPriorityOperator

protected boolean isHigherPriorityOperator(jTerm t)
Determines if provided jTerm is of higher priority than ','.

Parameters:
t - The jTerm to evaluate.
Returns:
true if jTerm is a builtin operator with higher priority than the ',' operator, false otherwise.