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.
The Scripting engine failed to evaluate JavaScript expression if it contains more than one Java function. In that case, only the first Java function gest called. How to resolve this issue?
For example
(_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;
}