Configuration

EvalEx can be configured through an ExpressionConfiguration object, which can be passed as a parameter to the Expression constructor.

Example usage, showing all default configuration values:

ExpressionConfiguration configuration=ExpressionConfiguration.builder()
        .allowOverwriteConstants(true)
        .arraysAllowed(true)
        .dateTimeFormatters(ExpressionConfiguration.DEFAULT_DATE_TIME_FORMATTERS)
        .dataAccessorSupplier(MapBasedDataAccessor::new)
        .decimalPlacesRounding(ExpressionConfiguration.DECIMAL_PLACES_ROUNDING_UNLIMITED)
        .defaultConstants(ExpressionConfiguration.StandardConstants)
        .functionDictionary(ExpressionConfiguration.StandardFunctionsDictionary)
        .implicitMultiplicationAllowed(true)
        .locale(Locale.getDefault())
        .mathContext(ExpressionConfiguration.DEFAULT_MATH_CONTEXT)
        .operatorDictionary(ExpressionConfiguration.StandardOperatorsDictionary)
        .powerOfPrecedence(OperatorIfc.OPERATOR_PRECEDENCE_POWER)
        .stripTrailingZeros(true)
        .structuresAllowed(true)
        .binaryAllowed(false)
        .singleQuoteStringLiteralsAllowed(false)
        .zoneId(ZoneId.systemDefault())
        .build();

        Expression expression=new Expression("2.128 + a",configuration);

Allow to Overwrite Constants

If set to true (default), then variables can be set that have the name of a constant. In that case, the constant value will be removed and a variable value will be set.

Arrays allowed

Specifies if the array index function is allowed (default is true). If set to false, the expression will throw a ParseException, if there is a ‘[’ is encountered in the expression and also no operator or function is defined for this character.

Binary allowed

Specifies if the binary1 (raw) data type is allowed for expressions that can not be converted to any known data type.

See chapter Data Types for details.

Data Accessor

The Data Accessor is responsible for storing and retrieving variable values.

See chapter Data Access for details.

The default implementation is the MapBasedDataAccessor, which stores all variables in a case-insensitive Map.

The supplier is called whenever a new Expression is created, so that each expression can own an own instance of an accessor. Custom implementations of the supplier and data accessor may allow expressions to share the same space.

Date Time Formatters

The date-time formatters. When parsing, each format will be tried and the first matching will be used. For formatting, only the first will be used.
By default, the ExpressionConfiguration.DEFAULT_DATE_TIME_FORMATTERS are used:

  • DateTimeFormatter.ISO_DATE_TIME
  • DateTimeFormatter.ISO_DATE
  • DateTimeFormatter.ISO_LOCAL_DATE_TIME
  • DateTimeFormatter.ISO_LOCAL_DATE
  • DateTimeFormatter.RFC_1123_DATE_TIME

Decimal Places Result

If specified, only the final result of the evaluation will be rounded to the specified number of decimal digits, using the MathContexts rounding mode.

The default value of DECIMAL_PLACES_ROUNDING_UNLIMITED will disable rounding.

Decimal Places Rounding

Specifies the amount of decimal places to round to in each operation or function. See chapter Precision, Scale and Rounding for details.

Default Constants

Specifies the default constants that can be used in every expression as a Map with the constant name and EvaluationValue as value. Each expression has a case-insensitive copy of the default constants. See the reference chapter for a list: Default Constants

Evaluation Value Converter

The converter to use when converting different data types to an EvaluationValue. The DefaultEvaluationValueConverter is used by default.

Function Dictionary

The function dictionary is used to look up the functions that are used in an expression.

See chapter Function Dictionary for details.

The default implementation is the MapBasedFunctionDictionary, which stores all variables in a case-insensitive Map.

Implicit Multiplication

Implicit multiplication automatically adds in expressions like “2x” or “(a+b)(b+c)” the missing multiplication operator, so that the expression reads “2*x” or “(a+b) * (b+c)”.

Implicit multiplication will not work for expressions like x(a+b), which will not be extended to “2*(a+b)”. This expression is treated as a call to function “x”, which, if not defined, will raise a parse exception.

An expression like “2(a+b)” will be expanded to “2*(a+b)”.

By default, implicit multiplication is enabled. It can be disabled with this configuration parameter.

Locale

The locale to use for number and date time formatting. By default, the system default locale is used.

Math Context

The math context is used throughout all operations and functions. The default has a precision of 68 and a rounding mode of HALF_EVEN.

See chapter Precision, Scale and Rounding for details.

Operator Dictionary

The operator dictionary is used to look up the operators that are used in an expression.

See chapter Operator Dictionary for details.

The default implementation is the MapBasedOperatorDictionary, which stores all variables in a case-insensitive Map.

Power Of Precedence

In mathematics, there is no general rule which precedence the power-of operator has. Consider the expression “-2^2”. The expression can have two different results, depending on the precedence:

  • If the precedence is lower than the unary minus, the result will be 4 (-2 * -2).
  • If the precedence is higher than the unary minus, the result will be -4 -(2 * 2).

By default, EvalEx uses a lower precedence. You can configure to use a higher precedence by specifying it here, or by using a predefined constant:

ExpressionConfiguration configuration=ExpressionConfiguration.builder()
        .powerOfPrecedence(OperatorIfc.OPERATOR_PRECEDENCE_POWER_HIGHER)
        .build();

// will now result in -4, instead of 4:
        Expression expression=new Expression("-2^2",configuration);

Single Quote String Literals

Specifies if the single quote character (‘) also can be used as a string literal delimiter, not only the double quote character (“) (default is false). If set to false, the parser will throw a ParseException, if a single quote is used.

Strip Trailing Zeros

If set to true (default), then the trailing decimal zeros in a number result will be stripped. E.g. a BigDecimal result of 9.0 will become 9 and -2.120000 will become -2.12.

Structures Allowed

Specifies if the structure separator (‘.’) operator is allowed (default is true). If set to false, the expression will throw a ParseException, if the a ‘.’ is encountered in the expression and also no operator or function is defined for this character.

Zone Id

The time zone id. By default, the system default zone ID is used.

ExpressionConfiguration configuration=ExpressionConfiguration.builder()
        .zoneId(ZoneId.of("Europe/Berlin"))
        .build();
  1. Since 3.3.0 


Copyright © 2012-2022 Udo Klimaschewski