Class CEL

java.lang.Object
com.libdbm.cel.CEL

public class CEL extends Object
The main entry point for evaluating CEL expressions.

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 Details

    • CEL

      public CEL()
      Creates a new CEL evaluator with the standard function library.
    • CEL

      public CEL(Functions functions)
      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

      public static Program compile(String expression, Functions functions)
      Compiles a CEL expression using the provided function library.

      This static convenience method avoids the need to instantiate CEL when callers just need to compile once.

      Parameters:
      expression - The CEL expression to compile
      functions - The function library to use when compiling the program
      Returns:
      A compiled Program using the provided functions
      Throws:
      ParseError - if the expression is invalid
    • eval

      public static Object eval(String expression, Functions functions, Map<String,Object> variables)
      Evaluates a CEL expression with the given variables using the provided function library.

      This static convenience method avoids the need to instantiate CEL when callers just need to evaluate an expression once.

      Parameters:
      expression - The CEL expression to evaluate
      functions - The function library to use when evaluating the expression
      variables - A map of variable names to their values
      Returns:
      The result of evaluating the expression
      Throws:
      ParseError - if the expression is invalid
      com.libdbm.cel.EvaluationError - if an error occurs during evaluation
    • compile

      public Program compile(String expression)
      Compiles a CEL expression into a reusable program.

      This method parses the expression and returns a Program that can be evaluated multiple times with different variables. This is more efficient than calling eval(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 invalid

      Example:

      
       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

      public Object eval(String expression, Map<String,Object> variables)
      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 reusable Program.

      Parameters:
      expression - The CEL expression to evaluate
      variables - A map of variable names to their values
      Returns:
      The result of evaluating the expression
      Throws:
      ParseError - if the expression is invalid
      com.libdbm.cel.EvaluationError - if an error occurs during evaluation

      Example:

      
       final Object result = cel.eval("user.age >= 18", Map.of(
           "user", Map.of("name", "Alice", "age", 25)
       ));