Data Access

By default, EvalEx has its own storage for variable values, the MapBasedDataAccessor. It stores and retrieves the variables in a case-insensitive TreeMap. A separate instance of the MapBasedDataAccessor will be created through the default configured supplier for each new Expression, separating the storage for each expression instance.

You can easily define your own data access interface, by defining a class that implements the DataAccessorInterface:

public interface DataAccessorIfc {

  /**
   * Retrieves a data value.
   *
   * @param variable The variable name, e.g. a variable or constant name.
   * @return The data value, or <code>null</code> if not found.
   */
  EvaluationValue getData(String variable);

  /**
   * Sets a data value.
   *
   * @param variable The variable name, e.g. a variable or constant name.
   * @param value The value to set.
   */
  void setData(String variable, EvaluationValue value);
}

Only two methods need to be implemented:

  • getData(String variable) - should return a value for a variable name.
  • setData(String variable, EvaluationValue value) - set a variable value.

The setData() method is only called when a variable is added to the expression, e.g. using the with() method. You may leave the implementation empty, if you do want to support this.

NOTE: The variable names that are passed to the methods are case-sensitive, just like they were entered in the expression.

The custom data accessor can then be specified in the expression configuration:

ExpressionConfiguration configuration = ExpressionConfiguration.builder()
    .dataAccessorSupplier(MyCustomDataAccessor::new)
    .build();

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

The example here creates a new instance of the MyCustomDataAccessor for each new instance of an expression. But you could also share an instance for all expressions:

final MyCustomDataAccessor customAccessor = new MyCustomDataAccessor();
ExpressionConfiguration configuration = ExpressionConfiguration.builder()
    .dataAccessorSupplier(() -> customAccessor)
    .build();

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

Copyright © 2012-2022 Udo Klimaschewski