Add RandomNumber GREL function

This commit is contained in:
Owen Stephens 2019-03-24 20:16:43 +00:00
parent fe6abedc52
commit 7425418011
3 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,71 @@
/*
Copyright 2010, Knowledge Integration Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Knowledge Integration Ltd. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.expr.functions.math;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
public class RandomNumber implements Function {
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 && args[0] != null && args[0] instanceof Number
&& args[1] != null && args[1] instanceof Number && ((Number) args[0]).intValue()<((Number) args[1]).intValue()) {
int randomNum = ThreadLocalRandom.current().nextInt(((Number) args[0]).intValue(), ((Number) args[1]).intValue()+1);
return randomNum;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two numbers, the first must be less than the second");
}
@Override
public String getDescription() {
return "Returns a pseudo-random integer between the lower and upper bound (inclusive)";
}
@Override
public String getParams() {
return "number lower bound, number upper bound";
}
@Override
public String getReturns() {
return "number";
}
}

View File

@ -92,6 +92,7 @@ import com.google.refine.expr.functions.math.Odd;
import com.google.refine.expr.functions.math.Pow;
import com.google.refine.expr.functions.math.Quotient;
import com.google.refine.expr.functions.math.Radians;
import com.google.refine.expr.functions.math.RandomNumber;
import com.google.refine.expr.functions.math.Round;
import com.google.refine.expr.functions.math.Sin;
import com.google.refine.expr.functions.math.Sinh;
@ -299,6 +300,7 @@ public class ControlFunctionRegistry {
registerFunction("combin", new Combin());
registerFunction("degrees", new Degrees());
registerFunction("radians", new Radians());
registerFunction("randomNumber", new RandomNumber());
registerFunction("gcd", new GreatestCommonDenominator());
registerFunction("lcm", new LeastCommonMultiple());
registerFunction("multinomial", new Multinomial());

View File

@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright (C) 2018, OpenRefine contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.google.refine.tests.expr.functions.math;
import java.util.Properties;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.refine.expr.functions.math.RandomNumber;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.arrays.InArray;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils;
public class RandomNumberTests extends RefineTest {
static Properties bindings;
@BeforeMethod
public void SetUp() {
bindings = new Properties();
}
@AfterMethod
public void TearDown() {
bindings = null;
}
@Test
public void serializeRandomNumber() {
String json = "{\"description\":\"Returns a pseudo-random integer between the lower and upper bound (inclusive)\",\"params\":\"number lower bound, number upper bound\",\"returns\":\"number\"}";
TestUtils.isSerializedTo(new RandomNumber(), json);
}
@Test
public void testRandomNumberParameters() {
Assert.assertTrue(invoke("randomNumber") instanceof EvalError);
Assert.assertTrue(invoke("randomNumber", "string1") instanceof EvalError);
Assert.assertTrue(invoke("randomNumber", "string1","string2") instanceof EvalError);
Assert.assertTrue(invoke("randomNumber", 3, 2) instanceof EvalError);
}
@Test
public void testRandomNumber() {
Object a = invoke("randomNumber", 1, 10);
Assert.assertTrue((int) a < 11 && (int) a > 0);
}
private static Object invoke(String name,Object... args) {
// registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) {
throw new IllegalArgumentException("Unknown function "+name);
}
if (args == null) {
return function.call(bindings,new Object[0]);
} else {
return function.call(bindings,args);
}
}
}