Package com.ezylang.evalex
Class Expression
- java.lang.Object
-
- com.ezylang.evalex.Expression
-
public class Expression extends java.lang.ObjectMain 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 Expressionand(java.lang.String variable, java.lang.Object value)Adds a variable value to the expression data storage.EvaluationValueconvertDoubleValue(double value)Converts a double value to anEvaluationValueby considering the configuredMathContext.EvaluationValueconvertValue(java.lang.Object value)Converts an object value to anEvaluationValueby considering the configurationEvaluationValue(Object, ExpressionConfiguration).Expressioncopy()Return a copy of the expression using the copy constructorExpression(com.ezylang.evalex.Expression).ASTNodecreateExpressionNode(java.lang.String expression)Create an AST representation for an expression string.EvaluationValueevaluate()Evaluates the expression by parsing it (if not done before) and the evaluating it.EvaluationValueevaluateSubtree(ASTNode startNode)Evaluates only a subtree of the abstract syntax tree.ASTNodegetAbstractSyntaxTree()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.voidvalidate()Validates the expression by parsing it and throwing an exception, if the parser fails.Expressionwith(java.lang.String variable, java.lang.Object value)Adds a variable value to the expression data storage.ExpressionwithValues(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.
-
-
-
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
-
evaluate
public EvaluationValue evaluate() throws EvaluationException, ParseException
Evaluates the expression by parsing it (if not done before) and the evaluating it.- Returns:
- The evaluation result value.
- Throws:
EvaluationException- If there were problems while evaluating the expression.ParseException- If there were problems while parsing the expression.
-
evaluateSubtree
public EvaluationValue evaluateSubtree(ASTNode startNode) throws EvaluationException
Evaluates only a subtree of the abstract syntax tree.- Parameters:
startNode- TheASTNodeto 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 ParseExceptionValidates 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.
-
copy
public Expression copy() throws ParseException
Return a copy of the expression using the copy constructorExpression(com.ezylang.evalex.Expression).- Returns:
- The copied Expression instance.
- Throws:
ParseException- If there were problems while parsing the existing expression.
-
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.
-
convertDoubleValue
public EvaluationValue convertDoubleValue(double value)
Converts a double value to anEvaluationValueby considering the configuredMathContext.- Parameters:
value- The double value to covert.- Returns:
- An
EvaluationValueof typeEvaluationValue.DataType.NUMBER.
-
convertValue
public EvaluationValue convertValue(java.lang.Object value)
Converts an object value to anEvaluationValueby considering the configurationEvaluationValue(Object, ExpressionConfiguration).- Parameters:
value- The object value to covert.- Returns:
- An
EvaluationValueof 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 ParseExceptionReturns all variables that are used i the expression, excluding the constants like e.g.PIorTRUEandFALSE.- 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 ParseExceptionReturns 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.
-
-