Apache Lucene open-source search software
SOLR-12811: Add enclosingDisk and associated geometric Stream Evaluators
Joel Bernsteina0487b04ea0b
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.javaindex 02968b28ee1..28c4c66be26 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)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.javanew file mode 100644index 00000000000..eb5d33f9064--- /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");+ }+ }++}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.javanew file mode 100644index 00000000000..45ecea1dfe0--- /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 filediff --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.javanew file mode 100644index 00000000000..1fae9eb6472--- /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 filediff --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.javanew file mode 100644index 00000000000..2248490de86--- /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 filediff --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.javaindex 169feadd451..3ae5547fb25 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"};@Testpublic void testLang() {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.javaindex 73aa0c9cafa..deb4522760f 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@@ -471,6 +471,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);+ }+@Testpublic void testCumulativeProbability() throws Exception {String expr = "cumulativeProbability(normalDistribution(500, 40), 500)";