Home

Tutorial

Table of contents:

Customizing built-in evaluator

Extending an evaluator

Javaluator comes with a basic set of common operators, functions, constants and brackets. If you need more, you can add your own.

In this example, we will add a new function: sqrt (square root)

First we have to create the Function instance. This instance describes the name, and argument counts of the function.

// The function has one argument and its name is "sqrt"
Function SQRT = new Function("sqrt", 1);

When creating the evaluator, we'll have to inform it that there is an extra function.
This is done by passing a parameters argument to the DoubleEvaluator constructor.

// Gets the default DoubleEvaluator's parameters
Parameters params = DoubleEvaluator.getDefaultParameters();
// add the new sqrt function to these parameters
params.add(SQRT);

We now have to extend the DoubleEvaluator class in order to implement the new function.
This is done by overriding its evaluate(Function function, Iterator arguments, Object evaluationContext) method.

// Create a new subclass of DoubleEvaluator that support the new function
DoubleEvaluator evaluator = new DoubleEvaluator(params) {
  @Override
  protected Double evaluate(Function function, Iterator arguments, Object evaluationContext) {
    if (function == SQRT) {
      // Implements the new function
      return Math.sqrt(arguments.next());
    } else {
      // If it's another function, pass it to DoubleEvaluator
      return super.evaluate(function, arguments, evaluationContext);
    }
  }
};

The same kind of mechanisms apply to adding a new operator or a new constant.

You can add support for new kind of brackets in the expression by adding them to the parameters.

// Add square brackets [] to the list of supported brackets in the expressions
params.addExpressionBracket(BracketPair.BRACKETS);



Here is a complete working sample code:

package com.fathzer.soft.javaluator.examples;
 
import java.util.Iterator;
 
import com.fathzer.soft.javaluator.DoubleEvaluator;
import com.fathzer.soft.javaluator.Function;
import com.fathzer.soft.javaluator.Parameters;
 
/** A subclass of DoubleEvaluator that supports SQRT function.
 */
public class ExtendedDoubleEvaluator extends DoubleEvaluator {
  /** Defines the new function (square root).*/
  private static final Function SQRT = new Function("sqrt", 1);
  private static final Parameters PARAMS;
 
  static {
    // Gets the default DoubleEvaluator's parameters
    PARAMS = DoubleEvaluator.getDefaultParameters();
    // add the new sqrt function to these parameters
    PARAMS.add(SQRT);
  }
 
  public ExtendedDoubleEvaluator() {
    super(PARAMS);
  }
 
  @Override
  protected Double evaluate(Function function, Iterator<Double> arguments, Object evaluationContext) {
    if (function == SQRT) {
      // Implements the new function
      return Math.sqrt(arguments.next());
    } else {
      // If it's another function, pass it to DoubleEvaluator
      return super.evaluate(function, arguments, evaluationContext);
    }
  }
 
  public static void main(String[] args) {
    // Test that all this stuff is ok
    String expression = "sqrt(abs(-2))^2";
    System.out.println (expression+" = "+new ExtendedDoubleEvaluator().evaluate(expression));
  }
 
}
 

The output is sqrt(abs(-2))^2 = 2.0000000000000004

Advertising

Back to top