Apache Lucene open-source search software
SOLR-12828: Add oscillate Stream Evaluator to support sine wave analysis
Joel Bernstein751bf7db208d
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 17f9d578027..02968b28ee1 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@@ -174,6 +174,7 @@ public class Lang {.withFunctionName("betaDistribution", BetaDistributionEvaluator.class).withFunctionName("polyfit", PolyFitEvaluator.class).withFunctionName("harmonicFit", HarmonicFitEvaluator.class)+ .withFunctionName("harmfit", HarmonicFitEvaluator.class).withFunctionName("loess", LoessEvaluator.class).withFunctionName("matrix", MatrixEvaluator.class).withFunctionName("transpose", TransposeEvaluator.class)@@ -261,7 +262,10 @@ public class Lang {.withFunctionName("getBaryCenter", GetBaryCenterEvaluator.class).withFunctionName("getArea", GetAreaEvaluator.class).withFunctionName("getBoundarySize", GetBoundarySizeEvaluator.class)-+ .withFunctionName("oscillate", OscillateEvaluator.class)+ .withFunctionName("getAmplitude", GetAmplitudeEvaluator.class)+ .withFunctionName("getPhase", GetPhaseEvaluator.class)+ .withFunctionName("getAngularFrequency", GetAngularFrequencyEvaluator.class)// Boolean Stream Evaluators.withFunctionName("and", AndEvaluator.class)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.javanew file mode 100644index 00000000000..131029bd686--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.java@@ -0,0 +1,42 @@+/*+ * 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.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class GetAmplitudeEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ private static final long serialVersionUID = 1;++ public GetAmplitudeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if(!(value instanceof VectorFunction)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));+ } else {+ VectorFunction vectorFunction = (VectorFunction)value;+ return vectorFunction.getFromContext("amplitude");+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.javanew file mode 100644index 00000000000..001e95e3b41--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.java@@ -0,0 +1,42 @@+/*+ * 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.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class GetAngularFrequencyEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ private static final long serialVersionUID = 1;++ public GetAngularFrequencyEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if(!(value instanceof VectorFunction)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));+ } else {+ VectorFunction vectorFunction = (VectorFunction)value;+ return vectorFunction.getFromContext("angularFrequency");+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.javanew file mode 100644index 00000000000..546ef20e8eb--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.java@@ -0,0 +1,42 @@+/*+ * 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.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class GetPhaseEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ private static final long serialVersionUID = 1;++ public GetPhaseEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if(!(value instanceof VectorFunction)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));+ } else {+ VectorFunction vectorFunction = (VectorFunction)value;+ return vectorFunction.getFromContext("phase");+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.javaindex 04c0b0c46ce..4e7ac6ea9fe 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java@@ -70,6 +70,9 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements Mpoints.add(x[i], y[i]);}+ double[] guess = new HarmonicCurveFitter.ParameterGuesser(points.toList()).guess();+ curveFitter = curveFitter.withStartPoint(guess);+double[] coef = curveFitter.fit(points.toList());HarmonicOscillator pf = new HarmonicOscillator(coef[0], coef[1], coef[2]);@@ -79,6 +82,12 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements Mlist.add(yvalue);}- return list;+ VectorFunction vectorFunction = new VectorFunction(pf, list);+ vectorFunction.addToContext("amplitude", coef[0]);+ vectorFunction.addToContext("angularFrequency", coef[1]);+ vectorFunction.addToContext("phase", coef[2]);++ return vectorFunction;+}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.javanew file mode 100644index 00000000000..6fdd3c5e672--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.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.ArrayList;++import org.apache.commons.math3.analysis.function.HarmonicOscillator;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class OscillateEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public OscillateEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object... objects) throws IOException{++ if(objects.length != 3) {+ throw new IOException("The oscillate function takes 3 arguments.");+ }++ double amp = ((Number)objects[0]).doubleValue();+ double om = ((Number)objects[1]).doubleValue();+ double phase = ((Number)objects[2]).doubleValue();+++ HarmonicOscillator pf = new HarmonicOscillator(amp, om, phase);++ List list = new ArrayList();+ for(int i=0; i<128; i++) {+ double yvalue= pf.value(i);+ list.add(yvalue);+ }++ return new VectorFunction(pf, list);++ }+}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.javaindex bc15e02d0b7..169feadd451 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@@ -52,7 +52,7 @@ public class TestLang extends LuceneTestCase {"poissonDistribution", "enumeratedDistribution", "probability", "sumDifference", "meanDifference","primes", "factorial", "movingMedian", "binomialCoefficient", "expMovingAvg", "monteCarlo", "constantDistribution","weibullDistribution", "mean", "mode", "logNormalDistribution", "zipFDistribution", "gammaDistribution",- "betaDistribution", "polyfit", "harmonicFit", "loess", "matrix", "transpose", "unitize",+ "betaDistribution", "polyfit", "harmonicFit", "harmfit", "loess", "matrix", "transpose", "unitize","triangularDistribution", "precision", "minMaxScale", "markovChain", "grandSum","scalarAdd", "scalarSubtract", "scalarMultiply", "scalarDivide", "sumRows","sumColumns", "diff", "corrPValues", "normalizeSum", "geometricDistribution", "olsRegress",@@ -71,7 +71,8 @@ public class TestLang extends LuceneTestCase {"cbrt", "coalesce", "uuid", "if", "convert", "valueAt", "memset", "fft", "ifft", "euclidean","manhattan","earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue", "knnRegress", "gaussfit","outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors",- "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize"};+ "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",+ "getAmplitude", "getPhase", "getAngularFrequency"};@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 67b0e082c53..73aa0c9cafa 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@@ -342,7 +342,6 @@ public class MathExpressionTest extends SolrCloudTestCase {List<Tuple> tuples = getTuples(solrStream);assertTrue(tuples.size() == 1);List<List<Number>>locVectors = (List<List<Number>>)tuples.get(0).get("b");- System.out.println(locVectors);int v=1;for(List<Number> row : locVectors) {double lat = row.get(0).doubleValue();@@ -2359,6 +2358,46 @@ public class MathExpressionTest extends SolrCloudTestCase {assertTrue(out.get(5).intValue() == 72);}+ @Test+ public void testOscillate() throws Exception {+ String cexpr = "let(echo=true," ++ " a=oscillate(10, .3, 2.9)," ++ " b=describe(a)," ++ " c=getValue(b, min)," ++ " d=getValue(b, max)," ++ " e=harmfit(a)," ++ " f=getAmplitude(e)," ++ " g=getAngularFrequency(e)," ++ " h=getPhase(e))";+ 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);+ List<Number> wave = (List<Number>)tuples.get(0).get("a");+ assertEquals(wave.size(), 128);+ Map desc = (Map)tuples.get(0).get("b");+ Number min = (Number)tuples.get(0).get("c");+ Number max = (Number)tuples.get(0).get("d");+ assertEquals(min.doubleValue(), -9.9, .1);+ assertEquals(max.doubleValue(), 9.9, .1);++ List<Number> wave1 = (List<Number>)tuples.get(0).get("e");+ assertEquals(wave1.size(), 128);++ Number amp = (Number)tuples.get(0).get("f");+ Number freq = (Number)tuples.get(0).get("g");+ Number pha = (Number)tuples.get(0).get("h");++ assertEquals(amp.doubleValue(), 10, .1);+ assertEquals(freq.doubleValue(), .3, .1);+ assertEquals(pha.doubleValue(), 2.9, .1);+ }+@Testpublic void testEbeAdd() throws Exception {String cexpr = "let(echo=true," +@@ -2410,6 +2449,7 @@ public class MathExpressionTest extends SolrCloudTestCase {}+@Testpublic void testSetAndGetValue() throws Exception {String cexpr = "let(echo=true," +