use of org.apache.lucene.expressions.Expression in project elasticsearch by elastic.
the class MoreExpressionTests method testExecutableScripts.
// series of unit test for using expressions as executable scripts
public void testExecutableScripts() throws Exception {
assumeTrue("test creates classes directly, cannot run with security manager", System.getSecurityManager() == null);
Map<String, Object> vars = new HashMap<>();
vars.put("a", 2.5);
vars.put("b", 3);
vars.put("xyz", -1);
Expression expr = JavascriptCompiler.compile("a+b+xyz");
CompiledScript compiledScript = new CompiledScript(ScriptType.INLINE, "", "expression", expr);
ExpressionExecutableScript ees = new ExpressionExecutableScript(compiledScript, vars);
assertEquals((Double) ees.run(), 4.5, 0.001);
ees.setNextVar("b", -2.5);
assertEquals((Double) ees.run(), -1, 0.001);
ees.setNextVar("a", -2.5);
ees.setNextVar("b", -2.5);
ees.setNextVar("xyz", -2.5);
assertEquals((Double) ees.run(), -7.5, 0.001);
String message;
try {
vars = new HashMap<>();
vars.put("a", 1);
ees = new ExpressionExecutableScript(compiledScript, vars);
ees.run();
fail("An incorrect number of variables were allowed to be used in an expression.");
} catch (GeneralScriptException se) {
message = se.getMessage();
assertThat(message + " should have contained number of variables", message.contains("number of variables"), equalTo(true));
}
try {
vars = new HashMap<>();
vars.put("a", 1);
vars.put("b", 3);
vars.put("c", -1);
ees = new ExpressionExecutableScript(compiledScript, vars);
ees.run();
fail("A variable was allowed to be set that does not exist in the expression.");
} catch (GeneralScriptException se) {
message = se.getMessage();
assertThat(message + " should have contained does not exist in", message.contains("does not exist in"), equalTo(true));
}
try {
vars = new HashMap<>();
vars.put("a", 1);
vars.put("b", 3);
vars.put("xyz", "hello");
ees = new ExpressionExecutableScript(compiledScript, vars);
ees.run();
fail("A non-number was allowed to be use in the expression.");
} catch (GeneralScriptException se) {
message = se.getMessage();
assertThat(message + " should have contained process numbers", message.contains("process numbers"), equalTo(true));
}
}
use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.
the class TestCustomFunctions method testDefaultList.
/** using the default map explicitly */
public void testDefaultList() throws Exception {
Map<String, Method> functions = JavascriptCompiler.DEFAULT_FUNCTIONS;
Expression expr = JavascriptCompiler.compile("sqrt(20)", functions, getClass().getClassLoader());
assertEquals(Math.sqrt(20), expr.evaluate(null), DELTA);
}
use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.
the class TestJavascriptFunction method assertEvaluatesTo.
private void assertEvaluatesTo(String expression, double expected) throws Exception {
Expression evaluator = JavascriptCompiler.compile(expression);
double actual = evaluator.evaluate(null);
assertEquals(expected, actual, DELTA);
}
use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.
the class TestCustomFunctions method testOneArgMethod.
/** tests a method with one arguments */
public void testOneArgMethod() throws Exception {
Map<String, Method> functions = new HashMap<>();
functions.put("foo", getClass().getMethod("oneArgMethod", double.class));
Expression expr = JavascriptCompiler.compile("foo(3)", functions, getClass().getClassLoader());
assertEquals(6, expr.evaluate(null), DELTA);
}
use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.
the class TestCustomFunctions method testThreeArgMethod.
/** tests a method with three arguments */
public void testThreeArgMethod() throws Exception {
Map<String, Method> functions = new HashMap<>();
functions.put("foo", getClass().getMethod("threeArgMethod", double.class, double.class, double.class));
Expression expr = JavascriptCompiler.compile("foo(3, 4, 5)", functions, getClass().getClassLoader());
assertEquals(12, expr.evaluate(null), DELTA);
}
Aggregations