Search in sources :

Example 36 with TypeRegistry

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

the class PruneTest method prune.

@ParameterizedTest
@MethodSource("pruneTestCases")
void prune(TestCase tc) {
    ParseResult parseResult = Parser.parseAllMacros(Source.newStringSource(tc.expr, "<input>"));
    if (parseResult.hasErrors()) {
        fail(parseResult.getErrors().toDisplayString());
    }
    EvalState state = newEvalState();
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = newPartialAttributeFactory(Container.defaultContainer, reg, reg);
    Interpreter interp = newStandardInterpreter(Container.defaultContainer, reg, reg, attrs);
    Interpretable interpretable = interp.newUncheckedInterpretable(parseResult.getExpr(), exhaustiveEval(state));
    interpretable.eval(testActivation(tc.in));
    Expr newExpr = pruneAst(parseResult.getExpr(), state);
    String actual = Unparser.unparse(newExpr, null);
    assertThat(actual).isEqualTo(tc.expect);
}
Also used : Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Expr(com.google.api.expr.v1alpha1.Expr) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 37 with TypeRegistry

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

the class AttributePatternsTest method unknownResolution.

@ParameterizedTest
@MethodSource("attributePatternsTestCases")
void unknownResolution(PatternTest tst) {
    Assumptions.assumeTrue(tst.disabled == null, tst.disabled);
    TypeRegistry reg = newRegistry();
    for (int i = 0; i < tst.matches.length; i++) {
        Attr m = tst.matches[i];
        Container cont = Container.defaultContainer;
        if (m.unchecked) {
            cont = Container.newContainer(Container.name(m.container));
        }
        AttributeFactory fac = newPartialAttributeFactory(cont, reg, reg);
        Attribute attr = genAttr(fac, m);
        PartialActivation partVars = newPartialActivation(emptyActivation(), tst.pattern);
        Object val = attr.resolve(partVars);
        assertThat(val).withFailMessage(() -> String.format("match: '%s', gen: '%s'", m, attr)).isInstanceOf(UnknownT.class);
    }
    for (int i = 0; i < tst.misses.length; i++) {
        Attr m = tst.misses[i];
        Container cont = Container.defaultContainer;
        if (m.unchecked) {
            cont = Container.newContainer(Container.name(m.container));
        }
        AttributeFactory fac = newPartialAttributeFactory(cont, reg, reg);
        Attribute attr = genAttr(fac, m);
        PartialActivation partVars = newPartialActivation(emptyActivation(), tst.pattern);
        assertThatThrownBy(() -> attr.resolve(partVars), "miss: '%s', gen: '%s'", m, attr);
    }
}
Also used : Container(org.projectnessie.cel.common.containers.Container) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 38 with TypeRegistry

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

the class AttributePatternsTest method crossReference.

@Test
void crossReference() {
    TypeRegistry reg = newRegistry();
    AttributeFactory fac = newPartialAttributeFactory(Container.defaultContainer, reg, reg);
    NamespacedAttribute a = fac.absoluteAttribute(1, "a");
    NamespacedAttribute b = fac.absoluteAttribute(2, "b");
    a.addQualifier(b);
    // Ensure that var a[b], the dynamic index into var 'a' is the unknown value
    // returned from attribute resolution.
    PartialActivation partVars = newPartialActivation(mapOf("a", new long[] { 1L, 2L }), newAttributePattern("b"));
    Object val = a.resolve(partVars);
    assertThat(val).isEqualTo(unknownOf(2));
    // Ensure that a[b], the dynamic index into var 'a' is the unknown value
    // returned from attribute resolution. Note, both 'a' and 'b' have unknown attribute
    // patterns specified. This changes the evaluation behavior slightly, but the end
    // result is the same.
    partVars = newPartialActivation(mapOf("a", new long[] { 1L, 2L }), newAttributePattern("a").qualInt(0), newAttributePattern("b"));
    val = a.resolve(partVars);
    assertThat(val).isEqualTo(unknownOf(2));
    // Note, that only 'a[0].c' will result in an unknown result since both 'a' and 'b'
    // have values. However, since the attribute being pattern matched is just 'a.b',
    // the outcome will indicate that 'a[b]' is unknown.
    partVars = newPartialActivation(mapOf("a", new long[] { 1, 2 }, "b", 0), newAttributePattern("a").qualInt(0).qualString("c"));
    val = a.resolve(partVars);
    assertThat(val).isEqualTo(unknownOf(2));
    // Test a positive case that returns a valid value even though the attribugte factory
    // is the partial attribute factory.
    partVars = newPartialActivation(mapOf("a", new long[] { 1, 2 }, "b", 0));
    val = a.resolve(partVars);
    assertThat(val).isEqualTo(1L);
    // Ensure the unknown attribute id moves when the attribute becomes more specific.
    partVars = newPartialActivation(mapOf("a", new long[] { 1, 2 }, "b", 0), newAttributePattern("a").qualInt(0).qualString("c"));
    // Qualify a[b] with 'c', a[b].c
    Qualifier c = fac.newQualifier(null, 3, "c");
    a.addQualifier(c);
    // The resolve step should return unknown
    val = a.resolve(partVars);
    assertThat(val).isEqualTo(unknownOf(3));
}
Also used : PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) 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 39 with TypeRegistry

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

the class AttributesTest method attributesRelativeAttr_Relative.

@Test
void attributesRelativeAttr_Relative() {
    Container cont = newContainer(Container.name("acme.ns"));
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Map<Object, Object> data = mapOf("a", mapOf(-1, mapOf("first", 1, "second", 2, "third", 3)), "b", 2L);
    Activation vars = newActivation(data);
    // The environment declares the following variables:
    // {
    // a: {
    // -1: {
    // "first": 1u,
    // "second": 2u,
    // "third": 3u,
    // }
    // },
    // b: 2u,
    // }
    // 
    // The map of input variables is also re-used as a map-literal <obj> in the expression.
    // 
    // The relative object under test is the following map literal.
    // <mp> {
    // 1u: "first",
    // 2u: "second",
    // 3u: "third",
    // }
    // 
    // The expression under test is:
    // <obj>.a[-1][<mp>[b]]
    // 
    // This is equivalent to:
    // <obj>.a[-1]["second"] -> 2u
    InterpretableConst obj = newConstValue(1, reg.nativeToValue(data));
    InterpretableConst mp = newConstValue(1, reg.nativeToValue(mapOf(1, "first", 2, "second", 3, "third")));
    Attribute relAttr = attrs.relativeAttribute(4, mp);
    Qualifier qualB = attrs.newQualifier(null, 5, attrs.absoluteAttribute(5, "b"));
    relAttr.addQualifier(qualB);
    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(relAttr);
    Object out = attr.resolve(vars);
    assertThat(out).isEqualTo(intOf(2));
    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 40 with TypeRegistry

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

the class ProviderTest method typeRegistryNewValue.

@Test
void typeRegistryNewValue() {
    TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
    Val sourceInfo = reg.newValue("google.api.expr.v1alpha1.SourceInfo", mapOf("location", stringOf("TestTypeRegistryNewValue"), "line_offsets", newGenericArrayList(reg, new Long[] { 0L, 2L }), "positions", newMaybeWrappedMap(reg, mapOf(1L, 2, 3L, 4))));
    assertThat(sourceInfo).matches(v -> !Err.isError(v));
    Message info = (Message) sourceInfo.value();
    SourceInfo srcInfo = SourceInfo.newBuilder().mergeFrom(info).build();
    assertThat(srcInfo).extracting(SourceInfo::getLocation, SourceInfo::getLineOffsetsList, SourceInfo::getPositionsMap).containsExactly("TestTypeRegistryNewValue", asList(0, 2), mapOf(1L, 2L, 3L, 4L));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Message(com.google.protobuf.Message) SourceInfo(com.google.api.expr.v1alpha1.SourceInfo) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

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