Search in sources :

Example 1 with Qualifier

use of org.projectnessie.cel.interpreter.AttributeFactory.Qualifier 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);
}
Also used : NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 2 with Qualifier

use of org.projectnessie.cel.interpreter.AttributeFactory.Qualifier 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);
}
Also used : NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 3 with Qualifier

use of org.projectnessie.cel.interpreter.AttributeFactory.Qualifier 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);
}
Also used : NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Qualifier

use of org.projectnessie.cel.interpreter.AttributeFactory.Qualifier 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);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Qualifier

use of org.projectnessie.cel.interpreter.AttributeFactory.Qualifier 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);
}
Also used : NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Any(com.google.protobuf.Any) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)17 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)17 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)15 Test (org.junit.jupiter.api.Test)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)14 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)14 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)13 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)13 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)12 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)10 Container (org.projectnessie.cel.common.containers.Container)4 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)4 InterpretableConst (org.projectnessie.cel.interpreter.Interpretable.InterpretableConst)4 NestedMessage (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage)2 Any (com.google.protobuf.Any)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 PartialActivation (org.projectnessie.cel.interpreter.Activation.PartialActivation)1 ConstantQualifier (org.projectnessie.cel.interpreter.AttributeFactory.ConstantQualifier)1 FieldQualifier (org.projectnessie.cel.interpreter.AttributeFactory.FieldQualifier)1