It is possible to use variables in expressions. For example, the following expression: "a + 2", takes the value of the variable a and adds 2 to this variable.
ExpressionType.TYPE_BOOL
: boolean variablesExpressionType.TYPE_INTEGER
: int variablesExpressionType.TYPE_FLOAT
: float variablesExpressionType.TYPE_NUMERIC
: numeric variables, which can be either int or float depending on the result of expressionsExpressionType.TYPE_STRING
: String variablesExpressionType.TYPE_DYNAMIC
: Dynamic types are types which can be used for any value type. For example, if the result of an expression is of the dynamic type, this expression result can be integer, float, boolean or StringExpressionType.TYPE_SCALAR
: Scalar typeExpressionType.TYPE_ARRAY
: Array typeEquation equation = ExpressionJ.parse("a + 1");the equation contains one variable of name a. This means that the following code will work:
Equation equation = ExpressionJ.parse("a + 1"); Map<String, Variable> vars = equation.getVariables(); // vars contains one variable with the name "a" vars.get("a").setValue(2); Object result = equation.eval(); // the result is 3
List<Variable> varList = new ArrayList(); Variable var = new Variable("a", Expression.TYPE_INTEGER); varList.add(var); Equation equation = ExpressionJ.parse("a + 1", varList); var.setValue(2); Object result = equation.eval(); // the result is 3
a
is a local variable: Equation equation = ExpressionJ.parse("int a = 2; return a + 1;"); Equation equation2 = ExpressionJ.parse("var a = 2; return a + 2;");whereas in the following expression a is a global variable[2] :
Equation equation = ExpressionJ.parse("a = 2; return a + 1;");
const
keyword allows to declare local constants which can not be modified after their creation. For example: Equation equation = ExpressionJ.parse("const a = 2; return a + 1"); Object result = equation.eval(); // the result is 3A local constant behave just like a variable, except that it can not be modified after it had been defined. For example, the following code is not allowed:
const a = 2; a = a + 2; return a;
Copyright 2018 Herve Girod. All Rights Reserved