eva/3 Application BuilderDeveloping Additional eva/3 Components Expression API  

Execute expressions

The execution of eva/3 Application Builder expressions are clsssified in two parts. The ExpressionParser analyzes the expression and creates an execution plan. The ExpressionInterpreter allows to execute the execution plan that was previously created by the ExpressionParser. This allows the execution of the same expression statement several times without creating a new execution plan. If an expression is used several times, the ExpressionInterpreter instance can be cached and reused.

	
import com.odc.eva3.rt.se.expression.ExpressionInterpreter;
import com.odc.eva3.rt.se.expression.ExpressionParser;

public class Test {

    public static void main(String[] args) {
        try {
            //create a new ExpressionParser instance with the desired expression statement
            ExpressionParser parser = new ExpressionParser("=5+6");

            //create a new ExpressionInterpreter instance. With the creation of the
            //new ExpressionInterpreter, the ExpressionParser starts parsing
            //the expression statement.
            ExpressionInterpreter interpreter = new ExpressionInterpreter(parser);

            //executed the expression statement. Setting the first argument to 'true' allows
            //preserving the parsed expression for another usage. Otherwise, the expression
            //won't be calculated again and returns the same result any time without making a
            //difference if values of reffering beans has changed.
            Object result = interpreter.compute(true);
        } catch (Exception e) {
            System.out.println("Expression has failed");
            e.printStackTrace();
        }
    }
}

The simplest and komfortablest way to execute an expression is to use the method eval(String expression, boolean cache). This method provides a prefabricated caching which can be activated with the cache parameter. A execution plan which was already created are used again automatically. Hint: The cached execution plan are used if the expression statement are exactly written with the same notation.

import com.odc.eva3.rt.se.expression.VBUtilFunctions;

public class Test {

    public static void main(String[] args) {
        try {
            //Execute the desired expression statement and
            //automatically cache the parsed expression statement.
            //The already parsed expression statement will be used
            //again, if exactly the same expression statement is
            //requested next time.
            Object result = VBUtilFunctions.eval("=5+7", true);
        } catch (Exception e) {
            System.out.println("Expression has failed");
            e.printStackTrace();
        }
    }
}