package aima.logic;
/**
* The Variable class provides a class of objects to represent variables
* inside expressions that are to be unified.
*
* @author Michael S. Braverman
* @see Unify
*/
public class Variable {
/**
* Creates a new variable with the given name. The implementation
* guarantees that two variables created with the same name will
* be equal according to the equals
predicate.
*
* @param baseName the name to give the variable.
* @see #equals
*/
public Variable(String baseName) {
name = baseName;
count = 0;
}
/**
* Creates a new variable based on the name of the given existing
* variable, but which is not equal to that variable in any way.
* This constructor method is useful in renaming variables inside
* expressions, where you want the resulting renamed expression to
* contain similar looking variables that are actually unique from
* the variables in the original expression.
*
* @param oldVar the variable whose name is to be used as a basis
* for the name of the newly created Variable
.
*/
public Variable(Variable oldVar) {
name = oldVar.name;
count = newVariableCounter++;
}
/**
* Calculates hashCode of this Variable
.
* Needed so that one can successfully hash a Variable
object
* in a java.util.Hashtable
*
* @return The hash code.
* @see java.util.Hashtable
*/
public int hashCode() {
return (int) (count+name.hashCode());
}
/**
* Determine if this Variable
object is equal to the
* other
passed Object.
*
*
* Note: the signature of the passed parameter is in terms of an
* Object
as opposed to simply a Variable
* because we want this method to match the signature of the
* equals
predicate in used to put
and
* get
entries in a java.util.Hashtable
.
*
* @return If other
is equal to this Variable
.
* @param other The Object
to compare with.
* @see java.util.Hashtable
*/
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Variable) {
Variable otherVar = (Variable) other;
return ((count==otherVar.count) && name.equals(otherVar.name));
} else {
return false;
}
}
/**
* Return the prefix character that should be used in the printed
* representation of all Variable
objects and is used
* on input to flag an atom as one that should be converted to a
* Variable
object.
*
* @return The prefix character denoting a Variable
.
*/
final static public char getPrefix() {
return prefix;
}
/**
* Creates a String
representation of this
* Variable
.
*
* @return The String
representation of this
* Variable
.
*/
public String toString() {
if (count != 0)
return prefix + name + "." + count;
else
return prefix + name;
}
/**
* The Variable
name.
*/
private String name;
/**
* A unique number to distinguish two Variable
s that
* may have the same name.
*
* @see #name
*/
private long count;
/**
* A counter to generate unique identity counts.
*
* @see #equals
*/
private static long newVariableCounter = 1;
/**
* The character prefix used to denote a variable.
*
* @see getPrefix()
*/
final private static char prefix = '?';
}