Search in sources :

Example 1 with RoleType

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

Example 2 with RoleType

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);
            }
        });
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) Context(com.vaticle.typedb.core.common.parameters.Context) LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE) RelationType(com.vaticle.typedb.core.concept.type.RelationType) LogicManager(com.vaticle.typedb.core.logic.LogicManager) HashSet(java.util.HashSet) PlaysConstraint(com.vaticle.typedb.core.pattern.constraint.type.PlaysConstraint) TYPE_CONSTRAINT_UNACCEPTED(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_CONSTRAINT_UNACCEPTED) ThingType(com.vaticle.typedb.core.concept.type.ThingType) CYCLIC_TYPE_HIERARCHY(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.CYCLIC_TYPE_HIERARCHY) RelatesConstraint(com.vaticle.typedb.core.pattern.constraint.type.RelatesConstraint) TYPE_NOT_FOUND(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_NOT_FOUND) OwnsConstraint(com.vaticle.typedb.core.pattern.constraint.type.OwnsConstraint) LinkedHashSet(java.util.LinkedHashSet) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) ATTRIBUTE_VALUE_TYPE_MISSING(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_MISSING) IS(com.vaticle.typeql.lang.common.TypeQLToken.Constraint.IS) ROLE_DEFINED_OUTSIDE_OF_RELATION(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROLE_DEFINED_OUTSIDE_OF_RELATION) SubConstraint(com.vaticle.typedb.core.pattern.constraint.type.SubConstraint) Set(java.util.Set) RegexConstraint(com.vaticle.typedb.core.pattern.constraint.type.RegexConstraint) ATTRIBUTE_VALUE_TYPE_MODIFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_MODIFIED) INVALID_DEFINE_SUB(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_DEFINE_SUB) RoleType(com.vaticle.typedb.core.concept.type.RoleType) FactoryTracingThreadStatic.traceOnThread(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.traceOnThread) List(java.util.List) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable) Optional(java.util.Optional) VariableRegistry(com.vaticle.typedb.core.pattern.variable.VariableRegistry) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ValueType(com.vaticle.typedb.core.concept.type.AttributeType.ValueType) ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE) OVERRIDDEN_NOT_SUPERTYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE) LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) RoleType(com.vaticle.typedb.core.concept.type.RoleType) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace)

Example 3 with 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;
    }
}
Also used : RoleType(com.vaticle.typedb.core.concept.type.RoleType) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ThingType(com.vaticle.typedb.core.concept.type.ThingType)

Example 4 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 5 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)

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