use of org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute in project cel-java by projectnessie.
the class CELTest method CustomInterpreterDecorator.
@Test
void CustomInterpreterDecorator() {
AtomicReference<Interpretable> lastInstruction = new AtomicReference<>();
InterpretableDecorator optimizeArith = i -> {
lastInstruction.set(i);
// Only optimize the instruction if it is a call.
if (!(i instanceof InterpretableCall)) {
return i;
}
InterpretableCall call = (InterpretableCall) i;
// Only optimize the math functions when they have constant arguments.
switch(call.function()) {
case "_+_":
case "_-_":
case "_*_":
case "_/_":
// These are all binary operators so they should have to arguments
Interpretable[] args = call.args();
// an empty activation and the value returns as a constant.
if (!(args[0] instanceof InterpretableConst) || !(args[1] instanceof InterpretableConst)) {
return i;
}
Val val = call.eval(emptyActivation());
if (isError(val)) {
throw new RuntimeException(val.toString());
}
return newConstValue(call.id(), val);
default:
return i;
}
};
Env env = newEnv(declarations(Decls.newVar("foo", Decls.Int)));
AstIssuesTuple astIss = env.compile("foo == -1 + 2 * 3 / 3");
env.program(astIss.getAst(), evalOptions(OptPartialEval), customDecorator(optimizeArith));
assertThat(lastInstruction.get()).isInstanceOf(InterpretableCall.class);
InterpretableCall call = (InterpretableCall) lastInstruction.get();
Interpretable[] args = call.args();
Interpretable lhs = args[0];
assertThat(lhs).isInstanceOf(InterpretableAttribute.class);
InterpretableAttribute lastAttr = (InterpretableAttribute) lhs;
NamespacedAttribute absAttr = (NamespacedAttribute) lastAttr.attr();
String[] varNames = absAttr.candidateVariableNames();
assertThat(varNames).containsExactly("foo");
Interpretable rhs = args[1];
assertThat(rhs).isInstanceOf(InterpretableConst.class);
InterpretableConst lastConst = (InterpretableConst) rhs;
// This is the last number produced by the optimization.
assertThat(lastConst.value()).isSameAs(IntOne);
}
use of org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute 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.interpreter.AttributeFactory.NamespacedAttribute 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.interpreter.AttributeFactory.NamespacedAttribute in project cel-java by projectnessie.
the class AttributesTest method benchmarkResolverFieldQualifier.
@Test
void benchmarkResolverFieldQualifier() {
TestAllTypes msg = TestAllTypes.newBuilder().setSingleNestedMessage(NestedMessage.newBuilder().setBb(123)).build();
TypeRegistry reg = newRegistry(msg);
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Activation vars = newActivation(mapOf("msg", msg));
NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
Type opType = reg.findType("google.api.expr.test.v1.proto3.TestAllTypes");
assertThat(opType).isNotNull();
Type fieldType = reg.findType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage");
assertThat(fieldType).isNotNull();
attr.addQualifier(makeQualifier(attrs, opType.getType(), 2, "single_nested_message"));
attr.addQualifier(makeQualifier(attrs, fieldType.getType(), 3, "bb"));
// Note: migrated to JMH
}
use of org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute in project cel-java by projectnessie.
the class AttributesTest method attributeMissingMsg_UnknownField.
@Test
void attributeMissingMsg_UnknownField() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newPartialAttributeFactory(Container.defaultContainer, reg, reg);
Any any = Any.pack(TestAllTypes.getDefaultInstance());
Activation vars = newPartialActivation(mapOf("missing_msg", any), newAttributePattern("missing_msg").qualString("field"));
// missing_msg.field
NamespacedAttribute attr = attrs.absoluteAttribute(1, "missing_msg");
Qualifier field = attrs.newQualifier(null, 2, "field");
attr.addQualifier(field);
Object out = attr.resolve(vars);
assertThat(out).isInstanceOf(UnknownT.class);
}
Aggregations