Expression4j Expression4J

Sample > Custom operator

About
Documentation
Sample
    Simple expression
    Agregated expression
    Predefined function
    User define function
    Custom element
    Custom operator
    External configuration
Download

Create a custom operator like >, < or #oper# (why not)

A custom operator is constitued by :
  • the ExpressionElement class used to parse the custom element
  • the MathematicalElement class used to store the custom element value manipulated by the new operator (if necessary).
  • all OperatorImpl class used to compute customoperator with ExpressionElement.
Expression does not support space. If spaces are present in the expression, the parsin will generate an error.

Add the ">" operator who use Boolean to Expression4j

  1. Define the MathematicalElement used to store Boolean element.
    public class BooleanMathematicalElement implements MathematicalElement {

    //parameter value used to store element in a config file.
    public final static String BOOLEAN_VALUE = "BoleanValue";

    private Boolean booleanValue;

    public BooleanMathematicalElement(boolean value) {
    super();
    this.booleanValue = Boolean.valueOf(value);
    }

    public double getComplexValue() throws MathematicalException {
    //no used
    return 0;
    }

    public Properties getProperties() {
    //used to store objet in xml file
    //(used
    by configuration manager)
    Properties result = new Properties();
    result.put(BOOLEAN_VALUE,booleanValue.toString());
    return result;
    }

    public double getRealValue() {
    //not used
    return 0;
    }

    public int getType() {
    //create a new constant for Boolean type.
    return 6;
    }

    public Object getValue() {
    //get the value of the String as an Object.
    return booleanValue;
    }

    public void setProperties(Properties properties) {
    //used to store objet in xml file
    //(used
    by configuration manager)
    booleanValue = Boolean.valueOf(
    properties.getProperty(BOOLEAN_VALUE));
    }

    }

  2. Define OperatorImpl class who manage ">" operation.
    public class OperatorSupRealReal implements OperatorImpl {

    public OperatorSupRealReal() {
    super();
    }

    public MathematicalElement compute(
    MathematicalElement leftElement,
    MathematicalElement rightElement) throws EvalException {
    if (leftElement.getType() != 1) {
    throw new EvalException("Left element" +
    "
    is not a RealMathematicalElement");
    }

    if (rightElement.getType() != 1) {
    throw new EvalException("Right element" +
    "
    is not a RealMathematicalElement");
    }

    BooleanMathematicalElement result =
    new BooleanMathematicalElement(
    leftElement.getRealValue() >
    rightElement.getRealValue());
    return result;
    }

    public int getLeftOperandeType() {
    return 1;
    }

    public String getOperatorName() {
    //this name must be the same
    //as
    the name given in the operator.
    return "sup";
    }

    public int getRightOperandeType() {
    return 1;
    }

    public boolean isUnaryOperator() {
    return false;
    }

    }


  3. Sample class who use the new Operator.
    public class CustomOperator {


    public static void main(String[] args) {
    try {
    //create a specific expression model
    ExpressionModel customOperatorExpressionModel =
    ExpressionModelFactory
    .createExpressionModel("CustomOperatorExpressionModel");

    //create the new operator
    Operator supOperator =
    OperatorFactory.createOperator("sup",">",false);

    //add binary operator supported by the specific expression model
    customOperatorExpressionModel.addBinaryOperator(supOperator,1);

    //add standardexpression element
    customOperatorExpressionModel.addExpressionElement(
    new
    ComplexOrRealExpressionElement(),1);
    customOperatorExpressionModel.addExpressionElement(
    new FunctionExpressionElement(),2);
    customOperatorExpressionModel.addExpressionElement(
    new ConstantOrVariableExpressionElement(),3);
    customOperatorExpressionModel.addExpressionElement(
    new ParenthesisExpressionElement(),4);

    //create a specific operator manager
    OperatorManager supOperatorManager =
    OperatorManagerFactory
    .createOperatorManager("supOperatorManager");

    //add operator specific to previous model
    supOperatorManager.addOperatorImpl(new OperatorSupRealReal());

    //create an expression with the specific expression model
    Expression expression = ExpressionFactory.createExpression("f()=2>3",
    ExpressionFactory.getCatalog(),customOperatorExpressionModel);

    //compute expression
    MathematicalElement result =
    expression.evaluate(supOperatorManager,null);

    //display result
    System.out.println("result: " + result.getValue());

    //create an expression with the specific expression model
    expression = ExpressionFactory.createExpression("f()=3>2",
    ExpressionFactory.getCatalog(),customOperatorExpressionModel);

    //compute expression
    result = expression.evaluate(supOperatorManager,null);

    //display result
    System.out.println("result: " + result.getValue());

    //create an expression with the specific expression model
    ExpressionFactory.createExpression("f(x)=x",
    ExpressionFactory.getCatalog(),customOperatorExpressionModel);
    Expression expression2 = ExpressionFactory.createExpression("g(x)=2>f(x)",
    ExpressionFactory.getCatalog(),customOperatorExpressionModel);

    //compute expression
    Parameters parameters = ExpressionFactory.createParameters();

    for (int i=0; i<10; i++) {
    parameters.addParameter("x",NumberFactory.createReal(i));
    result = expression2.evaluate(supOperatorManager,parameters);

    //display result
    System.out.println("result: i=" +i + " : " + result.getValue());
    }

    //but be carefull, the customOperatorExpressionModel does not manage
    //standard operator, the folowing expression is not valid
    try {
    ExpressionFactory.createExpression("f(x)=x*2",
    ExpressionFactory.getCatalog(),customOperatorExpressionModel);
    }
    catch (Exception e) {
    System.out.println("Could not parse standard" +
    "
    operator with customOperatorExpressionModel: " + e);
    }
    }
    catch (Exception e) {
    System.out.println("Error: " + e);
    }

    }

    }
 
SourceForge.net Logo Support This Project