public final class RpEval extends AbstractEval
To use do
Jep j = ...; Node node = ...; RpEval rpe = new RpEval(j); RpCommandList list = rpe.compile(node); double val = rpe.evaluate(list); System.out.println(val); rpe.cleanUp();
Variable values in the evaluator are stored in a array. The array index of a variable can be found using
Variable v = j.getVar("x"); int ref = rpe.getVarRef(v);and the value of the variable set using
rpe.setVarValue(ref,0.1234);Variable values can also be set using the standard
Variable.setValue()
or (slower) Jep.setVarVal(name,vlaue)
methods.
Setting the value of a jep variable will automatically update the corresponding rpe value but there will be a performance hit.
Setting the value of the rpe variable does not change the corresponding jep value.
The compile methods converts the expression represented by node into a string of commands. For example the expression "1+2*3" will be converted into the sequence of commands
Constant no 1 (pushes constant onto stack) Constant no 2 Constant no 3 Multiply scalers (multiplies last two entries on stack) Add scalers (adds last two entries on stack)The evaluate method executes these methods sequentially using a stack and returns the last object on the stack.
A few cautionary notes:
duplicate()
method creates a copy of the RpEval object which can evaluate the same
commandList in separate threads.Implementation notes A lot of things have been done to make it as fast as possible:
customFunctionCommands, functionHash, jep, ls, opSet
Modifier and Type | Method and Description |
---|---|
void |
cleanUp()
Removes observers and other cleanup needed when evaluator no longer used.
|
RpCommandList |
compile(Node node)
Compile the expressions to produce a set of commands in reverse Polish notation.
|
RpEval |
duplicate()
Returns a copy of the RpEval object which is safe to use for evaluation in a new thread.
|
double |
evaluate(RpCommandList comList)
Evaluate the expression.
|
protected java.lang.Object |
getConstantValue(short ref) |
Variable |
getVariable(int ref)
Gets the Jep Variable for a give reference number
|
int |
getVarRef(java.lang.String varName)
Gets the reference number for a given variable
|
int |
getVarRef(Variable var)
Gets the reference number for a given variable
|
double |
getVarValue(int ref)
Gets the value of a variable
|
void |
setVarValue(int ref,
double val)
Sets the value of a variable
|
java.lang.String |
toString() |
java.lang.String |
toString(RpCommand com)
Enhanced RpCommand to String conversion.
|
void |
updateJepVariables()
Copies all the values from the Rpe variable to the corresponding Jep variables.
|
java.lang.Object |
visit(ASTConstant node,
java.lang.Object data) |
java.lang.Object |
visit(ASTFunNode node,
java.lang.Object data) |
java.lang.Object |
visit(ASTOpNode node,
java.lang.Object data) |
java.lang.Object |
visit(ASTVarNode node,
java.lang.Object data) |
getFunction, getLightWeightInstance, getUserFunction, init, staticGetFunction
public RpEval(Jep jep)
public final RpCommandList compile(Node node) throws JepException
JepException
public final java.lang.Object visit(ASTConstant node, java.lang.Object data) throws JepException
JepException
public final java.lang.Object visit(ASTVarNode node, java.lang.Object data) throws JepException
JepException
public final java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws JepException
JepException
public final java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws JepException
JepException
public final double evaluate(RpCommandList comList) throws EvaluationException
comList
- list of commandsEvaluationException
public void cleanUp()
public Variable getVariable(int ref)
ref
- reference number for the variablepublic int getVarRef(Variable var) throws JepException
var
- Jep VariableJepException
public int getVarRef(java.lang.String varName) throws JepException
varName
- name of variableJepException
public void setVarValue(int ref, double val)
ref
- reference number for the variableval
- the value to set the variablepublic double getVarValue(int ref)
ref
- reference number for the variablepublic RpEval duplicate()
RpEval rpe1 = new RpEval(jep);
RpCommandList com = rpe1.compile(node);
int varRef = rpe1.getVarRef(jep.getVar("x"));
RpEval rpe2 = rpe1.duplicate();
rpe2.setVarValue(ref,0.5);
double result = rpe2.evaluate(com);
public void updateJepVariables()
public java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.Object getConstantValue(short ref)
getConstantValue
in class AbstractEval
public java.lang.String toString(RpCommand com)
com
- an RpCommand to convertCopyright © 2018 Singular Systems http://www.singularsys.com/jep