Search in sources :

Example 31 with TypeRegistry

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

the class AttributesTest method attributeStateTracking.

@ParameterizedTest
@MethodSource("attributeStateTrackingTests")
void attributeStateTracking(TestDef tc) {
    Source src = Source.newTextSource(tc.expr);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).isFalse();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tc.env != null) {
        env.add(tc.env);
    }
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    // Show that program planning will now produce an error.
    EvalState st = newEvalState();
    Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr(), optimize(), trackState(st));
    Activation in = newActivation(tc.in);
    Val out = i.eval(in);
    assertThat(out).extracting(o -> o.equal(tc.out)).isSameAs(True);
    for (Entry<Object, Object> iv : tc.state.entrySet()) {
        long id = ((Number) iv.getKey()).longValue();
        Object val = iv.getValue();
        Val stVal = st.value(id);
        assertThat(stVal).withFailMessage(() -> String.format("id(%d), val=%s, stVal=%s", id, val, stVal)).isNotNull();
        assertThat(stVal).withFailMessage(() -> String.format("id(%d), val=%s, stVal=%s", id, val, stVal)).isEqualTo(DefaultTypeAdapter.Instance.nativeToValue(val));
        deepEquals(String.format("id(%d)", id), stVal.value(), val);
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Arrays(java.util.Arrays) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Interpreter.optimize(org.projectnessie.cel.interpreter.Interpreter.optimize) Source(org.projectnessie.cel.common.Source) AttributePattern.newAttributePattern(org.projectnessie.cel.interpreter.AttributePattern.newAttributePattern) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) IntType(org.projectnessie.cel.common.types.IntT.IntType) True(org.projectnessie.cel.common.types.BoolT.True) Interpreter.trackState(org.projectnessie.cel.interpreter.Interpreter.trackState) Map(java.util.Map) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) Val(org.projectnessie.cel.common.types.ref.Val) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) MethodSource(org.junit.jupiter.params.provider.MethodSource) False(org.projectnessie.cel.common.types.BoolT.False) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Decls(org.projectnessie.cel.checker.Decls) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) Test(org.junit.jupiter.api.Test) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) List(java.util.List) UnknownT.unknownOf(org.projectnessie.cel.common.types.UnknownT.unknownOf) Entry(java.util.Map.Entry) DefaultTypeAdapter(org.projectnessie.cel.common.types.pb.DefaultTypeAdapter) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Any(com.google.protobuf.Any) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) NestedMessage(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage) Checker(org.projectnessie.cel.checker.Checker) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) Parser(org.projectnessie.cel.parser.Parser) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) Util.deepEquals(org.projectnessie.cel.Util.deepEquals) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) ProtoTypeRegistry.newRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newRegistry) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Type(com.google.api.expr.v1alpha1.Type) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) UnknownT(org.projectnessie.cel.common.types.UnknownT) Container(org.projectnessie.cel.common.containers.Container) Interpretable.newConstValue(org.projectnessie.cel.interpreter.Interpretable.newConstValue) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) Decl(com.google.api.expr.v1alpha1.Decl) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) Util.mapOf(org.projectnessie.cel.Util.mapOf) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) 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) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 32 with TypeRegistry

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

the class InterpreterTest method typeConversionOpt.

@ParameterizedTest
@MethodSource("typeConversionOptTests")
void typeConversionOpt(ConvTestCase tc) {
    Source src = newTextSource(tc.in);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    if (!tc.fail) {
        typeConversionOptCheck(tc, checkResult, interp);
    } else {
        Throwable err = catchThrowable(() -> typeConversionOptCheck(tc, checkResult, interp));
        assertThat(err).withFailMessage(() -> format("Expected '%s' to fail with '%s'", tc.in, tc.err)).isNotNull();
        // TODO 'err' below comes from "try-catch" of the preceding 'newInterpretable'
        // Show how the error returned during program planning is the same as the runtime
        // error which would be produced normally.
        Interpretable i2 = interp.newInterpretable(checkResult.getCheckedExpr());
        Val errVal = i2.eval(emptyActivation());
        String errValStr = errVal.toString();
        assertThat(errValStr).isEqualTo(err.getMessage());
        if (tc.err != null) {
            assertThat(errValStr).contains(tc.err);
        }
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) 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) ByteString(com.google.protobuf.ByteString) 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) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 33 with TypeRegistry

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

the class InterpreterTest method exhaustiveConditionalExpr.

@Test
void exhaustiveConditionalExpr() {
    Source src = newTextSource("a ? b < 1.0 : c == ['hello']");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    EvalState state = newEvalState();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter intr = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable interpretable = intr.newUncheckedInterpretable(parsed.getExpr(), exhaustiveEval(state));
    Activation vars = newActivation(mapOf("a", True, "b", doubleOf(0.999), "c", ListT.newStringArrayList(new String[] { "hello" })));
    Val result = interpretable.eval(vars);
    // Operator "_==_" is at Expr 7, should be evaluated in exhaustive mode
    // even though "a" is true
    Val ev = state.value(7);
    // "==" should be evaluated in exhaustive mode though unnecessary
    assertThat(ev).withFailMessage("Else expression expected to be true").isSameAs(True);
    assertThat(result).isSameAs(True);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) 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) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) 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) 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 34 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry 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 35 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry 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)

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