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);
}
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;
}
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;
}
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()));
}
}
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();
}
Aggregations