org.mvel2.MVEL类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(808)

本文整理了Java中org.mvel2.MVEL类的一些代码示例,展示了MVEL类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MVEL类的具体详情如下:
包路径:org.mvel2.MVEL
类名称:MVEL

MVEL介绍

[英]The MVEL convienence class is a collection of static methods that provides a set of easy integration points for MVEL. The vast majority of MVEL's core functionality can be directly accessed through methods in this class.
[中]MVEL便利类是一组静态方法,为MVEL提供了一组简单的集成点。MVEL的绝大多数核心功能都可以通过此类中的方法直接访问。

代码示例

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

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) {

代码示例来源:origin: alibaba/jetcache

@Override
  public Object apply(Object context) {
    return MVEL.eval(script, context);
  }
}

代码示例来源:origin: org.drools/drools-compiler

@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);
}

代码示例来源:origin: salaboy/jBPM5-Developer-Guide

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);
}

代码示例来源:origin: org.mvel/mvel2

public void testInlineWithWithLiteral2() {
 String expr = "'foo'.{ toString() . toString() . toString() }";
 assertEquals("foo", MVEL.eval(expr));
 Serializable s = MVEL.compileExpression(expr);
 assertEquals("foo", MVEL.executeExpression(s));
}

代码示例来源:origin: org.mvel/mvel2

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);
}

代码示例来源:origin: org.mvel/mvel2

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));
 }
}

代码示例来源:origin: org.mvel/mvel2

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));
}

代码示例来源:origin: kiegroup/jbpm

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);
  }
}

代码示例来源:origin: kiegroup/jbpm

@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);
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

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");
  }
}

代码示例来源:origin: kiegroup/jbpm

@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;
}

代码示例来源:origin: org.mvel/mvel2

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());
}

代码示例来源:origin: org.mvel/mvel2

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()));
}

代码示例来源:origin: kiegroup/jbpm

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);
}

代码示例来源:origin: org.mvel/mvel2

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()));
}

代码示例来源:origin: org.drools/drools-compiler

public Object compiledExecute(final String ex) {
  final Serializable compiled = MVEL.compileExpression( ex );
  return MVEL.executeExpression( compiled,
                  new Object(),
                  new HashMap() );
}

代码示例来源:origin: kiegroup/jbpm

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());
  }
}

代码示例来源:origin: kiegroup/jbpm

@Override
  public Object run() {
    return MVEL.executeExpression(compiler.compile(),
                   new HashMap());
  }
}, KiePolicyHelper.getAccessContext());

代码示例来源:origin: j-easy/easy-rules

@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);
    }
  }
}

相关文章