Variables are stored in a variable table. This table can be accessed during parsing or evaluation. During parsing, the parser looks up whether variables exists. And during evaluation the variables are looked up in the table in order to obtain their values.
Two options are available in working with variables:
After an expression has been parsed, variable values can be updated using either addVariable. This allows repeated evaluation of an expression with different variable values.
The value of a variable can be queried using getVariableValue(String) method. A further method getVariable(String) returns an object representing the variable.
Note that the VariableTable class stores objects representing variables rather than just
their values.
To enable parsing of undeclared variables, use setAllowUndeclared(true).
The default setting is false
(undeclared variables are not allowed).
If you do not know what variable names may occur in the expression before
parsing it, you can use setAllowUndeclared(true)
. With this option
enabled, it is not necessary to add variables to the parser before parsing
an expression. If a new variable is found while parsing, it is automatically
added to the symbol table. See Obtaining a list
of variables to read about how to access these variables.
While parsing an expression with undeclared variables allowed, a list of all the variables and constants that have been added to the parser, can be obtained with the getVariableTable method. With the returned variable table you can then check its contents to see which variables have been added while parsing.
Assignment allows the values of variables to be set by using the = operator in equations so it is possible to assign values with expressions like
x = 3,
then use the assigned values in an expression such as
y = x^2,
and y will have the value 9. To switch on assignment facilities, the setAllowAssignment(boolean) method of the main JEP object should be called. Calling setAllowUndeclared(true) is also required for JEP to parse the assignment equations.
Important: It is necessary to call the evaluate() method after parsing for the variable value to be assigned.
// standard initialisation
Jep j = new Jep();
j.setAllowUndeclared(true);
// switch assignment facilities on
j.setAllowAssignment(true);
// parse assignment equations
try {j.parse("x=3");} catch (ParseException e) {}
// evaluate it - no need to save the value returned
try {j.evaluate();} catch (EvaluationException e) {}
// parse a second equation
try {j.parse("y=2");} catch (ParseException e) {} try {j.evaluate();} catch (EvaluationException e) {}
// an equation involving the above variables try {j.parse("x^y");} catch (ParseException e) {}
try { Object val3 = j.evaluate(); System.out.println("Value is " + val3); } catch (EvaluationException e) {}
The Variable class implements the Observer/Observable pattern. This allows objects to be informed whenever variable values change. To register a class as an observer of a variable, use the addObserver(Observer) methods.