Refine search
@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); }
ParserContext context = new ParserContext(); context.addImport("MVEL", MVEL.class); context.addImport("MvelHelper", MvelHelper.class); exp = MVEL.compileExpression(modifiedRule, context); expressionCache.put(rule, exp); Object test = MVEL.executeExpression(exp, mvelParameters); if (test == null) {
public Object compiledExecute(final String ex) { final Serializable compiled = MVEL.compileExpression( ex ); return MVEL.executeExpression( compiled, new Object(), new HashMap() ); }
ParserContext context = new ParserContext(); Serializable domainExp1 = MVEL.compileExpression("offer.type.equals(OfferType.FULFILLMENT_GROUP) and (($ in order.fulfillmentGroups if $.type.equals(FulfillmentType.PHYSICAL)) != empty)", context); Boolean expressionOutcome1 = (Boolean)MVEL.executeExpression(domainExp1, domainVars); assert expressionOutcome1 != null && expressionOutcome1;
public void testElseIfCommentBugPreCompiled() throws Exception { // Comments can't appear before else if() - compilation works, but evaluation fails executeExpression(compileExpression("// This is never true\n" + "if (1==0) {\n" + " // Never reached\n" + "}\n" + "// This is always true...\n" + "else if (1==1) {" + " System.out.println('Got here!');" + "}\n")); }
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"); } }
ParserContext context = new ParserContext(); Serializable domainExp1 = MVEL.compileExpression("result = false; for (cat : currentItem.product.allParentCategories) {if (cat.name == 't-shirt') {result = true;}}; return result and order.subTotal.amount >= 50", context); Boolean expressionOutcome1 = (Boolean)MVEL.executeExpression(domainExp1, domainVars); assert expressionOutcome1 != null && expressionOutcome1; Serializable domainExp2 = MVEL.compileExpression("($ in currentItem.product.allParentCategories if $.name == 't-shirt') != empty and order.subTotal.amount >= 50", context); Boolean expressionOutcome2 = (Boolean)MVEL.executeExpression(domainExp2, domainVars); assert expressionOutcome2 != null && expressionOutcome2;
public void testMVEL222() throws IOException { String script = "for (int i= 0; i < 10; i++ ){ values[i] = 1.0; }"; Map<String, Object> scriptVars = new HashMap<String, Object>(); double[] values = new double[10]; scriptVars.put("values", values); Serializable expression = MVEL.compileExpression(script); for (int i = 0; i < 6; i++) { scriptVars.put("values", values); MVEL.executeExpression(expression, scriptVars); } }
public static void exerciseFailure() { 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", MvelTestOverloadUtils.class); Serializable exp = MVEL.compileExpression(rule, context); executeTestCase(exp, "TEST-INVALID"); boolean response = executeTestCase(exp, "TEST-VALID"); if (!response) { //We received the expected, corrupted expression state, so return true to validate the expected test results System.out.print("true"); } else { //We did not receive the expected, corrupted expression state. This can happen sometimes, since the ordering of methods //returned from the call to Class#getMethods for SelectizeCollectionUtilsTest is undetermined. Return false //since we did not validate the expected test results in this run. System.out.print("false"); } }
@Test public void testOfferAppliesToSpecificItems() { DiscreteOrderItemImpl orderItem = new DiscreteOrderItemImpl(); Sku sku = new SkuImpl(); sku.setRetailPrice(new Money("1")); sku.setId(1234L); orderItem.setSku(sku); OfferImpl offer = new OfferImpl(); offer.setType(OfferType.ORDER_ITEM); //Set up MVEL Context ParserContext context = new ParserContext(); //Import OfferType into the MVEL context since it may be used context.addImport("OfferType", OfferType.class); context.addImport("FulfillmentType", FulfillmentType.class); //Compile the MVEL Expression Serializable domainExp1 = MVEL.compileExpression("offer.type == OfferType.ORDER_ITEM and (currentItem.sku.id in [1234, 2345, 5678])", context); //Add variables to a HashMap that should be passed in to execute the expression HashMap<String, Object> domainVars = new HashMap<>(); domainVars.put("currentItem", orderItem); domainVars.put("offer", offer); //Execute the expression Boolean expressionOutcome1 = (Boolean)MVEL.executeExpression(domainExp1, domainVars); assert expressionOutcome1 != null && expressionOutcome1; }
public void testJIRA100b() { Serializable s = MVEL.compileExpression("java.math.BigDecimal axx = new java.math.BigDecimal( 10.0 ); java.math.BigDecimal bxx = " + "new java.math.BigDecimal( 10.0 ); java.math.BigDecimal cxx = axx + bxx; return cxx; "); assertEquals(new BigDecimal(20), executeExpression(s, new HashMap())); }
@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; }
@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 void testMVEL238() throws IOException { String expr = new String(loadFromFile(new File("src/test/java/org/mvel2/tests/MVEL238.mvel"))); Serializable s = MVEL.compileExpression(expr); System.out.println(MVEL.executeExpression(s, new HashMap())); System.out.println(MVEL.executeExpression(s, new HashMap())); }
public static Object eval(String str, Map<String, Object> vars) { ParserConfiguration pconf = new ParserConfiguration(); pconf.addPackageImport("org.jbpm.services.task"); // pconf.addPackageImport("org.jbpm.services.task.service"); pconf.addPackageImport("org.jbpm.services.task.query"); pconf.addPackageImport("java.util"); for(String entry : getInputs().keySet()){ pconf.addImport(entry, getInputs().get(entry)); } 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); } } public static String toString(Reader reader) throws IOException {
private void testMVELUntyped(String text) { String str = IMPORTS + text; ParserContext pctx = new ParserContext(); Map<String, Object> vars = new HashMap<String, Object>(); Object o = MVEL.compileExpression(str, pctx); MVEL.executeExpression(o, vars); }
public Object compiledExecute(String ex) { Serializable compiled = MVEL.compileExpression(ex); Object first = MVEL.executeExpression(compiled, null, map); Object second = MVEL.executeExpression(compiled, null, map); if (first != null && !first.getClass().isArray()) assertEquals(first, second); return second; } }
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); }
private void testMVELTyped(String text) { String str = IMPORTS + text; ParserContext pctx = new ParserContext(); pctx.setStrongTyping(true); Map<String, Object> vars = new HashMap<String, Object>(); Object o = MVEL.compileExpression(str, pctx); MVEL.executeExpression(o, vars); }
public void testSimple2() { OptimizerFactory.setDefaultOptimizer("ASM"); MVEL.executeExpression( MVEL.compileExpression("java.util.Collections.emptySet instanceof java.util.Set")); }