Skip to content

Instantly share code, notes, and snippets.

@gowrishankarin
Created August 14, 2014 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gowrishankarin/390e53cc64d934330bb4 to your computer and use it in GitHub Desktop.
Save gowrishankarin/390e53cc64d934330bb4 to your computer and use it in GitHub Desktop.
MVEL Expression Samples
Map vars = new HashMap();
vars.put("foobar", new Integer(120));
Boolean result = (Boolean) MVEL.eval("foobar > 99", vars);
if (result.booleanValue()) {
System.out.println("Hurrah");
}
result = (Boolean) MVEL.eval("foobar > 99 && foobar < 110", vars);
if (result.booleanValue()) {
System.out.println("Hurrah AND Operator");
}
vars.put("foobar", new Integer(70));
result = (Boolean) MVEL.eval("foobar < 0.8 * 100 ", vars);
if (result.booleanValue()) {
System.out.println("Hurrah less than 80% threshold");
}
vars.put("foobar", new Integer(70));
result = (Boolean) MVEL.eval("foobar == 100 ", vars);
if (result.booleanValue()) {
System.out.println("Hurrah less than 80% threshold");
}
/* Multi Variable */
vars.put("foobar", new Integer(120));
vars.put("rhs1", new Integer(99));
result = (Boolean) MVEL.eval("foobar > rhs1", vars);
if (result.booleanValue()) {
System.out.println("Hurrah");
}
vars.put("rhs2", new Integer(110));
result = (Boolean) MVEL.eval("foobar > rhs1 && foobar < rhs2", vars);
if (result.booleanValue()) {
System.out.println("Hurrah AND Operator");
}
vars.put("foobar", new Integer(70));
vars.put("rhs1", new Float(0.8));
vars.put("rhs2", new Integer(100));
result = (Boolean) MVEL.eval("foobar < rhs1 * rhs2 ", vars);
if (result.booleanValue()) {
System.out.println("Hurrah less than 80% threshold");
}
vars.put("foobar", new Integer(70));
vars.put("rhs1", new Integer(100));
result = (Boolean) MVEL.eval("foobar == rhs1 ", vars);
if (result.booleanValue()) {
System.out.println("Hurrah they are equal");
}
ParserContext pctx = new ParserContext();
pctx.stronglyTyped();
pctx.withInput("contentinstance", ContentInstance.class);
MVEL.compileExpression("content = contentinstance.content", pctx);
MVEL.compileExpression("contentInstanceId = contentinstance.contentInstanceId", pctx);
MVEL.compileExpression("creationTime = contentinstance.creationTime", pctx);
Map<String, Class> inputs = pctx.getVariables();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment