I am evaluating an inline Javascript Expression using Java-8’s ScriptEngine.
Example Expression
(_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100)
Where profile is a global scope bind Java Class object and received() is its local function. I Successfully evaluated this expression using scriptingEngine.eval(script) and it is calling profile.received('daily', 'avg', false) I mean to say functionsManager.received('daily', 'avg', false) as well.
Question: But Scripting engine failed to evaluate Javascript expression if it contains Java functions more than one. In that case, only first Java function get called. So how to resolve this issue?
eg.
(_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) && (profile.sent('monthly', 'count', false) < 5)
Script Engine:
private static ScriptEngine scriptingEngine;
Inside Constructor of main class
scriptingEngine = new ScriptEngineManager().getEngineByName("JavaScript");
scriptingEngine.getBindings(ScriptContext.GLOBAL_SCOPE).put(PROFILING_FUNCTIONS_PREFIX, functionsManager);
Evaluating Expression
Bindings bindings = new SimpleBindings(customMapOfVaraibles);
CompiledScript compiled = ((Compilable) scriptingEngine).compile(" (_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) && (profile.sent('monthly', 'count', false) < 5)");
compiled .eval(bindings)
functionsManager‘s methods look like this
public BigDecimal received(String intervalType, String statisticsType, boolean considerTranxType)
throws InvalidRequirementsException {
//Do calculations
return calculatedValue;
}
public BigDecimal sent(String intervalType, String statisticsType, boolean considerTranxType)
throws InvalidRequirementsException {
//Do calculations
return calculatedValue;
}