Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 12 additions & 21 deletions codelab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, ?> parameterValues) {
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
try {
CelRuntime.Program program = celRuntime.createProgram(ast);

Expand Down Expand Up @@ -510,7 +510,7 @@ CelAbstractSyntaxTree compile(String expression) {
*/
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
CelRuntimeFactory.plannerRuntimeBuilder()
// Provide the custom `contains` function implementation here.
.build();

Expand Down Expand Up @@ -590,7 +590,7 @@ Provide the function implementation to the runtime using the .addFunctionBinding
*/
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
CelRuntimeFactory.plannerRuntimeBuilder()
.addFunctionBindings(
CelFunctionBinding.from(
"map_contains_key_value",
Expand Down Expand Up @@ -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();
```
Expand Down Expand Up @@ -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`:

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions codelab/src/main/codelab/Exercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/Exercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CelAbstractSyntaxTree compile(String expression) {
*/
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
CelRuntimeFactory.plannerRuntimeBuilder()
// Provide the custom `contains` function implementation here.
.build();

Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/Exercise5.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ CelAbstractSyntaxTree compile(String expression) {
* @throws IllegalArgumentException If the compiled expression in AST fails to evaluate.
*/
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();

try {
CelRuntime.Program program = celRuntime.createProgram(ast);
Expand Down
4 changes: 1 addition & 3 deletions codelab/src/main/codelab/Exercise6.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ CelAbstractSyntaxTree compile(String expression) {
/** Evaluates the compiled AST with the user provided parameter values. */
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addMessageTypes(Request.getDescriptor())
.build();
CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build();

try {
CelRuntime.Program program = celRuntime.createProgram(ast);
Expand Down
4 changes: 1 addition & 3 deletions codelab/src/main/codelab/Exercise7.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ CelAbstractSyntaxTree compile(String expression) {
/** Evaluates the compiled AST with the user provided parameter values. */
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addMessageTypes(Request.getDescriptor())
.build();
CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build();

try {
CelRuntime.Program program = celRuntime.createProgram(ast);
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/Exercise8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/Exercise9.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/solutions/Exercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/solutions/Exercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ?> parameterValues) {
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
try {
CelRuntime.Program program = celRuntime.createProgram(ast);

Expand Down
3 changes: 1 addition & 2 deletions codelab/src/main/codelab/solutions/Exercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/solutions/Exercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CelAbstractSyntaxTree compile(String expression) {
*/
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
CelRuntimeFactory.plannerRuntimeBuilder()
.addFunctionBindings(
CelFunctionBinding.from(
"map_contains_key_value",
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/solutions/Exercise5.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ CelAbstractSyntaxTree compile(String expression) {
* @throws IllegalArgumentException If the compiled expression in AST fails to evaluate.
*/
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();

try {
CelRuntime.Program program = celRuntime.createProgram(ast);
Expand Down
4 changes: 1 addition & 3 deletions codelab/src/main/codelab/solutions/Exercise6.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ CelAbstractSyntaxTree compile(String expression) {
/** Evaluates the compiled AST with the user provided parameter values. */
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addMessageTypes(Request.getDescriptor())
.build();
CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build();

try {
CelRuntime.Program program = celRuntime.createProgram(ast);
Expand Down
4 changes: 1 addition & 3 deletions codelab/src/main/codelab/solutions/Exercise7.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ CelAbstractSyntaxTree compile(String expression) {
/** Evaluates the compiled AST with the user provided parameter values. */
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addMessageTypes(Request.getDescriptor())
.build();
CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build();

try {
CelRuntime.Program program = celRuntime.createProgram(ast);
Expand Down
7 changes: 2 additions & 5 deletions codelab/src/main/codelab/solutions/Exercise8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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();

/**
Expand Down
2 changes: 1 addition & 1 deletion codelab/src/main/codelab/solutions/Exercise9.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Loading