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/09/26 01:51:48 UTC
lucene-solr:branch_7x: SOLR-11398: Add weibullDistribution Stream
Evaluator
Repository: lucene-solr
Updated Branches:
refs/heads/branch_7x 749813d9d -> c80c745bb
SOLR-11398: Add weibullDistribution Stream Evaluator
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/c80c745b
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/c80c745b
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/c80c745b
Branch: refs/heads/branch_7x
Commit: c80c745bb873e3e1efe6ed3ea9338c59ca948195
Parents: 749813d
Author: Joel Bernstein <jb...@apache.org>
Authored: Mon Sep 25 21:34:45 2017 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Mon Sep 25 21:48:01 2017 -0400
----------------------------------------------------------------------
.../org/apache/solr/handler/StreamHandler.java | 6 +--
.../client/solrj/io/eval/MeanEvaluator.java | 57 ++++++++++++++++++++
.../io/eval/WeibullDistributionEvaluator.java | 48 +++++++++++++++++
.../solrj/io/stream/StreamExpressionTest.java | 47 ++++++++++++++++
4 files changed, 155 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c80c745b/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 0f059e3..4f50ca9 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -301,12 +301,12 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("movingMedian", MovingMedianEvaluator.class)
.withFunctionName("monteCarlo", MonteCarloEvaluator.class)
.withFunctionName("constantDistribution", ConstantDistributionEvaluator.class)
+ .withFunctionName("weibullDistribution", WeibullDistributionEvaluator.class)
+ .withFunctionName("mean", MeanEvaluator.class)
// Boolean Stream Evaluators
-
-
- .withFunctionName("and", AndEvaluator.class)
+ .withFunctionName("and", AndEvaluator.class)
.withFunctionName("eor", ExclusiveOrEvaluator.class)
.withFunctionName("eq", EqualToEvaluator.class)
.withFunctionName("gt", GreaterThanEvaluator.class)
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c80c745b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
new file mode 100644
index 0000000..c2e11f7
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
@@ -0,0 +1,57 @@
+/*
+ * 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.List;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.commons.math3.stat.StatUtils;
+
+
+public class MeanEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+ protected static final long serialVersionUID = 1L;
+
+ public MeanEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+ super(expression, factory);
+
+ if(1 != containedEvaluators.size()){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 1 value but found %d",expression,containedEvaluators.size()));
+ }
+ }
+
+ @Override
+ public Object doWork(Object value) throws IOException{
+ if(null == value){
+ throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is null", constructingFactory.getFunctionName(getClass())));
+ }
+ else if(value instanceof List){
+ List<Number> c = (List<Number>) value;
+ double[] data = new double[c.size()];
+ for(int i=0; i< c.size(); i++) {
+ data[i] = c.get(i).doubleValue();
+ }
+
+ return StatUtils.mean(data);
+ }
+ else{
+ throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is not a collection, instead a %s was found", constructingFactory.getFunctionName(getClass()), value.getClass().getSimpleName()));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c80c745b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/WeibullDistributionEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/WeibullDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/WeibullDistributionEvaluator.java
new file mode 100644
index 0000000..e1a3463
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/WeibullDistributionEvaluator.java
@@ -0,0 +1,48 @@
+/*
+ * 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.Locale;
+
+import org.apache.commons.math3.distribution.WeibullDistribution;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class WeibullDistributionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
+
+ private static final long serialVersionUID = 1;
+
+ public WeibullDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object first, Object second) throws IOException{
+ if(null == first){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
+ }
+ if(null == second){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));
+ }
+
+ Number shape = (Number)first;
+ Number scale = (Number)second;
+
+ return new WeibullDistribution(shape.doubleValue(), scale.doubleValue());
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c80c745b/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 e1075e3..44b8e54 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
@@ -6553,6 +6553,53 @@ public class StreamExpressionTest extends SolrCloudTestCase {
@Test
+ public void testWeibullDistribution() throws Exception {
+ String cexpr = "let(echo=true, " +
+ "a=percentile(sample(weibullDistribution(1, 10),5000), 50), " +
+ "b=percentile(sample(weibullDistribution(1, 50),5000), 50), " +
+ "c=percentile(sample(weibullDistribution(1, 100),5000), 50)," +
+ "d=percentile(sample(weibullDistribution(4, 10),5000), 50)," +
+ "e=percentile(sample(weibullDistribution(8, 10),5000), 50))";
+
+ 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);
+ Number a = (Number)tuples.get(0).get("a");
+ Number b = (Number)tuples.get(0).get("b");
+ Number c = (Number)tuples.get(0).get("c");
+ Number d = (Number)tuples.get(0).get("d");
+ Number e = (Number)tuples.get(0).get("e");
+ assertTrue(a.doubleValue() < b.doubleValue());
+ assertTrue(b.doubleValue() < c.doubleValue());
+ assertTrue(a.doubleValue() < d.doubleValue());
+ assertTrue(d.doubleValue() < e.doubleValue());
+ }
+
+
+ @Test
+ public void testMean() throws Exception {
+ String cexpr = "mean(array(1,2,3,4,5))";
+ 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);
+ Number mean = (Number)tuples.get(0).get("return-value");
+ assertEquals(mean.doubleValue(), 3.0D, 0.0D);
+ }
+
+
+ @Test
public void testScale() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();