本文整理汇总了Java中org.mvel2.MVEL.executeExpression方法的典型用法代码示例。如果您正苦于以下问题:Java MVEL.executeExpression方法的具体用法?Java MVEL.executeExpression怎么用?Java MVEL.executeExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.mvel2.MVEL
的用法示例。
在下文中一共展示了MVEL.executeExpression方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.mvel2.MVEL; //导入方法依赖的package包/类
public static void main(String[] args) {
Foo foo = new Foo();
foo.setName("test");
Map context = new HashMap();
String expression = "foo.name == 'test'";
VariableResolverFactory functionFactory = new MapVariableResolverFactory(context);
context.put("foo",foo);
Boolean result = (Boolean) MVEL.eval(expression,functionFactory);
System.out.println(result);
Serializable compileExpression = MVEL.compileExpression(expression);
result = (Boolean) MVEL.executeExpression(compileExpression, context, functionFactory);
System.out.print(result);
}
示例2: runMvel
import org.mvel2.MVEL; //导入方法依赖的package包/类
public static void runMvel(int xmax, int ymax, int zmax) {
Map context = new HashMap();
String expression = "x + y*2 - z";
Serializable compileExpression = MVEL.compileExpression(expression);
Integer result = 0;
Date start = new Date();
for (int xval = 0; xval < xmax; xval++) {
for (int yval = 0; yval < ymax; yval++) {
for (int zval = 0; zval <= zmax; zval++) {
context.put("x", xval);
context.put("y", yval);
context.put("z", zval);
VariableResolverFactory functionFactory = new MapVariableResolverFactory(context);
Integer cal = (Integer) MVEL.executeExpression(compileExpression, context, functionFactory);
result += cal;
}
}
}
Date end = new Date();
System.out.println("MVEL:time is : " + (end.getTime() - start.getTime()) + ",result is " + result);
}
示例3: write
import org.mvel2.MVEL; //导入方法依赖的package包/类
private void write(Writer writer, Object ctx, Map<String, Object> vars, List<Object> content) throws IOException {
for (Object obj : content) {
if (obj instanceof Var) {
write(writer, (Var) obj, ctx, vars);
} else if (obj instanceof Attr) {
Attr attr = (Attr) obj;
Object exp = attr.var.exp;
Object v = MVEL.executeExpression(exp, ctx, vars);
if (v != null && !v.equals(Boolean.FALSE)) {
writer.write(' ');
writer.write(attr.name);
writer.write("=\"");
if (!v.equals(Boolean.TRUE)) {
write(writer, attr.var, ctx, vars);
}
writer.write("\"");
}
} else if (obj instanceof Block) {
Block block = (Block) obj;
write(writer, ctx, vars, block.vars, block.content);
} else {
writer.write(obj.toString());
}
}
}
示例4: shouldApplyFeeToFulfillmentGroup
import org.mvel2.MVEL; //导入方法依赖的package包/类
/**
* If the SkuFee expression is null or empty, this method will always return true
*
* @param fee
* @param fulfillmentGroup
* @return
*/
protected boolean shouldApplyFeeToFulfillmentGroup(SkuFee fee, FulfillmentGroup fulfillmentGroup) {
boolean appliesToFulfillmentGroup = true;
String feeExpression = fee.getExpression();
if (!StringUtils.isEmpty(feeExpression)) {
Serializable exp = (Serializable) EXPRESSION_CACHE.get(feeExpression);
if (exp == null) {
ParserContext mvelContext = new ParserContext();
mvelContext.addImport("MVEL", MVEL.class);
mvelContext.addImport("MvelHelper", MvelHelper.class);
exp = MVEL.compileExpression(feeExpression, mvelContext);
EXPRESSION_CACHE.put(feeExpression, exp);
}
HashMap<String, Object> vars = new HashMap<String, Object>();
vars.put("fulfillmentGroup", fulfillmentGroup);
return (Boolean)MVEL.executeExpression(feeExpression, vars);
}
return appliesToFulfillmentGroup;
}
示例5: executeExpression
import org.mvel2.MVEL; //导入方法依赖的package包/类
/**
* Helpful method for processing a boolean MVEL expression and associated arguments.
*
* Caches the expression in an LRUCache.
* @param expression
* @param vars
* @return the result of the expression
*/
protected Boolean executeExpression(String expression, Map<String, Object> vars) {
Serializable exp = (Serializable) expressionCache.get(expression);
vars.put("MVEL", MVEL.class);
if (exp == null) {
try {
exp = MVEL.compileExpression(expression, getParserContext());
} catch (CompileException ce) {
LOG.warn("Compile exception processing phrase: " + expression,ce);
return Boolean.FALSE;
}
expressionCache.put(expression, exp);
}
try {
return (Boolean) MVEL.executeExpression(exp, vars);
} catch (Exception e) {
LOG.error(e);
}
return false;
}
示例6: nextValue
import org.mvel2.MVEL; //导入方法依赖的package包/类
@Override
public Object nextValue(Context context, Field field) {
Class type;
switch (field.getType()) {
case TypeConst.STRING:
type = String.class;
break;
case TypeConst.LONG:
type = Long.class;
break;
case TypeConst.INT:
type = Integer.class;
break;
case TypeConst.BOOLEAN:
type = Boolean.class;
break;
default:
throw new IllegalArgumentException("Field type not known: " + field.getType());
}
synchronized (context.getVariables()) {
return MVEL.executeExpression(compiledExpression, context, context.getVariables(), type);
}
}
示例7: evaluate
import org.mvel2.MVEL; //导入方法依赖的package包/类
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating MVEL expression: {1}", expression);
try {
Object expr = expressionCache.get(expression);
if (expr == null) {
expr = MVEL.compileExpression(expression);
expressionCache.put(expression, expr);
}
return MVEL.executeExpression(expr, values);
} catch (final Exception ex) {
throw new ExpressionEvaluationException("Evaluating MVEL expression failed: " + expression, ex);
}
}
示例8: calculate
import org.mvel2.MVEL; //导入方法依赖的package包/类
public R calculate(Object ctx){
R result = null;
try{
result = MVEL.executeExpression(getValue(), ctx, clz2);
}catch(Exception e){
String message = MessageFormatter
.arrayFormat("无法计算Id[{}], 返回值[{}], 参数[{}]的表达式", new Object[] { id, clz2, ctx}).getMessage();
log.error(message, e);
throw new IllegalArgumentException(message);
}
return result;
}
示例9: calculate
import org.mvel2.MVEL; //导入方法依赖的package包/类
public R calculate(Object context) {
R result = null;
try {
result = MVEL.executeExpression(getValue(), context, clz2);
} catch (Exception e) {
String message = MessageFormatter
.arrayFormat("无法计算Id[{}], 返回值[{}], 参数[{}]的表达式", new Object[] { id, clz2, context }).getMessage();
log.error(message, e);
throw new IllegalArgumentException(message);
}
return result;
}
示例10: main
import org.mvel2.MVEL; //导入方法依赖的package包/类
public static void main(String[] args) {
Serializable express = cache("Math.sqrt(Element)");
Map<String, String> map = new HashMap<String, String>();
map.put("Element", "4");
Number num = MVEL.executeExpression(express, map, Number.class);
System.out.println(num);
}
示例11: testStrongTypingModeComparison
import org.mvel2.MVEL; //导入方法依赖的package包/类
public void testStrongTypingModeComparison() {
ParserContext parserContext = new ParserContext();
parserContext.setStrongTyping(true);
parserContext.addInput("a", Long.class);
CompiledExpression compiledExpression = new ExpressionCompiler("a==0").compile(parserContext);
HashMap<String, Object> variables = new HashMap<String, Object>();
variables.put("a", 0l);
MVEL.executeExpression(compiledExpression, variables);
}
示例12: executeExpression
import org.mvel2.MVEL; //导入方法依赖的package包/类
/**
* Private method used by couldOfferApplyToOrder to execute the MVEL expression in the
* appliesToOrderRules to determine if this offer can be applied.
*
* @param expression
* @param vars
* @return a Boolean object containing the result of executing the MVEL expression
*/
public Boolean executeExpression(String expression, Map<String, Object> vars) {
try {
Serializable exp;
synchronized (EXPRESSION_CACHE) {
exp = (Serializable) EXPRESSION_CACHE.get(expression);
if (exp == null) {
ParserContext context = new ParserContext();
context.addImport("OfferType", OfferType.class);
context.addImport("FulfillmentType", FulfillmentType.class);
context.addImport("MVEL", MVEL.class);
context.addImport("MvelHelper", MvelHelper.class);
// StringBuffer completeExpression = new StringBuffer(functions.toString());
// completeExpression.append(" ").append(expression);
exp = MVEL.compileExpression(expression, context);
EXPRESSION_CACHE.put(expression, exp);
}
}
Object test = MVEL.executeExpression(exp, vars);
return (Boolean) test;
} catch (Exception e) {
//Unable to execute the MVEL expression for some reason
//Return false, but notify about the bad expression through logs
LOG.warn("Unable to parse and/or execute an mvel expression. Reporting to the logs and returning false " +
"for the match expression:" + expression, e);
return false;
}
}
示例13: testMvel
import org.mvel2.MVEL; //导入方法依赖的package包/类
public void testMvel(Bean bean, Serializable mvelExpr1, Serializable mvelExpr2) {
int iterations = 100;
for (int i = 0; i < iterations; i++) {
MVEL.executeExpression(mvelExpr1, bean);
MVEL.executeExpression(mvelExpr2, bean);
}
}
示例14: createPropertyVisibilityHandler
import org.mvel2.MVEL; //导入方法依赖的package包/类
public static PropertyVisibilityHandler createPropertyVisibilityHandler(ModelerFile modelerFile, final IBaseElementWidget baseElementWidget, final Object object, final Serializable exp) {
final IRootElement root = (IRootElement) modelerFile.getModelerScene().getBaseElementSpec();
return (PropertyVisibilityHandler) () -> {
Map vars = new HashMap();
vars.put("root", root);
vars.put("widget", baseElementWidget);
vars.put("_this", object);
vars.put("node", baseElementWidget.getBaseElementSpec());
return (Boolean) MVEL.executeExpression(exp, vars);
};
}
示例15: createPropertyChangeHandler
import org.mvel2.MVEL; //导入方法依赖的package包/类
public static PropertyChangeListener createPropertyChangeHandler(final ModelerFile modelerFile, final IBaseElementWidget baseElementWidget, final Object object, final Serializable exp) {
final IRootElement root = (IRootElement)modelerFile.getModelerScene().getBaseElementSpec();
return (oldValue, value) -> {
Map vars = new HashMap();
vars.put("root", root);
vars.put("widget", baseElementWidget);
vars.put("_this", object);
vars.put("node", baseElementWidget.getBaseElementSpec());
vars.put("value", value);
vars.put("scene", modelerFile.getModelerScene());
MVEL.executeExpression(exp, vars);
};
}