1414
1515package dev .cel .runtime ;
1616
17- import com .google .auto .value .AutoValue ;
18- import com .google .common .base .Preconditions ;
1917import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2018import com .google .errorprone .annotations .Immutable ;
2119import javax .annotation .concurrent .ThreadSafe ;
2220import com .google .protobuf .Message ;
2321import dev .cel .common .CelAbstractSyntaxTree ;
2422import dev .cel .common .CelOptions ;
2523import java .util .Map ;
26- import java .util .Optional ;
2724
2825/**
2926 * The CelRuntime creates executable {@code Program} instances from {@code CelAbstractSyntaxTree}
@@ -39,112 +36,69 @@ public interface CelRuntime {
3936 CelRuntimeBuilder toRuntimeBuilder ();
4037
4138 /** Creates an evaluable {@code Program} instance which is thread-safe and immutable. */
42- @ AutoValue
4339 @ Immutable
44- abstract class Program implements CelLiteRuntime .Program {
45-
46- @ Override
47- public Object eval () throws CelEvaluationException {
48- return evalInternal (Activation .EMPTY );
49- }
50-
51- @ Override
52- public Object eval (Map <String , ?> mapValue ) throws CelEvaluationException {
53- return evalInternal (Activation .copyOf (mapValue ));
54- }
40+ interface Program extends CelLiteRuntime .Program {
5541
5642 /** Evaluate the expression using {@code message} fields as the source of input variables. */
57- public Object eval (Message message ) throws CelEvaluationException {
58- return evalInternal (ProtoMessageActivationFactory .fromProto (message , getOptions ()));
59- }
43+ Object eval (Message message ) throws CelEvaluationException ;
6044
6145 /** Evaluate a compiled program with a custom variable {@code resolver}. */
62- public Object eval (CelVariableResolver resolver ) throws CelEvaluationException {
63- return evalInternal ((name ) -> resolver .find (name ).orElse (null ));
64- }
46+ Object eval (CelVariableResolver resolver ) throws CelEvaluationException ;
6547
6648 /**
6749 * Evaluate a compiled program with a custom variable {@code resolver} and late-bound functions
6850 * {@code lateBoundFunctionResolver}.
6951 */
70- public Object eval (CelVariableResolver resolver , CelFunctionResolver lateBoundFunctionResolver )
71- throws CelEvaluationException {
72- return evalInternal (
73- (name ) -> resolver .find (name ).orElse (null ),
74- lateBoundFunctionResolver ,
75- CelEvaluationListener .noOpListener ());
76- }
77-
78- @ Override
79- public Object eval (Map <String , ?> mapValue , CelFunctionResolver lateBoundFunctionResolver )
80- throws CelEvaluationException {
81- return evalInternal (
82- Activation .copyOf (mapValue ),
83- lateBoundFunctionResolver ,
84- CelEvaluationListener .noOpListener ());
85- }
52+ Object eval (CelVariableResolver resolver , CelFunctionResolver lateBoundFunctionResolver )
53+ throws CelEvaluationException ;
8654
8755 /**
8856 * Trace evaluates a compiled program without any variables and invokes the listener as
8957 * evaluation progresses through the AST.
9058 */
91- public Object trace (CelEvaluationListener listener ) throws CelEvaluationException {
92- return evalInternal (Activation .EMPTY , listener );
93- }
59+ Object trace (CelEvaluationListener listener ) throws CelEvaluationException ;
9460
9561 /**
9662 * Trace evaluates a compiled program using a {@code mapValue} as the source of input variables.
9763 * The listener is invoked as evaluation progresses through the AST.
9864 */
99- public Object trace (Map <String , ?> mapValue , CelEvaluationListener listener )
100- throws CelEvaluationException {
101- return evalInternal (Activation .copyOf (mapValue ), listener );
102- }
65+ Object trace (Map <String , ?> mapValue , CelEvaluationListener listener )
66+ throws CelEvaluationException ;
10367
10468 /**
10569 * Trace evaluates a compiled program using {@code message} fields as the source of input
10670 * variables. The listener is invoked as evaluation progresses through the AST.
10771 */
108- public Object trace (Message message , CelEvaluationListener listener )
109- throws CelEvaluationException {
110- return evalInternal (ProtoMessageActivationFactory .fromProto (message , getOptions ()), listener );
111- }
72+ Object trace (Message message , CelEvaluationListener listener ) throws CelEvaluationException ;
11273
11374 /**
11475 * Trace evaluates a compiled program using a custom variable {@code resolver}. The listener is
11576 * invoked as evaluation progresses through the AST.
11677 */
117- public Object trace (CelVariableResolver resolver , CelEvaluationListener listener )
118- throws CelEvaluationException {
119- return evalInternal ((name ) -> resolver .find (name ).orElse (null ), listener );
120- }
78+ Object trace (CelVariableResolver resolver , CelEvaluationListener listener )
79+ throws CelEvaluationException ;
12180
12281 /**
12382 * Trace evaluates a compiled program using a custom variable {@code resolver} and late-bound
12483 * functions {@code lateBoundFunctionResolver}. The listener is invoked as evaluation progresses
12584 * through the AST.
12685 */
127- public Object trace (
86+ Object trace (
12887 CelVariableResolver resolver ,
12988 CelFunctionResolver lateBoundFunctionResolver ,
13089 CelEvaluationListener listener )
131- throws CelEvaluationException {
132- return evalInternal (
133- (name ) -> resolver .find (name ).orElse (null ), lateBoundFunctionResolver , listener );
134- }
90+ throws CelEvaluationException ;
13591
13692 /**
13793 * Trace evaluates a compiled program using a {@code mapValue} as the source of input variables
13894 * and late-bound functions {@code lateBoundFunctionResolver}. The listener is invoked as
13995 * evaluation progresses through the AST.
14096 */
141- public Object trace (
97+ Object trace (
14298 Map <String , ?> mapValue ,
14399 CelFunctionResolver lateBoundFunctionResolver ,
144100 CelEvaluationListener listener )
145- throws CelEvaluationException {
146- return evalInternal (Activation .copyOf (mapValue ), lateBoundFunctionResolver , listener );
147- }
101+ throws CelEvaluationException ;
148102
149103 /**
150104 * Advance evaluation based on the current unknown context.
@@ -155,70 +109,6 @@ public Object trace(
155109 * <p>If no unknowns are declared in the context or {@link CelOptions#enableUnknownTracking()
156110 * UnknownTracking} is disabled, this is equivalent to eval.
157111 */
158- public Object advanceEvaluation (UnknownContext context ) throws CelEvaluationException {
159- return evalInternal (context , Optional .empty (), CelEvaluationListener .noOpListener ());
160- }
161-
162- private Object evalInternal (GlobalResolver resolver ) throws CelEvaluationException {
163- return evalInternal (
164- UnknownContext .create (resolver ), Optional .empty (), CelEvaluationListener .noOpListener ());
165- }
166-
167- private Object evalInternal (GlobalResolver resolver , CelEvaluationListener listener )
168- throws CelEvaluationException {
169- return evalInternal (UnknownContext .create (resolver ), Optional .empty (), listener );
170- }
171-
172- private Object evalInternal (
173- GlobalResolver resolver ,
174- CelFunctionResolver lateBoundFunctionResolver ,
175- CelEvaluationListener listener )
176- throws CelEvaluationException {
177- return evalInternal (
178- UnknownContext .create (resolver ), Optional .of (lateBoundFunctionResolver ), listener );
179- }
180-
181- /**
182- * Evaluate an expr node with an UnknownContext (an activation annotated with which attributes
183- * are unknown).
184- */
185- private Object evalInternal (
186- UnknownContext context ,
187- Optional <CelFunctionResolver > lateBoundFunctionResolver ,
188- CelEvaluationListener listener )
189- throws CelEvaluationException {
190- Interpretable impl = getInterpretable ();
191- if (getOptions ().enableUnknownTracking ()) {
192- Preconditions .checkState (
193- impl instanceof UnknownTrackingInterpretable ,
194- "Environment misconfigured. Requested unknown tracking without a compatible"
195- + " implementation." );
196-
197- UnknownTrackingInterpretable interpreter = (UnknownTrackingInterpretable ) impl ;
198- return interpreter .evalTrackingUnknowns (
199- RuntimeUnknownResolver .builder ()
200- .setResolver (context .variableResolver ())
201- .setAttributeResolver (context .createAttributeResolver ())
202- .build (),
203- lateBoundFunctionResolver ,
204- listener );
205- } else {
206- if (lateBoundFunctionResolver .isPresent ()) {
207- return impl .eval (context .variableResolver (), lateBoundFunctionResolver .get (), listener );
208- }
209- return impl .eval (context .variableResolver (), listener );
210- }
211- }
212-
213- /** Get the underlying {@link Interpretable} for the {@code Program}. */
214- abstract Interpretable getInterpretable ();
215-
216- /** Get the {@code CelOptions} configured for this program. */
217- abstract CelOptions getOptions ();
218-
219- /** Instantiate a new {@code Program} from the input {@code interpretable}. */
220- static Program from (Interpretable interpretable , CelOptions options ) {
221- return new AutoValue_CelRuntime_Program (interpretable , options );
222- }
112+ Object advanceEvaluation (UnknownContext context ) throws CelEvaluationException ;
223113 }
224114}
0 commit comments