ai.krr.fol
Class Variable

java.lang.Object
  extended by ai.krr.fol.Term
      extended by ai.krr.fol.Variable
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<Term>

public class Variable
extends Term

This class implements symbolic variables in Java. Like NamedSymbols, Variables are memory-unique objects, i.e. they are equal if and only if the handles are the same. This allows the creation of multiple variables with the same name that are not equal. It also allows for efficient equality testing. The unique index could also be used to compare variables.

Author:
Gerhard Wickler

Field Summary
protected  java.lang.String theName
           
protected  NamedSymbol theType
           
 
Constructor Summary
Variable(java.lang.String aName)
           It is possible to create Variables directly with this constructor.
 
Method Summary
protected  void addConstants(java.util.Set<Symbol> sys)
           This function adds all the constant Symbols used in this Term to the given Set.
protected  void addFreeVariables(java.util.Set<Variable> free, java.util.Set<Variable> bound)
           This function adds the free Variables in this Term to the first given Set.
protected  void addFunctions(java.util.Map<Symbol,java.lang.Integer> sys)
           This function adds all the function Symbols used in this Term to the given Set.
protected  void addVariables(java.util.Set<Variable> vars)
           This function adds this Variable to the given Set.
 Variable clone()
           Cloning a Variable returns the this Variable.
 Term clone(Substitution s)
           Cloning a Variable with a given Substitution returns a new Term which is determined by the given Substitution.
 int compareTo(Term other)
           This function compares this Variable to the given Term.
 int compareTo(Variable other)
           This function compares this Variable to the given Variable.
 java.lang.Object evaluate(Interpretation ipt, Substitution s)
           This function evaluates this Term under the given Interpretation and Substitution for Variables.
 boolean exceedsDepth(int depth)
           This function tests whether this Term exceeds the given depth which should be a non-negative integer.
 int getDepth()
           This function returns the nesting depth of this Term.
protected  long getIndex()
           This function returns the unique index of this Variable.
 java.lang.String getName()
           This function returns the name of this Variable.
 NamedSymbol getType()
           This function returns the Symbol that represents the type of this Variable.
 int hashCode()
           The hashCode of a Variable is the unique index of this variable.
 boolean isGround()
           This function tests whether this Term contains any Variables.
 void setType(NamedSymbol aSy)
           This function sets the type of this Variable to the given NamedSymbol (or null).
 java.lang.String toString()
           A Variable is printed as a question mark followed by the String that is its name and its index.
 boolean unify(Term other, Substitution s)
           This function attempts to extend the given Substitution so that this Term and the given Term are unified.
 
Methods inherited from class ai.krr.fol.Term
getVariables
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

theName

protected final java.lang.String theName

theType

protected NamedSymbol theType
Constructor Detail

Variable

public Variable(java.lang.String aName)

It is possible to create Variables directly with this constructor. However, this new Variable will be different from other Variables with the same name!

Parameters:
aName - the name of the Variable
Method Detail

clone

public Variable clone()

Cloning a Variable returns the this Variable. To obtain a new Variable with the same name as this one, use clone(Substitution) with an appropriate Substitution.

Specified by:
clone in class Term
Returns:
this Variable (same Java Object)

clone

public Term clone(Substitution s)

Cloning a Variable with a given Substitution returns a new Term which is determined by the given Substitution. Thus, the returned Term is not necessarily a Variable. The function Substitution.getValue(ai.krr.fol.Variable) of the Substitution class is used to generate the returned Term. If this requires extending the Substitution, the Substitution used must support this functionality.

To generate a new Variable with the same name as this one, this function can be called with a Substitution that does not contain this Variable. This will result in a new Variable that will then be consistently repleced by the extended Substitution.

Specified by:
clone in class Term
Parameters:
s - the Substitution that determines how to replace Variables
Returns:
a new Term that is an instance of this Term; the Substitution s will extended with any new Variable replacements introduced

isGround

public final boolean isGround()

This function tests whether this Term contains any Variables. Variables always contain Variables.

Specified by:
isGround in class Term
Returns:
always false

getDepth

public final int getDepth()

This function returns the nesting depth of this Term. The depth of a Variable is defined to be 0.

Specified by:
getDepth in class Term
Returns:
the depth of this Term when seen as a tree

exceedsDepth

public final boolean exceedsDepth(int depth)

This function tests whether this Term exceeds the given depth which should be a non-negative integer.

Specified by:
exceedsDepth in class Term
Parameters:
depth - the depth value that is tested for
Returns:
true iff this.getDepth() > depth

evaluate

public java.lang.Object evaluate(Interpretation ipt,
                                 Substitution s)

This function evaluates this Term under the given Interpretation and Substitution for Variables. If all sub-Terms occurring in the Term have a value assigned by the Interpretation the result will be a Constant containing a NamedSymbol which is an element of the Interpretation's domain.

Specified by:
evaluate in class Term
Parameters:
ipt - the Interpretation giving values for Terms
s - the Substitution for Variables occurring in this Term
Returns:
the value of this Term under the given Interpretation

unify

public boolean unify(Term other,
                     Substitution s)

This function attempts to extend the given Substitution so that this Term and the given Term are unified. It returns true if and only if this is possible. A Variable can in principle be unified with any other kind of Term. Whether this succeeds depends entirely on whether the given Substitution can be extended accordingly.

Specified by:
unify in class Term
Parameters:
other - the other Term this Variable is to be unified with
s - the Substitution to be extended for the unification
Returns:
whether a unifying extension of the given Substitution was possible

compareTo

public int compareTo(Term other)

This function compares this Variable to the given Term. Constants are considered to come before Variables and FunctionTerms come after Variables in the order defined here.

Specified by:
compareTo in interface java.lang.Comparable<Term>
Specified by:
compareTo in class Term
Parameters:
other - the Term to which this Variable is to be compared
Returns:
0 if the two Variables are identical; -1 if the given Term should come after this Variable; and +1 if the given Term should come before this Variable

getName

public final java.lang.String getName()

This function returns the name of this Variable.

Returns:
the name of this Variable without the index

setType

public void setType(NamedSymbol aSy)

This function sets the type of this Variable to the given NamedSymbol (or null). If the Variable already had a different type then this will be overwritten.

Parameters:
aSy - the (new) type of this Variable, including null

getType

public NamedSymbol getType()

This function returns the Symbol that represents the type of this Variable. It may be null if the type was undefined.

Returns:
the type of this Variable (or null)

compareTo

public int compareTo(Variable other)

This function compares this Variable to the given Variable. Two Variables are compared first by name and second by index, which in turn is related to the time when they were created.

Parameters:
other - the Variable to which this Variable is to be compared
Returns:
0 if the two Variables are identical; -1 if the given Variable should come after this Variable; and +1 if the given Variable should come before this Variable.

addConstants

protected void addConstants(java.util.Set<Symbol> sys)

This function adds all the constant Symbols used in this Term to the given Set. Variables contain NamedSymbols only if they are typed.

Specified by:
addConstants in class Term
Parameters:
sys - the Set that will contain all the Constants

addFunctions

protected final void addFunctions(java.util.Map<Symbol,java.lang.Integer> sys)

This function adds all the function Symbols used in this Term to the given Set.

Specified by:
addFunctions in class Term
Parameters:
sys - the Set that will contain all the function names

addVariables

protected final void addVariables(java.util.Set<Variable> vars)

This function adds this Variable to the given Set. If this Variable is already in the Set nothing is added.

Specified by:
addVariables in class Term
Parameters:
vars - a Set of Variables that will be extended

addFreeVariables

protected void addFreeVariables(java.util.Set<Variable> free,
                                java.util.Set<Variable> bound)

This function adds the free Variables in this Term to the first given Set. Variables in the second given Set are bound and will not be added.

Specified by:
addFreeVariables in class Term
Parameters:
free - a Set of free Variables that will be extended
bound - a Set of bound Variables that will not be added

getIndex

protected final long getIndex()

This function returns the unique index of this Variable.

Returns:
a long that is unique to this Variable

toString

public java.lang.String toString()

A Variable is printed as a question mark followed by the String that is its name and its index. Use getName() to get the String without the index.

Overrides:
toString in class java.lang.Object
Returns:
the print-name of this Variable

hashCode

public final int hashCode()

The hashCode of a Variable is the unique index of this variable.

Overrides:
hashCode in class java.lang.Object
Returns:
the index of this Variable