Variable substitution inside a function

52 views
Skip to first unread message

Rabbit

unread,
Oct 27, 2011, 11:11:11 AM10/27/11
to Jep Java Users
Hi,

I'd like to do something like this :

( x + y ) / moy(x+y,'scope')

Where moy() will calculate the average for the x value in a database
for the range 'scope'.

So, I'd like to be able to set the value of x and y for the first part
of the expression then
their values inside the moy() function.

Is there a way to do that ? maybe with CallbackEvaluationI ?

Thank you for you help.

Antonin

Richard Morris

unread,
Oct 31, 2011, 11:10:05 AM10/31/11
to jep-...@googlegroups.com
Antonin,

I'm not quite sure if I understand what you want to do, but
CallbackEvaluationI could help.
With callback evaluation you can access the actual nodes in the parse
tree. If you had
fun(x,'scope')
then you could fine the name of the variable and the value of the
string with code like

class MyCallback implements CallbackEvaluationI {
@Override
public Object evaluate(Node node, Evaluator pv)
throws EvaluationException {

Node lhs = node.jjtGetChild(0);
Node rhs = node.jjtGetChild(1);
String lhsVarName = lhs.getName();
String rhsValue = (String) rhs.getValue();
return null;
}
}

Things get trickier if the fist argument to the function is x+y. You
may find it easier to have a three argument function
( x + y ) / moy(x,y,'scope')
otherwise you will need to traverse the trees to find the variables.

You may want to evaluate the first argument multiple times

class MyAverageCallback implements CallbackEvaluationI {

@Override
public Object evaluate(Node node, Evaluator pv)
throws EvaluationException {

Object oldXValue
Node lhs = node.jjtGetChild(0);
Node rhs = node.jjtGetChild(1);
String lhsVarName = lhs.getName();
String rhsValue = (String) rhs.getValue();
return null;
}
}

> --
> You received this message because you are subscribed to the Google Groups "Jep Java Users" group.
> To post to this group, send email to jep-...@googlegroups.com.
> To unsubscribe from this group, send email to jep-users+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/jep-users?hl=en.
>
>

Richard Morris

unread,
Oct 31, 2011, 11:24:29 AM10/31/11
to jep-...@googlegroups.com
Antonin,

Message got sent before I finished it.

You may want to evaluate the first argument multiple times the
following code sets up two a simple array of possible values
and uses the callback function pv.eval(Node node) to evaluate the
expression for each elements in the arrays.

class MyCallback implements CallbackEvaluationI {

Jep jep;
Double[] XValues = new Double[]{1.,2.,3.,4.,5.};
Double[] YValues = new Double[]{1.,2.,3.,4.,5.};

public MyCallback(Jep jep) {
this.jep = jep;
}

@Override
public Object evaluate(Node node, Evaluator pv)
throws EvaluationException {

double total=0.;
Node lhs = node.jjtGetChild(0);
try {

Object oldXvalue = jep.getVariableValue("x");
Object oldYvalue = jep.getVariableValue("y");
for(int i=0;i<5;++i) {
jep.addVariable("x", XValues[i]);
jep.addVariable("y", YValues[i]);

double res = (Double) pv.eval(lhs);
total = total + res;

}
total /= 5;
jep.addVariable("x", oldXvalue);
jep.addVariable("y", oldYvalue);
} catch (JepException e) {
e.printStackTrace();
}
return total;
}
}


Richard

Reply all
Reply to author
Forward
0 new messages