mirror of https://github.com/apache/lucene.git
SOLR-11398: Add weibullDistribution Stream Evaluator
This commit is contained in:
parent
001fa289e4
commit
0e5c3aa3dc
|
@ -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)
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -6552,6 +6552,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();
|
||||
|
|
Loading…
Reference in New Issue