Class CEL
The CEL class provides methods to compile and evaluate CEL expressions. It supports
all standard CEL operators, functions, and macros.
Example:
final CEL cel = new CEL();
final Object result = cel.eval("x * 2 + y", Map.of("x", 10, "y", 5));
System.out.println(result); // 25
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCompiles a CEL expression into a reusable program.static ProgramCompiles a CEL expression using the provided function library.static ObjectEvaluates a CEL expression with the given variables using the provided function library.Evaluates a CEL expression with the given variables.
-
Constructor Details
-
CEL
public CEL()Creates a new CEL evaluator with the standard function library. -
CEL
Creates a new CEL evaluator.- Parameters:
functions- Optional custom function library. If not provided, the standard CEL function library will be used.
-
-
Method Details
-
compile
Compiles a CEL expression using the provided function library.This static convenience method avoids the need to instantiate
CELwhen callers just need to compile once.- Parameters:
expression- The CEL expression to compilefunctions- The function library to use when compiling the program- Returns:
- A compiled
Programusing the provided functions - Throws:
ParseError- if the expression is invalid
-
eval
Evaluates a CEL expression with the given variables using the provided function library.This static convenience method avoids the need to instantiate
CELwhen callers just need to evaluate an expression once.- Parameters:
expression- The CEL expression to evaluatefunctions- The function library to use when evaluating the expressionvariables- A map of variable names to their values- Returns:
- The result of evaluating the expression
- Throws:
ParseError- if the expression is invalidcom.libdbm.cel.EvaluationError- if an error occurs during evaluation
-
compile
Compiles a CEL expression into a reusable program.This method parses the expression and returns a
Programthat can be evaluated multiple times with different variables. This is more efficient than callingeval(java.lang.String, com.libdbm.cel.Functions, java.util.Map<java.lang.String, java.lang.Object>)repeatedly with the same expression.- Parameters:
expression- The CEL expression to compile- Returns:
- A compiled program
- Throws:
ParseError- if the expression is invalidExample:
final Program program = cel.compile("price * quantity"); final Object result1 = program.evaluate(Map.of("price", 10, "quantity", 5)); final Object result2 = program.evaluate(Map.of("price", 20, "quantity", 3));
-
eval
Evaluates a CEL expression with the given variables.This is a convenience method that compiles and evaluates the expression in one step. For better performance when evaluating the same expression multiple times, use
compile(java.lang.String, com.libdbm.cel.Functions)to create a reusableProgram.- Parameters:
expression- The CEL expression to evaluatevariables- A map of variable names to their values- Returns:
- The result of evaluating the expression
- Throws:
ParseError- if the expression is invalidcom.libdbm.cel.EvaluationError- if an error occurs during evaluationExample:
final Object result = cel.eval("user.age >= 18", Map.of( "user", Map.of("name", "Alice", "age", 25) ));
-