Search in sources :

Example 6 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class TestAtlasStructDef method testStructDefSetAttributeDefs.

@Test
public void testStructDefSetAttributeDefs() {
    AtlasStructDef structDef = ModelTestUtil.newStructDef();
    List<AtlasAttributeDef> oldAttributes = structDef.getAttributeDefs();
    List<AtlasAttributeDef> newttributes = ModelTestUtil.newAttributeDefsWithAllBuiltInTypes("newAttributes");
    structDef.setAttributeDefs(newttributes);
    for (AtlasAttributeDef attributeDef : oldAttributes) {
        assertFalse(structDef.hasAttribute(attributeDef.getName()));
    }
    for (AtlasAttributeDef attributeDef : newttributes) {
        assertTrue(structDef.hasAttribute(attributeDef.getName()));
    }
}
Also used : AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) Test(org.testng.annotations.Test)

Example 7 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasStructDefStoreV1 method toJsonFromAttribute.

@VisibleForTesting
public static String toJsonFromAttribute(AtlasAttribute attribute) {
    AtlasAttributeDef attributeDef = attribute.getAttributeDef();
    Map<String, Object> attribInfo = new HashMap<>();
    attribInfo.put("name", attributeDef.getName());
    attribInfo.put("dataType", attributeDef.getTypeName());
    attribInfo.put("isUnique", attributeDef.getIsUnique());
    attribInfo.put("isIndexable", attributeDef.getIsIndexable());
    attribInfo.put("isComposite", attribute.isOwnedRef());
    attribInfo.put("reverseAttributeName", attribute.getInverseRefAttributeName());
    final int lower;
    final int upper;
    if (attributeDef.getCardinality() == AtlasAttributeDef.Cardinality.SINGLE) {
        lower = attributeDef.getIsOptional() ? 0 : 1;
        upper = 1;
    } else {
        if (attributeDef.getIsOptional()) {
            lower = 0;
        } else {
            lower = attributeDef.getValuesMinCount() < 1 ? 1 : attributeDef.getValuesMinCount();
        }
        upper = attributeDef.getValuesMaxCount() < 2 ? Integer.MAX_VALUE : attributeDef.getValuesMaxCount();
    }
    Map<String, Object> multiplicity = new HashMap<>();
    multiplicity.put("lower", lower);
    multiplicity.put("upper", upper);
    multiplicity.put("isUnique", AtlasAttributeDef.Cardinality.SET.equals(attributeDef.getCardinality()));
    attribInfo.put("multiplicity", AtlasType.toJson(multiplicity));
    return AtlasType.toJson(attribInfo);
}
Also used : HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasStructDefStoreV1 method toStructDef.

public static AtlasStructDef toStructDef(AtlasVertex vertex, AtlasStructDef structDef, AtlasTypeDefGraphStoreV1 typeDefStore) throws AtlasBaseException {
    AtlasStructDef ret = (structDef != null) ? structDef : new AtlasStructDef();
    typeDefStore.vertexToTypeDef(vertex, ret);
    List<AtlasAttributeDef> attributeDefs = new ArrayList<>();
    List<String> attrNames = vertex.getProperty(AtlasGraphUtilsV1.getTypeDefPropertyKey(ret), List.class);
    if (CollectionUtils.isNotEmpty(attrNames)) {
        for (String attrName : attrNames) {
            String propertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(ret, attrName);
            String attribJson = vertex.getProperty(GraphHelper.encodePropertyKey(propertyKey), String.class);
            attributeDefs.add(toAttributeDefFromJson(structDef, AtlasType.fromJson(attribJson, Map.class), typeDefStore));
        }
    }
    ret.setAttributeDefs(attributeDefs);
    return ret;
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) ArrayList(java.util.ArrayList)

Example 9 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasStructDefStoreV1 method toAttributeDefFromJson.

@VisibleForTesting
public static AtlasAttributeDef toAttributeDefFromJson(AtlasStructDef structDef, Map attribInfo, AtlasTypeDefGraphStoreV1 typeDefStore) throws AtlasBaseException {
    AtlasAttributeDef ret = new AtlasAttributeDef();
    ret.setName((String) attribInfo.get("name"));
    ret.setTypeName((String) attribInfo.get("dataType"));
    ret.setIsUnique((Boolean) attribInfo.get("isUnique"));
    ret.setIsIndexable((Boolean) attribInfo.get("isIndexable"));
    if ((Boolean) attribInfo.get("isComposite")) {
        ret.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
    }
    final String reverseAttributeName = (String) attribInfo.get("reverseAttributeName");
    if (StringUtils.isNotBlank(reverseAttributeName)) {
        ret.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {

            {
                put(AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, reverseAttributeName);
            }
        }));
    }
    Map multiplicity = AtlasType.fromJson((String) attribInfo.get("multiplicity"), Map.class);
    Number minCount = (Number) multiplicity.get("lower");
    Number maxCount = (Number) multiplicity.get("upper");
    Boolean isUnique = (Boolean) multiplicity.get("isUnique");
    if (minCount == null || minCount.intValue() == 0) {
        ret.setIsOptional(true);
        ret.setValuesMinCount(0);
    } else {
        ret.setIsOptional(false);
        ret.setValuesMinCount(minCount.intValue());
    }
    if (maxCount == null || maxCount.intValue() < 2) {
        ret.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
        ret.setValuesMaxCount(1);
    } else {
        if (isUnique == null || isUnique == Boolean.FALSE) {
            ret.setCardinality(AtlasAttributeDef.Cardinality.LIST);
        } else {
            ret.setCardinality(AtlasAttributeDef.Cardinality.SET);
        }
        ret.setValuesMaxCount(maxCount.intValue());
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasConstraintDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef) HashMap(java.util.HashMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class TypeConverterUtil method toAtlasEntityDefs.

private static List<AtlasEntityDef> toAtlasEntityDefs(List<HierarchicalTypeDefinition<ClassType>> classTypeDefinitions, AtlasTypeRegistry registry) throws AtlasBaseException {
    List<AtlasEntityDef> atlasEntityDefs = new ArrayList<AtlasEntityDef>();
    for (HierarchicalTypeDefinition<ClassType> classType : classTypeDefinitions) {
        List<AtlasAttributeDef> attrDefs = new ArrayList<AtlasAttributeDef>();
        AtlasEntityDef atlasEntityDef = new AtlasEntityDef();
        String classTypeDefName = classType.typeName;
        atlasEntityDef.setName(classTypeDefName);
        atlasEntityDef.setDescription(classType.typeDescription);
        atlasEntityDef.setTypeVersion(classType.typeVersion);
        atlasEntityDef.setSuperTypes(classType.superTypes);
        AttributeDefinition[] attrDefinitions = classType.attributeDefinitions;
        for (AttributeDefinition oldAttr : attrDefinitions) {
            AtlasAttributeDef newAttr = toAtlasAttributeDef(oldAttr);
            attrDefs.add(newAttr);
        }
        atlasEntityDef.setAttributeDefs(attrDefs);
        atlasEntityDefs.add(atlasEntityDef);
    }
    return atlasEntityDefs;
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) ArrayList(java.util.ArrayList) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ClassType(org.apache.atlas.typesystem.types.ClassType)

Aggregations

AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)52 AtlasConstraintDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef)19 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)17 HashMap (java.util.HashMap)14 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)13 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)12 ArrayList (java.util.ArrayList)11 Test (org.testng.annotations.Test)10 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)9 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)8 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)7 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)5 AtlasEnumElementDef (org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef)4 Map (java.util.Map)3 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)3 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)2 ClassType (org.apache.atlas.typesystem.types.ClassType)2