use of com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.OWNS 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.common.Encoding.Edge.Type.OWNS in project grakn by graknlabs.
the class ThingTypeImpl method ownsAttribute.
private void ownsAttribute(AttributeTypeImpl attributeType) {
validateIsNotDeleted();
Forwardable<AttributeType, Order.Asc> owns = getSupertypes().filter(t -> !t.equals(this)).mergeMap(ThingType::getOwns, ASC);
if (owns.findFirst(attributeType).isPresent()) {
throw exception(TypeDBException.of(OWNS_ATT_NOT_AVAILABLE, attributeType.getLabel()));
}
TypeVertex attVertex = attributeType.vertex;
TypeEdge keyEdge;
if ((keyEdge = vertex.outs().edge(OWNS_KEY, attVertex)) != null)
keyEdge.delete();
vertex.outs().put(OWNS, attVertex);
}
Aggregations