Search in sources :

Example 61 with StreamEvaluator

use of org.apache.solr.client.solrj.io.eval.StreamEvaluator in project lucene-solr by apache.

the class CompoundEvaluatorTest method compoundTest.

@Test
public void compoundTest() throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator("add(mult(a,b),if(gt(c,d),e,f),g)");
    Object result;
    values.clear();
    values.put("a", 10);
    values.put("b", -3);
    values.put("c", "foo");
    values.put("d", "bar");
    values.put("e", 9);
    values.put("f", 2);
    values.put("g", 5);
    result = evaluator.evaluate(new Tuple(values));
    Assert.assertTrue(result instanceof Long);
    Assert.assertEquals(-16L, result);
    values.clear();
    values.put("a", .1);
    values.put("b", -3);
    values.put("c", "foo");
    values.put("d", "bar");
    values.put("e", 9);
    values.put("f", 2);
    values.put("g", 5);
    result = evaluator.evaluate(new Tuple(values));
    Assert.assertTrue(result instanceof Double);
    Assert.assertEquals(13.7, result);
}
Also used : StreamEvaluator(org.apache.solr.client.solrj.io.eval.StreamEvaluator) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 62 with StreamEvaluator

use of org.apache.solr.client.solrj.io.eval.StreamEvaluator in project lucene-solr by apache.

the class ConversionEvaluatorsTest method testFunction.

public void testFunction(String expression, Number expected) throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator(expression);
    StreamContext streamContext = new StreamContext();
    evaluator.setStreamContext(streamContext);
    Object result = evaluator.evaluate(new Tuple(values));
    assertTrue(result instanceof Number);
    assertEquals(expected, result);
}
Also used : StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) StreamEvaluator(org.apache.solr.client.solrj.io.eval.StreamEvaluator) Tuple(org.apache.solr.client.solrj.io.Tuple)

Example 63 with StreamEvaluator

use of org.apache.solr.client.solrj.io.eval.StreamEvaluator in project lucene-solr by apache.

the class ConversionEvaluatorsTest method testInvalidExpression.

@Test
public void testInvalidExpression() throws Exception {
    StreamEvaluator evaluator;
    try {
        evaluator = factory.constructEvaluator("convert(inches)");
        StreamContext streamContext = new StreamContext();
        evaluator.setStreamContext(streamContext);
        assertTrue(false);
    } catch (IOException e) {
        assertTrue(e.getCause().getCause().getMessage().contains("Invalid expression convert(inches) - expecting 3 value but found 1"));
    }
    try {
        evaluator = factory.constructEvaluator("convert(inches, yards, 3)");
        StreamContext streamContext = new StreamContext();
        evaluator.setStreamContext(streamContext);
        Tuple tuple = new Tuple(new HashMap());
        evaluator.evaluate(tuple);
        assertTrue(false);
    } catch (IOException e) {
        assertTrue(e.getCause().getCause().getMessage().contains("No conversion available from INCHES to YARDS"));
    }
}
Also used : HashMap(java.util.HashMap) StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) IOException(java.io.IOException) StreamEvaluator(org.apache.solr.client.solrj.io.eval.StreamEvaluator) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 64 with StreamEvaluator

use of org.apache.solr.client.solrj.io.eval.StreamEvaluator in project lucene-solr by apache.

the class CosineEvaluatorTest method noValue.

@Test
public void noValue() throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator("cos(a)");
    values.clear();
    Object result = evaluator.evaluate(new Tuple(values));
    assertNull(result);
}
Also used : StreamEvaluator(org.apache.solr.client.solrj.io.eval.StreamEvaluator) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 65 with StreamEvaluator

use of org.apache.solr.client.solrj.io.eval.StreamEvaluator in project lucene-solr by apache.

the class CosineEvaluatorTest method test.

private void test(Double value) throws IOException {
    StreamEvaluator evaluator = factory.constructEvaluator("cos(a)");
    values.clear();
    values.put("a", value);
    Object result = evaluator.evaluate(new Tuple(values));
    if (null == value) {
        Assert.assertNull(result);
    } else {
        Assert.assertTrue(result instanceof Double);
        Assert.assertEquals(Math.cos(value), result);
    }
}
Also used : StreamEvaluator(org.apache.solr.client.solrj.io.eval.StreamEvaluator) Tuple(org.apache.solr.client.solrj.io.Tuple)

Aggregations

StreamEvaluator (org.apache.solr.client.solrj.io.eval.StreamEvaluator)137 Tuple (org.apache.solr.client.solrj.io.Tuple)135 Test (org.junit.Test)119 StreamContext (org.apache.solr.client.solrj.io.stream.StreamContext)6 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 UUID (java.util.UUID)1 StreamOperation (org.apache.solr.client.solrj.io.ops.StreamOperation)1