An expression engine turns condition text into a result. Nutz EL can be understood by following token recognition, precedence handling and evaluation rather than memorizing package names. These examples use 1.b.53.

Start with an alert condition

import org.nutz.el.El;
import org.nutz.lang.Lang;
import org.nutz.lang.util.Context;

Context values = Lang.context();
values.set("a", 85);
values.set("b", 20);
Object result = El.eval(values, "a > 80 && a - b > 50");

This is a calling fragment. Check result against the rule’s expected type rather than casting unconditionally to Boolean. Establish behavior with missing variables and different numeric types too.

Follow precedence into evaluation

org.nutz.el.parse handles expression tokens, obj represents operands and opt represents operations. In the arithmetic package, ShuntingYard and RPN correspond to handling infix expressions and evaluating reverse Polish form.

For example, 1 + 2 * 3 must not simply execute left to right. Precedence requires multiplication before addition; parentheses change grouping. Start with small expressions without variables, then add context variables and comparisons to see the boundary between transformation and evaluation.

Validate configurable rules independently

Define division-by-zero, null, integer precision and return-type behavior. Record false results separately from evaluation errors and retain rule versions and sample inputs so decisions remain explainable.

The application and implementation version must explicitly restrict callable capabilities. The name EL does not guarantee isolation from files, networking or arbitrary methods. Limit provenance, length and execution resources before accepting external expressions. Test even trusted rules before activation.

References


blog comments powered by Disqus
Fork me on GitHub