Search in sources :

Example 71 with StreamEvaluator

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

the class AbsoluteValueEvaluatorTest method absoluteValueFromContext.

@Test
public void absoluteValueFromContext() throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator("abs(a)");
    StreamContext context = new StreamContext();
    evaluator.setStreamContext(context);
    Object result;
    context.getLets().put("a", 1);
    result = evaluator.evaluate(new Tuple());
    Assert.assertTrue(result instanceof Long);
    Assert.assertEquals(1L, result);
    context.getLets().put("a", 1.1);
    result = evaluator.evaluate(new Tuple());
    Assert.assertTrue(result instanceof Double);
    Assert.assertEquals(1.1D, result);
    context.getLets().put("a", -1.1);
    result = evaluator.evaluate(new Tuple());
    Assert.assertTrue(result instanceof Double);
    Assert.assertEquals(1.1D, result);
    context.getLets().put("a", factory.constructEvaluator("add(4,-6,34,-56)"));
    result = evaluator.evaluate(new Tuple());
    Assert.assertTrue(result instanceof Long);
    Assert.assertEquals(24L, 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) Test(org.junit.Test)

Example 72 with StreamEvaluator

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

the class AddEvaluatorTest method addTwoFieldsWithNull.

@Test
public void addTwoFieldsWithNull() throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator("add(a,b)");
    Object result;
    values.clear();
    values.put("a", 1);
    values.put("b", null);
    result = evaluator.evaluate(new Tuple(values));
    Assert.assertNull(result);
    values.clear();
    values.put("a", 1.1);
    values.put("b", null);
    result = evaluator.evaluate(new Tuple(values));
    Assert.assertNull(result);
    values.clear();
    values.put("a", null);
    values.put("b", 1.1);
    result = evaluator.evaluate(new Tuple(values));
    Assert.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 73 with StreamEvaluator

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

the class TangentEvaluatorTest method noValue.

@Test
public void noValue() throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator("tan(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 74 with StreamEvaluator

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

the class TemporalEvaluatorsTest method testInvalidValues.

@Test
public void testInvalidValues() throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator("year(a)");
    try {
        values.clear();
        values.put("a", 12);
        StreamContext streamContext = new StreamContext();
        evaluator.setStreamContext(streamContext);
        Object result = evaluator.evaluate(new Tuple(values));
        assertTrue(false);
    } catch (IOException e) {
        assertEquals("Invalid parameter 12 - The parameter must be a string formatted ISO_INSTANT or of type Long,Instant,Date,LocalDateTime or TemporalAccessor.", e.getMessage());
    }
    try {
        values.clear();
        values.put("a", "1995-12-31");
        StreamContext streamContext = new StreamContext();
        evaluator.setStreamContext(streamContext);
        Object result = evaluator.evaluate(new Tuple(values));
        assertTrue(false);
    } catch (IOException e) {
        assertEquals("Invalid parameter 1995-12-31 - The String must be formatted in the ISO_INSTANT date format.", e.getMessage());
    }
    try {
        values.clear();
        values.put("a", "");
        StreamContext streamContext = new StreamContext();
        evaluator.setStreamContext(streamContext);
        Object result = evaluator.evaluate(new Tuple(values));
        assertTrue(false);
    } catch (IOException e) {
        assertEquals("Invalid parameter  - The parameter must be a string formatted ISO_INSTANT or of type Long,Instant,Date,LocalDateTime or TemporalAccessor.", e.getMessage());
    }
    values.clear();
    values.put("a", null);
    assertNull(evaluator.evaluate(new Tuple(values)));
}
Also used : 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 75 with StreamEvaluator

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

the class TemporalEvaluatorsTest method testFunction.

public void testFunction(String expression, Object value, Number expected) throws Exception {
    StreamEvaluator evaluator = factory.constructEvaluator(expression);
    StreamContext streamContext = new StreamContext();
    evaluator.setStreamContext(streamContext);
    values.clear();
    values.put("a", value);
    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)

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