Search in sources :

Example 11 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class InterpreterTest method program.

static Program program(TestCase tst, InterpretableDecorator... opts) {
    // Configure the package.
    Container cont = Container.defaultContainer;
    if (tst.container != null) {
        cont = testContainer(tst.container);
    }
    if (tst.abbrevs != null) {
        cont = Container.newContainer(Container.name(cont.name()), Container.abbrevs(tst.abbrevs));
    }
    TypeRegistry reg;
    reg = newRegistry();
    if (tst.types != null) {
        reg = newRegistry(tst.types);
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    if (tst.attrs != null) {
        attrs = tst.attrs;
    }
    // Configure the environment.
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tst.env != null) {
        env.add(tst.env);
    }
    // Configure the program input.
    Activation vars = emptyActivation();
    if (tst.in != null) {
        vars = newActivation(tst.in);
    }
    // Adapt the test output, if needed.
    if (tst.out != null) {
        tst.out = reg.nativeToValue(tst.out);
    }
    Dispatcher disp = newDispatcher();
    disp.add(standardOverloads());
    if (tst.funcs != null) {
        disp.add(tst.funcs);
    }
    Interpreter interp = newInterpreter(disp, cont, reg, reg, attrs);
    // Parse the expression.
    Source s = newTextSource(tst.expr);
    ParseResult parsed = Parser.parseAllMacros(s);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Interpretable prg;
    if (tst.unchecked) {
        // Build the program plan.
        prg = interp.newUncheckedInterpretable(parsed.getExpr(), opts);
        return new Program(prg, vars);
    }
    // Check the expression.
    CheckResult checkResult = Checker.Check(parsed, s, env);
    assertThat(checkResult.hasErrors()).withFailMessage(() -> checkResult.getErrors().toDisplayString()).isFalse();
    // Build the program plan.
    prg = interp.newInterpretable(checkResult.getCheckedExpr(), opts);
    return new Program(prg, vars);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Dispatcher.newDispatcher(org.projectnessie.cel.interpreter.Dispatcher.newDispatcher) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource)

Example 12 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class InterpreterTest method logicalAndMissingType.

@Test
void logicalAndMissingType() {
    Source src = newTextSource("a && TestProto{c: true}.c");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    TypeRegistry reg = newRegistry();
    Container cont = Container.defaultContainer;
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter intr = newStandardInterpreter(cont, reg, reg, attrs);
    assertThatThrownBy(() -> intr.newUncheckedInterpretable(parsed.getExpr())).isInstanceOf(IllegalStateException.class).hasMessage("unknown type: TestProto");
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 13 with Container

use of org.projectnessie.cel.common.containers.Container 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 14 with Container

use of org.projectnessie.cel.common.containers.Container 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)

Aggregations

Container (org.projectnessie.cel.common.containers.Container)14 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)13 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)13 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)12 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)12 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)11 Test (org.junit.jupiter.api.Test)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)10 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)10 Source (org.projectnessie.cel.common.Source)9 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)9 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)8 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)8 Interpreter.newInterpreter (org.projectnessie.cel.interpreter.Interpreter.newInterpreter)7 CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)6 CheckerEnv.newStandardCheckerEnv (org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv)6 Val (org.projectnessie.cel.common.types.ref.Val)6 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)6