Search in sources :

Example 11 with ITypedStruct

use of org.apache.atlas.typesystem.ITypedStruct in project incubator-atlas by apache.

the class StructTest method testStructWithEmptyString.

@Test
public void testStructWithEmptyString() throws AtlasException {
    try {
        assertTrue(getTypeSystem().getTypeNames().contains("t3"));
        Struct s = new Struct(invalidStructType.getName());
        s.set("a", "");
        ITypedStruct ts = invalidStructType.convert(s, Multiplicity.REQUIRED);
    } catch (AtlasException e) {
        String err = "org.apache.atlas.typesystem.types.ValueConversionException: Cannot convert value 'org.apache.atlas.typesystem.Struct@1ba02' to datatype t3";
        Assert.assertEquals(e.toString(), err);
    }
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasException(org.apache.atlas.AtlasException) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 12 with ITypedStruct

use of org.apache.atlas.typesystem.ITypedStruct in project incubator-atlas by apache.

the class TraitTest method test1.

/*
     * Type Hierarchy is:
     *   A(a,b,c,d)
     *   B(b) extends A
     *   C(c) extends A
     *   D(d) extends B,C
     *
     * - There are a total of 11 fields in an instance of D
     * - an attribute that is hidden by a SubType can referenced by prefixing it with the
     * complete Path.
     *   For e.g. the 'b' attribute in A (that is a superType for B) is hidden the 'b' attribute
     *   in B.
     *   So it is available by the name 'A.B.D.b'
     *
     * - Another way to set attributes is to cast. Casting a 'D' instance of 'B' makes the 'A.B.D
     * .b' attribute
     *   available as 'A.B.b'. Casting one more time to an 'A' makes the 'A.B.b' attribute
     *   available as 'b'.
     */
@Test
public void test1() throws AtlasException {
    HierarchicalTypeDefinition A = createTraitTypeDef("A", null, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableSet.of("A"), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
    HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableSet.of("A"), createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
    HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableSet.of("B", "C"), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    defineTraits(A, B, C, D);
    TraitType DType = getTypeSystem().getDataType(TraitType.class, "D");
    //        for(String aName : DType.fieldMapping().fields.keySet()) {
    //            System.out.println(String.format("nameToQualifiedName.put(\"%s\", \"%s\");", aName, DType
    // .getQualifiedName(aName)));
    //        }
    Map<String, String> nameToQualifiedName = new HashMap();
    {
        nameToQualifiedName.put("d", "D.d");
        nameToQualifiedName.put("b", "B.b");
        nameToQualifiedName.put("c", "C.c");
        nameToQualifiedName.put("a", "A.a");
        nameToQualifiedName.put("A.B.D.b", "A.B.D.b");
        nameToQualifiedName.put("A.B.D.c", "A.B.D.c");
        nameToQualifiedName.put("A.B.D.d", "A.B.D.d");
        nameToQualifiedName.put("A.C.D.a", "A.C.D.a");
        nameToQualifiedName.put("A.C.D.b", "A.C.D.b");
        nameToQualifiedName.put("A.C.D.c", "A.C.D.c");
        nameToQualifiedName.put("A.C.D.d", "A.C.D.d");
    }
    Struct s1 = new Struct("D");
    s1.set("d", 1);
    s1.set("c", 1);
    s1.set("b", true);
    s1.set("a", 1);
    s1.set("A.B.D.b", true);
    s1.set("A.B.D.c", 2);
    s1.set("A.B.D.d", 2);
    s1.set("A.C.D.a", 3);
    s1.set("A.C.D.b", false);
    s1.set("A.C.D.c", 3);
    s1.set("A.C.D.d", 3);
    ITypedStruct ts = DType.convert(s1, Multiplicity.REQUIRED);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
    /*
         * cast to B and set the 'b' attribute on A.
         */
    TraitType BType = getTypeSystem().getDataType(TraitType.class, "B");
    IStruct s2 = DType.castAs(ts, "B");
    s2.set("A.B.b", false);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \tfalse\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
    /*
         * cast again to A and set the 'b' attribute on A.
         */
    TraitType AType = getTypeSystem().getDataType(TraitType.class, "A");
    IStruct s3 = BType.castAs(s2, "A");
    s3.set("b", true);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
}
Also used : HashMap(java.util.HashMap) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 13 with ITypedStruct

use of org.apache.atlas.typesystem.ITypedStruct in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method addTraits.

private void addTraits(ITypedReferenceableInstance typedInstance, AtlasVertex instanceVertex, ClassType classType) throws AtlasException {
    for (String traitName : typedInstance.getTraits()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("mapping trait {}", traitName);
        }
        GraphHelper.addProperty(instanceVertex, Constants.TRAIT_NAMES_PROPERTY_KEY, traitName);
        ITypedStruct traitInstance = (ITypedStruct) typedInstance.getTrait(traitName);
        // add the attributes for the trait instance
        mapTraitInstanceToVertex(traitInstance, classType, instanceVertex);
    }
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct)

Example 14 with ITypedStruct

use of org.apache.atlas.typesystem.ITypedStruct in project incubator-atlas by apache.

the class EnumTest method testStruct.

@Test
public void testStruct() throws AtlasException {
    TypeSystem ts = getTypeSystem();
    defineEnums(ts);
    StructType structType = ts.defineStructType("ts", true, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE), createOptionalAttrDef("enum1", ts.getDataType(EnumType.class, "HiveObjectType")), createOptionalAttrDef("e", DataTypes.INT_TYPE), createOptionalAttrDef("f", DataTypes.INT_TYPE), createOptionalAttrDef("g", DataTypes.LONG_TYPE), createOptionalAttrDef("enum2", ts.getDataType(EnumType.class, "PrincipalType")), createOptionalAttrDef("h", DataTypes.FLOAT_TYPE), createOptionalAttrDef("i", DataTypes.DOUBLE_TYPE), createOptionalAttrDef("j", DataTypes.BIGINTEGER_TYPE), createOptionalAttrDef("k", DataTypes.BIGDECIMAL_TYPE), createOptionalAttrDef("enum3", ts.getDataType(EnumType.class, "TxnState")), createOptionalAttrDef("l", DataTypes.DATE_TYPE), createOptionalAttrDef("m", ts.defineArrayType(DataTypes.INT_TYPE)), createOptionalAttrDef("n", ts.defineArrayType(DataTypes.BIGDECIMAL_TYPE)), createOptionalAttrDef("o", ts.defineMapType(DataTypes.STRING_TYPE, DataTypes.DOUBLE_TYPE)), createOptionalAttrDef("enum4", ts.getDataType(EnumType.class, "LockLevel")));
    Struct s = createStructWithEnum("ts");
    ITypedStruct typedS = structType.convert(s, Multiplicity.REQUIRED);
    Assert.assertEquals(typedS.toString(), "{\n" + "\ta : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\td : \t2\n" + "\tenum1 : \tGLOBAL\n" + "\te : \t1\n" + "\tf : \t1\n" + "\tg : \t1\n" + "\tenum2 : \tUSER\n" + "\th : \t1.0\n" + "\ti : \t1.0\n" + "\tj : \t1\n" + "\tk : \t1\n" + "\tenum3 : \tCOMMITTED\n" + "\tl : \t" + TEST_DATE + "\n" + "\tm : \t[1, 1]\n" + "\tn : \t[1.1, 1.1]\n" + "\to : \t{a=1.0, b=2.0}\n" + "\tenum4 : \tPARTITION\n" + "}");
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 15 with ITypedStruct

use of org.apache.atlas.typesystem.ITypedStruct in project incubator-atlas by apache.

the class ClassType method createInstanceWithTraits.

public ITypedReferenceableInstance createInstanceWithTraits(Id id, AtlasSystemAttributes systemAttributes, Referenceable r, String... traitNames) throws AtlasException {
    ImmutableMap.Builder<String, ITypedStruct> b = new ImmutableBiMap.Builder<>();
    if (traitNames != null) {
        for (String t : traitNames) {
            TraitType tType = typeSystem.getDataType(TraitType.class, t);
            IStruct iTraitObject = r == null ? null : r.getTrait(t);
            ITypedStruct trait = iTraitObject == null ? tType.createInstance() : tType.convert(iTraitObject, Multiplicity.REQUIRED);
            b.put(t, trait);
        }
    }
    return new ReferenceableInstance(id == null ? new Id(getName()) : id, getName(), systemAttributes, fieldMapping, new boolean[fieldMapping.fields.size()], new boolean[fieldMapping.fields.size()], fieldMapping.numBools == 0 ? null : new boolean[fieldMapping.numBools], fieldMapping.numBytes == 0 ? null : new byte[fieldMapping.numBytes], fieldMapping.numShorts == 0 ? null : new short[fieldMapping.numShorts], fieldMapping.numInts == 0 ? null : new int[fieldMapping.numInts], fieldMapping.numLongs == 0 ? null : new long[fieldMapping.numLongs], fieldMapping.numFloats == 0 ? null : new float[fieldMapping.numFloats], fieldMapping.numDoubles == 0 ? null : new double[fieldMapping.numDoubles], fieldMapping.numBigDecimals == 0 ? null : new BigDecimal[fieldMapping.numBigDecimals], fieldMapping.numBigInts == 0 ? null : new BigInteger[fieldMapping.numBigInts], fieldMapping.numDates == 0 ? null : new Date[fieldMapping.numDates], fieldMapping.numStrings == 0 ? null : new String[fieldMapping.numStrings], fieldMapping.numArrays == 0 ? null : new ImmutableList[fieldMapping.numArrays], fieldMapping.numMaps == 0 ? null : new ImmutableMap[fieldMapping.numMaps], fieldMapping.numStructs == 0 ? null : new StructInstance[fieldMapping.numStructs], fieldMapping.numReferenceables == 0 ? null : new ReferenceableInstance[fieldMapping.numReferenceables], fieldMapping.numReferenceables == 0 ? null : new Id[fieldMapping.numReferenceables], b.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) StructInstance(org.apache.atlas.typesystem.persistence.StructInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ImmutableMap(com.google.common.collect.ImmutableMap) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ReferenceableInstance(org.apache.atlas.typesystem.persistence.ReferenceableInstance) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id) IStruct(org.apache.atlas.typesystem.IStruct)

Aggregations

ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)29 Test (org.testng.annotations.Test)14 IStruct (org.apache.atlas.typesystem.IStruct)13 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)13 Struct (org.apache.atlas.typesystem.Struct)11 TraitType (org.apache.atlas.typesystem.types.TraitType)6 AtlasException (org.apache.atlas.AtlasException)5 ImmutableList (com.google.common.collect.ImmutableList)4 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)4 List (java.util.List)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 Id (org.apache.atlas.typesystem.persistence.Id)3 ArrayList (java.util.ArrayList)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 EntityChangeListener (org.apache.atlas.listener.EntityChangeListener)2 Referenceable (org.apache.atlas.typesystem.Referenceable)2 TypesDef (org.apache.atlas.typesystem.TypesDef)2 StructType (org.apache.atlas.typesystem.types.StructType)2 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)2 BeforeTest (org.testng.annotations.BeforeTest)2