Search in sources :

Example 1 with TypeEdge

use of com.vaticle.typedb.core.graph.edge.TypeEdge in project grakn by graknlabs.

the class ThingTypeImpl method ownsKey.

private void ownsKey(AttributeTypeImpl attributeType) {
    validateIsNotDeleted();
    TypeVertex attVertex = attributeType.vertex;
    TypeEdge ownsEdge, ownsKeyEdge;
    if (vertex.outs().edge(OWNS_KEY, attVertex) != null)
        return;
    if (!attributeType.isKeyable()) {
        throw exception(TypeDBException.of(OWNS_KEY_VALUE_TYPE, attributeType.getLabel(), attributeType.getValueType().name()));
    } else if (link(getSupertype().getOwns(attributeType.getValueType(), true), getSupertype().overriddenOwns(false, true)).anyMatch(a -> a.equals(attributeType))) {
        throw exception(TypeDBException.of(OWNS_KEY_NOT_AVAILABLE, attributeType.getLabel()));
    }
    if ((ownsEdge = vertex.outs().edge(OWNS, attVertex)) != null) {
        // TODO: These ownership and uniqueness checks should be parallelised to scale better
        getInstances().forEachRemaining(thing -> {
            FunctionalIterator<? extends Attribute> attrs = thing.getHas(attributeType);
            if (!attrs.hasNext())
                throw exception(TypeDBException.of(OWS_KEY_PRECONDITION_OWNERSHIP_KEY_TOO_MANY, vertex.label(), attVertex.label()));
            Attribute attr = attrs.next();
            if (attrs.hasNext())
                throw exception(TypeDBException.of(OWS_KEY_PRECONDITION_OWNERSHIP_KEY_MISSING, vertex.label(), attVertex.label()));
            else if (compareSize(attr.getOwners(this), 1) != 0) {
                throw exception(TypeDBException.of(OWNS_KEY_PRECONDITION_UNIQUENESS, attVertex.label(), vertex.label()));
            }
        });
        ownsEdge.delete();
    } else if (getInstances().first().isPresent()) {
        throw exception(TypeDBException.of(OWNS_KEY_PRECONDITION_NO_INSTANCES, vertex.label(), attVertex.label()));
    }
    ownsKeyEdge = vertex.outs().put(OWNS_KEY, attVertex);
    if (getSupertype().declaredOwns(false).findFirst(attributeType).isPresent())
        ownsKeyEdge.overridden(attVertex);
}
Also used : TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) INVALID_UNDEFINE_NONEXISTENT_OWNS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_OWNS) RelationImpl(com.vaticle.typedb.core.concept.thing.impl.RelationImpl) ASC(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.ASC) ThingType(com.vaticle.typedb.core.concept.type.ThingType) Iterators.loop(com.vaticle.typedb.core.common.iterator.Iterators.loop) OWNS_KEY_PRECONDITION_UNIQUENESS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_PRECONDITION_UNIQUENESS) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) OWNS_KEY_PRECONDITION_NO_INSTANCES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_PRECONDITION_NO_INSTANCES) OWNS_KEY_VALUE_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_VALUE_TYPE) SUB(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.SUB) EntityImpl(com.vaticle.typedb.core.concept.thing.impl.EntityImpl) INVALID_UNDEFINE_NONEXISTENT_PLAYS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_PLAYS) Forwardable(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable) INVALID_UNDEFINE_PLAYS_HAS_INSTANCES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_PLAYS_HAS_INSTANCES) ThingVertex(com.vaticle.typedb.core.graph.vertex.ThingVertex) Objects(java.util.Objects) List(java.util.List) ThingImpl(com.vaticle.typedb.core.concept.thing.impl.ThingImpl) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) PLAYS_ROLE_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.PLAYS_ROLE_NOT_AVAILABLE) OVERRIDDEN_NOT_SUPERTYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE) Iterators.link(com.vaticle.typedb.core.common.iterator.Iterators.link) INVALID_UNDEFINE_INHERITED_OWNS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_OWNS) Order(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order) Function(java.util.function.Function) Forwardable.iterateSorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.iterateSorted) INVALID_UNDEFINE_OWNS_HAS_INSTANCES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_OWNS_HAS_INSTANCES) OWNS_ABSTRACT_ATT_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ABSTRACT_ATT_TYPE) Forwardable.emptySorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.emptySorted) OWNS_ATT_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ATT_NOT_AVAILABLE) OWS_KEY_PRECONDITION_OWNERSHIP_KEY_TOO_MANY(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWS_KEY_PRECONDITION_OWNERSHIP_KEY_TOO_MANY) TYPE_HAS_SUBTYPES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_SUBTYPES) INVALID_UNDEFINE_INHERITED_PLAYS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_PLAYS) TYPE_HAS_INSTANCES_DELETE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_INSTANCES_DELETE) Encoding(com.vaticle.typedb.core.graph.common.Encoding) Nullable(javax.annotation.Nullable) PLAYS(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.PLAYS) GraphManager(com.vaticle.typedb.core.graph.GraphManager) UNRECOGNISED_VALUE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.UNRECOGNISED_VALUE) OWS_KEY_PRECONDITION_OWNERSHIP_KEY_MISSING(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWS_KEY_PRECONDITION_OWNERSHIP_KEY_MISSING) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) OWNS_KEY(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.OWNS_KEY) AttributeImpl(com.vaticle.typedb.core.concept.thing.impl.AttributeImpl) RoleType(com.vaticle.typedb.core.concept.type.RoleType) OWNS_KEY_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_NOT_AVAILABLE) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Iterators.compareSize(com.vaticle.typedb.core.common.iterator.Iterators.compareSize) PLAYS_ABSTRACT_ROLE_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.PLAYS_ABSTRACT_ROLE_TYPE) ROOT_TYPE_MUTATION(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROOT_TYPE_MUTATION) OWNS(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.OWNS) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) Collections(java.util.Collections) TYPE_HAS_INSTANCES_SET_ABSTRACT(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_INSTANCES_SET_ABSTRACT) Type(com.vaticle.typedb.core.concept.type.Type) OVERRIDE_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDE_NOT_AVAILABLE) TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex)

Example 2 with TypeEdge

use of com.vaticle.typedb.core.graph.edge.TypeEdge in project grakn by graknlabs.

the class ThingTypeImpl method getOwnsOverridden.

@Override
public AttributeType getOwnsOverridden(AttributeType attributeType) {
    TypeVertex attrVertex = graphMgr.schema().getType(attributeType.getLabel());
    if (attrVertex != null) {
        TypeEdge ownsEdge = vertex.outs().edge(OWNS_KEY, attrVertex);
        if (ownsEdge != null && ownsEdge.overridden().isPresent()) {
            return AttributeTypeImpl.of(graphMgr, ownsEdge.overridden().get());
        }
        ownsEdge = vertex.outs().edge(OWNS, attrVertex);
        if (ownsEdge != null && ownsEdge.overridden().isPresent()) {
            return AttributeTypeImpl.of(graphMgr, ownsEdge.overridden().get());
        }
    }
    return null;
}
Also used : TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex)

Example 3 with TypeEdge

use of com.vaticle.typedb.core.graph.edge.TypeEdge in project grakn by graknlabs.

the class TypeAdjacencyImpl method put.

@Override
public TypeEdge put(Encoding.Edge.Type encoding, TypeVertex adjacent) {
    assert !owner.isDeleted();
    TypeVertex from = isOut() ? owner : adjacent;
    TypeVertex to = isOut() ? adjacent : owner;
    TypeEdgeImpl edge = new TypeEdgeImpl.Buffered(encoding, from, to);
    edges.computeIfAbsent(encoding, e -> new ConcurrentSkipListSet<>()).add(getView(edge));
    if (isOut())
        ((TypeAdjacencyImpl<?>) to.ins()).putNonRecursive(edge);
    else
        ((TypeAdjacencyImpl<?>) from.outs()).putNonRecursive(edge);
    owner.setModified();
    return edge;
}
Also used : EdgeViewIID(com.vaticle.typedb.core.graph.iid.EdgeViewIID) ConcurrentSet(com.vaticle.typedb.common.collection.ConcurrentSet) KeyValue(com.vaticle.typedb.core.common.collection.KeyValue) TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) Predicate(java.util.function.Predicate) Order(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order) InfixIID(com.vaticle.typedb.core.graph.iid.InfixIID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Forwardable(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable) ASC(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.ASC) Forwardable.iterateSorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.iterateSorted) InEdgeIteratorImpl(com.vaticle.typedb.core.graph.adjacency.impl.TypeEdgeIterator.InEdgeIteratorImpl) ConcurrentMap(java.util.concurrent.ConcurrentMap) OutEdgeIteratorImpl(com.vaticle.typedb.core.graph.adjacency.impl.TypeEdgeIterator.OutEdgeIteratorImpl) Forwardable.emptySorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.emptySorted) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) VertexIID(com.vaticle.typedb.core.graph.iid.VertexIID) TypeAdjacency(com.vaticle.typedb.core.graph.adjacency.TypeAdjacency) Optional(java.util.Optional) TypeEdgeImpl(com.vaticle.typedb.core.graph.edge.impl.TypeEdgeImpl) Key(com.vaticle.typedb.core.graph.common.Storage.Key) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) ByteArray(com.vaticle.typedb.core.common.collection.ByteArray) Encoding(com.vaticle.typedb.core.graph.common.Encoding) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) TypeEdgeImpl(com.vaticle.typedb.core.graph.edge.impl.TypeEdgeImpl)

Example 4 with TypeEdge

use of com.vaticle.typedb.core.graph.edge.TypeEdge in project grakn by graknlabs.

the class ThingTypeImpl method unsetOwns.

@Override
public void unsetOwns(AttributeType attributeType) {
    validateIsNotDeleted();
    TypeEdge edge;
    TypeVertex attVertex = ((AttributeTypeImpl) attributeType).vertex;
    if (getInstances().anyMatch(thing -> thing.getHas(attributeType).first().isPresent())) {
        throw exception(TypeDBException.of(INVALID_UNDEFINE_OWNS_HAS_INSTANCES, vertex.label(), attVertex.label()));
    }
    if ((edge = vertex.outs().edge(OWNS_KEY, attVertex)) != null)
        edge.delete();
    else if ((edge = vertex.outs().edge(OWNS, attVertex)) != null)
        edge.delete();
    else if (this.getOwns().findFirst((AttributeTypeImpl) attributeType).isPresent()) {
        throw exception(TypeDBException.of(INVALID_UNDEFINE_INHERITED_OWNS, this.getLabel().toString(), attributeType.getLabel().toString()));
    } else {
        throw exception(TypeDBException.of(INVALID_UNDEFINE_NONEXISTENT_OWNS, this.getLabel().toString(), attributeType.getLabel().toString()));
    }
}
Also used : TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex)

Example 5 with TypeEdge

use of com.vaticle.typedb.core.graph.edge.TypeEdge in project grakn by graknlabs.

the class ThingTypeImpl method unsetPlays.

@Override
public void unsetPlays(RoleType roleType) {
    validateIsNotDeleted();
    TypeEdge edge = vertex.outs().edge(Encoding.Edge.Type.PLAYS, ((RoleTypeImpl) roleType).vertex);
    if (edge == null) {
        if (this.getPlays().findFirst(roleType).isPresent()) {
            throw exception(TypeDBException.of(INVALID_UNDEFINE_INHERITED_PLAYS, this.getLabel().toString(), roleType.getLabel().toString()));
        } else {
            throw exception(TypeDBException.of(INVALID_UNDEFINE_NONEXISTENT_PLAYS, this.getLabel().toString(), roleType.getLabel().toString()));
        }
    }
    if (getInstances().anyMatch(thing -> thing.getRelations(roleType).first().isPresent())) {
        throw exception(TypeDBException.of(INVALID_UNDEFINE_PLAYS_HAS_INSTANCES, vertex.label(), roleType.getLabel().toString()));
    }
    edge.delete();
}
Also used : TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge)

Aggregations

TypeEdge (com.vaticle.typedb.core.graph.edge.TypeEdge)6 TypeVertex (com.vaticle.typedb.core.graph.vertex.TypeVertex)5 ASC (com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.ASC)3 Forwardable (com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable)3 Order (com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order)3 Forwardable.emptySorted (com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.emptySorted)3 Forwardable.iterateSorted (com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.iterateSorted)3 UNRECOGNISED_VALUE (com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.UNRECOGNISED_VALUE)2 INVALID_UNDEFINE_INHERITED_OWNS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_OWNS)2 INVALID_UNDEFINE_INHERITED_PLAYS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_PLAYS)2 INVALID_UNDEFINE_NONEXISTENT_OWNS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_OWNS)2 INVALID_UNDEFINE_NONEXISTENT_PLAYS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_PLAYS)2 INVALID_UNDEFINE_OWNS_HAS_INSTANCES (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_OWNS_HAS_INSTANCES)2 INVALID_UNDEFINE_PLAYS_HAS_INSTANCES (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_PLAYS_HAS_INSTANCES)2 OVERRIDDEN_NOT_SUPERTYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE)2 OVERRIDE_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDE_NOT_AVAILABLE)2 OWNS_ABSTRACT_ATT_TYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ABSTRACT_ATT_TYPE)2 OWNS_ATT_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ATT_NOT_AVAILABLE)2 OWNS_KEY_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_NOT_AVAILABLE)2 OWNS_KEY_PRECONDITION_NO_INSTANCES (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_PRECONDITION_NO_INSTANCES)2