Search in sources :

Example 6 with PartialActivation

use of org.projectnessie.cel.interpreter.Activation.PartialActivation 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)

Aggregations

PartialActivation (org.projectnessie.cel.interpreter.Activation.PartialActivation)6 Test (org.junit.jupiter.api.Test)5 CEL.astToString (org.projectnessie.cel.CEL.astToString)4 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)4 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)4 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)4 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)4 Env.newEnv (org.projectnessie.cel.Env.newEnv)4 EvalResult (org.projectnessie.cel.Program.EvalResult)4 UnknownT (org.projectnessie.cel.common.types.UnknownT)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)2 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)2 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)2 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 Container (org.projectnessie.cel.common.containers.Container)1 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)1 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)1