Package org.projog.core.term
Class Variable
- java.lang.Object
-
- org.projog.core.term.Variable
-
- All Implemented Interfaces:
Term
public final class Variable extends java.lang.Object implements Term
Represents an unspecifiedTerm.A
Variablecan be either instantiated (representing another singleTerm) or uninstantiated (not representing any otherTerm).Variables are not constants. WhatTerm, if any, aVariableis instantiated with can vary during its life time. AVariablebecomes instantiated by calls tounify(Term)and becomes uninstantiated again by calls tobacktrack().
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringANONYMOUS_VARIABLE_IDprivate java.util.Map<VariableAttribute,Term>attributesA collection of attributes associated with the variable.private java.lang.StringidThe value by which the variable can be identifiedprivate TermvalueTheTermthis object is currently instantiated with (ornullif it is currently uninstantiated)
-
Constructor Summary
Constructors Modifier Constructor Description Variable()Creates an anonymous variable.Variable(java.lang.String id)privateVariable(java.lang.String id, java.util.Map<VariableAttribute,Term> attributes)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbacktrack()Reverts this variable to an uninstantiated state.Termcopy(java.util.Map<Variable,Variable> sharedVariables)Returns a copy of this term.Term[]getArgs()CallsTerm.getArgs()on theTermthis variable is instantiated with.TermgetArgument(int index)CallsTerm.getArgument(int)on theTermthis variable is instantiated with.TermgetAttributeOrDefault(VariableAttribute attributeKey, Term defaultValue)TermgetBound()Returns the term this object is bound to.java.lang.StringgetId()java.lang.StringgetName()CallsTerm.getName()on theTermthis variable is instantiated with.intgetNumberOfArguments()CallsTerm.getNumberOfArguments()on theTermthis variable is instantiated with.TermgetTerm()Returns the current instantiated state of this term.TermTypegetType()private TermgetValue()booleanisAnonymous()booleanisImmutable()Always returnsfalseeven if instantiated with an immutableTerm.private booleanpostUnify()voidputAttribute(VariableAttribute attributeKey, Term attributeValue)voidremoveAttribute(VariableAttribute attributeKey)java.lang.StringtoString()booleanunify(Term t)Attempts to unify this term to the specified term.private static booleanunifyVariablesWithAttributes(Variable v1, Variable v2)Unify two variables that both have attributes.
-
-
-
Field Detail
-
ANONYMOUS_VARIABLE_ID
public static final java.lang.String ANONYMOUS_VARIABLE_ID
- See Also:
- Constant Field Values
-
id
private final java.lang.String id
The value by which the variable can be identified
-
value
private Term value
TheTermthis object is currently instantiated with (ornullif it is currently uninstantiated)
-
attributes
private final java.util.Map<VariableAttribute,Term> attributes
A collection of attributes associated with the variable.
-
-
Constructor Detail
-
Variable
public Variable()
Creates an anonymous variable. The ID of the variable will be an underscore.
-
Variable
public Variable(java.lang.String id)
- Parameters:
id- value by which this variable can be identified
-
Variable
private Variable(java.lang.String id, java.util.Map<VariableAttribute,Term> attributes)
-
-
Method Detail
-
getAttributeOrDefault
public Term getAttributeOrDefault(VariableAttribute attributeKey, Term defaultValue)
-
putAttribute
public void putAttribute(VariableAttribute attributeKey, Term attributeValue)
-
removeAttribute
public void removeAttribute(VariableAttribute attributeKey)
-
getName
public java.lang.String getName()
CallsTerm.getName()on theTermthis variable is instantiated with.
-
getId
public java.lang.String getId()
- Returns:
- value provided in constructor by which this variable can be identified
-
isAnonymous
public boolean isAnonymous()
-
getArgs
public Term[] getArgs()
CallsTerm.getArgs()on theTermthis variable is instantiated with.- Specified by:
getArgsin interfaceTerm- Returns:
- array of this terms's arguments
- Throws:
java.lang.NullPointerException- if theVariableis currently uninstantiated- See Also:
Term.getArgument(int)
-
getNumberOfArguments
public int getNumberOfArguments()
CallsTerm.getNumberOfArguments()on theTermthis variable is instantiated with.- Specified by:
getNumberOfArgumentsin interfaceTerm- Returns:
- number of arguments in this term
- Throws:
java.lang.NullPointerException- if theVariableis currently uninstantiated
-
getArgument
public Term getArgument(int index)
CallsTerm.getArgument(int)on theTermthis variable is instantiated with.- Specified by:
getArgumentin interfaceTerm- Parameters:
index- index of the argument to return- Returns:
- the term at the specified position in this term's arguments
- Throws:
java.lang.NullPointerException- if theVariableis currently uninstantiated
-
unify
public boolean unify(Term t)
Description copied from interface:TermAttempts to unify this term to the specified term.The rules for deciding if two terms are unifiable are as follows:
- An uninstantiated
Variablewill unify with any term. As a result theVariablewill become instantiated to the other term. The instantiaton will be undone whenTerm.backtrack()is next called on theVariable - Non-variable terms will unify with other terms that are of the same
TermTypeand 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.
- Specified by:
unifyin interfaceTerm- Parameters:
t- the term to unify this term against- Returns:
trueif the attempt to unify this term to the given term was successful- See Also:
Term.backtrack()
- An uninstantiated
-
unifyVariablesWithAttributes
private static boolean unifyVariablesWithAttributes(Variable v1, Variable v2)
Unify two variables that both have attributes.
-
postUnify
private boolean postUnify()
-
getType
public TermType getType()
- Specified by:
getTypein interfaceTerm- Returns:
TermType.VARIABLEif this variable is uninstantiated else callsTerm.getType()on theTermthis variable is instantiated with.
-
isImmutable
public boolean isImmutable()
Always returnsfalseeven if instantiated with an immutableTerm.- Specified by:
isImmutablein interfaceTerm- Returns:
false
-
copy
public Term copy(java.util.Map<Variable,Variable> sharedVariables)
Description copied from interface:Term
-
getBound
public Term getBound()
Description copied from interface:TermReturns the term this object is bound to.For anything other than a
Variablethis method will returnthis. TODO think of a better name and explanation.
-
getTerm
public Term getTerm()
Description copied from interface:TermReturns the current instantiated state of this term.Returns a representation of this term with all instantiated
Variables replaced with the terms they are instantiated with.- Specified by:
getTermin interfaceTerm- Returns:
- itself if this variable is uninstantiated else calls
Term.getType()on theTermthis variable is instantiated with.
-
getValue
private Term getValue()
-
backtrack
public void backtrack()
Reverts this variable to an uninstantiated state.- Specified by:
backtrackin interfaceTerm- See Also:
Term.unify(Term)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- if this variable is uninstantiated then returns this variable's id else calls
toString()on theTermthis variable is instantiated with.
-
-