4848import org .projectnessie .cel .interpreter .InterpretableDecorator ;
4949import org .projectnessie .cel .interpreter .Interpreter ;
5050
51+ /**
52+ * Low-level factories and conversion utilities for CEL environments, programs, activations, and
53+ * ASTs.
54+ *
55+ * <p>Most applications begin with {@link Env#newEnv(EnvOption...)} and follow the
56+ * parse/check/program/evaluate lifecycle. This class supports integrations that need explicit
57+ * Protobuf AST conversion, partial activations, attribute patterns, or cost estimates.
58+ */
5159public final class CEL {
5260
5361 /**
@@ -187,7 +195,16 @@ static boolean nativePlanningPermitted(
187195 || evalOptions .size () == 1 && evalOptions .contains (EvalOption .OptOptimize ));
188196 }
189197
190- /** CheckedExprToAst converts a checked expression proto message to an Ast. */
198+ /**
199+ * Converts a checked-expression Protobuf message to an AST.
200+ *
201+ * <p>The returned source is reconstructed from the message's {@link SourceInfo}; it need not
202+ * contain the original source text.
203+ *
204+ * @param checkedExpr checked expression message
205+ * @return an AST that is considered checked when the message contains non-empty type metadata
206+ * @throws NullPointerException if {@code checkedExpr} is {@code null}
207+ */
191208 public static Ast checkedExprToAst (CheckedExpr checkedExpr ) {
192209 Map <Long , Reference > refMap = checkedExpr .getReferenceMapMap ();
193210 Map <Long , Type > typeMap = checkedExpr .getTypeMapMap ();
@@ -200,9 +217,16 @@ public static Ast checkedExprToAst(CheckedExpr checkedExpr) {
200217 }
201218
202219 /**
203- * AstToCheckedExpr converts an Ast to an protobuf CheckedExpr value .
220+ * Converts a checked AST to a checked-expression Protobuf message .
204221 *
205- * <p>If the Ast.IsChecked() returns false, this conversion method will return an error.
222+ * <p>The conversion preserves CEL-Java's current expression, reference, type, and source-info
223+ * shape. It does not establish cross-runtime support for implementation-specific macro
224+ * expansions.
225+ *
226+ * @param a checked AST
227+ * @return checked-expression message
228+ * @throws NullPointerException if {@code a} is {@code null}
229+ * @throws IllegalArgumentException if {@code a} is unchecked
206230 */
207231 public static CheckedExpr astToCheckedExpr (Ast a ) {
208232 if (!a .isChecked ()) {
@@ -216,68 +240,107 @@ public static CheckedExpr astToCheckedExpr(Ast a) {
216240 .build ();
217241 }
218242
219- /** ParsedExprToAst converts a parsed expression proto message to an Ast. */
243+ /**
244+ * Converts a parsed-expression Protobuf message to an unchecked AST.
245+ *
246+ * <p>The returned source is reconstructed from the message's {@link SourceInfo}; it need not
247+ * contain the original source text.
248+ *
249+ * @param parsedExpr parsed expression message
250+ * @return unchecked AST
251+ * @throws NullPointerException if {@code parsedExpr} is {@code null}
252+ */
220253 public static Ast parsedExprToAst (ParsedExpr parsedExpr ) {
221254 SourceInfo si = parsedExpr .getSourceInfo ();
222255 return new Ast (parsedExpr .getExpr (), si , newInfoSource (si ));
223256 }
224257
225- /** AstToParsedExpr converts an Ast to an protobuf ParsedExpr value. */
258+ /**
259+ * Converts an AST to a parsed-expression Protobuf message.
260+ *
261+ * <p>Checked reference and type maps are not part of {@link ParsedExpr} and are therefore not
262+ * included.
263+ *
264+ * @param a parsed or checked AST
265+ * @return parsed-expression message
266+ * @throws NullPointerException if {@code a} is {@code null}
267+ */
226268 public static ParsedExpr astToParsedExpr (Ast a ) {
227269 return ParsedExpr .newBuilder ().setExpr (a .getExpr ()).setSourceInfo (a .getSourceInfo ()).build ();
228270 }
229271
230272 /**
231- * AstToString converts an Ast back to a string if possible .
273+ * Unparses an AST to stable CEL source text .
232274 *
233275 * <p>Note, the conversion may not be an exact replica of the original expression, but will
234276 * produce a string that is semantically equivalent and whose textual representation is stable.
277+ *
278+ * @param a AST to unparse
279+ * @return stable CEL expression text
280+ * @throws NullPointerException if {@code a} is {@code null}
235281 */
236282 public static String astToString (Ast a ) {
237283 Expr expr = a .getExpr ();
238284 SourceInfo info = a .getSourceInfo ();
239285 return unparse (expr , info );
240286 }
241287
242- /** NoVars returns an empty Activation. */
288+ /**
289+ * Returns an activation with no variable bindings.
290+ *
291+ * @return empty activation
292+ */
243293 public static Activation noVars () {
244294 return emptyActivation ();
245295 }
246296
247297 /**
248- * PartialVars returns a PartialActivation which contains variables and a set of AttributePattern
249- * values that indicate variables or parts of variables whose value are not yet known.
298+ * Creates a partial activation containing bindings and attributes whose values are unknown.
299+ *
300+ * <p>{@code vars} may be an {@link Activation}, a Java map, a {@link
301+ * java.util.function.Function}, or an {@link
302+ * org.projectnessie.cel.interpreter.ActivationFunction}. The returned activation copies the
303+ * unknown-pattern array but retains its mutable pattern elements.
250304 *
251- * <p>The `vars` value may either be an interpreter.Activation or any valid input to the
252- * interpreter.NewActivation call.
305+ * @param vars bindings accepted by {@link Activation#newActivation(Object)}
306+ * @param unknowns attribute patterns that evaluate as unknown
307+ * @return partial activation
308+ * @throws NullPointerException if {@code vars} or {@code unknowns} is {@code null}
309+ * @throws IllegalArgumentException if {@code vars} is not a supported activation input
253310 */
254311 public static PartialActivation partialVars (Object vars , AttributePattern ... unknowns ) {
255312 return newPartialActivation (vars , unknowns );
256313 }
257314
258315 /**
259- * AttributePattern returns an AttributePattern that matches a top-level variable. The pattern is
260- * mutable, and its methods support the specification of one or more qualifier patterns.
316+ * Creates a mutable attribute pattern matching a top-level variable.
261317 *
262- * <p>For example, the AttributePattern(`a`).QualString(`b`) represents a variable access `a` with
263- * a string field or index qualification `b`. This pattern will match Attributes `a`, and `a.b`,
264- * but not `a.c`.
318+ * <p>For example, {@code attributePattern("a").qualString("b")} represents the access {@code
319+ * a.b}. It matches attributes {@code a} and {@code a.b}, but not {@code a.c}.
265320 *
266321 * <p>When using a CEL expression within a container, e.g. a package or namespace, the variable
267322 * name in the pattern must match the qualified name produced during the variable namespace
268- * resolution. For example, when variable `a` is declared within an expression whose container is
269- * ` ns.app` , the fully qualified variable name may be ` ns.app.a`, `ns.a`, or `a` per the CEL
270- * namespace resolution rules. Pick the fully qualified variable name that makes sense within the
271- * container as the AttributePattern ` varName` argument .
323+ * resolution. For example, when variable {@code a} is declared within an expression whose
324+ * container is {@code ns.app} , the fully qualified variable name may be {@code ns.app.a}, {@code
325+ * ns.a}, or {@code a} per the CEL namespace resolution rules. Use the qualified name selected by
326+ * resolution as {@code varName} .
272327 *
273- * <p>See the interpreter.AttributePattern and interpreter.AttributeQualifierPattern for more info
274- * about how to create and manipulate AttributePattern values.
328+ * @param varName top-level or resolved qualified variable name
329+ * @return mutable attribute pattern
275330 */
276331 public static AttributePattern attributePattern (String varName ) {
277332 return newAttributePattern (varName );
278333 }
279334
280- /** EstimateCost returns the heuristic cost interval for the program. */
335+ /**
336+ * Returns the heuristic cost interval reported by an object that implements {@link Coster}.
337+ *
338+ * <p>A cost estimate is descriptive only; it does not impose an evaluation budget or runtime
339+ * limit.
340+ *
341+ * @param p program or other cost-reporting object
342+ * @return reported cost interval, or {@link Cost#Unknown} when {@code p} is not a {@code Coster}
343+ */
281344 public static Cost estimateCost (Object p ) {
282345 if (p instanceof Coster ) {
283346 return ((Coster ) p ).cost ();
0 commit comments