Skip to content

Commit 7abcfd9

Browse files
committed
Refresh API documentation and guides
Update Javadocs and public documentation to describe the current APIs, evaluation behavior, and supported integrations.
1 parent 28ff803 commit 7abcfd9

160 files changed

Lines changed: 7006 additions & 1279 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 108 additions & 432 deletions
Large diffs are not rendered by default.

‎core/src/main/java/org/projectnessie/cel/Ast.java‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,21 @@
2525
import org.projectnessie.cel.common.Source;
2626

2727
/**
28-
* Ast representing the checked or unchecked expression, its source, and related metadata such as
29-
* source position information.
28+
* A parsed or type-checked CEL abstract syntax tree and its source metadata.
29+
*
30+
* <p>An AST returned by {@link Env#parse(String)} is unchecked. Passing it to {@link
31+
* Env#check(Ast)} produces a checked AST with reference and type metadata. Prefer a checked AST
32+
* when creating a {@link Program}; unchecked ASTs are supported but provide less planning
33+
* information.
34+
*
35+
* <p>The expression and source-info values are immutable Protobuf messages. The constructors retain
36+
* the supplied {@link Source} and, for the full constructor, the supplied reference and type maps.
37+
* Callers that construct an AST directly must keep retained mutable state stable while the AST is
38+
* in use.
39+
*
40+
* <p>{@link CEL#astToParsedExpr(Ast)} and {@link CEL#astToCheckedExpr(Ast)} expose the
41+
* corresponding Protobuf representations. Exporting those messages does not by itself guarantee
42+
* that another CEL runtime recognizes implementation-specific macro expansions.
3043
*/
3144
public final class Ast {
3245
private final Expr expr;
@@ -35,10 +48,28 @@ public final class Ast {
3548
final Map<Long, Reference> refMap;
3649
final Map<Long, Type> typeMap;
3750

51+
/**
52+
* Creates an unchecked AST.
53+
*
54+
* @param expr parsed CEL expression
55+
* @param info source and position metadata
56+
* @param source source used to parse the expression
57+
*/
3858
public Ast(Expr expr, SourceInfo info, Source source) {
3959
this(expr, info, source, new HashMap<>(), new HashMap<>());
4060
}
4161

62+
/**
63+
* Creates an AST with explicit reference and type metadata.
64+
*
65+
* <p>The maps are retained rather than copied. A non-empty type map marks the AST as checked.
66+
*
67+
* @param expr parsed or checked CEL expression
68+
* @param info source and position metadata
69+
* @param source source used to parse or check the expression
70+
* @param refMap checked reference metadata keyed by expression ID
71+
* @param typeMap checked type metadata keyed by expression ID
72+
*/
4273
public Ast(
4374
Expr expr,
4475
SourceInfo info,
@@ -52,30 +83,46 @@ public Ast(
5283
this.typeMap = typeMap;
5384
}
5485

55-
/** Expr returns the proto serializable instance of the parsed/checked expression. */
86+
/**
87+
* Returns the parsed or checked expression Protobuf message.
88+
*
89+
* @return expression tree
90+
*/
5691
public Expr getExpr() {
5792
return expr;
5893
}
5994

60-
/** IsChecked returns whether the Ast value has been successfully type-checked. */
95+
/**
96+
* Reports whether this AST contains checked type metadata.
97+
*
98+
* @return {@code true} when the type map is non-null and non-empty
99+
*/
61100
public boolean isChecked() {
62101
return typeMap != null && !typeMap.isEmpty();
63102
}
64103

104+
/**
105+
* Returns the source associated with this AST.
106+
*
107+
* @return retained source
108+
*/
65109
public Source getSource() {
66110
return source;
67111
}
68112

69113
/**
70-
* SourceInfo returns character offset and newling position information about expression elements.
114+
* Returns source-location and macro-expansion metadata for expression elements.
115+
*
116+
* @return source information
71117
*/
72118
public SourceInfo getSourceInfo() {
73119
return info;
74120
}
75121

76122
/**
77-
* ResultType returns the output type of the expression if the Ast has been type-checked, else
78-
* returns decls.Dyn as the parse step cannot infer the type.
123+
* Returns the checked result type of this expression.
124+
*
125+
* @return the root expression's checked type, or {@link Decls#Dyn} for an unchecked AST
79126
*/
80127
public Type getResultType() {
81128
if (!isChecked()) {
@@ -85,8 +132,12 @@ public Type getResultType() {
85132
}
86133

87134
/**
88-
* Source returns a view of the input used to create the Ast. This source may be complete or
89-
* constructed from the SourceInfo.
135+
* Returns the source text associated with this AST.
136+
*
137+
* <p>This is the retained source's content, not a newly unparsed representation of the
138+
* expression. Use {@link CEL#astToString(Ast)} to obtain a stable unparsed expression.
139+
*
140+
* @return source content
90141
*/
91142
@Override
92143
public String toString() {

‎core/src/main/java/org/projectnessie/cel/CEL.java‎

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,34 @@
4848
import org.projectnessie.cel.interpreter.InterpretableDecorator;
4949
import 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+
*/
5159
public 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

Comments
 (0)