use of org.projectnessie.cel.interpreter.AttributeFactory.Attribute 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.Attribute 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.Attribute in project cel-java by projectnessie.
the class AttributesTest method attributesRelativeAttr.
@Test
void attributesRelativeAttr() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", 1);
Activation vars = newActivation(data);
// The relative attribute under test is applied to a map literal:
// {
// a: {-1: [2, 42], b: 1}
// b: 1
// }
//
// The expression being evaluated is: <map-literal>.a[-1][b] -> 42
InterpretableConst op = newConstValue(1, reg.nativeToValue(data));
Attribute attr = attrs.relativeAttribute(1, op);
Qualifier qualA = attrs.newQualifier(null, 2, "a");
Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
attr.addQualifier(qualA);
attr.addQualifier(qualNeg1);
attr.addQualifier(attrs.absoluteAttribute(4, "b"));
Object out = attr.resolve(vars);
assertThat(out).isEqualTo(intOf(42));
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.interpreter.AttributeFactory.Attribute in project cel-java by projectnessie.
the class AttributesTest method attributesRelativeAttr_OneOf.
@Test
void attributesRelativeAttr_OneOf() {
TypeRegistry reg = newRegistry();
Container cont = newContainer(Container.name("acme.ns"));
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "acme.b", 1);
Activation vars = newActivation(data);
// The relative attribute under test is applied to a map literal:
// {
// a: {-1: [2, 42], b: 1}
// b: 1
// }
//
// The expression being evaluated is: <map-literal>.a[-1][b] -> 42
//
// However, since the test is validating what happens with maybe attributes
// the attribute resolution must also consider the following variations:
// - <map-literal>.a[-1][acme.ns.b]
// - <map-literal>.a[-1][acme.b]
//
// The correct behavior should yield the value of the last alternative.
InterpretableConst op = newConstValue(1, reg.nativeToValue(data));
Attribute attr = attrs.relativeAttribute(1, op);
Qualifier qualA = attrs.newQualifier(null, 2, "a");
Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
attr.addQualifier(qualA);
attr.addQualifier(qualNeg1);
attr.addQualifier(attrs.maybeAttribute(4, "b"));
Object out = attr.resolve(vars);
assertThat(out).isEqualTo(intOf(42));
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.interpreter.AttributeFactory.Attribute in project cel-java by projectnessie.
the class AttributesTest method attributesOneofAttr.
@Test
void attributesOneofAttr() {
TypeRegistry reg = newRegistry();
Container cont = newContainer(Container.name("acme.ns"));
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf("b", new int[] { 2, 42 }), "acme.a.b", 1, "acme.ns.a.b", "found");
Activation vars = newActivation(data);
// a.b -> should resolve to acme.ns.a.b per namespace resolution rules.
Attribute attr = attrs.maybeAttribute(1, "a");
Qualifier qualB = attrs.newQualifier(null, 2, "b");
attr.addQualifier(qualB);
Object out = attr.resolve(vars);
assertThat(out).isEqualTo("found");
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
Aggregations