Skip to content

Commit bbe5ed4

Browse files
l46kokcopybara-github
authored andcommitted
Rename MutableAst to AstMutator
PiperOrigin-RevId: 623249439
1 parent 432a954 commit bbe5ed4

File tree

6 files changed

+57
-56
lines changed

6 files changed

+57
-56
lines changed

optimizer/src/main/java/dev/cel/optimizer/MutableAst.java renamed to optimizer/src/main/java/dev/cel/optimizer/AstMutator.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
import java.util.function.Predicate;
5252
import java.util.stream.Collectors;
5353

54-
/** MutableAst contains logic for mutating a {@link CelAbstractSyntaxTree}. */
54+
/** AstMutator contains logic for mutating a {@link CelAbstractSyntaxTree}. */
5555
@Immutable
56-
public final class MutableAst {
56+
public final class AstMutator {
5757
private static final ExprIdGenerator NO_OP_ID_GENERATOR = id -> id;
5858
private final long iterationLimit;
5959

6060
/**
61-
* Returns a new instance of a Mutable AST with the iteration limit set.
61+
* Returns a new instance of a AST Mutator with the iteration limit set.
6262
*
6363
* <p>Mutation is performed by walking the existing AST until the expression node to replace is
6464
* found, then the new subtree is walked to complete the mutation. Visiting of each node
@@ -67,11 +67,11 @@ public final class MutableAst {
6767
*
6868
* @param iterationLimit Must be greater than 0.
6969
*/
70-
public static MutableAst newInstance(long iterationLimit) {
71-
return new MutableAst(iterationLimit);
70+
public static AstMutator newInstance(long iterationLimit) {
71+
return new AstMutator(iterationLimit);
7272
}
7373

74-
private MutableAst(long iterationLimit) {
74+
private AstMutator(long iterationLimit) {
7575
Preconditions.checkState(iterationLimit > 0L);
7676
this.iterationLimit = iterationLimit;
7777
}
@@ -773,15 +773,15 @@ private CelExpr.Builder mutateExpr(
773773
CelExpr.Builder root,
774774
CelExpr.Builder newExpr,
775775
long exprIdToReplace) {
776-
MutableExprVisitor mutableAst =
776+
MutableExprVisitor astMutator =
777777
MutableExprVisitor.newInstance(idGenerator, newExpr, exprIdToReplace, iterationLimit);
778-
return mutableAst.visit(root);
778+
return astMutator.visit(root);
779779
}
780780

781781
private CelExpr.Builder renumberExprIds(ExprIdGenerator idGenerator, CelExpr.Builder root) {
782-
MutableExprVisitor mutableAst =
782+
MutableExprVisitor astMutator =
783783
MutableExprVisitor.newInstance(idGenerator, root, Integer.MIN_VALUE, iterationLimit);
784-
return mutableAst.visit(root);
784+
return astMutator.visit(root);
785785
}
786786

787787
private static long getMaxId(CelAbstractSyntaxTree ast) {
@@ -831,7 +831,7 @@ public abstract static class MangledComprehensionAst {
831831
private static MangledComprehensionAst of(
832832
CelAbstractSyntaxTree ast,
833833
ImmutableMap<MangledComprehensionName, MangledComprehensionType> mangledComprehensionMap) {
834-
return new AutoValue_MutableAst_MangledComprehensionAst(ast, mangledComprehensionMap);
834+
return new AutoValue_AstMutator_MangledComprehensionAst(ast, mangledComprehensionMap);
835835
}
836836
}
837837

@@ -851,7 +851,7 @@ public abstract static class MangledComprehensionType {
851851
private static MangledComprehensionType of(
852852
Optional<CelType> iterVarType, Optional<CelType> resultType) {
853853
Preconditions.checkArgument(iterVarType.isPresent() || resultType.isPresent());
854-
return new AutoValue_MutableAst_MangledComprehensionType(iterVarType, resultType);
854+
return new AutoValue_AstMutator_MangledComprehensionType(iterVarType, resultType);
855855
}
856856
}
857857

@@ -869,7 +869,7 @@ public abstract static class MangledComprehensionName {
869869
public abstract String resultName();
870870

871871
private static MangledComprehensionName of(String iterVarName, String resultName) {
872-
return new AutoValue_MutableAst_MangledComprehensionName(iterVarName, resultName);
872+
return new AutoValue_AstMutator_MangledComprehensionName(iterVarName, resultName);
873873
}
874874
}
875875

@@ -889,7 +889,7 @@ abstract static class BindMacro {
889889
abstract CelExpr bindMacro();
890890

891891
private static BindMacro of(CelExpr bindExpr, CelExpr bindMacro) {
892-
return new AutoValue_MutableAst_BindMacro(bindExpr, bindMacro);
892+
return new AutoValue_AstMutator_BindMacro(bindExpr, bindMacro);
893893
}
894894
}
895895
}

optimizer/src/main/java/dev/cel/optimizer/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ java_library(
7979
java_library(
8080
name = "mutable_ast",
8181
srcs = [
82-
"MutableAst.java",
82+
"AstMutator.java",
8383
"MutableExprVisitor.java",
8484
],
8585
tags = [

optimizer/src/main/java/dev/cel/optimizer/optimizers/ConstantFoldingOptimizer.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import dev.cel.common.navigation.CelNavigableAst;
3333
import dev.cel.common.navigation.CelNavigableExpr;
3434
import dev.cel.extensions.CelOptionalLibrary.Function;
35+
import dev.cel.optimizer.AstMutator;
3536
import dev.cel.optimizer.CelAstOptimizer;
3637
import dev.cel.optimizer.CelOptimizationException;
37-
import dev.cel.optimizer.MutableAst;
3838
import dev.cel.parser.Operator;
3939
import dev.cel.runtime.CelEvaluationException;
4040
import java.util.Collection;
@@ -73,7 +73,7 @@ public static ConstantFoldingOptimizer newInstance(
7373
0, Optional.empty(), Function.OPTIONAL_NONE.getFunction(), ImmutableList.of());
7474

7575
private final ConstantFoldingOptions constantFoldingOptions;
76-
private final MutableAst mutableAst;
76+
private final AstMutator astMutator;
7777

7878
@Override
7979
public OptimizationResult optimize(CelNavigableAst navigableAst, Cel cel)
@@ -117,7 +117,7 @@ public OptimizationResult optimize(CelNavigableAst navigableAst, Cel cel)
117117
// If the output is a list, map, or struct which contains optional entries, then prune it
118118
// to make sure that the optionals, if resolved, do not surface in the output literal.
119119
CelAbstractSyntaxTree newAst = pruneOptionalElements(navigableAst);
120-
return OptimizationResult.create(mutableAst.renumberIdsConsecutively(newAst));
120+
return OptimizationResult.create(astMutator.renumberIdsConsecutively(newAst));
121121
}
122122

123123
private static boolean canFold(CelNavigableExpr navigableExpr) {
@@ -236,7 +236,7 @@ private Optional<CelAbstractSyntaxTree> maybeFold(
236236
}
237237

238238
return maybeAdaptEvaluatedResult(result)
239-
.map(celExpr -> mutableAst.replaceSubtree(ast, celExpr, node.id()));
239+
.map(celExpr -> astMutator.replaceSubtree(ast, celExpr, node.id()));
240240
}
241241

242242
private Optional<CelExpr> maybeAdaptEvaluatedResult(Object result) {
@@ -289,7 +289,7 @@ private Optional<CelAbstractSyntaxTree> maybeRewriteOptional(
289289
// An empty optional value was encountered. Rewrite the tree with optional.none call.
290290
// This is to account for other optional functions returning an empty optional value
291291
// e.g: optional.ofNonZeroValue(0)
292-
return Optional.of(mutableAst.replaceSubtree(ast, OPTIONAL_NONE_EXPR, expr.id()));
292+
return Optional.of(astMutator.replaceSubtree(ast, OPTIONAL_NONE_EXPR, expr.id()));
293293
}
294294
} else if (!expr.callOrDefault().function().equals(Function.OPTIONAL_OF.getFunction())) {
295295
Object unwrappedResult = optResult.get();
@@ -309,7 +309,7 @@ private Optional<CelAbstractSyntaxTree> maybeRewriteOptional(
309309
.build())
310310
.build())
311311
.build();
312-
return Optional.of(mutableAst.replaceSubtree(ast, newOptionalOfCall, expr.id()));
312+
return Optional.of(astMutator.replaceSubtree(ast, newOptionalOfCall, expr.id()));
313313
}
314314

315315
return Optional.empty();
@@ -338,7 +338,7 @@ private Optional<CelAbstractSyntaxTree> maybePruneBranches(
338338
}
339339
CelExpr result = cond.constant().booleanValue() ? truthy : falsy;
340340

341-
return Optional.of(mutableAst.replaceSubtree(ast, result, expr.id()));
341+
return Optional.of(astMutator.replaceSubtree(ast, result, expr.id()));
342342
} else if (function.equals(Operator.IN.getFunction())) {
343343
CelExpr callArg = call.args().get(1);
344344
if (!callArg.exprKind().getKind().equals(Kind.CREATE_LIST)) {
@@ -348,7 +348,7 @@ private Optional<CelAbstractSyntaxTree> maybePruneBranches(
348348
CelCreateList haystack = callArg.createList();
349349
if (haystack.elements().isEmpty()) {
350350
return Optional.of(
351-
mutableAst.replaceSubtree(
351+
astMutator.replaceSubtree(
352352
ast,
353353
CelExpr.newBuilder().setConstant(CelConstant.ofValue(false)).build(),
354354
expr.id()));
@@ -363,7 +363,7 @@ private Optional<CelAbstractSyntaxTree> maybePruneBranches(
363363
if (elem.constantOrDefault().equals(needleValue)
364364
|| elem.identOrDefault().equals(needleValue)) {
365365
return Optional.of(
366-
mutableAst.replaceSubtree(
366+
astMutator.replaceSubtree(
367367
ast,
368368
CelExpr.newBuilder().setConstant(CelConstant.ofValue(true)).build(),
369369
expr.id()));
@@ -396,16 +396,17 @@ private Optional<CelAbstractSyntaxTree> maybeShortCircuitCall(
396396
}
397397

398398
if (arg.constant().booleanValue() == shortCircuit) {
399-
return Optional.of(mutableAst.replaceSubtree(ast, arg, expr.id()));
399+
return Optional.of(astMutator.replaceSubtree(ast, arg, expr.id()));
400400
}
401401
}
402402

403403
ImmutableList<CelExpr> newArgs = newArgsBuilder.build();
404404
if (newArgs.isEmpty()) {
405-
return Optional.of(mutableAst.replaceSubtree(ast, call.args().get(0), expr.id()));
405+
CelExpr shortCircuitTarget = call.args().get(0); // either args(0) or args(1) would work here
406+
return Optional.of(astMutator.replaceSubtree(ast, shortCircuitTarget, expr.id()));
406407
}
407408
if (newArgs.size() == 1) {
408-
return Optional.of(mutableAst.replaceSubtree(ast, newArgs.get(0), expr.id()));
409+
return Optional.of(astMutator.replaceSubtree(ast, newArgs.get(0), expr.id()));
409410
}
410411

411412
// TODO: Support folding variadic AND/ORs.
@@ -483,7 +484,7 @@ private CelAbstractSyntaxTree pruneOptionalListElements(CelAbstractSyntaxTree as
483484
updatedIndicesBuilder.add(newOptIndex);
484485
}
485486

486-
return mutableAst.replaceSubtree(
487+
return astMutator.replaceSubtree(
487488
ast,
488489
CelExpr.newBuilder()
489490
.setCreateList(
@@ -530,7 +531,7 @@ private CelAbstractSyntaxTree pruneOptionalMapElements(CelAbstractSyntaxTree ast
530531
}
531532

532533
if (modified) {
533-
return mutableAst.replaceSubtree(
534+
return astMutator.replaceSubtree(
534535
ast,
535536
CelExpr.newBuilder()
536537
.setCreateMap(
@@ -576,7 +577,7 @@ private CelAbstractSyntaxTree pruneOptionalStructElements(
576577
}
577578

578579
if (modified) {
579-
return mutableAst.replaceSubtree(
580+
return astMutator.replaceSubtree(
580581
ast,
581582
CelExpr.newBuilder()
582583
.setCreateStruct(
@@ -622,6 +623,6 @@ public static Builder newBuilder() {
622623

623624
private ConstantFoldingOptimizer(ConstantFoldingOptions constantFoldingOptions) {
624625
this.constantFoldingOptions = constantFoldingOptions;
625-
this.mutableAst = MutableAst.newInstance(constantFoldingOptions.maxIterationLimit());
626+
this.astMutator = AstMutator.newInstance(constantFoldingOptions.maxIterationLimit());
626627
}
627628
}

optimizer/src/main/java/dev/cel/optimizer/optimizers/SubexpressionOptimizer.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
import dev.cel.common.types.SimpleType;
5252
import dev.cel.extensions.CelOptionalLibrary;
5353
import dev.cel.extensions.CelOptionalLibrary.Function;
54+
import dev.cel.optimizer.AstMutator;
55+
import dev.cel.optimizer.AstMutator.MangledComprehensionAst;
5456
import dev.cel.optimizer.CelAstOptimizer;
55-
import dev.cel.optimizer.MutableAst;
56-
import dev.cel.optimizer.MutableAst.MangledComprehensionAst;
5757
import dev.cel.parser.Operator;
5858
import java.util.ArrayList;
5959
import java.util.Arrays;
@@ -107,7 +107,7 @@ public class SubexpressionOptimizer implements CelAstOptimizer {
107107
Extension.create("cel_block", Version.of(1L, 1L), Component.COMPONENT_RUNTIME);
108108

109109
private final SubexpressionOptimizerOptions cseOptions;
110-
private final MutableAst mutableAst;
110+
private final AstMutator astMutator;
111111
private final ImmutableSet<String> cseEliminableFunctions;
112112

113113
/**
@@ -140,7 +140,7 @@ public OptimizationResult optimize(CelNavigableAst navigableAst, Cel cel) {
140140

141141
private OptimizationResult optimizeUsingCelBlock(CelNavigableAst navigableAst, Cel cel) {
142142
MangledComprehensionAst mangledComprehensionAst =
143-
mutableAst.mangleComprehensionIdentifierNames(
143+
astMutator.mangleComprehensionIdentifierNames(
144144
navigableAst.getAst(),
145145
MANGLED_COMPREHENSION_IDENTIFIER_PREFIX,
146146
MANGLED_COMPREHENSION_RESULT_PREFIX);
@@ -176,7 +176,7 @@ private OptimizationResult optimizeUsingCelBlock(CelNavigableAst navigableAst, C
176176
"No value present for expr ID: " + semanticallyEqualNode.id()));
177177

178178
astToModify =
179-
mutableAst.replaceSubtree(
179+
astMutator.replaceSubtree(
180180
astToModify,
181181
CelExpr.newBuilder()
182182
.setIdent(CelIdent.newBuilder().setName(blockIdentifier).build())
@@ -235,8 +235,8 @@ private OptimizationResult optimizeUsingCelBlock(CelNavigableAst navigableAst, C
235235

236236
// Wrap the optimized expression in cel.block
237237
astToModify =
238-
mutableAst.wrapAstWithNewCelBlock(CEL_BLOCK_FUNCTION, astToModify, subexpressions);
239-
astToModify = mutableAst.renumberIdsConsecutively(astToModify);
238+
astMutator.wrapAstWithNewCelBlock(CEL_BLOCK_FUNCTION, astToModify, subexpressions);
239+
astToModify = astMutator.renumberIdsConsecutively(astToModify);
240240

241241
// Tag the AST with cel.block designated as an extension
242242
astToModify = tagAstExtension(astToModify);
@@ -359,7 +359,7 @@ private static ImmutableList<CelVarDecl> newBlockIndexVariableDeclarations(
359359

360360
private OptimizationResult optimizeUsingCelBind(CelNavigableAst navigableAst) {
361361
CelAbstractSyntaxTree astToModify =
362-
mutableAst
362+
astMutator
363363
.mangleComprehensionIdentifierNames(
364364
navigableAst.getAst(),
365365
MANGLED_COMPREHENSION_IDENTIFIER_PREFIX,
@@ -395,7 +395,7 @@ private OptimizationResult optimizeUsingCelBind(CelNavigableAst navigableAst) {
395395
"No value present for expr ID: " + semanticallyEqualNode.id()));
396396

397397
astToModify =
398-
mutableAst.replaceSubtree(
398+
astMutator.replaceSubtree(
399399
astToModify,
400400
CelExpr.newBuilder()
401401
.setIdent(CelIdent.newBuilder().setName(bindIdentifier).build())
@@ -414,7 +414,7 @@ private OptimizationResult optimizeUsingCelBind(CelNavigableAst navigableAst) {
414414

415415
// Insert the new bind call
416416
astToModify =
417-
mutableAst.replaceSubtreeWithNewBindMacro(
417+
astMutator.replaceSubtreeWithNewBindMacro(
418418
astToModify, bindIdentifier, cseCandidate, lca.expr(), lca.id());
419419

420420
// Retain the existing macro calls in case if the bind identifiers are replacing a subtree
@@ -436,7 +436,7 @@ private OptimizationResult optimizeUsingCelBind(CelNavigableAst navigableAst) {
436436
return OptimizationResult.create(astToModify);
437437
}
438438

439-
astToModify = mutableAst.renumberIdsConsecutively(astToModify);
439+
astToModify = astMutator.renumberIdsConsecutively(astToModify);
440440

441441
return OptimizationResult.create(astToModify);
442442
}
@@ -653,14 +653,14 @@ private CelExpr normalizeForEquality(CelExpr celExpr) {
653653
.setSelect(presenceTestExpr.select().toBuilder().setTestOnly(false).build())
654654
.build();
655655

656-
celExpr = mutableAst.replaceSubtree(celExpr, newExpr, newExpr.id());
656+
celExpr = astMutator.replaceSubtree(celExpr, newExpr, newExpr.id());
657657
}
658658

659659
if (iterCount >= cseOptions.iterationLimit()) {
660660
throw new IllegalStateException("Max iteration count reached.");
661661
}
662662

663-
return mutableAst.clearExprIds(celExpr);
663+
return astMutator.clearExprIds(celExpr);
664664
}
665665

666666
@VisibleForTesting
@@ -778,7 +778,7 @@ public static Builder newBuilder() {
778778

779779
private SubexpressionOptimizer(SubexpressionOptimizerOptions cseOptions) {
780780
this.cseOptions = cseOptions;
781-
this.mutableAst = MutableAst.newInstance(cseOptions.iterationLimit());
781+
this.astMutator = AstMutator.newInstance(cseOptions.iterationLimit());
782782
this.cseEliminableFunctions =
783783
ImmutableSet.<String>builder()
784784
.addAll(CSE_DEFAULT_ELIMINABLE_FUNCTIONS)

0 commit comments

Comments
 (0)