Class Variable

java.lang.Object
org.projog.core.term.Variable
All Implemented Interfaces:
Term

public final class Variable extends 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 Details

  • Constructor Details

    • Variable

      public Variable()
      Creates an anonymous variable. The ID of the variable will be an underscore.
    • Variable

      public Variable(String id)
      Parameters:
      id - value by which this variable can be identified
  • Method Details

    • getAttributeOrDefault

      public Term getAttributeOrDefault(VariableAttribute attributeKey, Term defaultValue)
    • putAttribute

      public void putAttribute(VariableAttribute attributeKey, Term attributeValue)
    • removeAttribute

      public void removeAttribute(VariableAttribute attributeKey)
    • getName

      public 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:
      NullPointerException - if the Variable is currently uninstantiated
    • getId

      public String getId()
      Returns:
      value provided in constructor by which this variable can be identified
    • isAnonymous

      public boolean isAnonymous()
    • 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:
      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:
      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:
    • getType

      public TermType getType()
      Returns TermType.VARIABLE if uninstantiated else TermType of instantiated Term.
      Specified by:
      getType in interface Term
      Returns:
      TermType.VARIABLE if this variable is uninstantiated else calls Term.getType() on the Term this variable is instantiated with.
    • 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(Map<Variable,Term> 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.
    • backtrack

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

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