Craig,
The easiest way to achieve this is to sub class FastEvaluator to
create a method which records each node as its is evaluated
class PathEvaluator extends FastEvaluator {
@Override
protected Object nodeAccept(Node node) throws EvaluationException {
System.out.println("Visit "+node.toString());
return super.nodeAccept(node);
}
}
This overrides an undocumented method of that evaluator which is
called for each node evaluated. This method simply prints the nodes as
they are visited and then calls the superclass method to perform the
evaluation. You would use
@Test
public void testEvaluationPath() throws JepException {
String s = "if(Z==1," +
"if(Y==2,if(X==3,\"A\",\"B\"),if(W==4,\"C\",\"D\"))," +
"if(V==5,if(U==6,\"E\",\"F\"),if(T==7,\"G\",\"H\")))";
// Create the evaluator
Evaluator ev = new PathEvaluator();
// Set up jep using this evaluator
Jep jep = new Jep(ev);
jep.addVariable("Z", 1);
jep.addVariable("Y", 2);
jep.addVariable("X", 3);
jep.addVariable("W", 1);
jep.addVariable("U", 1);
jep.addVariable("V", 1);
jep.addVariable("T", 1);
// Parse and evaluate the equation
Node n = jep.parse(s);
Object res = jep.evaluate(n);
System.out.println("Result: "+res);
}
More sophisticated actions are possible in that method. You could use
the Node.setHook() methods to annotate nodes as they are visited.
Richard
On May 14, 7:45 pm, Craig Giordano <
craig.giord...@gmail.com> wrote:
> Is there a way I can get the path through the node tree when Jep does a
> call to evaluate?
>
> For example, if I have the following expression:
>
> @IF({Z}=1,@IF({Y}=2,@IF({X}=3,"A","B"),@IF({W}=4,"C","D")),@IF({V}=5,@IF({U }=6,"E","F"),@IF({T}=7,"G","H")))
>
> that evaluates to "H", can I get a reference to the node that is the answer
> or a path leading to that node? My use case is that I would like to mark
> up the expression after evaluation to show which path was followed to find
> the answer. For example, I would emit the following to show the path to
> the answer.
>
> <FONT
> COLOR="red">@IF({Z}=1.0</FONT>,@IF({Y}=2.0,@IF({X}=3.0,"A","B"),@IF({W}=4.0 ,"C","D")),<FONT