From d35da77b72af3a6ed2132e7d59712713c2ca4a3a Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Wed, 12 Jul 2017 10:23:56 -0400 Subject: [PATCH] SOLR-11046: Add residuals Stream Evaluator --- .../apache/solr/handler/StreamHandler.java | 2 +- .../solrj/io/eval/ResidualsEvaluator.java | 82 +++++++++++++++++++ .../solrj/io/stream/StreamExpressionTest.java | 24 ++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java index 3901c61bbb5..ea2a931f890 100644 --- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java @@ -217,8 +217,8 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware, .withFunctionName("scale", ScaleEvaluator.class) .withFunctionName("sequence", SequenceEvaluator.class) .withFunctionName("addAll", AddAllEvaluator.class) + .withFunctionName("residuals", ResidualsEvaluator.class) - // Boolean Stream Evaluators .withFunctionName("and", AndEvaluator.class) .withFunctionName("eor", ExclusiveOrEvaluator.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java new file mode 100644 index 00000000000..9a9c8699124 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.client.solrj.io.eval; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Locale; +import java.util.List; + +import org.apache.solr.client.solrj.io.Tuple; +import org.apache.solr.client.solrj.io.stream.expr.Explanation; +import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType; +import org.apache.solr.client.solrj.io.stream.expr.Expressible; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter; +import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; + +public class ResidualsEvaluator extends ComplexEvaluator implements Expressible { + + private static final long serialVersionUID = 1; + + public ResidualsEvaluator(StreamExpression expression, + StreamFactory factory) throws IOException { + super(expression, factory); + + if(3 != subEvaluators.size()){ + throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting three values (regression result and two numeric arrays) but found %d",expression,subEvaluators.size())); + } + } + + public List evaluate(Tuple tuple) throws IOException { + + StreamEvaluator r = subEvaluators.get(0); + StreamEvaluator a = subEvaluators.get(1); + StreamEvaluator b = subEvaluators.get(2); + + RegressionEvaluator.RegressionTuple rt= (RegressionEvaluator.RegressionTuple)r.evaluate(tuple); + List listA = (List)a.evaluate(tuple); + List listB = (List)b.evaluate(tuple); + List residuals = new ArrayList(); + + for(int i=0; i tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List out = (List)tuples.get(0).get("res"); + assertTrue(out.size() == 6); + assertTrue(out.get(0).intValue() == -1); + assertTrue(out.get(1).intValue() == -2); + assertTrue(out.get(2).intValue() == -3); + assertTrue(out.get(3).intValue() == -4); + assertTrue(out.get(4).intValue() == -5); + assertTrue(out.get(5).intValue() == -6); + } + + @Test public void testAnova() throws Exception { String cexpr = "anova(array(1,2,3,5,4,6), array(5,2,3,5,4,6), array(1,2,7,5,4,6))";