diff --git a/README.md b/README.md index e2424a85c..dbcceb7d8 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ public class HelloWorld { private static final CelCompiler CEL_COMPILER = CelCompilerFactory.standardCelCompilerBuilder().addVar("my_var", SimpleType.STRING).build(); private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntimeFactory.plannerRuntimeBuilder().build(); public void run() throws CelValidationException, CelEvaluationException { // Compile the expression into an Abstract Syntax Tree. @@ -144,7 +144,7 @@ found in the [`CelCompilerBuilder`][9]. Some CEL use cases only require parsing of an expression in order to be useful. For example, one example might want to check whether the expression contains any nested comprehensions, or possibly to pass the parsed expression to a C++ or Go -binary for evaluation. Presently, Java does not support parse-only evaluation. +binary for evaluation. ```java CelValidationResult parseResult = @@ -231,7 +231,7 @@ Expressions can be evaluated using once they are type-checked/compiled by creating a `CelRuntime.Program` from a `CelAbstractSyntaxTree`: ```java -CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); +CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { CelRuntime.Program program = celRuntime.createProgram(compileResult.getAst()); return program.eval( diff --git a/codelab/README.md b/codelab/README.md index 83257d186..00d7f729e 100644 --- a/codelab/README.md +++ b/codelab/README.md @@ -140,7 +140,7 @@ private static final CelCompiler CEL_COMPILER = // CelRuntime can also be initialized statically and cached just like the // compiler. private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .setOptions(CEL_OPTIONS) .build(); ``` @@ -232,7 +232,7 @@ Copy the following into eval method: Object eval(CelAbstractSyntaxTree ast) { // Construct a CelRuntime instance // CelRuntime is immutable just like the compiler and can be moved to a static final member. - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { // Plan the program @@ -314,7 +314,7 @@ Let's make the evaluation work now. Copy into the eval method: * @throws IllegalArgumentException If the compiled expression in AST fails to evaluate. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); @@ -510,7 +510,7 @@ CelAbstractSyntaxTree compile(String expression) { */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() // Provide the custom `contains` function implementation here. .build(); @@ -590,7 +590,7 @@ Provide the function implementation to the runtime using the .addFunctionBinding */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "map_contains_key_value", @@ -1136,7 +1136,7 @@ private static final CelCompiler CEL_COMPILER = .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); ``` @@ -1362,11 +1362,11 @@ public void optimize_commonSubexpressionElimination_success() throws Exception { } ``` -CSE will rewrite this expression using a specialized internal function -`cel.@block`. The first argument contain a list duplicate subexpressions -and the second argument is the rewritten result expression that is semantically -the same as the original expression. The subexpressions are lazily evaluated and -memoized when accessed by index (e.g: `@index0`). +CSE optimizes the expression by rewriting it to use a specialized internal +function `cel.@block`. This function takes a list of duplicate subexpressions +as its first argument, and a semantically equivalent rewritten expression as +its second. The subexpressions are lazily evaluated and memoized when accessed +by index (e.g., `@index0`). Make the following changes in `Exercise8.java`: @@ -1375,19 +1375,10 @@ private static final CelOptimizer CEL_OPTIMIZER = CelOptimizerFactory.standardCelOptimizerBuilder(CEL_COMPILER, CEL_RUNTIME) .addAstOptimizers( ConstantFoldingOptimizer.getInstance(), - SubexpressionOptimizer.newInstance( - SubexpressionOptimizerOptions.newBuilder().enableCelBlock(true).build())) + SubexpressionOptimizer.getInstance()) .build(); ``` -As seen here, the usage of `cel.block` must explicitly be enabled as it is -only supported in CEL-Java as of now. Disabling `cel.block` will instead rewrite -the AST using cascaded `cel.bind` macros. Prefer using the block format if -possible as it is a more efficient format for evaluation. - -> [!CAUTION] -> You MUST disable `cel.block` if you are targeting `cel-go` or `cel-cpp` for the runtime until its support has been added in those stacks. - Re-run the tests to confirm that they pass. ## Custom AST Validation diff --git a/codelab/src/main/codelab/Exercise3.java b/codelab/src/main/codelab/Exercise3.java index 77b57f339..1745920f8 100644 --- a/codelab/src/main/codelab/Exercise3.java +++ b/codelab/src/main/codelab/Exercise3.java @@ -27,8 +27,7 @@ final class Exercise3 { private static final CelCompiler CEL_COMPILER = CelCompilerFactory.standardCelCompilerBuilder().setResultType(SimpleType.BOOL).build(); - private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder().build(); + private static final CelRuntime CEL_RUNTIME = CelRuntimeFactory.plannerRuntimeBuilder().build(); /** * Compiles the given expression and evaluates it. diff --git a/codelab/src/main/codelab/Exercise4.java b/codelab/src/main/codelab/Exercise4.java index df4d3ab1c..402255152 100644 --- a/codelab/src/main/codelab/Exercise4.java +++ b/codelab/src/main/codelab/Exercise4.java @@ -63,7 +63,7 @@ CelAbstractSyntaxTree compile(String expression) { */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() // Provide the custom `contains` function implementation here. .build(); diff --git a/codelab/src/main/codelab/Exercise5.java b/codelab/src/main/codelab/Exercise5.java index eca2a5df7..00a64e261 100644 --- a/codelab/src/main/codelab/Exercise5.java +++ b/codelab/src/main/codelab/Exercise5.java @@ -55,7 +55,7 @@ CelAbstractSyntaxTree compile(String expression) { * @throws IllegalArgumentException If the compiled expression in AST fails to evaluate. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/Exercise6.java b/codelab/src/main/codelab/Exercise6.java index 9991fb566..71d8e09f4 100644 --- a/codelab/src/main/codelab/Exercise6.java +++ b/codelab/src/main/codelab/Exercise6.java @@ -59,9 +59,7 @@ CelAbstractSyntaxTree compile(String expression) { /** Evaluates the compiled AST with the user provided parameter values. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(Request.getDescriptor()) - .build(); + CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/Exercise7.java b/codelab/src/main/codelab/Exercise7.java index ce2efd88e..2d2fa2c3a 100644 --- a/codelab/src/main/codelab/Exercise7.java +++ b/codelab/src/main/codelab/Exercise7.java @@ -58,9 +58,7 @@ CelAbstractSyntaxTree compile(String expression) { /** Evaluates the compiled AST with the user provided parameter values. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(Request.getDescriptor()) - .build(); + CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/Exercise8.java b/codelab/src/main/codelab/Exercise8.java index d38854687..107e95038 100644 --- a/codelab/src/main/codelab/Exercise8.java +++ b/codelab/src/main/codelab/Exercise8.java @@ -40,7 +40,7 @@ final class Exercise8 { .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); diff --git a/codelab/src/main/codelab/Exercise9.java b/codelab/src/main/codelab/Exercise9.java index 85705390c..8129800cb 100644 --- a/codelab/src/main/codelab/Exercise9.java +++ b/codelab/src/main/codelab/Exercise9.java @@ -55,7 +55,7 @@ final class Exercise9 { .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelValidator CEL_VALIDATOR = diff --git a/codelab/src/main/codelab/solutions/Exercise1.java b/codelab/src/main/codelab/solutions/Exercise1.java index 0807a1931..e1b3b2269 100644 --- a/codelab/src/main/codelab/solutions/Exercise1.java +++ b/codelab/src/main/codelab/solutions/Exercise1.java @@ -73,7 +73,7 @@ CelAbstractSyntaxTree compile(String expression) { Object eval(CelAbstractSyntaxTree ast) { // Construct a CelRuntime instance // CelRuntime is immutable just like the compiler and can be moved to a static final member. - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { // Plan the program diff --git a/codelab/src/main/codelab/solutions/Exercise2.java b/codelab/src/main/codelab/solutions/Exercise2.java index 5a1c1e8cc..4525a6f60 100644 --- a/codelab/src/main/codelab/solutions/Exercise2.java +++ b/codelab/src/main/codelab/solutions/Exercise2.java @@ -66,7 +66,7 @@ CelAbstractSyntaxTree compile(String expression, String variableName, CelType va * @throws IllegalArgumentException If the compiled expression in AST fails to evaluate. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/solutions/Exercise3.java b/codelab/src/main/codelab/solutions/Exercise3.java index 590dfd0df..80c9a8beb 100644 --- a/codelab/src/main/codelab/solutions/Exercise3.java +++ b/codelab/src/main/codelab/solutions/Exercise3.java @@ -27,8 +27,7 @@ final class Exercise3 { private static final CelCompiler CEL_COMPILER = CelCompilerFactory.standardCelCompilerBuilder().setResultType(SimpleType.BOOL).build(); - private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder().build(); + private static final CelRuntime CEL_RUNTIME = CelRuntimeFactory.plannerRuntimeBuilder().build(); /** * Compiles the given expression and evaluates it. diff --git a/codelab/src/main/codelab/solutions/Exercise4.java b/codelab/src/main/codelab/solutions/Exercise4.java index b3cc82a24..129a9d7b5 100644 --- a/codelab/src/main/codelab/solutions/Exercise4.java +++ b/codelab/src/main/codelab/solutions/Exercise4.java @@ -81,7 +81,7 @@ CelAbstractSyntaxTree compile(String expression) { */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "map_contains_key_value", diff --git a/codelab/src/main/codelab/solutions/Exercise5.java b/codelab/src/main/codelab/solutions/Exercise5.java index e948adfed..8206efa33 100644 --- a/codelab/src/main/codelab/solutions/Exercise5.java +++ b/codelab/src/main/codelab/solutions/Exercise5.java @@ -60,7 +60,7 @@ CelAbstractSyntaxTree compile(String expression) { * @throws IllegalArgumentException If the compiled expression in AST fails to evaluate. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/solutions/Exercise6.java b/codelab/src/main/codelab/solutions/Exercise6.java index 9b6c59949..dba84291b 100644 --- a/codelab/src/main/codelab/solutions/Exercise6.java +++ b/codelab/src/main/codelab/solutions/Exercise6.java @@ -62,9 +62,7 @@ CelAbstractSyntaxTree compile(String expression) { /** Evaluates the compiled AST with the user provided parameter values. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(Request.getDescriptor()) - .build(); + CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/solutions/Exercise7.java b/codelab/src/main/codelab/solutions/Exercise7.java index e5be29171..e45002f71 100644 --- a/codelab/src/main/codelab/solutions/Exercise7.java +++ b/codelab/src/main/codelab/solutions/Exercise7.java @@ -60,9 +60,7 @@ CelAbstractSyntaxTree compile(String expression) { /** Evaluates the compiled AST with the user provided parameter values. */ Object eval(CelAbstractSyntaxTree ast, Map parameterValues) { CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(Request.getDescriptor()) - .build(); + CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build(); try { CelRuntime.Program program = celRuntime.createProgram(ast); diff --git a/codelab/src/main/codelab/solutions/Exercise8.java b/codelab/src/main/codelab/solutions/Exercise8.java index 161089354..f23bb7aa8 100644 --- a/codelab/src/main/codelab/solutions/Exercise8.java +++ b/codelab/src/main/codelab/solutions/Exercise8.java @@ -27,7 +27,6 @@ import dev.cel.optimizer.CelOptimizerFactory; import dev.cel.optimizer.optimizers.ConstantFoldingOptimizer; import dev.cel.optimizer.optimizers.SubexpressionOptimizer; -import dev.cel.optimizer.optimizers.SubexpressionOptimizer.SubexpressionOptimizerOptions; import dev.cel.runtime.CelEvaluationException; import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelRuntimeFactory; @@ -52,7 +51,7 @@ final class Exercise8 { .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); @@ -69,9 +68,7 @@ final class Exercise8 { private static final CelOptimizer CEL_OPTIMIZER = CelOptimizerFactory.standardCelOptimizerBuilder(CEL_COMPILER, CEL_RUNTIME) .addAstOptimizers( - ConstantFoldingOptimizer.getInstance(), - SubexpressionOptimizer.newInstance( - SubexpressionOptimizerOptions.newBuilder().enableCelBlock(true).build())) + ConstantFoldingOptimizer.getInstance(), SubexpressionOptimizer.getInstance()) .build(); /** diff --git a/codelab/src/main/codelab/solutions/Exercise9.java b/codelab/src/main/codelab/solutions/Exercise9.java index 2b45c3539..7ea1c1a52 100644 --- a/codelab/src/main/codelab/solutions/Exercise9.java +++ b/codelab/src/main/codelab/solutions/Exercise9.java @@ -62,7 +62,7 @@ final class Exercise9 { .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); private static final CelValidator CEL_VALIDATOR =