Search in sources :

Example 26 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class PbTypeDescriptionTest method checkedType.

@Test
void checkedType() {
    Db pbdb = newDb();
    TestAllTypes msg = TestAllTypes.getDefaultInstance();
    String msgName = msg.getDescriptorForType().getFullName();
    pbdb.registerMessage(msg);
    PbTypeDescription td = pbdb.describeType(msgName);
    assertThat(td).isNotNull();
    FieldDescription field = td.fieldByName("map_string_string");
    assertThat(field).isNotNull();
    Type mapType = Decls.newMapType(Decls.String, Decls.String);
    assertThat(field.checkedType()).isEqualTo(mapType);
    field = td.fieldByName("repeated_nested_message");
    assertThat(field).isNotNull();
    Type listType = Decls.newListType(Decls.newObjectType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage"));
    assertThat(field.checkedType()).isEqualTo(listType);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) ByteString(com.google.protobuf.ByteString) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) NestedTestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes) Db.newDb(org.projectnessie.cel.common.types.pb.Db.newDb) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class JacksonTypeDescriptionTest method checkMapType.

private void checkMapType(JacksonRegistry reg, String prop, Class<?> keyClass, com.google.api.expr.v1alpha1.Type keyType, Class<?> valueClass, com.google.api.expr.v1alpha1.Type valueType) {
    JacksonFieldType ft = (JacksonFieldType) reg.findFieldType(CollectionsObject.class.getName(), prop);
    assertThat(ft).isNotNull();
    JavaType javaType = ft.propertyWriter().getType();
    assertThat(javaType).extracting(JavaType::isMapLikeType).isEqualTo(true);
    assertThat(javaType.getKeyType()).extracting(JavaType::getRawClass).isSameAs(keyClass);
    assertThat(javaType.getContentType()).extracting(JavaType::getRawClass).isSameAs(valueClass);
    assertThat(ft.type).extracting(com.google.api.expr.v1alpha1.Type::getMapType).extracting(MapType::getKeyType, MapType::getValueType).containsExactly(keyType, valueType);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ListType(com.google.api.expr.v1alpha1.Type.ListType) JavaType(com.fasterxml.jackson.databind.JavaType) MapType(com.google.api.expr.v1alpha1.Type.MapType) InnerType(org.projectnessie.cel.types.jackson.types.InnerType)

Example 28 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class JacksonTypeDescriptionTest method collectionsObjectTypeTest.

@Test
void collectionsObjectTypeTest() throws Exception {
    CollectionsObject collectionsObject = new CollectionsObject();
    // populate (primitive) map types
    collectionsObject.stringBooleanMap = singletonMap("a", true);
    collectionsObject.byteShortMap = singletonMap((byte) 1, (short) 2);
    collectionsObject.intLongMap = singletonMap(1, 2L);
    collectionsObject.ulongTimestampMap = singletonMap(ULong.valueOf(1), Timestamp.newBuilder().setSeconds(1).build());
    collectionsObject.ulongZonedDateTimeMap = singletonMap(ULong.valueOf(1), ZonedDateTime.of(LocalDateTime.ofEpochSecond(1, 0, ZoneOffset.UTC), ZoneId.of("UTC")));
    collectionsObject.stringProtoDurationMap = singletonMap("a", Duration.newBuilder().setSeconds(1).build());
    collectionsObject.stringJavaDurationMap = singletonMap("a", java.time.Duration.ofSeconds(1));
    collectionsObject.stringBytesMap = singletonMap("a", ByteString.copyFrom(new byte[] { (byte) 1 }));
    collectionsObject.floatDoubleMap = singletonMap(1f, 2d);
    // populate (primitive) list types
    collectionsObject.stringList = asList("a", "b", "c");
    collectionsObject.booleanList = asList(true, true, false, false);
    collectionsObject.byteList = asList((byte) 1, (byte) 2, (byte) 3);
    collectionsObject.shortList = asList((short) 4, (short) 5, (short) 6);
    collectionsObject.intList = asList(7, 8, 9);
    collectionsObject.longList = asList(10L, 11L, 12L);
    collectionsObject.ulongList = asList(ULong.valueOf(1), ULong.valueOf(2), ULong.valueOf(3));
    collectionsObject.timestampList = asList(Timestamp.newBuilder().setSeconds(1).build(), Timestamp.newBuilder().setSeconds(2).build(), Timestamp.newBuilder().setSeconds(3).build());
    collectionsObject.zonedDateTimeList = asList(ZonedDateTime.of(LocalDateTime.ofEpochSecond(1, 0, ZoneOffset.UTC), ZoneId.of("UTC")), ZonedDateTime.of(LocalDateTime.ofEpochSecond(2, 0, ZoneOffset.UTC), ZoneId.of("UTC")), ZonedDateTime.of(LocalDateTime.ofEpochSecond(3, 0, ZoneOffset.UTC), ZoneId.of("UTC")));
    collectionsObject.durationList = asList(Duration.newBuilder().setSeconds(1).build(), Duration.newBuilder().setSeconds(2).build(), Duration.newBuilder().setSeconds(3).build());
    collectionsObject.javaDurationList = asList(java.time.Duration.ofSeconds(1), java.time.Duration.ofSeconds(2), java.time.Duration.ofSeconds(3));
    collectionsObject.bytesList = asList(ByteString.copyFrom(new byte[] { (byte) 1 }), ByteString.copyFrom(new byte[] { (byte) 2 }), ByteString.copyFrom(new byte[] { (byte) 3 }));
    collectionsObject.floatList = asList(1f, 2f, 3f);
    collectionsObject.doubleList = asList(1d, 2d, 3d);
    // populate inner/nested type list/map
    InnerType inner1 = new InnerType();
    inner1.intProp = 1;
    inner1.wrappedIntProp = 2;
    collectionsObject.stringInnerMap = singletonMap("a", inner1);
    InnerType inner2 = new InnerType();
    inner2.intProp = 3;
    inner2.wrappedIntProp = 4;
    collectionsObject.innerTypes = asList(inner1, inner2);
    // populate enum-related fields
    collectionsObject.anEnum = AnEnum.ENUM_VALUE_2;
    collectionsObject.anEnumList = asList(AnEnum.ENUM_VALUE_2, AnEnum.ENUM_VALUE_3);
    collectionsObject.anEnumStringMap = singletonMap(AnEnum.ENUM_VALUE_2, "a");
    collectionsObject.stringAnEnumMap = singletonMap("a", AnEnum.ENUM_VALUE_2);
    // prepare registry
    JacksonRegistry reg = (JacksonRegistry) newRegistry();
    reg.register(CollectionsObject.class);
    Val collectionsVal = reg.nativeToValue(collectionsObject);
    assertThat(collectionsVal).isInstanceOf(ObjectT.class);
    ObjectT obj = (ObjectT) collectionsVal;
    for (String field : CollectionsObject.ALL_PROPERTIES) {
        assertThat(obj.isSet(stringOf(field))).isSameAs(True);
        assertThat(obj.get(stringOf(field))).isNotNull();
        Val fieldVal = obj.get(stringOf(field));
        Object fieldObj = CollectionsObject.class.getDeclaredField(field).get(collectionsObject);
        if (fieldObj instanceof Map) {
            assertThat(fieldVal).isInstanceOf(MapT.class);
        } else if (fieldObj instanceof List) {
            assertThat(fieldVal).isInstanceOf(ListT.class);
        }
        assertThat(fieldVal.equal(reg.nativeToValue(fieldObj))).isSameAs(True);
    }
    // check a few properties manually/explicitly
    MapT mapVal = (MapT) obj.get(stringOf("intLongMap"));
    assertThat(mapVal).extracting(MapT::size, m -> m.contains(intOf(42)), m -> m.contains(intOf(1)), m -> m.contains(intOf(2)), m -> m.contains(intOf(3)), m -> m.get(intOf(1))).containsExactly(intOf(1), False, True, False, False, intOf(2));
    ListT listVal = (ListT) obj.get(stringOf("ulongList"));
    assertThat(listVal).extracting(ListT::size, l -> l.contains(uintOf(42)), l -> l.contains(uintOf(1)), l -> l.contains(uintOf(2)), l -> l.contains(uintOf(3)), l -> l.get(intOf(0)), l -> l.get(intOf(1)), l -> l.get(intOf(2))).containsExactly(intOf(3), False, True, True, True, uintOf(1), uintOf(2), uintOf(3));
    mapVal = (MapT) obj.get(stringOf("stringInnerMap"));
    assertThat(mapVal).extracting(MapT::size, m -> m.contains(stringOf("42")), m -> m.contains(stringOf("a"))).containsExactly(intOf(1), False, True);
    ObjectT i = (ObjectT) mapVal.get(stringOf("a"));
    assertThat(i).extracting(o -> o.get(stringOf("intProp")), o -> o.get(stringOf("wrappedIntProp"))).containsExactly(intOf(1), intOf(2));
    listVal = (ListT) obj.get(stringOf("innerTypes"));
    assertThat(listVal).extracting(ListT::size).isEqualTo(intOf(2));
    i = (ObjectT) listVal.get(intOf(0));
    assertThat(i).extracting(o -> o.get(stringOf("intProp")), o -> o.get(stringOf("wrappedIntProp"))).containsExactly(intOf(1), intOf(2));
    i = (ObjectT) listVal.get(intOf(1));
    assertThat(i).extracting(o -> o.get(stringOf("intProp")), o -> o.get(stringOf("wrappedIntProp"))).containsExactly(intOf(3), intOf(4));
    // verify enums
    Val x = obj.get(stringOf("anEnum"));
    assertThat(x).isInstanceOf(IntT.class).isEqualTo(intOf(AnEnum.ENUM_VALUE_2.ordinal()));
    listVal = (ListT) obj.get(stringOf("anEnumList"));
    assertThat(listVal).extracting(l -> l.get(intOf(0)), l -> l.get(intOf(1))).containsExactly(intOf(AnEnum.ENUM_VALUE_2.ordinal()), intOf(AnEnum.ENUM_VALUE_3.ordinal()));
    mapVal = (MapT) obj.get(stringOf("anEnumStringMap"));
    assertThat(mapVal).extracting(l -> l.get(intOf(AnEnum.ENUM_VALUE_2.ordinal()))).isEqualTo(stringOf("a"));
    mapVal = (MapT) obj.get(stringOf("stringAnEnumMap"));
    assertThat(mapVal).extracting(l -> l.get(stringOf("a"))).isEqualTo(intOf(AnEnum.ENUM_VALUE_2.ordinal()));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ListT(org.projectnessie.cel.common.types.ListT) MapT(org.projectnessie.cel.common.types.MapT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) AnEnum(org.projectnessie.cel.types.jackson.types.AnEnum) IntT(org.projectnessie.cel.common.types.IntT) ULong(org.projectnessie.cel.common.ULong) Timestamp(com.google.protobuf.Timestamp) ListType(com.google.api.expr.v1alpha1.Type.ListType) Err(org.projectnessie.cel.common.types.Err) True(org.projectnessie.cel.common.types.BoolT.True) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) JavaType(com.fasterxml.jackson.databind.JavaType) ZoneOffset(java.time.ZoneOffset) Val(org.projectnessie.cel.common.types.ref.Val) JacksonRegistry.newRegistry(org.projectnessie.cel.types.jackson.JacksonRegistry.newRegistry) MapType(com.google.api.expr.v1alpha1.Type.MapType) False(org.projectnessie.cel.common.types.BoolT.False) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) NullT(org.projectnessie.cel.common.types.NullT) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) ZoneId(java.time.ZoneId) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) CollectionsObject(org.projectnessie.cel.types.jackson.types.CollectionsObject) TypeT(org.projectnessie.cel.common.types.TypeT) InnerType(org.projectnessie.cel.types.jackson.types.InnerType) Duration(com.google.protobuf.Duration) List(java.util.List) ObjectT(org.projectnessie.cel.common.types.ObjectT) Checked(org.projectnessie.cel.common.types.pb.Checked) TypeKindCase(com.google.api.expr.v1alpha1.Type.TypeKindCase) IntT(org.projectnessie.cel.common.types.IntT) InnerType(org.projectnessie.cel.types.jackson.types.InnerType) ByteString(com.google.protobuf.ByteString) CollectionsObject(org.projectnessie.cel.types.jackson.types.CollectionsObject) ListT(org.projectnessie.cel.common.types.ListT) ObjectT(org.projectnessie.cel.common.types.ObjectT) CollectionsObject(org.projectnessie.cel.types.jackson.types.CollectionsObject) Arrays.asList(java.util.Arrays.asList) List(java.util.List) MapT(org.projectnessie.cel.common.types.MapT) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.jupiter.api.Test)

Example 29 with Type

use of com.google.api.expr.v1alpha1.Type in project java-smt by sosy-lab.

the class CVC4FormulaCreator method getFormulaType.

@SuppressWarnings("unchecked")
@Override
public <T extends Formula> FormulaType<T> getFormulaType(T pFormula) {
    Type t = extractInfo(pFormula).getType();
    if (pFormula instanceof BitvectorFormula) {
        checkArgument(t.isBitVector(), "BitvectorFormula with actual type %s: %s", t, pFormula);
        return (FormulaType<T>) getFormulaType(extractInfo(pFormula));
    } else if (pFormula instanceof FloatingPointFormula) {
        checkArgument(t.isFloatingPoint(), "FloatingPointFormula with actual type %s: %s", t, pFormula);
        edu.stanford.CVC4.FloatingPointType fpType = new edu.stanford.CVC4.FloatingPointType(t);
        return (FormulaType<T>) FormulaType.getFloatingPointType((int) fpType.getExponentSize(), // without sign bit
        (int) fpType.getSignificandSize() - 1);
    } else if (pFormula instanceof ArrayFormula<?, ?>) {
        FormulaType<T> arrayIndexType = getArrayFormulaIndexType((ArrayFormula<T, T>) pFormula);
        FormulaType<T> arrayElementType = getArrayFormulaElementType((ArrayFormula<T, T>) pFormula);
        return (FormulaType<T>) FormulaType.getArrayType(arrayIndexType, arrayElementType);
    }
    return super.getFormulaType(pFormula);
}
Also used : BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) CVC4BitvectorFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4BitvectorFormula) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) CVC4.vectorType(edu.stanford.CVC4.vectorType) Type(edu.stanford.CVC4.Type) BitVectorType(edu.stanford.CVC4.BitVectorType) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) ArrayType(edu.stanford.CVC4.ArrayType) FunctionType(edu.stanford.CVC4.FunctionType) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) CVC4FloatingPointFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4FloatingPointFormula) ArrayFormula(org.sosy_lab.java_smt.api.ArrayFormula) CVC4ArrayFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4ArrayFormula) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType)

Example 30 with Type

use of com.google.api.expr.v1alpha1.Type in project java-smt by sosy-lab.

the class CVC4FormulaCreator method declareUFImpl.

@Override
public Expr declareUFImpl(String pName, Type pReturnType, List<Type> pArgTypes) {
    Expr exp = functionsCache.get(pName);
    if (exp == null) {
        vectorType args = new vectorType();
        for (Type t : pArgTypes) {
            args.add(t);
        }
        exp = exprManager.mkVar(pName, exprManager.mkFunctionType(args, pReturnType));
        functionsCache.put(pName, exp);
    }
    return exp;
}
Also used : FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) CVC4.vectorType(edu.stanford.CVC4.vectorType) Type(edu.stanford.CVC4.Type) BitVectorType(edu.stanford.CVC4.BitVectorType) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) ArrayType(edu.stanford.CVC4.ArrayType) FunctionType(edu.stanford.CVC4.FunctionType) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) Expr(edu.stanford.CVC4.Expr) CVC4.vectorType(edu.stanford.CVC4.vectorType)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 MapType (com.google.api.expr.v1alpha1.Type.MapType)15 Type (edu.stanford.CVC4.Type)14 Type (com.google.spanner.v1.Type)12 Test (org.junit.jupiter.api.Test)12 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)11 ByteString (com.google.protobuf.ByteString)11 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 ArrayList (java.util.ArrayList)11 Type (org.apache.xbean.asm9.Type)10 Expr (com.google.api.expr.v1alpha1.Expr)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 ListValue (com.google.protobuf.ListValue)8 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7