@Override public Object get(Map<String, Object> model) { // HashMap<String, Object> local = new HashMap<String, Object>(model); Object ob = null; for (Expression expression : expressions) { ob = expression.get(model); } return ob; }
@Override public Object get(Map<String, Object> model) { Object o = inner.get(model); try { return clazz.cast(o); } catch (ClassCastException e) { throw new ClassCastException(o.getClass().getCanonicalName() + " cannot be cast to " + clazz.getCanonicalName()); } } }
@Override public Object get(Map<String, Object> model) { Object o = inner.get(model); try { return clazz.cast(o); } catch (ClassCastException e) { throw new ClassCastException(o.getClass().getCanonicalName() + " cannot be cast to " + clazz.getCanonicalName()); } } }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model); return (T) ex.get(model); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, null); return (T) ex.get(null); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model); return (T) ex.get(model); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model, Map<String, UserFunction> functions) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model, functions); return (T) ex.get(model); }
@Override public Object get(Map<String, Object> model) { if (result != null) return result; Object r = condition.get(model); Boolean result = toBoolean(r); if (condition.hardcoded) this.result = result; return result; }
@Override public Object get(Map<String, Object> model) { if (result != null) return result; Object r = condition.get(model); Boolean result = toBoolean(r); if (condition.hardcoded) this.result = result; return result; }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, null); return (T) ex.get(null); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model, Map<String, UserFunction> functions) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model, functions); return (T) ex.get(model); }
@Test public void testClone() throws Exception { String exp = "1+\"ololo\".substring(2)"; Expression eh = EvalTools.prepare(exp); Object ob1 = eh.get(null); Object ob2 = eh.get(null); assertTrue(ob1 == ob2); eh = eh.clone(); Object ob3 = eh.get(null); assertTrue(ob1 != ob3); assertTrue(ob1.equals(ob3)); }
@Test public void test_imports_2() { String s = "import " + AtomicInteger.class.getCanonicalName() + ";\n" + "i = new AtomicInteger(1)"; Expression expression = EvalTools.prepare(s); Map model = new HashMap(); expression.get(model); Assert.assertEquals(1, model.size()); Assert.assertEquals(1, ((AtomicInteger) model.get("i")).get()); }
@Test public void testComment_1() { Map<String, Object> model = new HashMap<String, Object>(); String s = "a = 1\n//a=2"; Assert.assertEquals(1, EvalTools.prepare(s).get(model)); }
@Test public void test_4() { String s = "a { b { c = 'value'} }"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", ((Map) ((Map) config.get("a")).get("b")).get("c")); }
@Test public void test_closures() { String s = "a = 1\n" + "s = \"|${{->a}}|\"\n" + "c = {a}\n" + "a=2"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(Integer.valueOf(2), config.get("a", 0)); Assert.assertEquals("|2|", config.get("s").toString()); }
@Test public void test_closure() { String s = "" + "closure = {'bar'}\n" + "foo = closure()" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("bar", config.get("foo")); }
@Test public void test_5() { String s = "a { b = 'value'; c = 'value 2' }"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", ((Map) config.get("a")).get("b")); Assert.assertEquals("value 2", ((Map) config.get("a")).get("c")); }
@Test public void test_comment_4() { String s = "b = 'this // is not a comment'\n" + "c = '/*also just a string*/'"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("this // is not a comment", config.get("b", "")); Assert.assertEquals("/*also just a string*/", config.get("c", "")); }