Search in sources :

Example 1 with Expression

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));
    }
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) GeneralScriptException(org.elasticsearch.script.GeneralScriptException)

Example 2 with Expression

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);
}
Also used : Expression(org.apache.lucene.expressions.Expression) Method(java.lang.reflect.Method)

Example 3 with Expression

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);
}
Also used : Expression(org.apache.lucene.expressions.Expression)

Example 4 with Expression

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);
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) Method(java.lang.reflect.Method)

Example 5 with Expression

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);
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) Method(java.lang.reflect.Method)

Aggregations

Expression (org.apache.lucene.expressions.Expression)18 Method (java.lang.reflect.Method)8 HashMap (java.util.HashMap)8 ParseException (java.text.ParseException)4 SimpleBindings (org.apache.lucene.expressions.SimpleBindings)4 SortField (org.apache.lucene.search.SortField)4 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 AccessControlContext (java.security.AccessControlContext)1 VariableContext (org.apache.lucene.expressions.js.VariableContext)1 FacetResult (org.apache.lucene.facet.FacetResult)1 Facets (org.apache.lucene.facet.Facets)1 FacetsCollector (org.apache.lucene.facet.FacetsCollector)1 TaxonomyFacetSumValueSource (org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource)1 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)1 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1