use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class CELTest method Customtypes.
@Test
void Customtypes() {
Type exprType = Decls.newObjectType("google.api.expr.v1alpha1.Expr");
TypeRegistry reg = newEmptyRegistry();
Env e = newEnv(customTypeAdapter(reg), customTypeProvider(reg), container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance(), BoolT.BoolType, IntT.IntType, StringT.StringType), declarations(Decls.newVar("expr", exprType)));
AstIssuesTuple astIss = e.compile("expr == Expr{id: 2,\n" + "\t\t\tcall_expr: Expr.Call{\n" + "\t\t\t\tfunction: \"_==_\",\n" + "\t\t\t\targs: [\n" + "\t\t\t\t\tExpr{id: 1, ident_expr: Expr.Ident{ name: \"a\" }},\n" + "\t\t\t\t\tExpr{id: 3, ident_expr: Expr.Ident{ name: \"b\" }}]\n" + "\t\t\t}}");
assertThat(astIss.getAst().getResultType()).isEqualTo(Decls.Bool);
Program prg = e.program(astIss.getAst());
Object vars = mapOf("expr", Expr.newBuilder().setId(2).setCallExpr(Call.newBuilder().setFunction("_==_").addAllArgs(asList(Expr.newBuilder().setId(1).setIdentExpr(Ident.newBuilder().setName("a")).build(), Expr.newBuilder().setId(3).setIdentExpr(Ident.newBuilder().setName("b")).build()))).build());
EvalResult out = prg.eval(vars);
assertThat(out.getVal()).isSameAs(True);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderBench method newValue.
@Benchmark
public void newValue(NewValueState state) {
TypeRegistry reg = state.reg;
reg.newValue("google.api.expr.v1.SourceInfo", mapOf("Location", stringOf("BenchmarkTypeProvider_NewValue"), "LineOffsets", newGenericArrayList(reg, new Object[] { 0L, 2L }), "Positions", newMaybeWrappedMap(reg, mapOf(1L, 2L, 2L, 4L))));
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesBench method attributesConditionalAttr_FalseBranch.
@Benchmark
public void attributesConditionalAttr_FalseBranch() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", mapOf("c", mapOf(-1, new int[] { 2, 42 })));
Activation vars = newActivation(data);
// (false ? a : b.c)[-1][1]
NamespacedAttribute tv = attrs.absoluteAttribute(2, "a");
Attribute fv = attrs.maybeAttribute(3, "b");
Qualifier qualC = attrs.newQualifier(null, 4, "c");
fv.addQualifier(qualC);
Attribute cond = attrs.conditionalAttribute(1, newConstValue(0, False), tv, fv);
Qualifier qualNeg1 = attrs.newQualifier(null, 5, intOf(-1));
Qualifier qual1 = attrs.newQualifier(null, 6, intOf(1));
cond.addQualifier(qualNeg1);
cond.addQualifier(qual1);
Object out = cond.resolve(vars);
assertThat(out).isEqualTo(42);
assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesBench method attributesConditionalAttr_TrueBranch.
@Benchmark
public void attributesConditionalAttr_TrueBranch() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", mapOf("c", mapOf(-1, new int[] { 2, 42 })));
Activation vars = newActivation(data);
// (true ? a : b.c)[-1][1]
NamespacedAttribute tv = attrs.absoluteAttribute(2, "a");
Attribute fv = attrs.maybeAttribute(3, "b");
Qualifier qualC = attrs.newQualifier(null, 4, "c");
fv.addQualifier(qualC);
Attribute cond = attrs.conditionalAttribute(1, newConstValue(0, True), tv, fv);
Qualifier qualNeg1 = attrs.newQualifier(null, 5, intOf(-1));
Qualifier qual1 = attrs.newQualifier(null, 6, intOf(1));
cond.addQualifier(qualNeg1);
cond.addQualifier(qual1);
Object out = cond.resolve(vars);
assertThat(out).isEqualTo(42);
assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class Env method extend.
/**
* Extend the current environment with additional options to produce a new Env.
*
* <p>Note, the extended Env value should not share memory with the original. It is possible,
* however, that a CustomTypeAdapter or CustomTypeProvider options could provide values which are
* mutable. To ensure separation of state between extended environments either make sure the
* TypeAdapter and TypeProvider are immutable, or that their underlying implementations are based
* on the ref.TypeRegistry which provides a Copy method which will be invoked by this method.
*/
public Env extend(List<EnvOption> opts) {
if (chkErr != null) {
throw chkErr;
}
// Copy slices.
List<Decl> decsCopy = new ArrayList<>(declarations);
List<Macro> macsCopy = new ArrayList<>(macros);
List<ProgramOption> progOptsCopy = new ArrayList<>(progOpts);
// Copy the adapter / provider if they appear to be mutable.
TypeAdapter adapter = this.adapter;
TypeProvider provider = this.provider;
// TypeRegistry as the base implementation are captured below.
if (this.adapter instanceof TypeRegistry && this.provider instanceof TypeRegistry) {
TypeRegistry adapterReg = (TypeRegistry) this.adapter;
TypeRegistry providerReg = (TypeRegistry) this.provider;
TypeRegistry reg = providerReg.copy();
provider = reg;
// to the same ref.TypeRegistry as the provider.
if (adapterReg.equals(providerReg)) {
adapter = reg;
} else {
// Otherwise, make a copy of the adapter.
adapter = adapterReg.copy();
}
} else if (this.provider instanceof TypeRegistry) {
provider = ((TypeRegistry) this.provider).copy();
} else if (this.adapter instanceof TypeRegistry) {
adapter = ((TypeRegistry) this.adapter).copy();
}
Set<EnvFeature> featuresCopy = EnumSet.copyOf(this.features);
Env ext = new Env(this.container, decsCopy, macsCopy, adapter, provider, featuresCopy, progOptsCopy);
return ext.configure(opts);
}
Aggregations