To parse an expression, you need to use one of the parsexxx
static methods of the ExpressionJ class[1]
. The methods return an Equation which can evaluated.
Expressions can be parsed from a String, an URL, or a File.
List<Variable> vars = new ArrayList(); Variable var = new Variable("a", Expression.TYPE_INTEGER); vars.add(var); Equation equation = ExpressionJ.parse("return a + 1;", vars); var.setValue(2); Object result = equation.eval(); // the result is 3
Equation equation = ExpressionJ.parse("a = 2; return a + 1;");Then the Expression.getVariables() will return a Map with one variable for the name
a
.ExpressionJ.acceptUndefinedVariables(false); Equation equation = ExpressionJ.parse("a = 2; return a + 1;");
parsexxx
static methods of the EquationParser classCopyright 2018 Herve Girod. All Rights Reserved