Class Eval

java.lang.Object
org.djutils.eval.Eval

public class Eval extends Object
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
  • 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

      public Eval setRetrieveValue(RetrieveValue retrieveValue)
      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

      public Eval setUnitParser(UnitParser unitParser)
      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

      public Object evaluate(String expression) throws RuntimeException
      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

      public Boolean evaluateAsBoolean(String expression) throws RuntimeException
      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

      public double evaluateAsDouble(String expression) throws RuntimeException
      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

      public Collection<Function> builtInFunctions()
      Create and return a collection of all built in functions.
      Returns:
      Collection<Function>; all built in functions
    • setUserDefinedFunctions

      public Eval setUserDefinedFunctions(Map<String,Function> userDefinedFunctionMap)
      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

      public Object evaluateExpression(String expression) throws RuntimeException
      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