Skip to content

Commit e714d2a

Browse files
l46kokcopybara-github
authored andcommitted
Update documentation and CEL-Java codelabs to use planner
PiperOrigin-RevId: 940712133
1 parent 0b2bf66 commit e714d2a

18 files changed

Lines changed: 32 additions & 54 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class HelloWorld {
8484
private static final CelCompiler CEL_COMPILER =
8585
CelCompilerFactory.standardCelCompilerBuilder().addVar("my_var", SimpleType.STRING).build();
8686
private static final CelRuntime CEL_RUNTIME =
87-
CelRuntimeFactory.standardCelRuntimeBuilder().build();
87+
CelRuntimeFactory.plannerRuntimeBuilder().build();
8888

8989
public void run() throws CelValidationException, CelEvaluationException {
9090
// Compile the expression into an Abstract Syntax Tree.
@@ -144,7 +144,7 @@ found in the [`CelCompilerBuilder`][9].
144144
Some CEL use cases only require parsing of an expression in order to be useful.
145145
For example, one example might want to check whether the expression contains any
146146
nested comprehensions, or possibly to pass the parsed expression to a C++ or Go
147-
binary for evaluation. Presently, Java does not support parse-only evaluation.
147+
binary for evaluation.
148148

149149
```java
150150
CelValidationResult parseResult =
@@ -231,7 +231,7 @@ Expressions can be evaluated using once they are type-checked/compiled by
231231
creating a `CelRuntime.Program` from a `CelAbstractSyntaxTree`:
232232

233233
```java
234-
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
234+
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
235235
try {
236236
CelRuntime.Program program = celRuntime.createProgram(compileResult.getAst());
237237
return program.eval(

codelab/README.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private static final CelCompiler CEL_COMPILER =
140140
// CelRuntime can also be initialized statically and cached just like the
141141
// compiler.
142142
private static final CelRuntime CEL_RUNTIME =
143-
CelRuntimeFactory.standardCelRuntimeBuilder()
143+
CelRuntimeFactory.plannerRuntimeBuilder()
144144
.setOptions(CEL_OPTIONS)
145145
.build();
146146
```
@@ -232,7 +232,7 @@ Copy the following into eval method:
232232
Object eval(CelAbstractSyntaxTree ast) {
233233
// Construct a CelRuntime instance
234234
// CelRuntime is immutable just like the compiler and can be moved to a static final member.
235-
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
235+
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
236236

237237
try {
238238
// Plan the program
@@ -314,7 +314,7 @@ Let's make the evaluation work now. Copy into the eval method:
314314
* @throws IllegalArgumentException If the compiled expression in AST fails to evaluate.
315315
*/
316316
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
317-
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
317+
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
318318
try {
319319
CelRuntime.Program program = celRuntime.createProgram(ast);
320320

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

@@ -590,7 +590,7 @@ Provide the function implementation to the runtime using the .addFunctionBinding
590590
*/
591591
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
592592
CelRuntime celRuntime =
593-
CelRuntimeFactory.standardCelRuntimeBuilder()
593+
CelRuntimeFactory.plannerRuntimeBuilder()
594594
.addFunctionBindings(
595595
CelFunctionBinding.from(
596596
"map_contains_key_value",
@@ -1136,7 +1136,7 @@ private static final CelCompiler CEL_COMPILER =
11361136
.addMessageTypes(AttributeContext.Request.getDescriptor())
11371137
.build();
11381138
private static final CelRuntime CEL_RUNTIME =
1139-
CelRuntimeFactory.standardCelRuntimeBuilder()
1139+
CelRuntimeFactory.plannerRuntimeBuilder()
11401140
.addMessageTypes(AttributeContext.Request.getDescriptor())
11411141
.build();
11421142
```
@@ -1362,11 +1362,11 @@ public void optimize_commonSubexpressionElimination_success() throws Exception {
13621362
}
13631363
```
13641364

1365-
CSE will rewrite this expression using a specialized internal function
1366-
`cel.@block`. The first argument contain a list duplicate subexpressions
1367-
and the second argument is the rewritten result expression that is semantically
1368-
the same as the original expression. The subexpressions are lazily evaluated and
1369-
memoized when accessed by index (e.g: `@index0`).
1365+
CSE optimizes the expression by rewriting it to use a specialized internal
1366+
function `cel.@block`. This function takes a list of duplicate subexpressions
1367+
as its first argument, and a semantically equivalent rewritten expression as
1368+
its second. The subexpressions are lazily evaluated and memoized when accessed
1369+
by index (e.g., `@index0`).
13701370

13711371
Make the following changes in `Exercise8.java`:
13721372

@@ -1375,19 +1375,10 @@ private static final CelOptimizer CEL_OPTIMIZER =
13751375
CelOptimizerFactory.standardCelOptimizerBuilder(CEL_COMPILER, CEL_RUNTIME)
13761376
.addAstOptimizers(
13771377
ConstantFoldingOptimizer.getInstance(),
1378-
SubexpressionOptimizer.newInstance(
1379-
SubexpressionOptimizerOptions.newBuilder().enableCelBlock(true).build()))
1378+
SubexpressionOptimizer.getInstance())
13801379
.build();
13811380
```
13821381

1383-
As seen here, the usage of `cel.block` must explicitly be enabled as it is
1384-
only supported in CEL-Java as of now. Disabling `cel.block` will instead rewrite
1385-
the AST using cascaded `cel.bind` macros. Prefer using the block format if
1386-
possible as it is a more efficient format for evaluation.
1387-
1388-
> [!CAUTION]
1389-
> 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.
1390-
13911382
Re-run the tests to confirm that they pass.
13921383

13931384
## Custom AST Validation

codelab/src/main/codelab/Exercise3.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ final class Exercise3 {
2727

2828
private static final CelCompiler CEL_COMPILER =
2929
CelCompilerFactory.standardCelCompilerBuilder().setResultType(SimpleType.BOOL).build();
30-
private static final CelRuntime CEL_RUNTIME =
31-
CelRuntimeFactory.standardCelRuntimeBuilder().build();
30+
private static final CelRuntime CEL_RUNTIME = CelRuntimeFactory.plannerRuntimeBuilder().build();
3231

3332
/**
3433
* Compiles the given expression and evaluates it.

codelab/src/main/codelab/Exercise4.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ CelAbstractSyntaxTree compile(String expression) {
6363
*/
6464
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
6565
CelRuntime celRuntime =
66-
CelRuntimeFactory.standardCelRuntimeBuilder()
66+
CelRuntimeFactory.plannerRuntimeBuilder()
6767
// Provide the custom `contains` function implementation here.
6868
.build();
6969

codelab/src/main/codelab/Exercise5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ CelAbstractSyntaxTree compile(String expression) {
5555
* @throws IllegalArgumentException If the compiled expression in AST fails to evaluate.
5656
*/
5757
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
58-
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
58+
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
5959

6060
try {
6161
CelRuntime.Program program = celRuntime.createProgram(ast);

codelab/src/main/codelab/Exercise6.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ CelAbstractSyntaxTree compile(String expression) {
5959
/** Evaluates the compiled AST with the user provided parameter values. */
6060
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
6161
CelRuntime celRuntime =
62-
CelRuntimeFactory.standardCelRuntimeBuilder()
63-
.addMessageTypes(Request.getDescriptor())
64-
.build();
62+
CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build();
6563

6664
try {
6765
CelRuntime.Program program = celRuntime.createProgram(ast);

codelab/src/main/codelab/Exercise7.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ CelAbstractSyntaxTree compile(String expression) {
5858
/** Evaluates the compiled AST with the user provided parameter values. */
5959
Object eval(CelAbstractSyntaxTree ast, Map<String, ?> parameterValues) {
6060
CelRuntime celRuntime =
61-
CelRuntimeFactory.standardCelRuntimeBuilder()
62-
.addMessageTypes(Request.getDescriptor())
63-
.build();
61+
CelRuntimeFactory.plannerRuntimeBuilder().addMessageTypes(Request.getDescriptor()).build();
6462

6563
try {
6664
CelRuntime.Program program = celRuntime.createProgram(ast);

codelab/src/main/codelab/Exercise8.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class Exercise8 {
4040
.addMessageTypes(AttributeContext.Request.getDescriptor())
4141
.build();
4242
private static final CelRuntime CEL_RUNTIME =
43-
CelRuntimeFactory.standardCelRuntimeBuilder()
43+
CelRuntimeFactory.plannerRuntimeBuilder()
4444
.addMessageTypes(AttributeContext.Request.getDescriptor())
4545
.build();
4646

codelab/src/main/codelab/Exercise9.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ final class Exercise9 {
5555
.addMessageTypes(AttributeContext.Request.getDescriptor())
5656
.build();
5757
private static final CelRuntime CEL_RUNTIME =
58-
CelRuntimeFactory.standardCelRuntimeBuilder()
58+
CelRuntimeFactory.plannerRuntimeBuilder()
5959
.addMessageTypes(AttributeContext.Request.getDescriptor())
6060
.build();
6161
private static final CelValidator CEL_VALIDATOR =

codelab/src/main/codelab/solutions/Exercise1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CelAbstractSyntaxTree compile(String expression) {
7373
Object eval(CelAbstractSyntaxTree ast) {
7474
// Construct a CelRuntime instance
7575
// CelRuntime is immutable just like the compiler and can be moved to a static final member.
76-
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
76+
CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build();
7777

7878
try {
7979
// Plan the program

0 commit comments

Comments
 (0)