Java Code Examples for org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef#getCardinality()

The following examples show how to use org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef#getCardinality() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtlasTypeUtil.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static Multiplicity getMultiplicity(AtlasAttributeDef attributeDef) {
    int lower;
    int upper;
    if (attributeDef.getCardinality() == 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();
    }

    return new Multiplicity(lower, upper, Cardinality.SET.equals(attributeDef.getCardinality()));
}
 
Example 2
Source File: AtlasStructDefStoreV1.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@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());
    attribInfo.put("defaultValue", attributeDef.getDefaultValue());

    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);
}
 
Example 3
Source File: AtlasStructType.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
    Map<String, AtlasAttribute> a = new HashMap<>();

    for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
        AtlasType      attrType  = typeRegistry.getType(attributeDef.getTypeName());
        AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType);

        Cardinality cardinality = attributeDef.getCardinality();

        if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) {
            if (!(attrType instanceof AtlasArrayType)) {
                throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY,
                                             getTypeName(), attributeDef.getName());
            }

            AtlasArrayType arrayType = (AtlasArrayType)attrType;

            arrayType.setMinCount(attributeDef.getValuesMinCount());
            arrayType.setMaxCount(attributeDef.getValuesMaxCount());
        }

        a.put(attributeDef.getName(), attribute);
    }

    resolveConstraints(typeRegistry);

    this.allAttributes  = Collections.unmodifiableMap(a);
    this.uniqAttributes = getUniqueAttributes(this.allAttributes);
}
 
Example 4
Source File: AtlasStructDefStoreV2.java    From atlas with Apache License 2.0 4 votes vote down vote up
@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("includeInNotification", attributeDef.getIncludeInNotification());
    attribInfo.put("isComposite", attribute.isOwnedRef());
    attribInfo.put("reverseAttributeName", attribute.getInverseRefAttributeName());
    attribInfo.put("defaultValue", attributeDef.getDefaultValue());
    attribInfo.put("description", attributeDef.getDescription());
    attribInfo.put("searchWeight", attributeDef.getSearchWeight());
    attribInfo.put("indexType", attributeDef.getIndexType());

    if(attributeDef.getOptions() != null) {
        attribInfo.put("options", AtlasType.toJson(attributeDef.getOptions()));
    }

    attribInfo.put("displayName", attributeDef.getDisplayName());

    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);
}
 
Example 5
Source File: AtlasStructType.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override
void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
    Map<String, AtlasAttribute> a = new HashMap<>();

    for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
        AtlasType      attrType  = typeRegistry.getType(attributeDef.getTypeName());
        AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType);

        Cardinality cardinality = attributeDef.getCardinality();

        if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) {
            if (!(attrType instanceof AtlasArrayType)) {
                throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY,
                                             getTypeName(), attributeDef.getName());
            }

            AtlasArrayType arrayType = (AtlasArrayType)attrType;

            arrayType.setMinCount(attributeDef.getValuesMinCount());
            arrayType.setMaxCount(attributeDef.getValuesMaxCount());
            arrayType.setCardinality(cardinality);
        }

        //check if attribute type is not classification
        if (attrType instanceof AtlasArrayType) {
            attrType = ((AtlasArrayType) attrType).getElementType();
        } else if (attrType instanceof AtlasMapType) {
            attrType = ((AtlasMapType) attrType).getValueType();
        }

        if (attrType instanceof AtlasClassificationType) {
            throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_TYPE_INVALID, getTypeName(), attributeDef.getName());
        }

        if (attrType instanceof AtlasBusinessMetadataType) {
            throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_TYPE_INVALID, getTypeName(), attributeDef.getName());
        }

        a.put(attributeDef.getName(), attribute);
    }

    resolveConstraints(typeRegistry);

    this.allAttributes  = Collections.unmodifiableMap(a);
    this.uniqAttributes = getUniqueAttributes(this.allAttributes);
}