Search in sources :

Example 6 with 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();
}
Also used : Relation(com.vaticle.typedb.core.concept.thing.Relation) RoleType(com.vaticle.typedb.core.concept.type.RoleType) Map(java.util.Map) Thing(com.vaticle.typedb.core.concept.thing.Thing)

Example 7 with RoleType

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();
    }
}
Also used : Relation(com.vaticle.typedb.core.concept.thing.Relation) RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) TypeDB(com.vaticle.typedb.core.TypeDB) Thing(com.vaticle.typedb.core.concept.thing.Thing)

Example 8 with RoleType

use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.

the class DataImporter method getRoleType.

private RoleType getRoleType(RelationType relationType, DataProto.Item.Relation.Role roleMsg) {
    String unscopedRoleLabel;
    String roleLabel = roleMsg.getLabel();
    if (roleLabel.contains(":"))
        unscopedRoleLabel = roleLabel.split(":")[1];
    else
        unscopedRoleLabel = roleLabel;
    RoleType roleType = relationType.getRelates(unscopedRoleLabel);
    if (roleType == null) {
        throw TypeDBException.of(ROLE_TYPE_NOT_FOUND, roleLabel, relationType.getLabel().name());
    } else
        return roleType;
}
Also used : RoleType(com.vaticle.typedb.core.concept.type.RoleType)

Example 9 with RoleType

use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.

the class RelationTypeSteps method relation_type_get_role_type_get_supertype.

@Then("relation\\( ?{type_label} ?) get role\\( ?{type_label} ?) get supertype: {scoped_label}")
public void relation_type_get_role_type_get_supertype(String relationLabel, String roleLabel, Parameters.ScopedLabel superLabel) {
    RoleType superType = tx().concepts().getRelationType(superLabel.scope()).getRelates(superLabel.label());
    assertEquals(superType, tx().concepts().getRelationType(relationLabel).getRelates(roleLabel).getSupertype());
}
Also used : RoleType(com.vaticle.typedb.core.concept.type.RoleType) Then(io.cucumber.java.en.Then)

Example 10 with RoleType

use of com.vaticle.typedb.core.concept.type.RoleType in project grakn by graknlabs.

the class ThingTypeSteps method thing_type_set_plays_role_as_throws_exception.

@When("{root_label}\\( ?{type_label} ?) set plays role: {scoped_label} as {scoped_label}; throws exception")
public void thing_type_set_plays_role_as_throws_exception(RootLabel rootLabel, String typeLabel, Parameters.ScopedLabel roleLabel, Parameters.ScopedLabel overriddenLabel) {
    RoleType roleType = tx().concepts().getRelationType(roleLabel.scope()).getRelates(roleLabel.label());
    RoleType overriddenType = tx().concepts().getRelationType(overriddenLabel.scope()).getRelates(overriddenLabel.label());
    assertThrows(() -> get_thing_type(rootLabel, typeLabel).setPlays(roleType, overriddenType));
}
Also used : RoleType(com.vaticle.typedb.core.concept.type.RoleType) When(io.cucumber.java.en.When)

Aggregations

RoleType (com.vaticle.typedb.core.concept.type.RoleType)21 RelationType (com.vaticle.typedb.core.concept.type.RelationType)11 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)7 TypeDB (com.vaticle.typedb.core.TypeDB)6 EntityType (com.vaticle.typedb.core.concept.type.EntityType)5 When (io.cucumber.java.en.When)5 List (java.util.List)5 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)4 Relation (com.vaticle.typedb.core.concept.thing.Relation)4 ThingType (com.vaticle.typedb.core.concept.type.ThingType)4 Test (org.junit.Test)4 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)3 ThreadTrace (com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace)2 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)2 TYPE_ROOT_MISMATCH (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_ROOT_MISMATCH)2 RELATION_ABSTRACT_ROLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_ABSTRACT_ROLE)2 RELATION_NO_ROLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_NO_ROLE)2 RELATION_RELATES_ROLE_FROM_SUPERTYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_RELATES_ROLE_FROM_SUPERTYPE)2 RELATION_RELATES_ROLE_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_RELATES_ROLE_NOT_AVAILABLE)2 ROOT_TYPE_MUTATION (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROOT_TYPE_MUTATION)2