Search in sources :

Example 1 with AtlasRelationshipEdgeDirection

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection in project atlas by apache.

the class EntityGraphMapper method getArrayElementsUsingRelationship.

public static List<Object> getArrayElementsUsingRelationship(AtlasVertex vertex, AtlasAttribute attribute, AtlasType elementType) {
    List<Object> ret = null;
    if (AtlasGraphUtilsV1.isReference(elementType)) {
        AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection();
        String edgeLabel = attribute.getRelationshipEdgeLabel();
        Iterator<AtlasEdge> edgesForLabel = GraphHelper.getEdgesForLabel(vertex, edgeLabel, edgeDirection);
        ret = IteratorUtils.toList(edgesForLabel);
    }
    return ret;
}
Also used : AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 2 with AtlasRelationshipEdgeDirection

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection in project atlas by apache.

the class EntityGraphMapper method mapObjectIdValueUsingRelationship.

private AtlasEdge mapObjectIdValueUsingRelationship(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapObjectIdValueUsingRelationship({})", ctx);
    }
    AtlasVertex attributeVertex = context.getDiscoveryContext().getResolvedEntityVertex(getGuid(ctx.getValue()));
    AtlasVertex entityVertex = ctx.getReferringVertex();
    AtlasEdge ret;
    if (attributeVertex == null) {
        AtlasObjectId objectId = getObjectId(ctx.getValue());
        attributeVertex = (objectId != null) ? context.getDiscoveryContext().getResolvedEntityVertex(objectId) : null;
    }
    if (attributeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, (ctx.getValue() == null ? null : ctx.getValue().toString()));
    }
    String attributeName = ctx.getAttribute().getName();
    AtlasType type = typeRegistry.getType(AtlasGraphUtilsV1.getTypeName(entityVertex));
    AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection();
    String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel();
    if (type instanceof AtlasEntityType) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        // use relationship to create/update edges
        if (entityType.hasRelationshipAttribute(attributeName)) {
            Map<String, Object> relationshipAttributes = getRelationshipAttributes(ctx.getValue());
            if (ctx.getCurrentEdge() != null) {
                ret = updateRelationship(ctx.getCurrentEdge(), entityVertex, attributeVertex, edgeDirection, relationshipAttributes);
            } else {
                String relationshipName = graphHelper.getRelationshipDefName(entityVertex, entityType, attributeName);
                AtlasVertex fromVertex;
                AtlasVertex toVertex;
                if (edgeDirection == IN) {
                    fromVertex = attributeVertex;
                    toVertex = entityVertex;
                } else {
                    fromVertex = entityVertex;
                    toVertex = attributeVertex;
                }
                boolean relationshipExists = isRelationshipExists(fromVertex, toVertex, edgeLabel);
                ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, relationshipAttributes);
                // for import use the relationship guid provided
                if (context.isImport()) {
                    AtlasGraphUtilsV1.setProperty(ret, Constants.GUID_PROPERTY_KEY, getRelationshipGuid(ctx.getValue()));
                }
                // record entity update on both relationship vertices
                if (!relationshipExists) {
                    recordEntityUpdate(attributeVertex);
                }
            }
        } else {
            // use legacy way to create/update edges
            if (LOG.isDebugEnabled()) {
                LOG.debug("No RelationshipDef defined between {} and {} on attribute: {}", getTypeName(entityVertex), getTypeName(attributeVertex), attributeName);
            }
            ret = mapObjectIdValue(ctx, context);
        }
    } else {
        // if type is StructType having objectid as attribute
        ret = mapObjectIdValue(ctx, context);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapObjectIdValueUsingRelationship({})", ctx);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 3 with AtlasRelationshipEdgeDirection

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection in project atlas by apache.

the class EntityGraphRetriever method mapVertexToAttribute.

private Object mapVertexToAttribute(AtlasVertex entityVertex, AtlasAttribute attribute, AtlasEntityExtInfo entityExtInfo) throws AtlasBaseException {
    Object ret = null;
    AtlasType attrType = attribute.getAttributeType();
    String vertexPropertyName = attribute.getQualifiedName();
    String edgeLabel = EDGE_LABEL_PREFIX + vertexPropertyName;
    boolean isOwnedAttribute = attribute.isOwnedRef();
    AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Mapping vertex {} to atlas entity {}.{}", entityVertex, attribute.getDefinedInDef().getName(), attribute.getName());
    }
    switch(attrType.getTypeCategory()) {
        case PRIMITIVE:
            ret = mapVertexToPrimitive(entityVertex, vertexPropertyName, attribute.getAttributeDef());
            break;
        case ENUM:
            ret = GraphHelper.getProperty(entityVertex, vertexPropertyName);
            break;
        case STRUCT:
            ret = mapVertexToStruct(entityVertex, edgeLabel, null, entityExtInfo);
            break;
        case OBJECT_ID_TYPE:
            ret = mapVertexToObjectId(entityVertex, edgeLabel, null, entityExtInfo, isOwnedAttribute, edgeDirection);
            break;
        case ARRAY:
            ret = mapVertexToArray(entityVertex, (AtlasArrayType) attrType, vertexPropertyName, entityExtInfo, isOwnedAttribute, edgeDirection);
            break;
        case MAP:
            ret = mapVertexToMap(entityVertex, (AtlasMapType) attrType, vertexPropertyName, entityExtInfo, isOwnedAttribute, edgeDirection);
            break;
        case CLASSIFICATION:
            // do nothing
            break;
    }
    return ret;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasType(org.apache.atlas.type.AtlasType) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 4 with AtlasRelationshipEdgeDirection

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection in project incubator-atlas by apache.

the class AtlasRelationshipType method addRelationshipEdgeDirection.

private void addRelationshipEdgeDirection() {
    AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1();
    AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2();
    AtlasAttribute end1Attribute = end1Type.getRelationshipAttribute(endDef1.getName());
    AtlasAttribute end2Attribute = end2Type.getRelationshipAttribute(endDef2.getName());
    // default relationship edge direction is end1 (out) -> end2 (in)
    AtlasRelationshipEdgeDirection end1Direction = OUT;
    AtlasRelationshipEdgeDirection end2Direction = IN;
    if (endDef1.getIsLegacyAttribute() && endDef2.getIsLegacyAttribute()) {
        end2Direction = OUT;
    } else if (!endDef1.getIsLegacyAttribute() && endDef2.getIsLegacyAttribute()) {
        end1Direction = IN;
        end2Direction = OUT;
    }
    end1Attribute.setRelationshipEdgeDirection(end1Direction);
    end2Attribute.setRelationshipEdgeDirection(end2Direction);
}
Also used : AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef)

Example 5 with AtlasRelationshipEdgeDirection

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection in project incubator-atlas by apache.

the class EntityGraphMapper method mapObjectIdValueUsingRelationship.

private AtlasEdge mapObjectIdValueUsingRelationship(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapObjectIdValueUsingRelationship({})", ctx);
    }
    AtlasVertex attributeVertex = context.getDiscoveryContext().getResolvedEntityVertex(getGuid(ctx.getValue()));
    AtlasVertex entityVertex = ctx.getReferringVertex();
    AtlasEdge ret;
    if (attributeVertex == null) {
        AtlasObjectId objectId = getObjectId(ctx.getValue());
        attributeVertex = (objectId != null) ? context.getDiscoveryContext().getResolvedEntityVertex(objectId) : null;
    }
    if (attributeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, (ctx.getValue() == null ? null : ctx.getValue().toString()));
    }
    String attributeName = ctx.getAttribute().getName();
    AtlasType type = typeRegistry.getType(AtlasGraphUtilsV1.getTypeName(entityVertex));
    AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection();
    String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel();
    if (type instanceof AtlasEntityType) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        // use relationship to create/update edges
        if (entityType.hasRelationshipAttribute(attributeName)) {
            if (ctx.getCurrentEdge() != null) {
                ret = updateRelationship(ctx.getCurrentEdge(), attributeVertex, edgeDirection, ctx.getAttribute());
                recordEntityUpdate(attributeVertex);
            } else {
                String relationshipName = graphHelper.getRelationshipDefName(entityVertex, entityType, attributeName);
                AtlasVertex fromVertex;
                AtlasVertex toVertex;
                if (edgeDirection == IN) {
                    fromVertex = attributeVertex;
                    toVertex = entityVertex;
                } else {
                    fromVertex = entityVertex;
                    toVertex = attributeVertex;
                }
                boolean relationshipExists = isRelationshipExists(fromVertex, toVertex, edgeLabel);
                ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, ctx.getAttribute());
                // record entity update on both relationship vertices
                if (!relationshipExists) {
                    recordEntityUpdate(attributeVertex);
                }
            }
        } else {
            // use legacy way to create/update edges
            if (LOG.isDebugEnabled()) {
                LOG.debug("No RelationshipDef defined between {} and {} on attribute: {}", getTypeName(entityVertex), getTypeName(attributeVertex), attributeName);
            }
            ret = mapObjectIdValue(ctx, context);
        }
    } else {
        // if type is StructType having objectid as attribute
        ret = mapObjectIdValue(ctx, context);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapObjectIdValueUsingRelationship({})", ctx);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Aggregations

AtlasRelationshipEdgeDirection (org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection)7 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 AtlasType (org.apache.atlas.type.AtlasType)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)2 AtlasRelationshipEndDef (org.apache.atlas.model.typedef.AtlasRelationshipEndDef)2 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)2 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)1 AtlasMapType (org.apache.atlas.type.AtlasMapType)1