You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2017/07/13 17:14:49 UTC

lucene-solr:master: SOLR-10802: Add plot Stream Expression

Repository: lucene-solr
Updated Branches:
  refs/heads/master 1661a37a4 -> c40f6d7d7


SOLR-10802: Add plot Stream Expression


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/c40f6d7d
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/c40f6d7d
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/c40f6d7d

Branch: refs/heads/master
Commit: c40f6d7d7994b5b1cc63d60bbe897f8e4eba2972
Parents: 1661a37
Author: Joel Bernstein <jb...@apache.org>
Authored: Thu Jul 13 13:04:48 2017 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Thu Jul 13 13:05:47 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |   1 +
 .../solr/client/solrj/io/stream/PlotStream.java | 211 +++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   |  34 ++-
 3 files changed, 244 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c40f6d7d/solr/core/src/java/org/apache/solr/handler/StreamHandler.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 ea2a931..d52972d 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -218,6 +218,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("sequence", SequenceEvaluator.class)
         .withFunctionName("addAll", AddAllEvaluator.class)
         .withFunctionName("residuals", ResidualsEvaluator.class)
+        .withFunctionName("plot", PlotStream.class)
 
         // Boolean Stream Evaluators
         .withFunctionName("and", AndEvaluator.class)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c40f6d7d/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/PlotStream.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/PlotStream.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/PlotStream.java
new file mode 100644
index 0000000..b652de9
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/PlotStream.java
@@ -0,0 +1,211 @@
+/*
+ * 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.stream;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.SingleValueComparator;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.eval.StreamEvaluator;
+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.StreamExplanation;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class PlotStream extends TupleStream implements Expressible {
+
+  private static final long serialVersionUID = 1;
+  private StreamContext streamContext;
+
+  private Map<String,String> stringParams = new HashMap<>();
+  private Map<String,StreamEvaluator> evaluatorParams = new HashMap<>();
+  private Map<String,TupleStream> streamParams = new HashMap<>();
+  private List<String> fieldNames = new ArrayList();
+  private Map<String, String> fieldLabels = new HashMap();
+
+  private boolean finished;
+
+  public PlotStream(StreamExpression expression, StreamFactory factory) throws IOException {
+
+    fieldNames.add("plot");
+    fieldNames.add("data");
+    fieldLabels.put("plot","plot");
+    fieldLabels.put("data", "data");
+
+    List<StreamExpressionNamedParameter> namedParams = factory.getNamedOperands(expression);
+    //Get all the named params
+    for(StreamExpressionNamedParameter np : namedParams) {
+      String name = np.getName();
+      //fieldNames.add(name);
+      //fieldLabels.put(name, name);
+      StreamExpressionParameter param = np.getParameter();
+
+      // we're going to split these up here so we only make the choice once
+      // order of these in read() doesn't matter
+      if(param instanceof StreamExpressionValue) {
+        stringParams.put(name, ((StreamExpressionValue)param).getValue());
+      } else if (factory.isEvaluator((StreamExpression) param)) {
+        StreamEvaluator evaluator = factory.constructEvaluator((StreamExpression) param);
+        evaluatorParams.put(name, evaluator);
+      } else if(factory.isStream((StreamExpression)param)) {
+        TupleStream tupleStream = factory.constructStream((StreamExpression) param);
+        streamParams.put(name, tupleStream);
+      }
+      else{
+        throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - only string, evaluator, or stream named parameters are supported, but param %d is none of those",expression, name));
+      }
+    }
+  }
+
+  @Override
+  public StreamExpression toExpression(StreamFactory factory) throws IOException{
+    return toExpression(factory, true);
+  }
+
+  private StreamExpression toExpression(StreamFactory factory, boolean includeStreams) throws IOException {
+    // function name
+    StreamExpression expression = new StreamExpression(factory.getFunctionName(this.getClass()));
+
+    // add string based params
+    for(Entry<String,String> param : stringParams.entrySet()){
+      expression.addParameter(new StreamExpressionNamedParameter(param.getKey(), param.getValue()));
+    }
+
+    // add evaluator based params
+    for(Entry<String,StreamEvaluator> param : evaluatorParams.entrySet()){
+      expression.addParameter(new StreamExpressionNamedParameter(param.getKey(), param.getValue().toExpression(factory)));
+    }
+
+    // add stream based params
+    for(Entry<String,TupleStream> param : streamParams.entrySet()){
+      if(includeStreams){
+        expression.addParameter(new StreamExpressionNamedParameter(param.getKey(), ((Expressible)param.getValue()).toExpression(factory)));
+      }
+      else{
+        expression.addParameter(new StreamExpressionNamedParameter(param.getKey(), "<stream>"));
+      }
+    }
+
+    return expression;
+  }
+
+  @Override
+  public Explanation toExplanation(StreamFactory factory) throws IOException {
+
+    StreamExplanation explanation = new StreamExplanation(getStreamNodeId().toString());
+    explanation.setFunctionName(factory.getFunctionName(this.getClass()));
+    explanation.setImplementingClass(this.getClass().getName());
+    explanation.setExpressionType(ExpressionType.STREAM_DECORATOR);
+    explanation.setExpression(toExpression(factory, false).toString());
+
+    return explanation;
+  }
+
+  public void setStreamContext(StreamContext context) {
+    this.streamContext = context;
+
+    // also set in evalators and streams
+    for(StreamEvaluator evaluator : evaluatorParams.values()){
+      evaluator.setStreamContext(context);
+    }
+
+    for(TupleStream stream : streamParams.values()){
+      stream.setStreamContext(context);
+    }
+  }
+
+  public List<TupleStream> children() {
+    List<TupleStream> l =  new ArrayList<TupleStream>();
+    return l;
+  }
+
+  public Tuple read() throws IOException {
+
+    if(finished) {
+      Map<String,Object> m = new HashMap<>();
+      m.put("EOF", true);
+      return new Tuple(m);
+    } else {
+      finished = true;
+      Map<String, Object> values = new HashMap<>();
+
+      String plot= stringParams.get("type");
+      StreamEvaluator xvalues = evaluatorParams.get("x");
+      StreamEvaluator yvalues = evaluatorParams.get("y");
+
+      List<Number> y = (List<Number>)yvalues.evaluateOverContext();
+      List<Number> x = null;
+
+      if(xvalues == null) {
+        //x is null so add a sequence
+        x = new ArrayList();
+        for(int i=0; i<y.size(); i++) {
+          x.add(i+1);
+        }
+      } else {
+        x = (List<Number>) xvalues.evaluateOverContext();
+      }
+
+      List<List<Number>> xy = new ArrayList();
+      for(int i=0; i<x.size(); i++) {
+        List<Number> pair = new ArrayList();
+        pair.add(x.get(i));
+        pair.add(y.get(i));
+        xy.add(pair);
+      }
+
+      values.put("plot", plot);
+      values.put("data", xy);
+
+      Tuple tup = new Tuple(values);
+      tup.fieldLabels = fieldLabels;
+      tup.fieldNames = fieldNames;
+      return tup;
+    }
+  }
+
+  public void close() throws IOException {
+    // Nothing to do here
+  }
+
+  public void open() throws IOException {
+    // nothing to do here
+  }
+
+  /** Return the stream sort - ie, the order in which records are returned */
+  public StreamComparator getStreamSort(){
+    return new SingleValueComparator();
+  }
+
+  public int getCost() {
+    return 0;
+  }
+
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c40f6d7d/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
index ff105f6..d749ede 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
@@ -6058,10 +6058,40 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     List<Tuple> tuples = getTuples(solrStream);
     assertTrue(tuples.size() == 1);
     Map out = (Map)tuples.get(0).get("return-value");
-    assertEquals((double)out.get("p-value"), 0.788298D, .0001);
-    assertEquals((double)out.get("f-ratio"), 0.24169D, .0001);
+    assertEquals((double) out.get("p-value"), 0.788298D, .0001);
+    assertEquals((double) out.get("f-ratio"), 0.24169D, .0001);
   }
 
+
+  @Test
+  public void testPlot() throws Exception {
+    String cexpr = "plot(type=scatter, x=array(1,2,3), y=array(5,6,3))";
+    ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
+    paramsLoc.set("expr", cexpr);
+    paramsLoc.set("qt", "/stream");
+    String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;
+    TupleStream solrStream = new SolrStream(url, paramsLoc);
+    StreamContext context = new StreamContext();
+    solrStream.setStreamContext(context);
+    List<Tuple> tuples = getTuples(solrStream);
+    assertTrue(tuples.size() == 1);
+    String plot = tuples.get(0).getString("plot");
+    assertTrue(plot.equals("scatter"));
+    List<List<Number>> data = (List<List<Number>>)tuples.get(0).get("data");
+    assertTrue(data.size() == 3);
+    List<Number> pair1 = data.get(0);
+    assertTrue(pair1.get(0).intValue() == 1);
+    assertTrue(pair1.get(1).intValue() == 5);
+    List<Number> pair2 = data.get(1);
+    assertTrue(pair2.get(0).intValue() == 2);
+    assertTrue(pair2.get(1).intValue() == 6);
+    List<Number> pair3 = data.get(2);
+    assertTrue(pair3.get(0).intValue() == 3);
+    assertTrue(pair3.get(1).intValue() == 3);
+  }
+
+
+
   @Test
   public void testMovingAverage() throws Exception {
     String cexpr = "movingAvg(array(1,2,3,4,5,6,7), 4)";