Class Expression


  • public class Expression
    extends java.lang.Object
    Main class that allow creating, parsing, passing parameters and evaluating an expression string.
    See Also:
    EvalEx Homepage
    • Constructor Summary

      Constructors 
      Constructor Description
      Expression​(Expression expression)
      Creates a copy with the same expression string, configuration and syntax tree from an existing expression.
      Expression​(java.lang.String expressionString)
      Creates a new expression with the default configuration.
      Expression​(java.lang.String expressionString, ExpressionConfiguration configuration)
      Creates a new expression with a custom configuration.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Expression and​(java.lang.String variable, java.lang.Object value)
      Adds a variable value to the expression data storage.
      EvaluationValue convertDoubleValue​(double value)
      Converts a double value to an EvaluationValue by considering the configured MathContext.
      EvaluationValue convertValue​(java.lang.Object value)
      Converts an object value to an EvaluationValue by considering the configuration EvaluationValue(Object, ExpressionConfiguration).
      Expression copy()
      Return a copy of the expression using the copy constructor Expression(com.ezylang.evalex.Expression).
      ASTNode createExpressionNode​(java.lang.String expression)
      Create an AST representation for an expression string.
      EvaluationValue evaluate()
      Evaluates the expression by parsing it (if not done before) and the evaluating it.
      EvaluationValue evaluateSubtree​(ASTNode startNode)
      Evaluates only a subtree of the abstract syntax tree.
      ASTNode getAbstractSyntaxTree()
      Returns the root ode of the parsed abstract syntax tree.
      java.util.List<ASTNode> getAllASTNodes()
      Returns the list of all nodes of the abstract syntax tree.
      java.util.Set<java.lang.String> getUndefinedVariables()
      Returns all variables that are used in the expression, but have no value assigned.
      java.util.Set<java.lang.String> getUsedVariables()
      Returns all variables that are used i the expression, excluding the constants like e.g.
      void validate()
      Validates the expression by parsing it and throwing an exception, if the parser fails.
      Expression with​(java.lang.String variable, java.lang.Object value)
      Adds a variable value to the expression data storage.
      Expression withValues​(java.util.Map<java.lang.String,​?> values)
      Adds all variables values defined in the map with their name (key) and value to the data storage.If a value with the same name already exists, it is overridden.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Expression

        public Expression​(java.lang.String expressionString)
        Creates a new expression with the default configuration. The expression is not parsed until it is first evaluated or validated.
        Parameters:
        expressionString - A string holding an expression.
      • Expression

        public Expression​(java.lang.String expressionString,
                          ExpressionConfiguration configuration)
        Creates a new expression with a custom configuration. The expression is not parsed until it is first evaluated or validated.
        Parameters:
        expressionString - A string holding an expression.
      • Expression

        public Expression​(Expression expression)
                   throws ParseException
        Creates a copy with the same expression string, configuration and syntax tree from an existing expression. The existing expression will be parsed to populate the syntax tree.
        Parameters:
        expression - An existing expression.
        Throws:
        ParseException - If there were problems while parsing the existing expression.
    • Method Detail

      • evaluateSubtree

        public EvaluationValue evaluateSubtree​(ASTNode startNode)
                                        throws EvaluationException
        Evaluates only a subtree of the abstract syntax tree.
        Parameters:
        startNode - The ASTNode to start evaluation from.
        Returns:
        The evaluation result value.
        Throws:
        EvaluationException - If there were problems while evaluating the expression.
      • getAbstractSyntaxTree

        public ASTNode getAbstractSyntaxTree()
                                      throws ParseException
        Returns the root ode of the parsed abstract syntax tree.
        Returns:
        The abstract syntax tree root node.
        Throws:
        ParseException - If there were problems while parsing the expression.
      • validate

        public void validate()
                      throws ParseException
        Validates the expression by parsing it and throwing an exception, if the parser fails.
        Throws:
        ParseException - If there were problems while parsing the expression.
      • with

        public Expression with​(java.lang.String variable,
                               java.lang.Object value)
        Adds a variable value to the expression data storage. If a value with the same name already exists, it is overridden. The data type will be determined by examining the passed value object. An exception is thrown, if he found data type is not supported.
        Parameters:
        variable - The variable name.
        value - The variable value.
        Returns:
        The Expression instance, to allow chaining of methods.
      • and

        public Expression and​(java.lang.String variable,
                              java.lang.Object value)
        Adds a variable value to the expression data storage. If a value with the same name already exists, it is overridden. The data type will be determined by examining the passed value object. An exception is thrown, if he found data type is not supported.
        Parameters:
        variable - The variable name.
        value - The variable value.
        Returns:
        The Expression instance, to allow chaining of methods.
      • withValues

        public Expression withValues​(java.util.Map<java.lang.String,​?> values)
        Adds all variables values defined in the map with their name (key) and value to the data storage.If a value with the same name already exists, it is overridden. The data type will be determined by examining the passed value object. An exception is thrown, if he found data type is not supported.
        Parameters:
        values - A map with variable values.
        Returns:
        The Expression instance, to allow chaining of methods.
      • createExpressionNode

        public ASTNode createExpressionNode​(java.lang.String expression)
                                     throws ParseException
        Create an AST representation for an expression string. The node can then be used as a sub-expression. Subexpressions are not cached.
        Parameters:
        expression - The expression string.
        Returns:
        The root node of the expression AST representation.
        Throws:
        ParseException - On any parsing error.
      • convertValue

        public EvaluationValue convertValue​(java.lang.Object value)
        Converts an object value to an EvaluationValue by considering the configuration EvaluationValue(Object, ExpressionConfiguration).
        Parameters:
        value - The object value to covert.
        Returns:
        An EvaluationValue of the detected type and value.
      • getAllASTNodes

        public java.util.List<ASTNode> getAllASTNodes()
                                               throws ParseException
        Returns the list of all nodes of the abstract syntax tree.
        Returns:
        The list of all nodes in the parsed expression.
        Throws:
        ParseException - If there were problems while parsing the expression.
      • getUsedVariables

        public java.util.Set<java.lang.String> getUsedVariables()
                                                         throws ParseException
        Returns all variables that are used i the expression, excluding the constants like e.g. PI or TRUE and FALSE.
        Returns:
        All used variables excluding constants.
        Throws:
        ParseException - If there were problems while parsing the expression.
      • getUndefinedVariables

        public java.util.Set<java.lang.String> getUndefinedVariables()
                                                              throws ParseException
        Returns all variables that are used in the expression, but have no value assigned.
        Returns:
        All variables that have no value assigned.
        Throws:
        ParseException - If there were problems while parsing the expression.