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
8 changes: 8 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/CelRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ Object trace(
CelEvaluationListener listener)
throws CelEvaluationException;

/**
* Trace evaluates a compiled program using {@code partialVars} as the source of input variables
* and unknown attribute patterns. The listener is invoked as evaluation progresses through the
* AST.
*/
Object trace(PartialVars partialVars, CelEvaluationListener listener)
throws CelEvaluationException;

/**
* Advance evaluation based on the current unknown context.
*
Expand Down
11 changes: 11 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ public Object trace(
.trace(Activation.copyOf(mapValue), lateBoundFunctionResolver, null, listener);
}

@Override
public Object trace(PartialVars partialVars, CelEvaluationListener listener)
throws CelEvaluationException {
return ((PlannedProgram) program)
.trace(
(name) -> partialVars.resolver().find(name).orElse(null),
EMPTY_FUNCTION_RESOLVER,
partialVars,
listener);
}

@Override
public Object advanceEvaluation(UnknownContext context) throws CelEvaluationException {
throw new UnsupportedOperationException("Unsupported operation.");
Expand Down
9 changes: 9 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/ProgramImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public Object trace(
return evalInternal(Activation.copyOf(mapValue), lateBoundFunctionResolver, listener);
}

@Override
public Object trace(PartialVars partialVars, CelEvaluationListener listener)
throws CelEvaluationException {
return evalInternal(
UnknownContext.create(partialVars.resolver(), partialVars.unknowns()),
/* lateBoundFunctionResolver= */ Optional.empty(),
Optional.of(listener));
}

@Override
public Object advanceEvaluation(UnknownContext context) throws CelEvaluationException {
return evalInternal(context, Optional.empty(), Optional.empty());
Expand Down
46 changes: 46 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ java_library(
":eval_create_list",
":eval_create_map",
":eval_create_struct",
":eval_exhaustive_and",
":eval_exhaustive_conditional",
":eval_exhaustive_or",
":eval_fold",
":eval_late_bound_call",
":eval_optional_or",
Expand Down Expand Up @@ -446,6 +449,7 @@ java_library(
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"//runtime:interpreter_util",
"//runtime:partial_vars",
"//runtime:resolved_overload",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -498,6 +502,48 @@ java_library(
],
)

java_library(
name = "eval_exhaustive_and",
srcs = ["EvalExhaustiveAnd.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//common/ast",
"//common/values",
"//runtime:accumulated_unknowns",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "eval_exhaustive_or",
srcs = ["EvalExhaustiveOr.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//common/ast",
"//common/values",
"//runtime:accumulated_unknowns",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "eval_exhaustive_conditional",
srcs = ["EvalExhaustiveConditional.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//common/ast",
"//runtime:accumulated_unknowns",
"//runtime:evaluation_exception",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "eval_block",
srcs = ["EvalBlock.java"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime.planner;

import static dev.cel.runtime.planner.EvalHelpers.evalNonstrictly;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.values.ErrorValue;
import dev.cel.runtime.AccumulatedUnknowns;
import dev.cel.runtime.GlobalResolver;

/**
* Implementation of logical AND with exhaustive evaluation (non-short-circuiting).
*
* <p>It evaluates all arguments, but prioritizes a false result over unknowns and errors to
* maintain semantic consistency with short-circuiting evaluation.
*/
@Immutable
final class EvalExhaustiveAnd extends PlannedInterpretable {

@SuppressWarnings("Immutable")
private final PlannedInterpretable[] args;

@Override
Object evalInternal(GlobalResolver resolver, ExecutionFrame frame) {
AccumulatedUnknowns accumulatedUnknowns = null;
ErrorValue errorValue = null;
boolean hasFalse = false;

for (PlannedInterpretable arg : args) {
Object argVal = evalNonstrictly(arg, resolver, frame);
if (argVal instanceof Boolean) {
if (!((boolean) argVal)) {
hasFalse = true;
}
}

// If we already encountered a false, we do not need to accumulate unknowns or errors
// from subsequent terms because the final result will be false anyway.
if (hasFalse) {
continue;
}

if (argVal instanceof AccumulatedUnknowns) {
accumulatedUnknowns =
accumulatedUnknowns == null
? (AccumulatedUnknowns) argVal
: accumulatedUnknowns.merge((AccumulatedUnknowns) argVal);
} else if (argVal instanceof ErrorValue) {
if (errorValue == null) {
errorValue = (ErrorValue) argVal;
}
}
}

if (hasFalse) {
return false;
}

if (accumulatedUnknowns != null) {
return accumulatedUnknowns;
}

if (errorValue != null) {
return errorValue;
}

return true;
}

static EvalExhaustiveAnd create(CelExpr expr, PlannedInterpretable[] args) {
return new EvalExhaustiveAnd(expr, args);
}

private EvalExhaustiveAnd(CelExpr expr, PlannedInterpretable[] args) {
super(expr);
this.args = args;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime.planner;

import static dev.cel.runtime.planner.EvalHelpers.evalNonstrictly;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.ast.CelExpr;
import dev.cel.runtime.AccumulatedUnknowns;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.GlobalResolver;

/**
* Implementation of conditional operator (ternary) with exhaustive evaluation
* (non-short-circuiting).
*
* <p>It evaluates all three arguments (condition, truthy, and falsy branches) but returns the
* result based on the condition, maintaining semantic consistency with short-circuiting evaluation.
*/
@Immutable
final class EvalExhaustiveConditional extends PlannedInterpretable {

@SuppressWarnings("Immutable")
private final PlannedInterpretable[] args;

@Override
Object evalInternal(GlobalResolver resolver, ExecutionFrame frame) throws CelEvaluationException {
PlannedInterpretable condition = args[0];
PlannedInterpretable truthy = args[1];
PlannedInterpretable falsy = args[2];

Object condResult = condition.eval(resolver, frame);
Object truthyVal = evalNonstrictly(truthy, resolver, frame);
Object falsyVal = evalNonstrictly(falsy, resolver, frame);

if (condResult instanceof AccumulatedUnknowns) {
return condResult;
}

if (!(condResult instanceof Boolean)) {
throw new IllegalArgumentException(
String.format("Expected boolean value, found :%s", condResult));
}

return (boolean) condResult ? truthyVal : falsyVal;
}

static EvalExhaustiveConditional create(CelExpr expr, PlannedInterpretable[] args) {
return new EvalExhaustiveConditional(expr, args);
}

private EvalExhaustiveConditional(CelExpr expr, PlannedInterpretable[] args) {
super(expr);
this.args = args;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime.planner;

import static dev.cel.runtime.planner.EvalHelpers.evalNonstrictly;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.values.ErrorValue;
import dev.cel.runtime.AccumulatedUnknowns;
import dev.cel.runtime.GlobalResolver;

/**
* Implementation of logical OR with exhaustive evaluation (non-short-circuiting).
*
* <p>It evaluates all arguments, but prioritizes a true result over unknowns and errors to maintain
* semantic consistency with short-circuiting evaluation.
*/
@Immutable
final class EvalExhaustiveOr extends PlannedInterpretable {

@SuppressWarnings("Immutable")
private final PlannedInterpretable[] args;

@Override
Object evalInternal(GlobalResolver resolver, ExecutionFrame frame) {
AccumulatedUnknowns accumulatedUnknowns = null;
ErrorValue errorValue = null;
boolean hasTrue = false;

for (PlannedInterpretable arg : args) {
Object argVal = evalNonstrictly(arg, resolver, frame);
if (argVal instanceof Boolean) {
if ((boolean) argVal) {
hasTrue = true;
}
}

// If we already encountered a true, we do not need to accumulate unknowns or errors
// from subsequent terms because the final result will be true anyway.
if (hasTrue) {
continue;
}

if (argVal instanceof AccumulatedUnknowns) {
accumulatedUnknowns =
accumulatedUnknowns == null
? (AccumulatedUnknowns) argVal
: accumulatedUnknowns.merge((AccumulatedUnknowns) argVal);
} else if (argVal instanceof ErrorValue) {
if (errorValue == null) {
errorValue = (ErrorValue) argVal;
}
}
}

if (hasTrue) {
return true;
}

if (accumulatedUnknowns != null) {
return accumulatedUnknowns;
}

if (errorValue != null) {
return errorValue;
}

return false;
}

static EvalExhaustiveOr create(CelExpr expr, PlannedInterpretable[] args) {
return new EvalExhaustiveOr(expr, args);
}

private EvalExhaustiveOr(CelExpr expr, PlannedInterpretable[] args) {
super(expr);
this.args = args;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.InterpreterUtil;

@Immutable
abstract class PlannedInterpretable {
Expand All @@ -29,7 +30,7 @@ final Object eval(GlobalResolver resolver, ExecutionFrame frame) throws CelEvalu
Object result = evalInternal(resolver, frame);
CelEvaluationListener listener = frame.getListener();
if (listener != null) {
listener.callback(expr, result);
listener.callback(expr, InterpreterUtil.maybeAdaptToCelUnknownSet(result));
}
return result;
}
Expand Down
Loading
Loading