Skip to content

Instantly share code, notes, and snippets.

@greenlaw110
Last active December 17, 2015 21:19
Show Gist options
  • Save greenlaw110/5674283 to your computer and use it in GitHub Desktop.
Save greenlaw110/5674283 to your computer and use it in GitHub Desktop.
rythm dynamic expression evaluation
private Map<String, com.greenpineyu.fel.Expression> fels = new HashMap<String, Expression>();
private FelEngine fel = new FelEngineImpl();
private Expression getFel(String expr, Map<String, Object> context) {
StringBuilder sb = new StringBuilder(expr);
for (Object s : context.keySet()) {
Object o = context.get(s);
sb.append("|").append(s).append(":");
if (null == o) sb.append("null");
else sb.append(o.getClass());
}
String k = sb.toString();
Expression e = fels.get(k);
if (null == e) {
FelEngine fel = new FelEngineImpl();
FelContext ctx = fel.getContext();
for (String s : context.keySet()) {
ctx.set(s, context.get(s));
}
e = fel.compile(expr, ctx);
fels.put(k, e);
}
return e;
}
public Object eval(String script, Map<String, Object> context) {
//return MVEL.eval(script, context);
FelEngine fel = new FelEngineImpl();
FelContext ctx = fel.getContext();
for (String s : context.keySet()) {
ctx.set(s, context.get(s));
}
return getFel(script, context).eval(ctx);
}
private Map<String, Serializable> mvels = new HashMap<String, Serializable>();
public Object eval(String script, Map<String, Object> context) {
Serializable ce = mvels.get(script);
if (null == ce) {
ce = MVEL.compileExpression(script);
mvels.put(script, ce);
}
return MVEL.executeExpression(ce, context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment