Class Variable

  • All Implemented Interfaces:
    Term

    public final class Variable
    extends java.lang.Object
    implements Term
    Represents an unspecified Term.

    A Variable can be either instantiated (representing another single Term) or uninstantiated (not representing any other Term). Variables 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().

    • 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
        The Term this object is currently instantiated with (or null if 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

      • getName

        public java.lang.String getName()
        Calls Term.getName() on the Term this variable is instantiated with.
        Specified by:
        getName in interface Term
        Returns:
        a string representation of this term
        Throws:
        java.lang.NullPointerException - if the Variable is currently uninstantiated
      • 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()
        Calls Term.getArgs() on the Term this variable is instantiated with.
        Specified by:
        getArgs in interface Term
        Returns:
        array of this terms's arguments
        Throws:
        java.lang.NullPointerException - if the Variable is currently uninstantiated
        See Also:
        Term.getArgument(int)
      • getNumberOfArguments

        public int getNumberOfArguments()
        Calls Term.getNumberOfArguments() on the Term this variable is instantiated with.
        Specified by:
        getNumberOfArguments in interface Term
        Returns:
        number of arguments in this term
        Throws:
        java.lang.NullPointerException - if the Variable is currently uninstantiated
      • getArgument

        public Term getArgument​(int index)
        Calls Term.getArgument(int) on the Term this variable is instantiated with.
        Specified by:
        getArgument in interface Term
        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 the Variable is currently uninstantiated
      • unify

        public boolean unify​(Term t)
        Description copied from interface: Term
        Attempts to unify this term to the specified term.

        The rules for deciding if two terms are unifiable are as follows:

        • An uninstantiated 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
        • Non-variable terms will unify with other terms that are of the same 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.
        Note: can leave things in "half-state" on failure as neither List or Predicate backtrack earlier args.
        Specified by:
        unify in interface Term
        Parameters:
        t - the term to unify this term against
        Returns:
        true if the attempt to unify this term to the given term was successful
        See Also:
        Term.backtrack()
      • unifyVariablesWithAttributes

        private static boolean unifyVariablesWithAttributes​(Variable v1,
                                                            Variable v2)
        Unify two variables that both have attributes.
      • postUnify

        private boolean postUnify()
      • isImmutable

        public boolean isImmutable()
        Always returns false even if instantiated with an immutable Term.
        Specified by:
        isImmutable in interface Term
        Returns:
        false
      • copy

        public Term copy​(java.util.Map<Variable,​Variable> sharedVariables)
        Description copied from interface: Term
        Returns a copy of this term.

        The returned copy will share any immutable terms contained in this term. The returned copy will contain new instances for any Variables contained in this term. The sharedVariables parameter keeps track of which Variables have already been copied.

        Specified by:
        copy in interface Term
        Parameters:
        sharedVariables - keeps track of which Variables have already been copied (key = original version, value = version used in copy)
        Returns:
        a copy of this term
      • getBound

        public Term getBound()
        Description copied from interface: Term
        Returns the term this object is bound to.

        For anything other than a Variable this method will return this. TODO think of a better name and explanation.

        Specified by:
        getBound in interface Term
      • getTerm

        public Term getTerm()
        Description copied from interface: Term
        Returns 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:
        getTerm in interface Term
        Returns:
        itself if this variable is uninstantiated else calls Term.getType() on the Term this variable is instantiated with.
      • getValue

        private Term getValue()
      • backtrack

        public void backtrack()
        Reverts this variable to an uninstantiated state.
        Specified by:
        backtrack in interface Term
        See Also:
        Term.unify(Term)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        if this variable is uninstantiated then returns this variable's id else calls toString() on the Term this variable is instantiated with.