Refine search
ParserContext context = new ParserContext(); context.addImport("MVEL", MVEL.class); context.addImport("MvelHelper", MvelHelper.class); context.addImport("CollectionUtils", SelectizeCollectionUtils.class); if (MapUtils.isNotEmpty(additionalContextImports)) { exp = MVEL.compileExpression(modifiedRule, context); expressionCache.put(rule, exp); Object test = MVEL.executeExpression(exp, mvelParameters); if (test == null) {
@Override public Object apply(Object context) { return MVEL.eval(script, context); } }
@Test public void test1() { final ParserContext pc = new ParserContext(); pc.addInput("x", String.class); pc.setStrongTyping(true); final Object o = MVEL.compileExpression("x.startsWith('d')", pc); final Map vars = new HashMap(); vars.put("x", "d"); MVEL.executeExpression(o, vars); System.out.println(o); }
public static Object eval(String str, Map vars) { ExpressionCompiler compiler = new ExpressionCompiler(str.trim()); ParserContext context = new ParserContext(); context.addPackageImport("org.jbpm.task"); context.addPackageImport("org.jbpm.task.service"); context.addPackageImport("org.jbpm.task.query"); context.addPackageImport("java.util"); vars.put("now", new Date()); return MVEL.executeExpression(compiler.compile(context), vars); }
public void testInlineWithWithLiteral2() { String expr = "'foo'.{ toString() . toString() . toString() }"; assertEquals("foo", MVEL.eval(expr)); Serializable s = MVEL.compileExpression(expr); assertEquals("foo", MVEL.executeExpression(s)); }
public void testStrongTypingModeComparison() { ParserContext parserContext = new ParserContext(); parserContext.setStrongTyping(true); parserContext.addInput("a", Long.class); CompiledExpression compiledExpression = new ExpressionCompiler("a==0", parserContext).compile(); HashMap<String, Object> variables = new HashMap<String, Object>(); variables.put("a", 0l); MVEL.executeExpression(compiledExpression, variables); }
public void testAssignmentOnTwoDimensionalArrayUsingIndexedInput() { ParserConfiguration conf = new ParserConfiguration(); ParserContext pctx = new ParserContext( conf ); pctx.setIndexAllocation( true ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("array", Double[][].class); pctx.addIndexedInput("array"); Map vars = new HashMap() {{ put("array", new Double[2][2]); }}; assertEquals(42.0, MVEL.executeExpression(MVEL.compileExpression("array[1][1] = 42.0;\narray[1][1]", pctx), vars)); } }
public void testJIRA161() { Serializable s = MVEL.compileExpression("1==-(-1)", ParserContext.create().stronglyTyped()); assertEquals(1 == -(-1), MVEL.executeExpression(s)); ParserContext ctx = new ParserContext(); ctx.setStrongTyping(true); CompiledExpression compiledExpression = new ExpressionCompiler("1==-(-1)", ctx).compile(); assertEquals(1 == -(-1), MVEL.executeExpression(compiledExpression)); }
public static Object eval(String str, Map<String, Object> vars) { ParserConfiguration pconf = new ParserConfiguration(); pconf.addPackageImport("org.kie.internal.task.api.model"); pconf.addPackageImport("org.jbpm.services.task"); pconf.addPackageImport("org.jbpm.services.task.impl.model"); pconf.addPackageImport("org.jbpm.services.task.query"); pconf.addPackageImport("org.jbpm.services.task.internals.lifecycle"); pconf.addImport(Status.class); pconf.addImport(Allowed.class); pconf.addPackageImport("java.util"); ParserContext context = new ParserContext(pconf); Serializable s = MVEL.compileExpression(str.trim(), context); if (vars != null) { return MVELSafeHelper.getEvaluator().executeExpression(s, vars); } else { return MVELSafeHelper.getEvaluator().executeExpression(s); } }
@Override public Object compile(String expression, Map<String, Object> parameters) { logger.debug("About to compile mvel expression {}", expression); ClassLoader classLoader = (ClassLoader) parameters.get("classloader"); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(classLoader); ParserContext context = new ParserContext(config); if (parameters != null) { @SuppressWarnings("unchecked") Set<String> imports = (Set<String>)parameters.get("imports"); if (imports != null) { for(String clazz : imports) { try { Class<?> cl = Class.forName(clazz, true, classLoader); context.addImport(cl.getSimpleName(), cl); } catch (ClassNotFoundException e) { logger.warn("Unable to load class {} due to {}", clazz, e.getException()); }; } } } return MVEL.compileExpression(expression, context); }
public static void exerciseWorkaround() { System.setProperty("mvel2.disable.jit", "true"); String rule = "CollectionUtils.intersection(level1.level2.getMultiValueSkuAttributes()[\"TEST-VALID\"],[\"TEST-VALID\"]).size()>0"; ParserContext context = new ParserContext(); context.addImport("CollectionUtils", SelectizeCollectionUtils.class); Serializable exp = MVEL.compileExpression(rule, context); executeTestCase(exp, "TEST-INVALID"); boolean response = executeTestCase(exp, "TEST-VALID"); if (!response) { //With the workaround, we should never get here System.out.print("false"); } else { //The expression should never be corrupted now that we've removed the overloaded method (this is the workaround) System.out.print("true"); } }
@Override public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) { Object instance = null; InternalRuntimeManager manager = null; if (contextParams.containsKey("runtimeManager")) { manager = (InternalRuntimeManager) contextParams.get("runtimeManager"); instance = manager.getCacheManager().get(model.getIdentifier()); if (instance != null) { return instance; } } ParserConfiguration config = new ParserConfiguration(); config.setClassLoader(cl); ParserContext ctx = new ParserContext(config); if (contextParams != null) { for (Map.Entry<String, Object> entry : contextParams.entrySet()) { ctx.addVariable(entry.getKey(), entry.getValue().getClass()); } } Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx); instance = MVELSafeHelper.getEvaluator().executeExpression( compiledExpression, contextParams ); if (manager != null && instance instanceof Cacheable) { manager.getCacheManager().add(model.getIdentifier(), instance); } return instance; }
public void testGetCorrectInputs() { String str = "total = total + $cheese.price"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("total", int.class); pctx.addInput("$cheese", Cheese.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty()); }
public void testConcatWithLineBreaks() { ParserContext ctx = new ParserContext(); ctx.setDebugSymbols(true); ctx.setSourceFile("source.mv"); ExpressionCompiler parser = new ExpressionCompiler("\"foo\"+\n\"bar\"", ctx); assertEquals("foobar", executeExpression(parser.compile())); }
public Object eval(String str, Map vars) { ParserContext context = new ParserContext(); context.addPackageImport("org.jbpm.task"); context.addPackageImport("org.jbpm.task.service"); context.addPackageImport("org.jbpm.task.query"); context.addPackageImport("java.util"); vars.put("now", new Date()); return MVELSafeHelper.getEvaluator().executeExpression(MVEL.compileExpression(str, context), vars); }
public void testDynamicImports3() { String expression = "import java.util.*; HashMap map = new HashMap(); map.size()"; ExpressionCompiler compiler = new ExpressionCompiler(expression); Serializable s = compiler.compile(); assertEquals(0, executeExpression(s, new DefaultLocalVariableResolverFactory())); assertEquals(0, MVEL.eval(expression, new HashMap())); }
public Object compiledExecute(final String ex) { final Serializable compiled = MVEL.compileExpression( ex ); return MVEL.executeExpression( compiled, new Object(), new HashMap() ); }
public static Object eval(final String expression) { ExpressionCompiler compiler = new ExpressionCompiler(getRevisedExpression(expression), WID_PARSER_CONTEXT); if(KiePolicyHelper.isPolicyEnabled()) { return AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { return MVEL.executeExpression(compiler.compile(), new HashMap()); } }, KiePolicyHelper.getAccessContext()); } else { return MVEL.executeExpression(compiler.compile(), new HashMap()); } }
@Override public Object run() { return MVEL.executeExpression(compiler.compile(), new HashMap()); } }, KiePolicyHelper.getAccessContext());
@Override public void execute(Facts facts) { try { MVEL.executeExpression(compiledExpression, facts.asMap()); } catch (Exception e) { LOGGER.debug("Unable to evaluate expression: '" + expression + "' on facts: " + facts, e); } } }