Package org.apache.lucene.expressions

Examples of org.apache.lucene.expressions.Expression


   * bindings.
   */
  public DocumentExpressionDictionary(IndexReader reader, String field,
      String weightExpression, Set<SortField> sortFields, String payload) {
    super(reader, field, null, payload);
    Expression expression = null;
    try {
      expression = JavascriptCompiler.compile(weightExpression);
    } catch (ParseException e) {
      throw new RuntimeException();
    }
    SimpleBindings bindings = new SimpleBindings();
    for (SortField sortField: sortFields) {
      bindings.add(sortField);
    }
    weightsValueSource = expression.getValueSource(bindings);
   
  }
View Full Code Here


public class TestJavascriptFunction extends LuceneTestCase {
  private static double DELTA = 0.0000001;
 
  private void assertEvaluatesTo(String expression, double expected) throws Exception {
    Expression evaluator = JavascriptCompiler.compile(expression);
    double actual = evaluator.evaluate(0, null);
    assertEquals(expected, actual, DELTA);
  }
View Full Code Here

  }
 
  /** 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(0, null), DELTA);
  }
View Full Code Here

 
  /** tests a method with no arguments */
  public void testNoArgMethod() throws Exception {
    Map<String,Method> functions = new HashMap<String,Method>();
    functions.put("foo", getClass().getMethod("zeroArgMethod"));
    Expression expr = JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
    assertEquals(5, expr.evaluate(0, null), DELTA);
  }
View Full Code Here

 
  /** tests a method with one arguments */
  public void testOneArgMethod() throws Exception {
    Map<String,Method> functions = new HashMap<String,Method>();
    functions.put("foo", getClass().getMethod("oneArgMethod", double.class));
    Expression expr = JavascriptCompiler.compile("foo(3)", functions, getClass().getClassLoader());
    assertEquals(6, expr.evaluate(0, null), DELTA);
  }
View Full Code Here

 
  /** tests a method with three arguments */
  public void testThreeArgMethod() throws Exception {
    Map<String,Method> functions = new HashMap<String,Method>();
    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(0, null), DELTA);
  }
View Full Code Here

  /** tests a map with 2 functions */
  public void testTwoMethods() throws Exception {
    Map<String,Method> functions = new HashMap<String,Method>();
    functions.put("foo", getClass().getMethod("zeroArgMethod"));
    functions.put("bar", getClass().getMethod("oneArgMethod", double.class));
    Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions, getClass().getClassLoader());
    assertEquals(11, expr.evaluate(0, null), DELTA);
  }
View Full Code Here

    Map<String,Method> functions = Collections.singletonMap("bar", barMethod);
    assertNotSame(thisLoader, fooClass.getClassLoader());
    assertNotSame(thisLoader, barMethod.getDeclaringClass().getClassLoader());
   
    // this should pass:
    Expression expr = JavascriptCompiler.compile("bar()", functions, childLoader);
    assertEquals(2.0, expr.evaluate(0, null), DELTA);
   
    // use our classloader, not the foreign one, which should fail!
    try {
      JavascriptCompiler.compile("bar()", functions, thisLoader);
      fail();
    } catch (IllegalArgumentException e) {
      assertTrue(e.getMessage().contains("is not declared by a class which is accessible by the given parent ClassLoader"));
    }
   
    // mix foreign and default functions
    Map<String,Method> mixedFunctions = new HashMap<String,Method>(JavascriptCompiler.DEFAULT_FUNCTIONS);
    mixedFunctions.putAll(functions);
    expr = JavascriptCompiler.compile("bar()", mixedFunctions, childLoader);
    assertEquals(2.0, expr.evaluate(0, null), DELTA);
    expr = JavascriptCompiler.compile("sqrt(20)", mixedFunctions, childLoader);
    assertEquals(Math.sqrt(20), expr.evaluate(0, null), DELTA);
   
    // use our classloader, not the foreign one, which should fail!
    try {
      JavascriptCompiler.compile("bar()", mixedFunctions, thisLoader);
      fail();
View Full Code Here

  /** the method throws an exception. We should check the stack trace that it contains the source code of the expression as file name. */
  public void testThrowingException() throws Exception {
    Map<String,Method> functions = new HashMap<String,Method>();
    functions.put("foo", StaticThrowingException.class.getMethod("method"));
    String source = "3 * foo() / 5";
    Expression expr = JavascriptCompiler.compile(source, functions, getClass().getClassLoader());
    try {
      expr.evaluate(0, null);
      fail();
    } catch (ArithmeticException e) {
      assertEquals(MESSAGE, e.getMessage());
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
View Full Code Here

  /** test that namespaces work with custom expressions. */
  public void testNamespaces() throws Exception {
    Map<String, Method> functions = new HashMap<String, Method>();
    functions.put("foo.bar", getClass().getMethod("zeroArgMethod"));
    String source = "foo.bar()";
    Expression expr = JavascriptCompiler.compile(source, functions, getClass().getClassLoader());
    assertEquals(5, expr.evaluate(0, null), DELTA);
  }
View Full Code Here

TOP

Related Classes of org.apache.lucene.expressions.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.