Search in sources :

Example 26 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class AttributesTest method attributesConditionalAttr_TrueBranch.

@Test
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);
// Note: migrated to JMH
}
Also used : NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) 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 27 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class AttributesTest method benchmarkResolverCustomQualifier.

@Test
void benchmarkResolverCustomQualifier() {
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = new CustAttrFactory(newAttributeFactory(Container.defaultContainer, reg, reg));
    NestedMessage msg = NestedMessage.newBuilder().setBb(123).build();
    Activation vars = newActivation(mapOf("msg", msg));
    NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
    Qualifier qualBB = attrs.newQualifier(Type.newBuilder().setMessageType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage").build(), 2, "bb");
    attr.addQualifier(qualBB);
// Note: Migrated to JMH
}
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) NestedMessage(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class AttributesTest method attributesMissingMsg.

@Test
void attributesMissingMsg() {
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
    Any any = Any.pack(TestAllTypes.getDefaultInstance());
    Activation vars = newActivation(mapOf("missing_msg", any));
    // missing_msg.field
    NamespacedAttribute attr = attrs.absoluteAttribute(1, "missing_msg");
    Qualifier field = attrs.newQualifier(null, 2, "field");
    attr.addQualifier(field);
    assertThatThrownBy(() -> attr.resolve(vars)).isInstanceOf(IllegalStateException.class).hasMessage("unknown type 'google.api.expr.test.v1.proto3.TestAllTypes'");
}
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)

Example 29 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class AttributesTest method attributesRelativeAttr_Conditional.

@Test
void attributesRelativeAttr_Conditional() {
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
    Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", new int[] { 0, 1 }, "c", new Object[] { 1, 0 });
    Activation vars = newActivation(data);
    // The relative attribute under test is applied to a map literal:
    // {
    // a: {-1: [2, 42], b: 1}
    // b: [0, 1],
    // c: {1, 0},
    // }
    // 
    // The expression being evaluated is:
    // <map-literal>.a[-1][(false ? b : c)[0]] -> 42
    // 
    // Effectively the same as saying <map-literal>.a[-1][c[0]]
    InterpretableConst cond = newConstValue(2, False);
    Attribute condAttr = attrs.conditionalAttribute(4, cond, attrs.absoluteAttribute(5, "b"), attrs.absoluteAttribute(6, "c"));
    Qualifier qual0 = attrs.newQualifier(null, 7, 0);
    condAttr.addQualifier(qual0);
    InterpretableConst obj = newConstValue(1, reg.nativeToValue(data));
    Attribute attr = attrs.relativeAttribute(1, obj);
    Qualifier qualA = attrs.newQualifier(null, 2, "a");
    Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
    attr.addQualifier(qualA);
    attr.addQualifier(qualNeg1);
    attr.addQualifier(condAttr);
    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 30 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class AttributesTest method resolverCustomQualifier.

@Test
void resolverCustomQualifier() {
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = new CustAttrFactory(newAttributeFactory(Container.defaultContainer, reg, reg));
    NestedMessage msg = NestedMessage.newBuilder().setBb(123).build();
    Activation vars = newActivation(mapOf("msg", msg));
    NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
    Qualifier qualBB = attrs.newQualifier(Type.newBuilder().setMessageType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage").build(), 2, "bb");
    attr.addQualifier(qualBB);
    Object out = attr.resolve(vars);
    assertThat(out).isEqualTo(123);
    assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
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) NestedMessage(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)51 Test (org.junit.jupiter.api.Test)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)26 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)25 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)21 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)21 Val (org.projectnessie.cel.common.types.ref.Val)20 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)20 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)19 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)16 Container (org.projectnessie.cel.common.containers.Container)14 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)12 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)12 MethodSource (org.junit.jupiter.params.provider.MethodSource)11 ProtoTypeRegistry (org.projectnessie.cel.common.types.pb.ProtoTypeRegistry)10 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)10 Source (org.projectnessie.cel.common.Source)9 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)9 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)8