@Override public UserFunction clone() { String[] args = null; if (argsNames != null) { args = new String[argsNames.length]; System.arraycopy(argsNames, 0, args, 0, args.length); } if (eh == null) { return new UserFunction(name, exp, args); } else { return new UserFunction(name, eh.clone(), args); } }
private Object lookupInFunctions() { UserFunction function = functions.get(functionName).clone(); function.setUserFunctions(functions); return function; } }
function.setArgs(args); function.setUserFunctions(functions); } else { thatObject = new Function(thatObject, methodName, args);
UserFunction y = new UserFunction("y", "x*2", "x"); functions = new HashMap<String, UserFunction>(); functions.put(y.getName(), y); assertEquals(10, EvalTools.evaluate("y(5)", null, functions)); functions.put("y", new UserFunction("y", "x*2", "x")); assertEquals(8, EvalTools.evaluate("y(2*(10/5))", null, functions)); functions.put("y", new UserFunction("y", "x*2", "x")); functions.put("z", new UserFunction("z", "y(x)+x", "x")); assertEquals(15, EvalTools.evaluate("z(5)", null, functions)); functions.put("y", new UserFunction("y", "x*2", "x")); functions.put("z", new UserFunction("z", "y(x)+x", "x")); assertEquals(11, EvalTools.evaluate("z(5) - y(2)", null, functions)); functions.put("y", new UserFunction("y", "x*2", "x")); functions.put("z", new UserFunction("z", "y(x)+x", "x")); assertEquals(9, EvalTools.evaluate("z(5) - z(2)", null, functions)); functions.put("y", new UserFunction("y", "2*2")); assertEquals(4, EvalTools.evaluate("y()", null, functions)); model.put("g", 0); functions = new HashMap<String, UserFunction>(); functions.put("it", new UserFunction("it", "i<end&&(g=++x)==g?it(i+1,end):g", "i", "end")); assertEquals(10, EvalTools.evaluate("it(0,10)", model, functions)); assertEquals(10, model.get("x"));
function.setArgs(args); function.setUserFunctions(functions); } else { thatObject = new Function(thatObject, methodName, args);
private Object lookupInFunctions() { UserFunction function = functions.get(functionName).clone(); function.setUserFunctions(functions); return function; } }
@Override public UserFunction clone() { String[] args = null; if (argsNames != null) { args = new String[argsNames.length]; System.arraycopy(argsNames, 0, args, 0, args.length); } if (eh == null) { return new UserFunction(name, exp, args); } else { return new UserFunction(name, eh.clone(), args); } }
@Test public void testMapParam() { Map<String, Object> model = new HashMap<String, Object>(); Map<String, UserFunction> functions = new HashMap<String, UserFunction>(); List<String> imports = new ArrayList<String>(); Expression exp; functions.put("foo", new UserFunction("foo", "args['x']", "args")); exp = EvalTools.prepare("foo([x:'bar'])", model, functions, imports, false); Assert.assertEquals("bar", exp.get(model)); exp = EvalTools.prepare("foo(x:'bar')", model, functions, imports, false); Assert.assertEquals("bar", exp.get(model)); exp = EvalTools.prepare("foo(x:'bar', y:'foo')", model, functions, imports, false); Assert.assertEquals("bar", exp.get(model)); functions.put("foobar", new UserFunction("foobar", "foo.x + separator + bar.y", "foo", "separator", "bar")); exp = EvalTools.prepare("foobar([x:'bar'], ' - ', [y:'foo'])", model, functions, imports, false); Assert.assertEquals("bar - foo", exp.get(model)); exp = EvalTools.prepare("foobar(x:'bar', ' - ', y:'foo')", model, functions, imports, false); Assert.assertEquals("bar - foo", exp.get(model)); exp = EvalTools.prepare("foobar(x:'bar', y:'BAR', ' - ', y:'foo', x:'FOO')", model, functions, imports, false); Assert.assertEquals("bar - foo", exp.get(model)); }