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 /**
54- * newProgram creates a program instance with an environment, an ast, and an optional list of
55- * ProgramOption values.
62+ * Creates an executable program for an AST in an environment.
63+ *
64+ * <p>Options are applied in argument order. A checked AST provides type and reference metadata
65+ * used during planning; an unchecked AST is accepted but may require more runtime dispatch. The
66+ * resulting program can be reused for multiple evaluations.
67+ *
68+ * <p>Native evaluation, when permitted by the selected options, is planner-selected evaluation
69+ * over supported Java-native representations. Unsupported shapes fall back to the established
70+ * evaluator; this does not generate Java or native machine code.
5671 *
57- * <p>If the program cannot be configured the prog will be nil, with a non-nil error response.
72+ * @param e environment supplying types and name resolution
73+ * @param ast parsed or checked expression
74+ * @param opts program configuration tokens, normally created by {@link ProgramOption} factories
75+ * @return reusable executable program
76+ * @throws NullPointerException if {@code e}, {@code ast}, or {@code opts} is {@code null}, or if
77+ * an option is {@code null} or returns {@code null}
78+ * @throws RuntimeException if program planning or option application fails
5879 */
5980 public static Program newProgram (Env e , Ast ast , ProgramOption ... opts ) {
6081 // Build the dispatcher, interpreter, and default program value.
@@ -174,7 +195,16 @@ static boolean nativePlanningPermitted(
174195 || evalOptions .size () == 1 && evalOptions .contains (EvalOption .OptOptimize ));
175196 }
176197
177- /** 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+ */
178208 public static Ast checkedExprToAst (CheckedExpr checkedExpr ) {
179209 Map <Long , Reference > refMap = checkedExpr .getReferenceMapMap ();
180210 Map <Long , Type > typeMap = checkedExpr .getTypeMapMap ();
@@ -187,9 +217,16 @@ public static Ast checkedExprToAst(CheckedExpr checkedExpr) {
187217 }
188218
189219 /**
190- * AstToCheckedExpr converts an Ast to an protobuf CheckedExpr value.
220+ * Converts a checked AST to a checked-expression Protobuf message.
221+ *
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.
191225 *
192- * <p>If the Ast.IsChecked() returns false, this conversion method will return an error.
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
193230 */
194231 public static CheckedExpr astToCheckedExpr (Ast a ) {
195232 if (!a .isChecked ()) {
@@ -203,68 +240,107 @@ public static CheckedExpr astToCheckedExpr(Ast a) {
203240 .build ();
204241 }
205242
206- /** 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+ */
207253 public static Ast parsedExprToAst (ParsedExpr parsedExpr ) {
208254 SourceInfo si = parsedExpr .getSourceInfo ();
209255 return new Ast (parsedExpr .getExpr (), si , newInfoSource (si ));
210256 }
211257
212- /** 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+ */
213268 public static ParsedExpr astToParsedExpr (Ast a ) {
214269 return ParsedExpr .newBuilder ().setExpr (a .getExpr ()).setSourceInfo (a .getSourceInfo ()).build ();
215270 }
216271
217272 /**
218- * AstToString converts an Ast back to a string if possible .
273+ * Unparses an AST to stable CEL source text .
219274 *
220275 * <p>Note, the conversion may not be an exact replica of the original expression, but will
221276 * 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}
222281 */
223282 public static String astToString (Ast a ) {
224283 Expr expr = a .getExpr ();
225284 SourceInfo info = a .getSourceInfo ();
226285 return unparse (expr , info );
227286 }
228287
229- /** NoVars returns an empty Activation. */
288+ /**
289+ * Returns an activation with no variable bindings.
290+ *
291+ * @return empty activation
292+ */
230293 public static Activation noVars () {
231294 return emptyActivation ();
232295 }
233296
234297 /**
235- * PartialVars returns a PartialActivation which contains variables and a set of AttributePattern
236- * 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.
237304 *
238- * <p>The `vars` value may either be an interpreter.Activation or any valid input to the
239- * 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
240310 */
241311 public static PartialActivation partialVars (Object vars , AttributePattern ... unknowns ) {
242312 return newPartialActivation (vars , unknowns );
243313 }
244314
245315 /**
246- * AttributePattern returns an AttributePattern that matches a top-level variable. The pattern is
247- * mutable, and its methods support the specification of one or more qualifier patterns.
316+ * Creates a mutable attribute pattern matching a top-level variable.
248317 *
249- * <p>For example, the AttributePattern(`a`).QualString(`b`) represents a variable access `a` with
250- * a string field or index qualification `b`. This pattern will match Attributes `a`, and `a.b`,
251- * 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}.
252320 *
253321 * <p>When using a CEL expression within a container, e.g. a package or namespace, the variable
254322 * name in the pattern must match the qualified name produced during the variable namespace
255- * resolution. For example, when variable `a` is declared within an expression whose container is
256- * ` ns.app` , the fully qualified variable name may be ` ns.app.a`, `ns.a`, or `a` per the CEL
257- * namespace resolution rules. Pick the fully qualified variable name that makes sense within the
258- * 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} .
259327 *
260- * <p>See the interpreter.AttributePattern and interpreter.AttributeQualifierPattern for more info
261- * about how to create and manipulate AttributePattern values.
328+ * @param varName top-level or resolved qualified variable name
329+ * @return mutable attribute pattern
262330 */
263331 public static AttributePattern attributePattern (String varName ) {
264332 return newAttributePattern (varName );
265333 }
266334
267- /** 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+ */
268344 public static Cost estimateCost (Object p ) {
269345 if (p instanceof Coster ) {
270346 return ((Coster ) p ).cost ();
0 commit comments