use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.
the class UnifyRelationConcludableTest method addRolePlayer.
private void addRolePlayer(Relation relation, String role, Thing player) {
RelationType relationType = relation.getType();
RoleType roleType = relationType.getRelates(role);
assert roleType != null : "Role type " + role + " does not exist in relation type " + relation.getType().getLabel();
relation.addPlayer(roleType, player);
}
use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.
the class Definer method definePlays.
private void definePlays(ThingType thingType, Set<PlaysConstraint> playsConstraints) {
try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "define_plays")) {
playsConstraints.forEach(plays -> {
define(plays.relation().get());
LabelConstraint roleTypeLabel = plays.role().label().get();
RoleType roleType = getRoleType(roleTypeLabel).asRoleType();
if (plays.overridden().isPresent()) {
String overriddenLabelName = plays.overridden().get().label().get().properLabel().name();
Optional<? extends RoleType> overriddenType = roleType.getSupertypes().filter(rt -> rt.getLabel().name().equals(overriddenLabelName)).first();
if (overriddenType.isPresent()) {
thingType.setPlays(roleType, overriddenType.get());
} else {
throw TypeDBException.of(OVERRIDDEN_NOT_SUPERTYPE, roleTypeLabel.scopedLabel(), overriddenLabelName);
}
} else {
thingType.setPlays(roleType);
}
});
}
}
use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.
the class Definer method getRoleType.
private RoleType getRoleType(LabelConstraint label) {
try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "get_role_type")) {
// defined by their Relation Types ahead of time
assert label.scope().isPresent();
ThingType thingType;
RoleType roleType;
if ((thingType = conceptMgr.getThingType(label.scope().get())) == null) {
throw TypeDBException.of(TYPE_NOT_FOUND, label.scope().get());
} else if (!thingType.isRelationType()) {
throw TypeDBException.of(ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE, label.scopedLabel(), label.scope().get());
} else if ((roleType = thingType.asRelationType().getRelates(label.label())) == null) {
throw TypeDBException.of(TYPE_NOT_FOUND, label.scopedLabel());
}
return roleType;
}
}
use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.
the class DataExporter method relation.
private DataProto.Item relation(Relation relation) {
status.relationCount.incrementAndGet();
DataProto.Item.Relation.Builder relationBuilder = DataProto.Item.Relation.newBuilder().setId(relation.getIID().toBase64String()).setLabel(relation.getType().getLabel().name());
Map<? extends RoleType, ? extends List<? extends Thing>> playersByRole = relation.getPlayersByRoleType();
for (Map.Entry<? extends RoleType, ? extends List<? extends Thing>> rolePlayers : playersByRole.entrySet()) {
RoleType role = rolePlayers.getKey();
DataProto.Item.Relation.Role.Builder roleBuilder = DataProto.Item.Relation.Role.newBuilder().setLabel(role.getLabel().scopedName());
for (Thing player : rolePlayers.getValue()) {
status.roleCount.incrementAndGet();
roleBuilder.addPlayer(DataProto.Item.Relation.Role.Player.newBuilder().setId(player.getIID().toBase64String()));
}
relationBuilder.addRole(roleBuilder);
}
readOwnerships(relation).forEachRemaining(a -> {
status.ownershipCount.incrementAndGet();
relationBuilder.addAttribute(a);
});
return DataProto.Item.newBuilder().setRelation(relationBuilder).build();
}
use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.
the class DataImporter method importSkippedRelations.
private void importSkippedRelations() {
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
skippedRelations.forEach(msg -> {
RelationType relType = transaction.concepts().getRelationType(msg.getLabel());
if (relType == null)
throw TypeDBException.of(TYPE_NOT_FOUND, msg.getLabel());
Relation importedRelation = relType.create();
idMapper.put(msg.getId(), importedRelation.getIID());
int ownershipCount = 0;
for (DataProto.Item.OwnedAttribute ownership : msg.getAttributeList()) {
Thing attribute = transaction.concepts().getThing(idMapper.get(ownership.getId()));
assert attribute != null;
importedRelation.setHas(attribute.asAttribute());
ownershipCount++;
}
status.ownershipCount.addAndGet(ownershipCount);
});
skippedRelations.forEach(msg -> {
RelationType relType = transaction.concepts().getRelationType(msg.getLabel());
Relation relation = transaction.concepts().getThing(idMapper.get(msg.getId())).asRelation();
msg.getRoleList().forEach(roleMsg -> {
RoleType roleType = getRoleType(relType, roleMsg);
for (DataProto.Item.Relation.Role.Player playerMessage : roleMsg.getPlayerList()) {
Thing player = transaction.concepts().getThing(idMapper.get(playerMessage.getId()));
if (player == null)
throw TypeDBException.of(PLAYER_NOT_FOUND, relType.getLabel());
else
relation.addPlayer(roleType, player);
}
});
});
transaction.commit();
}
}
Aggregations