public final class Variable extends Object implements Term
Term
.
A Variable
can be either instantiated (representing another single Term
) or uninstantiated (not
representing any other Term
). Variable
s are not constants. What Term
, if any, a
Variable
is instantiated with can vary during its life time. A Variable
becomes instantiated by calls
to unify(Term)
and becomes uninstantiated again by calls to backtrack()
.
Modifier and Type | Field and Description |
---|---|
static String |
ANONYMOUS_VARIABLE_ID |
Constructor and Description |
---|
Variable()
Creates an anonymous variable.
|
Variable(String id) |
Modifier and Type | Method and Description |
---|---|
void |
backtrack()
Reverts this variable to an uninstantiated state.
|
Term |
copy(Map<Variable,Variable> sharedVariables)
Returns a copy of this term.
|
Term[] |
getArgs()
Calls
Term.getArgs() on the Term this variable is instantiated with. |
Term |
getArgument(int index)
Calls
Term.getArgument(int) on the Term this variable is instantiated with. |
Term |
getAttributeOrDefault(VariableAttribute attributeKey,
Term defaultValue) |
Term |
getBound()
Returns the term this object is bound to.
|
String |
getId() |
String |
getName()
Calls
Term.getName() on the Term this variable is instantiated with. |
int |
getNumberOfArguments()
Calls
Term.getNumberOfArguments() on the Term this variable is instantiated with. |
Term |
getTerm()
Returns the current instantiated state of this term.
|
TermType |
getType()
|
boolean |
isAnonymous() |
boolean |
isImmutable()
Always returns
false even if instantiated with an immutable Term . |
void |
putAttribute(VariableAttribute attributeKey,
Term attributeValue) |
void |
removeAttribute(VariableAttribute attributeKey) |
String |
toString() |
boolean |
unify(Term t)
Attempts to unify this term to the specified term.
|
public static final String ANONYMOUS_VARIABLE_ID
public Variable()
public Variable(String id)
id
- value by which this variable can be identifiedpublic Term getAttributeOrDefault(VariableAttribute attributeKey, Term defaultValue)
public void putAttribute(VariableAttribute attributeKey, Term attributeValue)
public void removeAttribute(VariableAttribute attributeKey)
public String getName()
Term.getName()
on the Term
this variable is instantiated with.getName
in interface Term
NullPointerException
- if the Variable
is currently uninstantiatedpublic String getId()
public boolean isAnonymous()
public Term[] getArgs()
Term.getArgs()
on the Term
this variable is instantiated with.getArgs
in interface Term
NullPointerException
- if the Variable
is currently uninstantiatedTerm.getArgument(int)
public int getNumberOfArguments()
Term.getNumberOfArguments()
on the Term
this variable is instantiated with.getNumberOfArguments
in interface Term
NullPointerException
- if the Variable
is currently uninstantiatedpublic Term getArgument(int index)
Term.getArgument(int)
on the Term
this variable is instantiated with.getArgument
in interface Term
index
- index of the argument to returnNullPointerException
- if the Variable
is currently uninstantiatedpublic boolean unify(Term t)
Term
The rules for deciding if two terms are unifiable are as follows:
Variable
will unify with any term. As a result the Variable
will become
instantiated to the other term. The instantiaton will be undone when Term.backtrack()
is next called on the
Variable
TermType
and have the same value.
The exact meaning of "having the same value" will vary between term types but will include that the two terms
being unified have the same number of arguments and that all of their corresponding arguments unify.unify
in interface Term
t
- the term to unify this term againsttrue
if the attempt to unify this term to the given term was successfulTerm.backtrack()
public TermType getType()
getType
in interface Term
TermType.VARIABLE
if this variable is uninstantiated else calls Term.getType()
on the
Term
this variable is instantiated with.public boolean isImmutable()
false
even if instantiated with an immutable Term
.isImmutable
in interface Term
false
public Term copy(Map<Variable,Variable> sharedVariables)
Term
public Term getBound()
Term
For anything other than a Variable
this method will return this
. TODO think of a better name and
explanation.
public Term getTerm()
Term
Returns a representation of this term with all instantiated Variable
s replaced with the terms they are
instantiated with.
getTerm
in interface Term
Term.getType()
on the Term
this
variable is instantiated with.public void backtrack()
backtrack
in interface Term
Term.unify(Term)
Copyright © 2024. All rights reserved.