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 2018/10/04 14:47:46 UTC

lucene-solr:branch_7x: SOLR-12811: Add enclosingDisk and associated geometric Stream Evaluators

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x fe819ea27 -> 66b01fcff


SOLR-12811: Add enclosingDisk and associated geometric Stream Evaluators


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

Branch: refs/heads/branch_7x
Commit: 66b01fcff961d47d853f890c10556fc56748f7d6
Parents: fe819ea
Author: Joel Bernstein <jb...@apache.org>
Authored: Thu Oct 4 10:27:32 2018 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Thu Oct 4 10:44:58 2018 -0400

----------------------------------------------------------------------
 .../org/apache/solr/client/solrj/io/Lang.java   |  4 ++
 .../solrj/io/eval/EnclosingDiskEvaluator.java   | 64 ++++++++++++++++++++
 .../solrj/io/eval/GetCenterEvaluator.java       | 52 ++++++++++++++++
 .../solrj/io/eval/GetRadiusEvaluator.java       | 44 ++++++++++++++
 .../io/eval/GetSupportPointsEvaluator.java      | 56 +++++++++++++++++
 .../apache/solr/client/solrj/io/TestLang.java   |  3 +-
 .../solrj/io/stream/MathExpressionTest.java     | 43 +++++++++++++
 7 files changed, 265 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
index 02968b2..28c4c66 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
@@ -266,6 +266,10 @@ public class Lang {
         .withFunctionName("getAmplitude", GetAmplitudeEvaluator.class)
         .withFunctionName("getPhase", GetPhaseEvaluator.class)
         .withFunctionName("getAngularFrequency", GetAngularFrequencyEvaluator.class)
+        .withFunctionName("enclosingDisk", EnclosingDiskEvaluator.class)
+        .withFunctionName("getCenter", GetCenterEvaluator.class)
+        .withFunctionName("getRadius", GetRadiusEvaluator.class)
+        .withFunctionName("getSupportPoints", GetSupportPointsEvaluator.class)
         // Boolean Stream Evaluators
 
         .withFunctionName("and", AndEvaluator.class)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnclosingDiskEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnclosingDiskEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnclosingDiskEvaluator.java
new file mode 100644
index 0000000..eb5d33f
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnclosingDiskEvaluator.java
@@ -0,0 +1,64 @@
+/*
+ * 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.ArrayList;
+
+import org.apache.commons.math3.geometry.enclosing.EnclosingBall;
+import org.apache.commons.math3.geometry.enclosing.WelzlEncloser;
+import org.apache.commons.math3.geometry.euclidean.twod.DiskGenerator;
+import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D;
+import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class EnclosingDiskEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {
+  protected static final long serialVersionUID = 1L;
+
+  public EnclosingDiskEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    if(objects[0] instanceof Matrix) {
+      return getEnclosingDisk((Matrix)objects[0]);
+    } else {
+      throw new IOException("The enclosingDisk function operates on a matrix of 2D vectors");
+    }
+  }
+
+  public static EnclosingBall getEnclosingDisk(Matrix matrix) throws IOException {
+    double[][] data = matrix.getData();
+    List<Vector2D> points = new ArrayList(data.length);
+    if(data[0].length == 2) {
+      for(double[] row : data) {
+        points.add(new Vector2D(row[0], row[1]));
+      }
+
+      WelzlEncloser<Euclidean2D, Vector2D> welzlEncloser = new WelzlEncloser(.001, new DiskGenerator());
+      EnclosingBall enclosingBall = welzlEncloser.enclose(points);
+      return enclosingBall;
+    } else {
+      throw new IOException("The enclosingDisk function operates on a matrix of 2D vectors");
+    }
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetCenterEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetCenterEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetCenterEvaluator.java
new file mode 100644
index 0000000..45ecea1
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetCenterEvaluator.java
@@ -0,0 +1,52 @@
+/*
+ * 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.ArrayList;
+
+import java.util.Locale;
+
+import org.apache.commons.math3.geometry.enclosing.EnclosingBall;
+import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GetCenterEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+  private static final long serialVersionUID = 1;
+
+  public GetCenterEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object value) throws IOException {
+    if(!(value instanceof EnclosingBall)){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting an EnclosingBall",toExpression(constructingFactory), value.getClass().getSimpleName()));
+    } else {
+      EnclosingBall enclosingBall = (EnclosingBall)value;
+      Vector2D vec = (Vector2D)enclosingBall.getCenter();
+      List<Number> center = new ArrayList();
+      center.add(vec.getX());
+      center.add(vec.getY());
+      return center;
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetRadiusEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetRadiusEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetRadiusEvaluator.java
new file mode 100644
index 0000000..1fae9eb
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetRadiusEvaluator.java
@@ -0,0 +1,44 @@
+/*
+ * 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.geometry.enclosing.EnclosingBall;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GetRadiusEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+  private static final long serialVersionUID = 1;
+
+  public GetRadiusEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object value) throws IOException {
+    if(!(value instanceof EnclosingBall)){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting an EnclosingBall",toExpression(constructingFactory), value.getClass().getSimpleName()));
+    } else {
+      EnclosingBall enclosingBall = (EnclosingBall)value;
+      return enclosingBall.getRadius();
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetSupportPointsEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetSupportPointsEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetSupportPointsEvaluator.java
new file mode 100644
index 0000000..2248490
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetSupportPointsEvaluator.java
@@ -0,0 +1,56 @@
+/*
+ * 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.geometry.enclosing.EnclosingBall;
+
+import org.apache.commons.math3.geometry.Point;
+import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GetSupportPointsEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+  private static final long serialVersionUID = 1;
+
+  public GetSupportPointsEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object value) throws IOException {
+    if(!(value instanceof EnclosingBall)){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting an EnclosingBall",toExpression(constructingFactory), value.getClass().getSimpleName()));
+    } else {
+      EnclosingBall enclosingBall = (EnclosingBall)value;
+      Point[] points = enclosingBall.getSupport();
+      double[][] data = new double[points.length][2];
+      int i=0;
+      for(Point point : points) {
+        Vector2D eu = (Vector2D)point;
+        data[i][0] = eu.getX();
+        data[i][1] = eu.getY();
+        ++i;
+      }
+
+      return new Matrix(data);
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
index 169fead..3ae5547 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
@@ -72,7 +72,8 @@ public class TestLang extends LuceneTestCase {
       "earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue", "knnRegress", "gaussfit",
       "outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors",
       "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
-      "getAmplitude", "getPhase", "getAngularFrequency"};
+      "getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
+      "getSupportPoints"};
 
   @Test
   public void testLang() {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/66b01fcf/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
index 73aa0c9..deb4522 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
@@ -472,6 +472,49 @@ public class MathExpressionTest extends SolrCloudTestCase {
   }
 
   @Test
+  public void testEnclosingDisk() throws Exception {
+    String expr = "let(echo=true," +
+        "              x=array(96.42894739701268, 99.11076410926444, 95.71563821370013,101.4356840561301, 96.17912865782684, 113.430677406492, 109.5927785287056, 87.26561260238425, 103.3122002816537, 100.4959815617706, 92.78972440872515, 92.98815024042877, 89.1448359089767, 104.9410622701036, 106.5546761317927, 102.0132643274808, 119.6726096270366, 97.61388415294184, 106.7928221374049, 94.31369945729962, 87.37098859879977, 82.8015657665458, 88.84342877874248, 94.58797342988339, 92.38720473619748)," +
+        "              y=array(97.43395922838836, 109.5441846957560, 78.82698890096127, 96.67181538737611,95.52423701473863, 85.3391529394878, 87.01956497912255, 111.5289690656729,86.41034184809114, 84.11696923489203, 109.3874354244069, 102.3391063812790,109.0604436531823,102.7957014900897,114.4376483055848,107.4387578165579,106.2490201384653,103.4490197583837,93.8201540211101,101.6060721649409, 115.3512636715722,119.1046170610335,99.74910277836263,104.2116724112481, 86.02222520549304)," +
+        "              c=transpose(matrix(x, y))," +
+        "              d=enclosingDisk(c)," +
+        "              e=getCenter(d)," +
+        "              f=getRadius(d)," +
+        "              g=getSupportPoints(d))";
+    ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
+    paramsLoc.set("expr", expr);
+    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);
+
+    List<Number> center = (List<Number>)tuples.get(0).get("e");
+    assertEquals(center.get(0).doubleValue(), 97.40659699625388, 0.0);
+    assertEquals(center.get(1).doubleValue(), 101.57826559647323, 0.0);
+
+    double radius =tuples.get(0).getDouble("f");
+    assertEquals(radius, 22.814029299535, 0.0);
+
+    List<List<Number>> supportPoints = (List<List<Number>>)tuples.get(0).get("g");
+    List<Number> support1 = supportPoints.get(0);
+    assertEquals(support1.get(0).doubleValue(), 95.71563821370013, 0.0);
+    assertEquals(support1.get(1).doubleValue(), 78.82698890096127, 0.0);
+
+    List<Number> support2 = supportPoints.get(1);
+    assertEquals(support2.get(0).doubleValue(), 82.8015657665458, 0.0);
+    assertEquals(support2.get(1).doubleValue(), 119.1046170610335, 0.0);
+
+    List<Number> support3 = supportPoints.get(2);
+    assertEquals(support3.get(0).doubleValue(), 113.430677406492, 0.0);
+    assertEquals(support3.get(1).doubleValue(), 85.3391529394878, 0.0);
+  }
+
+  @Test
   public void testCumulativeProbability() throws Exception {
     String expr = "cumulativeProbability(normalDistribution(500, 40), 500)";
     ModifiableSolrParams paramsLoc = new ModifiableSolrParams();