com.sri.oaa2.icl
Class IclTerm

java.lang.Object
  |
  +--com.sri.oaa2.icl.IclTerm
All Implemented Interfaces:
java.lang.Cloneable, OaaPrologVocabTokenTypes, java.io.Serializable
Direct Known Subclasses:
com.sri.oaa2.icl.IclAtomic, com.sri.oaa2.icl.IclComposite

public abstract class IclTerm
extends java.lang.Object
implements java.lang.Cloneable, OaaPrologVocabTokenTypes, java.io.Serializable

The root of the IclTerm class heirarchy. Implements the composite pattern.

See Also:
Serialized Form

Field Summary
protected  java.util.ArrayList children
           
protected  boolean readOnly
           
protected  int type
           
 
Fields inherited from interface com.sri.oaa2.icl.OaaPrologVocabTokenTypes
BACKSLASH, BANG, COLON, COMMA, DBL_COLON, DBLQUOTE, DBLQUOTED, DIV, DOT, ENDPAREN_LITERAL, EOF, EQUAL, EXPONENT, FLOAT, GROUP, ICLDATAQ, ICLDATAQ_LITERAL, IDENT, INT, LBRACE, LBRACK, LIST, LPAREN, MINUS, NULL_TREE_LOOKAHEAD, NUM_FLOAT, NUM_INT, PIPE, PLUS, RBRACE, RBRACK, RPAREN, SEMI, SIGNED_EXPONENT, SPECIAL_CHAR_LITERAL, STAR, STR, STRING_LITERAL, STRUCT, TERM_LITERAL, TILDE, TURNSTILE, VAR, VARIABLE, WS
 
Constructor Summary
  IclTerm()
          Create an empty IclTerm.
protected IclTerm(int i)
          Create a new term of a specific type.
 
Method Summary
protected  java.lang.Object accept(OaaPrologVisitor visitor, java.lang.Object data)
          Accept a visitor.
abstract  void add(IclTerm n)
          Add a child to the end of the list of children.
abstract  void add(int index, IclTerm n)
          Add a child to this term at the given index
abstract  void addAll(IclTerm l)
          Add all the children of the given term to this term.
 void checkWritable()
          Check read only flag.
protected  java.lang.Object childrenAccept(OaaPrologVisitor visitor, java.lang.Object data)
          Have children accept the visitor.
abstract  void clearTerms()
          Remove all children of this node.
 java.lang.Object clone()
          Clone this node.
protected  void dump(java.lang.String prefix)
          Override this method if you want to customize how the node dumps out its children.
static IclTerm fromString(boolean failFast, java.lang.String t)
          Generate an IclTerm from a string, throwing a RuntimeException on any parsing exceptions.
static IclTerm fromString(java.lang.String t)
          Generate an IclTerm from a string.
protected abstract  IclTerm getChild(int i)
          Get the child at the given Index.
protected  java.util.ArrayList getChildren()
          Get the children of this node.
abstract  int getNumChildren()
          Get the number of children of this node.
abstract  IclTerm getTerm(int i)
          Get the term at the given index.
 int getType()
          Get the token type for this node.
 void iclAddToList(IclTerm t)
          Deprecated. use add
 void iclAddToList(IclTerm t, boolean atEnd)
          Deprecated. use add(IclTerm) or add(int, IclTerm)
 void iclAppend(IclTerm inList)
          Deprecated. use add
 java.util.ArrayList iclArguments()
          Deprecated. use toArrayList()
 java.util.ListIterator iclArgumentsIterator()
          Deprecated. use listIterator() or iterator()
 IclTerm iclClone()
          Deprecated. use clone()
 void iclDelete(IclTerm t)
          Deprecated. use removeUnifying
static IclTerm iclDerefTerm(IclTerm t1, java.util.HashMap bindings)
          Deprecated. use Unifier.getInstance().deref()
 float iclFloat()
          Deprecated. use ToFloat.getInstance().from() or ((IclFloat)this).toFloat or ((IclFloat)this).toDouble
 java.lang.String iclFunctor()
          Deprecated. use ToFunctor.getInstance().from() or ((IclStruct)this).getFunctor()
 int iclInt()
          Deprecated. use ToInt.getInstance().from() or ((IclInt)this).toInt
 java.util.ListIterator iclListIterator()
          Deprecated. use listIterator() or iterator()
 int iclListLen()
          Deprecated. use size
 IclTerm iclNthTerm(int index)
          Deprecated. use getTerm(index - 1)
 int iclNumTerms()
          Deprecated. use size
 IclTerm iclRemoveElement(IclTerm t)
          Deprecated. use removeUnifying
 void iclRemoveFromList(IclTerm t)
          Deprecated. use removeUnifying
 void iclReplaceElement(IclTerm oldTerm, IclTerm newTerm)
          Deprecated. use replaceUnifying
 void iclReplaceNthTerm(int position, IclTerm newTerm)
          Deprecated. use replaceElement
 void iclRetractAll()
          Deprecated. use clearTerms
 char iclStarter()
          Deprecated. use ToStarter.getInstance().from() or ((IclGroup)this).getStarter
 java.lang.String iclStr()
          Deprecated. use toIdentifyingString()
static IclTerm iclUnify(IclTerm t1, IclTerm t2)
          Deprecated. use Unifier.getInstance().unify()
static IclTerm iclUnify(IclTerm t1, IclTerm t2, boolean inDebug)
          Deprecated. use Unifier.getInstance().unify()
static IclTerm iclUnify(IclTerm t1, IclTerm t2, java.util.HashMap bindings)
          Deprecated. use Unifier.getInstance().unify()
abstract  boolean isAtomic()
          Check if this term is atomic--can never have children.
abstract  boolean isComposite()
          Check if this term is composite--can have children.
 boolean isEmptyList()
          Check if this term is the empty list.
 boolean isFloat()
          Check if this term is a float.
 boolean isGroup()
          Check if this term is a group.
 boolean isIclDataQ()
          Check if this term is an IclDataQ.
 boolean isInt()
          Check if this term is an int.
 boolean isList()
          Check if this term is a list.
 boolean isOfType(int t)
          Check if this term is of a certain type.
 boolean isReadOnly()
          Get read only flag.
 boolean isStr()
          Check if this term is an atom.
 boolean isStruct()
          Check if this term is a struct.
 boolean isVar()
          Check if this term is an variable.
abstract  java.util.Iterator iterator()
          Get an iterator for the children of this term.
abstract  java.util.ListIterator listIterator()
          Get an iterator for the children of this term.
abstract  void removeAllUnifying(IclTerm n)
          Remove all terms from this term's children for which the given term unifies.
abstract  void removeElement(int i)
          Remove the child at the given index.
abstract  IclTerm removeUnifying(IclTerm n)
          Remove the first child found that unifies with the given one.
abstract  void replaceElement(int target, IclTerm replacement)
          Replace the term at the given index.
abstract  void replaceUnifying(IclTerm target, IclTerm replacement)
          Replace all terms that unify with target with replacement.
 void setReadOnly(boolean r)
          Set read only flag.
 void setType(int ttype)
          Set the token type for this node.
abstract  int size()
          Get the number of children of this node.
abstract  java.util.ArrayList toArrayList()
          Deprecated. Use iterator() or listIterator() methods.
 java.lang.String toIdentifyingString()
          Get the identifying string.
 java.lang.String toString()
          Convert this term into a string.
 java.lang.String toString(java.lang.String prefix)
          Do the conversion with a prefix.
 java.util.Vector toVector()
          Deprecated. use toArrayList()
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

children

protected java.util.ArrayList children

readOnly

protected boolean readOnly

type

protected int type
Constructor Detail

IclTerm

public IclTerm()
Create an empty IclTerm. You should only use this if you know what you're doing.


IclTerm

protected IclTerm(int i)
Create a new term of a specific type. Should only be used by subclasses.

Method Detail

getType

public int getType()
Get the token type for this node.


setType

public void setType(int ttype)
Set the token type for this node.


setReadOnly

public final void setReadOnly(boolean r)
Set read only flag.


isReadOnly

public final boolean isReadOnly()
Get read only flag.


checkWritable

public final void checkWritable()
Check read only flag. This throws a RuntimeException for backwards compatibility.

Throws:
java.lang.RuntimeException - if not writable.

isAtomic

public abstract boolean isAtomic()
Check if this term is atomic--can never have children.


isComposite

public abstract boolean isComposite()
Check if this term is composite--can have children.


isOfType

public final boolean isOfType(int t)
Check if this term is of a certain type.

Returns:
boolean: true if this term is of the given type and false otherwise

isStruct

public final boolean isStruct()
Check if this term is a struct.

Returns:
boolean: true if this term is an IclStruct and false otherwise

isList

public final boolean isList()
Check if this term is a list.

Returns:
boolean: true if term is an IclList; false otherwise

isGroup

public final boolean isGroup()
Check if this term is a group.

Returns:
boolean: true if term is an IclGroup; false otherwise

isIclDataQ

public final boolean isIclDataQ()
Check if this term is an IclDataQ.

Returns:
boolean: true if term is an IclDataQ; false otherwise

isInt

public final boolean isInt()
Check if this term is an int.

Returns:
boolean: true if term is an IclInt; false otherwise

isFloat

public final boolean isFloat()
Check if this term is a float.

Returns:
boolean: true if term is an IclFloat; false otherwise

isVar

public final boolean isVar()
Check if this term is an variable.

Returns:
boolean: true if term is an IclVar; false otherwise

isStr

public final boolean isStr()
Check if this term is an atom.

Returns:
boolean: true if term is an IclStr; false otherwise

isEmptyList

public final boolean isEmptyList()
Check if this term is the empty list.

Returns:
boolean: true if term is an empty list; false otherwise

toArrayList

public abstract java.util.ArrayList toArrayList()
Deprecated. Use iterator() or listIterator() methods.

Get the children of this node as an ArrayList. Returns a new, empty ArrayList for nodes without children. For IclTerms that cannot have children, this returns null. You should not alter this ArrayList.

Returns:
ArrayList the list of children--altering this will alter the internal data structure used by the IclTerm! Don't do it! Use the listIterator() or iterator() methods.

getChildren

protected final java.util.ArrayList getChildren()
Get the children of this node.


clone

public java.lang.Object clone()
Clone this node.

Overrides:
clone in class java.lang.Object
Returns:
Object: the clone

getNumChildren

public abstract int getNumChildren()
Get the number of children of this node.

Returns:
int: the number of children

add

public abstract void add(IclTerm n)
                  throws java.lang.UnsupportedOperationException
Add a child to the end of the list of children.

Parameters:
n - the term to add.
Throws:
UnsupportOperationException - if term cannot have children
java.lang.UnsupportedOperationException

add

public abstract void add(int index,
                         IclTerm n)
                  throws java.lang.UnsupportedOperationException
Add a child to this term at the given index

Throws:
java.lang.UnsupportedOperationException - if term cannot have children

addAll

public abstract void addAll(IclTerm l)
                     throws java.lang.UnsupportedOperationException
Add all the children of the given term to this term. The order in which they are added is the same order in which they are encountered using l.toArrayList().listIterator(). Uses the add(IclTerm) method to actually do the adding.

Parameters:
l - the IclTerm containing the children to add.
Throws:
java.lang.UnsupportedOperationException - if term cannot have children

clearTerms

public abstract void clearTerms()
                         throws java.lang.UnsupportedOperationException
Remove all children of this node.

Throws:
java.lang.UnsupportedOperationException - if term cannot have children

getChild

protected abstract IclTerm getChild(int i)
                             throws java.lang.UnsupportedOperationException
Get the child at the given Index. Assumes children exists.

Returns:
IclTerm: the child at the index
Throws:
java.lang.UnsupportedOperationException - if term cannot have children

getTerm

public abstract IclTerm getTerm(int i)
                         throws java.lang.UnsupportedOperationException
Get the term at the given index. For a readonly term, returns a clone.

Parameters:
i - index of term (first term has index 0, and last has index (size() -1)
Returns:
IclTerm: the term, or null if no such child
Throws:
java.lang.IndexOutOfBoundsException - if i is out of bounds
java.lang.UnsupportedOperationException

size

public abstract int size()
Get the number of children of this node.

Returns:
int: the number of children

accept

protected java.lang.Object accept(OaaPrologVisitor visitor,
                                  java.lang.Object data)
Accept a visitor. This is handy for traversing an IclTerm tree. It's also handy for doing conversions, as in the ToInt class.

Parameters:
visitor - the visitor
data - generic data for the visitor
Returns:
Object: generic return value
See Also:
for an alternative

childrenAccept

protected final java.lang.Object childrenAccept(OaaPrologVisitor visitor,
                                                java.lang.Object data)
Have children accept the visitor. This is the second part of the visitor interface.

Parameters:
visitor - the visitor
data - generic data for the visitor
Returns:
Object: generic return value

toString

public final java.lang.String toString()
Convert this term into a string.

Unfortunately, this is very slow! If you happen to know a term is atomic, you might be better off casting to the specific type, and then grabbing the data from one of the public methods of that class.

Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(java.lang.String prefix)
Do the conversion with a prefix.


toIdentifyingString

public java.lang.String toIdentifyingString()
Get the identifying string. Usually, this is whatever is returned by toString(), but is the functor in the case of an IclStruct, and the unquoted atom in the case of an IclStr.


dump

protected final void dump(java.lang.String prefix)
Override this method if you want to customize how the node dumps out its children.


removeAllUnifying

public abstract void removeAllUnifying(IclTerm n)
                                throws java.lang.UnsupportedOperationException
Remove all terms from this term's children for which the given term unifies.

Parameters:
n - the term against which children must unify to be removed
Throws:
java.lang.UnsupportedOperationException - if term cannot have children

removeUnifying

public abstract IclTerm removeUnifying(IclTerm n)
                                throws java.lang.UnsupportedOperationException
Remove the first child found that unifies with the given one.

Parameters:
n - the term against which to unify
Returns:
IclTerm: the removed term, or null
Throws:
java.lang.UnsupportedOperationException - if term cannot have children

removeElement

public abstract void removeElement(int i)
                            throws java.lang.UnsupportedOperationException
Remove the child at the given index.

Parameters:
i - the index
Throws:
java.lang.UnsupportedOperationException - if term cannot have children

replaceUnifying

public abstract void replaceUnifying(IclTerm target,
                                     IclTerm replacement)
                              throws java.lang.UnsupportedOperationException
Replace all terms that unify with target with replacement. Note that we don't use copies of the replacement, but the replacement itself.

Parameters:
target - the target against which to unify
replacement - the replacement
Throws:
java.lang.UnsupportedOperationException - if term cannot have children

replaceElement

public abstract void replaceElement(int target,
                                    IclTerm replacement)
                             throws java.lang.IndexOutOfBoundsException,
                                    java.lang.UnsupportedOperationException
Replace the term at the given index. If there is no child at the given index, nothing happens. Again, no copies of the replacement are used.

Parameters:
target - the index to replace
replacement - the replacement
Throws:
java.lang.IndexOutOfBoundsException - if target is out of bounds
java.lang.UnsupportedOperationException

fromString

public static final IclTerm fromString(boolean failFast,
                                       java.lang.String t)
Generate an IclTerm from a string, throwing a RuntimeException on any parsing exceptions. Thread safe.

Parameters:
failFast - if true, throw RuntimeException on error; if false, just display errors to stderr
t - the string from which to generate an IclTerm
Returns:
IclTerm: the new IclTerm or null if it could not be parsed
Throws:
java.lang.RuntimeException - if the string could not be parsed

fromString

public static final IclTerm fromString(java.lang.String t)
                                throws antlr_oaa.RecognitionException,
                                       antlr_oaa.TokenStreamException
Generate an IclTerm from a string. Thread safe.

Returns:
IclTerm: the new IclTerm or null if the passed in String is ""--in the future, this may change to throw an exception in this case
Throws:
antlr_oaa.RecognitionException - if the string could not be parsed
antlr_oaa.TokenStreamException - if the string could not be tokenized

iterator

public abstract java.util.Iterator iterator()
Get an iterator for the children of this term. If no iterator is possible for this type of term, returns an empty Iterator that cannot be modified. If there are no children, but children are possible, then return an empty Iterator. All optional methods of the Iterator interface are implemented if the term is composite.

Returns:
Iterator an iterator over the children of this term
See Also:
isAtomic(), isComposite()

listIterator

public abstract java.util.ListIterator listIterator()
Get an iterator for the children of this term. If no iterator is possible for this type of term, returns an empty ListIterator that cannot be modified. If there are no children, but children are possible, then return an empty Iterator. All optional methods of the ListIterator interface are implemented if the term is composite.

Returns:
ListIterator, an iterator over the children of this term
See Also:
isAtomic(), isComposite()

iclListIterator

public final java.util.ListIterator iclListIterator()
Deprecated. use listIterator() or iterator()


iclArgumentsIterator

public final java.util.ListIterator iclArgumentsIterator()
Deprecated. use listIterator() or iterator()


iclClone

public final IclTerm iclClone()
Deprecated. use clone()


iclAddToList

public final void iclAddToList(IclTerm t,
                               boolean atEnd)
Deprecated. use add(IclTerm) or add(int, IclTerm)


iclAddToList

public final void iclAddToList(IclTerm t)
Deprecated. use add


iclDelete

public final void iclDelete(IclTerm t)
Deprecated. use removeUnifying


iclRemoveElement

public final IclTerm iclRemoveElement(IclTerm t)
Deprecated. use removeUnifying


iclRemoveFromList

public final void iclRemoveFromList(IclTerm t)
Deprecated. use removeUnifying


iclReplaceElement

public final void iclReplaceElement(IclTerm oldTerm,
                                    IclTerm newTerm)
Deprecated. use replaceUnifying


iclReplaceNthTerm

public final void iclReplaceNthTerm(int position,
                                    IclTerm newTerm)
Deprecated. use replaceElement


iclAppend

public final void iclAppend(IclTerm inList)
Deprecated. use add


iclRetractAll

public final void iclRetractAll()
Deprecated. use clearTerms


iclListLen

public final int iclListLen()
Deprecated. use size


iclNumTerms

public final int iclNumTerms()
Deprecated. use size


iclNthTerm

public final IclTerm iclNthTerm(int index)
Deprecated. use getTerm(index - 1)


iclUnify

public static final IclTerm iclUnify(IclTerm t1,
                                     IclTerm t2,
                                     boolean inDebug)
Deprecated. use Unifier.getInstance().unify()


iclUnify

public static final IclTerm iclUnify(IclTerm t1,
                                     IclTerm t2)
Deprecated. use Unifier.getInstance().unify()


iclUnify

public static final IclTerm iclUnify(IclTerm t1,
                                     IclTerm t2,
                                     java.util.HashMap bindings)
Deprecated. use Unifier.getInstance().unify()


iclDerefTerm

public static final IclTerm iclDerefTerm(IclTerm t1,
                                         java.util.HashMap bindings)
Deprecated. use Unifier.getInstance().deref()


iclStr

public java.lang.String iclStr()
Deprecated. use toIdentifyingString()


toVector

public java.util.Vector toVector()
Deprecated. use toArrayList()


iclInt

public int iclInt()
Deprecated. use ToInt.getInstance().from() or ((IclInt)this).toInt


iclStarter

public char iclStarter()
Deprecated. use ToStarter.getInstance().from() or ((IclGroup)this).getStarter


iclFloat

public float iclFloat()
Deprecated. use ToFloat.getInstance().from() or ((IclFloat)this).toFloat or ((IclFloat)this).toDouble


iclFunctor

public java.lang.String iclFunctor()
Deprecated. use ToFunctor.getInstance().from() or ((IclStruct)this).getFunctor()


iclArguments

public java.util.ArrayList iclArguments()
Deprecated. use toArrayList()