Package org.djutils.eval
Class Eval
java.lang.Object
org.djutils.eval.Eval
Eval - evaluate mathematical expression. Derived from software developed between 2002-2016 by Peter Knoppers.
CONSIDER: implement string datatype.
The precedence of binary operators follows the list of the Java Operator Precedence Table, skipping bitwise and other operators that make no sense for this evaluator and adding the exponentiation (^) operator.
- Author:
- Peter Knoppers
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static interface
Interface for comparator for two double values. -
Constructor Summary
ConstructorDescriptionEval()
Construct a new evaluator with no RetrieveValue object and no added/overridden function and no added/overridden units. -
Method Summary
Modifier and TypeMethodDescriptionCreate and return a collection of all built in functions.Evaluate a mathematical expressionevaluateAsBoolean
(String expression) Evaluate a mathematical expression, check that the result is a logical value and return that value as a Booleandouble
evaluateAsDouble
(String expression) Evaluate a mathematical expression, check that the result is a floating point value and return that value as a double.evaluateExpression
(String expression) Evaluate one expression.setRetrieveValue
(RetrieveValue retrieveValue) Set or replace the RetrieveValue object of this evaluator.setUnitParser
(UnitParser unitParser) Install a unit parser (or replace or remove a previously installed unit parser).setUserDefinedFunctions
(Map<String, Function> userDefinedFunctionMap) Install a map of user-defined functions.
-
Constructor Details
-
Eval
public Eval()Construct a new evaluator with no RetrieveValue object and no added/overridden function and no added/overridden units.
-
-
Method Details
-
setRetrieveValue
Set or replace the RetrieveValue object of this evaluator.- Parameters:
retrieveValue
- RetrieveValue; the new RetrieveValue object (may be null to only delete the currently active RetrieveValue object).- Returns:
- Eval; this (for easy method chaining)
-
setUnitParser
Install a unit parser (or replace or remove a previously installed unit parser). A user supplied unit parser takes precedence over the built-in unit parser (that can only handle SI strings; see SIDimensions.of).- Parameters:
unitParser
- UnitParser; the new unit parser or null to remove a previously installed unit parser- Returns:
- Eval; this (for easy method chainging)
-
evaluate
Evaluate a mathematical expression- Parameters:
expression
- String; the expression- Returns:
- Object; The value of the evaluated expression
- Throws:
RuntimeException
- when the expression cannot be evaluated
-
evaluateAsBoolean
Evaluate a mathematical expression, check that the result is a logical value and return that value as a Boolean- Parameters:
expression
- String; the expression- Returns:
- Boolean; the result of the expression
- Throws:
RuntimeException
- when the expression could not be evaluated, or the result is not a logical value
-
evaluateAsDouble
Evaluate a mathematical expression, check that the result is a floating point value and return that value as a double. If the result is strongly typed (some DJUNITS quantity), the SI value is returned.- Parameters:
expression
- String; the expression- Returns:
- double; the result of the expression
- Throws:
RuntimeException
- when the expression could not be evaluated, or the result is not a logical value
-
builtInFunctions
Create and return a collection of all built in functions.- Returns:
- Collection<Function>; all built in functions
-
setUserDefinedFunctions
Install a map of user-defined functions. If a built-in function has the same name as a user-defined function; the user-defined function takes precedence.- Parameters:
userDefinedFunctionMap
- Map<String, Function>; map that maps the name of the function to a Function object- Returns:
- Eval; this (for easy method chaining)
-
evaluateExpression
Evaluate one expression.- Parameters:
expression
- String; the expression to evaluate- Returns:
- Object; the result of the evaluation (DoubleScalar or Boolean)
- Throws:
RuntimeException
- when the expression could not be evaluated, or the result is not a logical value
-