Class Expression<T>

java.lang.Object
fr.uga.pddl4j.parser.Expression<T>
Type Parameters:
T - The type of internal representation used. The parser used string presentation. The instantiation process used integer representation for efficiency.
All Implemented Interfaces:
Locatable, Serializable, Iterable<Expression<T>>

public class Expression<T> extends Object implements Locatable, Iterable<Expression<T>>, Serializable
This class implements an expression. An expression defines a syntactical tree. It is used by the parser and during the instantiation process to manipulate a planning problem.
See Also:
Serialized Form
  • Constructor Details

    • Expression

      public Expression(Expression<T> other)
      Creates a new expression from another. This constructor creates a deep copy.
      Parameters:
      other - the other expression.
    • Expression

      public Expression(Connector connector)
      Creates a new expression with a specified connector.
      Parameters:
      connector - the connector.
      See Also:
      Connector
    • Expression

      public Expression()
      Creates a new empty AND expression.
  • Method Details

    • getConnector

      public final Connector getConnector()
      Returns the connector of this expression.
      Returns:
      the connector of this expression.
    • setConnector

      public final void setConnector(Connector connective)
      Set the connector of this expression.
      Parameters:
      connective - the connective.
    • getSymbol

      public final Symbol<T> getSymbol()
      Returns the symbol to this expression. Expression with a symbol are predicate, function or task formula.
      Returns:
      the symbol the new symbol of this expression. If this expression is not ATOM, a FUNCTION or TASK the returned symbol is null.
    • setSymbol

      public final void setSymbol(Symbol<T> symbol)
      Sets a new symbol to this expression. Expression with a symbol are predicate, function or task formula.
      Parameters:
      symbol - the new symbol of this expression.
    • getArguments

      public final List<Symbol<T>> getArguments()
      Returns the arguments of the atomic formula represented by this expression. Only expression with a symbol such as predicate, function or task formula have arguments.
      Returns:
      the arguments of the atomic formula represented by this expression.
    • setArguments

      public final void setArguments(List<Symbol<T>> arguments)
      Sets the argument of the atomic formula represented by this expression. Only expression with a symbol such as predicate, function or task formula have arguments.
      Parameters:
      arguments - the arguments of the atomic formula represented by this expression.
    • addArgument

      public final boolean addArgument(Symbol<T> argument)
      Adds an argument to this expression.
      Parameters:
      argument - the argument to add.
      Returns:
      true if the argument was added false otherwise.
    • getQuantifiedVariables

      public final List<TypedSymbol<T>> getQuantifiedVariables()
      Returns the list of quantified variables of this expression.
      Returns:
      the list of quantified variables of this expression.
    • setQuantifiedVariables

      public final void setQuantifiedVariables(List<TypedSymbol<T>> variables)
      Sets the quantified variables of this expression.
      Parameters:
      variables - the quantified variables of this expression.
    • addQuantifiedVariable

      public final boolean addQuantifiedVariable(TypedSymbol<T> variable)
      Adds a quantified variable to this expression.
      Parameters:
      variable - the quantified variable to add.
      Returns:
      true if the variable was added false otherwise.
    • getValue

      public final Double getValue()
      Returns the numeric value of this expression.
      Returns:
      the numeric value of this expression.
    • setValue

      public final void setValue(Double value)
      Set the numeric value of this expression.
      Parameters:
      value - the numeric value of this expression.
    • getVariable

      public final Symbol<T> getVariable()
      Returns the variable of this expression.
      Returns:
      the variable of this expression.
    • setVariable

      public final void setVariable(Symbol<T> variable)
      Sets a new variable to this expression.
      Parameters:
      variable - the new variable to set.
    • getPrefName

      public final Symbol<T> getPrefName()
      Returns the name of the preference associated to this expression.
      Returns:
      the name of the preference or null if the preference name was not initialized.
    • setPrefName

      public final void setPrefName(Symbol<T> name)
      Sets a name to the preference associated to this expression.
      Parameters:
      name - the name of the preference to set.
    • getTaskID

      public final Symbol<T> getTaskID()
      Returns the taskID associated to this expression. The taskID is use in HTN planning to make alias of task and in method constraints, e.g., hold-before, etc.
      Returns:
      the taskID associated to this expression.
    • setTaskID

      public final void setTaskID(Symbol<T> taskID)
      Set the taskID associated to this expression. The taskID is use in HTN planning to make alias of task and in method constraints, e.g., hold-before, etc.
      Parameters:
      taskID - the taskID to set.
    • addChild

      public final boolean addChild(Expression<T> exp)
      Add a new child expression to this expression.
      Parameters:
      exp - the child to add
      Returns:
      true if the expression was added; false otherwise
      Throws:
      RuntimeException - if the specified node is null
    • setChildren

      public final void setChildren(List<Expression<T>> children)
      Sets the list of children expressions of this expression.
      Parameters:
      children - the children expression to set.
    • getChildren

      public final List<Expression<T>> getChildren()
      Returns the list of children of this expression.
      Returns:
      the list of children of this expression.
    • isPrimtive

      public final boolean isPrimtive()
      Returns true if the expression is a primitive task.
      Returns:
      true if the expression is a primitive task, false otherwise.
    • setPrimtive

      public final void setPrimtive(boolean flag)
      Sets the boolean flag used to specified if the expression is a primitive task to a specified value.
      Parameters:
      flag - the flag.
    • getTimeSpecifier

      public final TimeSpecifier getTimeSpecifier()
      Returns the time specifier of this expression. The time specifiers are used to express the start or the end of task. It is always associated with the taskID of the expression.
      Returns:
      the time specifier of this expression.
    • setTimeSpecifier

      public final void setTimeSpecifier(TimeSpecifier timeSpecifier)
      Sets the time specifier of this expression. The time specifiers are used to express the start or the end of task. It is always associated with the taskID of the expression.
      Parameters:
      timeSpecifier - the time specifier to set.
    • getLocation

      public final Location getLocation()
      Return the location of this expression. The location is used by the parser to store the location of the expression the file and indicate warning or error. The location is null when expression is used during the instantiation process to optimize memory used.
      Specified by:
      getLocation in interface Locatable
      Returns:
      the location of this expression.
      See Also:
      Location
    • setLocation

      public final void setLocation(Location location)
      Sets the location of this expression. The location is used by the parser to store the location of the expression the file and indicate warning or error. The location is null when expression is used during the instantiation process to optimize memory used.
      Parameters:
      location - the location to set.
    • setBegin

      public final void setBegin(fr.uga.pddl4j.parser.lexer.Token begin)
      Sets the begin line and column of the expression from a specified token.
      Specified by:
      setBegin in interface Locatable
      Parameters:
      begin - the first token of the expression.
    • setEnd

      public final void setEnd(fr.uga.pddl4j.parser.lexer.Token end)
      Sets the end line and column of the expression from a specified token.
      Specified by:
      setEnd in interface Locatable
      Parameters:
      end - the last token of the expression.
    • assign

      public final void assign(Expression<T> exp)
      Assigns a specified expression to this expression. After the method call the expression is equals to the expression in parameter. The assignment is swallow, i.e., the assignment does not make a deep copy of the content of the expression in parameter. After assignment, the specified expression is equal to this expression.
      Parameters:
      exp - the expression to assigned to this expression.
    • isLiteral

      public final boolean isLiteral()
      Returns if this expression is a literal. A literal is an atomic formula or a negated atomic formula.
      Returns:
      true if this expression is a literal false otherwise.
    • toNNF

      public final void toNNF()
      Sets the expression into negative normal form. After the method call, negation can occur only before atomic formula with ATOM connector, time specifier (at start, at end, overall) can only occur before literal and method constraints (hold-before, hold-after and hold-between) can only occur before literal.
    • expandQuantifiedExpression

      public void expandQuantifiedExpression(Map<T,​Set<Symbol<T>>> domains)
      Expands the quantified expressions contained in a specified expression.
      Parameters:
      domains - the value domains associated with the type of quantified variables.
    • expandQuantifiedExpression

      public void expandQuantifiedExpression(Map<T,​Set<Symbol<T>>> domains, AtomicFormulaSimplifier<T> simplifier)
      Expands the quantified expressions contained in a specified expression.
      Parameters:
      domains - the value domains associated with the type of quantified variables.
      simplifier - the atomic formula simplifier used to simply atomic formula when it is possible in order to quickly cut off the instantiation of the logical formula. This parameter can be null. In that case no simplification will be done.
    • substitute

      public boolean substitute(Symbol<T> var, Symbol<T> cons)
      Substitutes all occurrence of a specified variable into an expression by a constant.
      Parameters:
      var - the variable.
      cons - the constant.
      Returns:
      true if at least one occurrence of the specified was substituted; false otherwise.
    • substitute

      public boolean substitute(Symbol<T> var, Symbol<T> cons, AtomicFormulaSimplifier<T> simplifier)
      Substitutes all occurrence of a specified variable into an expression by a constant.
      Parameters:
      var - the variable.
      cons - the constant.
      simplifier - the atomic formula simplifier used to simply atomic formula when it is possible in order to quickly cut off the instantiation of the logical formula. This parameter can be null. In that case no simplification will be done.
      Returns:
      true if at least one occurrence of the specified was substituted; false otherwise.
    • simplify

      public final boolean simplify()
      This method simplify a specified expression. The rules of simplification are as below:
      • not true eqv false
      • true ^ phi eqv phi
      • false ^ phi eqv false
      • true v phi eqv true
      • false v phi eqv false
      • not false eqv true
      • phi ^ phi eqv phi
      • phi v phi eqv phi
      • phi ^ not phi eqv false
      • phi v not phi eqv true
      • x = x eqv true
      • x = y eqv false
      Returns:
      true if the expression was simplified; false otherwise.
    • contains

      public final boolean contains(Expression<T> exp)
      Returns if a specified expression is contains, i.e., is a sub-expression of this expression. More formally, returns true if and only if this expression contains at least one subexpression s such that s.equals(exp).
      Parameters:
      exp - the expression to test.
      Returns:
      true if the specified expression exp is a sub-expression of this expression; false otherwise.
    • remove

      public final boolean remove(Expression<T> exp)
      Removes all the occurrences of a specified expression contained in this expression and returns true if and only if at least one occurrence was removed.
      Parameters:
      exp - the expression to remove.
      Returns:
      true if the specified expression exp was removed; false otherwise.
    • equals

      public boolean equals(Object object)
      Returns if the expression is equal to another object. The primitive flag and the task id are not used for comparison.
      Overrides:
      equals in class Object
      Parameters:
      object - the other object.
      See Also:
      Object.equals(java.lang.Object)
    • hashCode

      public int hashCode()
      Returns the hash code value of the expression.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value of the expression.
      See Also:
      Object.hashCode()
    • isMalformedExpression

      public final boolean isMalformedExpression()
      Returns if this expression is malformed. An expression is considered as well in the following cases:
      • OR and AND expressions have to have all their child well-formed.
      • EQUAL_ATOM expressions are well-formed if the expression contains an atom of at least two symbols.
      • Quantified expressions (EXISTS, FORALL) is well formed if it has at least one quantified variable and one child expression.
      • IMPLY, WHEN, EQUAL_COMPARISON, LESS_COMPARISON, LESS_OR_EQUAL_COMPARISON, GREATER_COMPARISON, GREATER_OR_EQUAL_COMPARISON, ASSIGN, INCREASE, DECREASE, SCALE_UP, SCALE_DOWN, MULTIPLICATION, DIVISION, MINUS, PLUS, SOMETIME_AFTER_CONSTRAINT, SOMETIME_BEFORE_CONSTRAINT expressions are well-formed if they have to child and both child are well-formed.
      • NOT, UMINUS, AT_START, AT_END, OVER_ALL, MINIMIZE, MAXIMIZE, F_EXP_T, F_EXP, AT_END_METHOD_CONSTRAINT, AT_START_METHOD_CONSTRAINT, ALWAYS_METHOD_CONSTRAINT, AT_MOST_ONCE_METHOD_CONSTRAINT, SOMETIME_METHOD_CONSTRAINT expression are well-formed if they have on child and if it is well-formed.
      • ATOM, TASKS, and F_HEAD expressions without any symbols as arguments are considered as well formed.
      • TIMED_LITERAL, WITHIN_CONSTRAINT, HOLD_AFTER_CONSTRAINT, HOLD_BEFORE_METHOD_CONSTRAINT, HOLD_AFTER_METHOD_CONSTRAINT, SOMETIME_BEFORE_METHOD_CONSTRAINT, SOMETIME_AFTER_METHOD_CONSTRAINT expressions are considered as well-formed if they have well-formed child and a time value.
      • HOLD_DURING_CONSTRAINT, HOLD_BETWEEN_METHOD_CONSTRAINT, HOLD_DURING_METHOD_CONSTRAINT expressions are well-formed if they have two child well-formed and an time interval value.
      • ALWAYS_WITHIN expression must have at least two children to be considered as well formed and time interval value.
      • NUMBER expressions are considered is they have a value.
      • TIME_VAR, IS_VIOLATED exprssions are always considered as well-formed
      Returns:
      true if the expression is malformed; false otherwise.
    • toString

      public String toString()
      Returns a string representation of this node.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this node.
      See Also:
      Object.toString()
    • toString

      public String toString(String baseOffset) throws MalformedExpressionException
      Returns a string representation of this parser node.
      Parameters:
      baseOffset - the offset white space from the left used for indentation.
      Returns:
      a string representation of this parser node.
      Throws:
      MalformedExpressionException - if the expression is malformed.
    • toCNF

      public void toCNF()
      Convert an expression in conjunctive normal form (CNF).
    • toDNF

      public void toDNF()
      Convert an expression in disjunctive normal form (DNF).
    • getTaskIDs

      public static Set<Symbol<String>> getTaskIDs(Expression<String> exp)
      Returns the set of task IDs contains in an expression.
      Parameters:
      exp - the expression.
      Returns:
      the set of task IDs contains in exp.
    • isLeaf

      public final boolean isLeaf()
      Returns if this expression is a leaf of the syntax tree. An expression is a leaf if it is an ATOM, a TASK, a FN_HEAD, an EQUAL_ATOM, a TASK_ID, a TIMED_TASK_ID, a TIME_VAR, a NUMBER, TRUE or FALSE.
      Returns:
      if this expression is a leaf of the syntax tree.
    • iterator

      public Iterator<Expression<T>> iterator()
      Returns an iterator over the children expressions of the expression. this iterator walks the syntax tree of the expression in depth first.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      an iterator over the children expressions of the expression.