Search in sources :

Example 1 with ThingType

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

the class BasicTest method assert_transaction_read.

private static void assert_transaction_read(TypeDB.Transaction transaction) {
    assertTrue(transaction.isOpen());
    assertTrue(transaction.type().isRead());
    ThingType rootType = transaction.concepts().getRootThingType();
    EntityType rootEntityType = transaction.concepts().getRootEntityType();
    RelationType rootRelationType = transaction.concepts().getRootRelationType();
    AttributeType rootAttributeType = transaction.concepts().getRootAttributeType();
    Util.assertNotNulls(rootType, rootEntityType, rootRelationType, rootAttributeType);
    Stream<Consumer<TypeDB.Transaction>> typeAssertions = Stream.of(tx -> {
        AttributeType.String name = tx.concepts().getAttributeType("name").asString();
        Util.assertNotNulls(name);
        assertEquals(rootAttributeType, name.getSupertype());
    }, tx -> {
        AttributeType.Long age = tx.concepts().getAttributeType("age").asLong();
        Util.assertNotNulls(age);
        assertEquals(rootAttributeType, age.getSupertype());
    }, tx -> {
        RelationType marriage = tx.concepts().getRelationType("marriage");
        RoleType husband = marriage.getRelates("husband");
        RoleType wife = marriage.getRelates("wife");
        Util.assertNotNulls(marriage, husband, wife);
        assertEquals(rootRelationType, marriage.getSupertype());
        assertEquals(rootRelationType.getRelates("role"), husband.getSupertype());
        assertEquals(rootRelationType.getRelates("role"), wife.getSupertype());
    }, tx -> {
        RelationType employment = tx.concepts().getRelationType("employment");
        RoleType employee = employment.getRelates("employee");
        RoleType employer = employment.getRelates("employer");
        Util.assertNotNulls(employment, employee, employer);
        assertEquals(rootRelationType, employment.getSupertype());
    }, tx -> {
        EntityType person = tx.concepts().getEntityType("person");
        Util.assertNotNulls(person);
        assertEquals(rootEntityType, person.getSupertype());
        Stream<Consumer<TypeDB.Transaction>> subPersonAssertions = Stream.of(tx2 -> {
            EntityType man = tx2.concepts().getEntityType("man");
            Util.assertNotNulls(man);
            assertEquals(person, man.getSupertype());
        }, tx2 -> {
            EntityType woman = tx2.concepts().getEntityType("woman");
            Util.assertNotNulls(woman);
            assertEquals(person, woman.getSupertype());
        });
        subPersonAssertions.parallel().forEach(assertions -> assertions.accept(tx));
    }, tx -> {
        EntityType company = tx.concepts().getEntityType("company");
        Util.assertNotNulls(company);
        assertEquals(rootEntityType, company.getSupertype());
    });
    typeAssertions.parallel().forEach(assertion -> assertion.accept(transaction));
}
Also used : EntityType(com.vaticle.typedb.core.concept.type.EntityType) Consumer(java.util.function.Consumer) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) TypeDB(com.vaticle.typedb.core.TypeDB) ThingType(com.vaticle.typedb.core.concept.type.ThingType)

Example 2 with ThingType

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

the class BasicTest method write_types_concurrently.

@Test
public void write_types_concurrently() throws IOException, InterruptedException {
    Util.resetDirectory(dataDir);
    try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
        typedb.create(database);
        assertTrue(typedb.isOpen());
        assertEquals(1, typedb.all().size());
        assertEquals(database, typedb.all().iterator().next().name());
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
            assertTrue(session.isOpen());
            assertEquals(database, session.database().name());
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
                assertTrue(transaction.isOpen());
                assertTrue(transaction.type().isRead());
                Stream<Consumer<TypeDB.Transaction>> rootTypeAssertions = Stream.of(tx -> {
                    ThingType rootType = tx.concepts().getRootThingType();
                    assertNotNull(rootType);
                }, tx -> {
                    EntityType rootEntityType = tx.concepts().getRootEntityType();
                    assertNotNull(rootEntityType);
                }, tx -> {
                    RelationType rootRelationType = tx.concepts().getRootRelationType();
                    assertNotNull(rootRelationType);
                }, tx -> {
                    AttributeType rootAttributeType = tx.concepts().getRootAttributeType();
                    assertNotNull(rootAttributeType);
                });
                rootTypeAssertions.parallel().forEach(assertion -> assertion.accept(transaction));
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                assertTrue(transaction.isOpen());
                assertTrue(transaction.type().isWrite());
                ThingType rootType = transaction.concepts().getRootThingType();
                EntityType rootEntityType = transaction.concepts().getRootEntityType();
                RelationType rootRelationType = transaction.concepts().getRootRelationType();
                AttributeType rootAttributeType = transaction.concepts().getRootAttributeType();
                Util.assertNotNulls(rootType, rootEntityType, rootRelationType, rootAttributeType);
                Stream<Consumer<TypeDB.Transaction>> typeAssertions = Stream.of(tx -> {
                    AttributeType name = tx.concepts().putAttributeType("name", STRING).asString();
                    Util.assertNotNulls(name);
                    assertEquals(rootAttributeType, name.getSupertype());
                }, tx -> {
                    AttributeType.Long age = tx.concepts().putAttributeType("age", LONG).asLong();
                    Util.assertNotNulls(age);
                    assertEquals(rootAttributeType, age.getSupertype());
                }, tx -> {
                    RelationType marriage = tx.concepts().putRelationType("marriage");
                    marriage.setRelates("husband");
                    marriage.setRelates("wife");
                    Util.assertNotNulls(marriage);
                    assertEquals(rootRelationType, marriage.getSupertype());
                }, tx -> {
                    RelationType employment = tx.concepts().putRelationType("employment");
                    employment.setRelates("employee");
                    employment.setRelates("employer");
                    Util.assertNotNulls(employment);
                    assertEquals(rootRelationType, employment.getSupertype());
                }, tx -> {
                    EntityType person = tx.concepts().putEntityType("person");
                    Util.assertNotNulls(person);
                    assertEquals(rootEntityType, person.getSupertype());
                    Stream<Consumer<TypeDB.Transaction>> subPersonAssertions = Stream.of(tx2 -> {
                        EntityType man = tx2.concepts().putEntityType("man");
                        man.setSupertype(person);
                        Util.assertNotNulls(man);
                        assertEquals(person, man.getSupertype());
                    }, tx2 -> {
                        EntityType woman = tx2.concepts().putEntityType("woman");
                        woman.setSupertype(person);
                        Util.assertNotNulls(woman);
                        assertEquals(person, woman.getSupertype());
                    });
                    subPersonAssertions.parallel().forEach(assertions -> assertions.accept(tx));
                }, tx -> {
                    EntityType company = tx.concepts().putEntityType("company");
                    Util.assertNotNulls(company);
                    assertEquals(rootEntityType, company.getSupertype());
                });
                typeAssertions.parallel().forEach(assertion -> assertion.accept(transaction));
                transaction.commit();
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
                assert_transaction_read(transaction);
            }
        }
    }
    try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
        assertTrue(typedb.isOpen());
        assertEquals(1, typedb.all().size());
        assertEquals(database, typedb.all().iterator().next().name());
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
            assertTrue(session.isOpen());
            assertEquals(database, session.database().name());
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
                assert_transaction_read(transaction);
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                AttributeType.String gender = transaction.concepts().putAttributeType("gender", STRING).asString();
                EntityType school = transaction.concepts().putEntityType("school");
                RelationType teaching = transaction.concepts().putRelationType("teaching");
                teaching.setRelates("teacher");
                teaching.setRelates("student");
                RoleType teacher = teaching.getRelates("teacher");
                RoleType student = teaching.getRelates("student");
                Util.assertNotNulls(gender, school, teaching, teacher, student);
                transaction.commit();
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
                assert_transaction_read(transaction);
                AttributeType.String gender = transaction.concepts().getAttributeType("gender").asString();
                EntityType school = transaction.concepts().getEntityType("school");
                RelationType teaching = transaction.concepts().getRelationType("teaching");
                RoleType teacher = teaching.getRelates("teacher");
                RoleType student = teaching.getRelates("student");
                Util.assertNotNulls(gender, school, teaching, teacher, student);
            }
        }
    }
}
Also used : RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) ThingType(com.vaticle.typedb.core.concept.type.ThingType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Consumer(java.util.function.Consumer) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Test(org.junit.Test)

Example 3 with ThingType

use of com.vaticle.typedb.core.concept.type.ThingType 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 4 with ThingType

use of com.vaticle.typedb.core.concept.type.ThingType 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 5 with ThingType

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

the class Definer method define.

private ThingType define(TypeVariable variable) {
    try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "define")) {
        assert variable.label().isPresent();
        LabelConstraint labelConstraint = variable.label().get();
        if (labelConstraint.scope().isPresent() && variable.constraints().size() > 1) {
            throw TypeDBException.of(ROLE_DEFINED_OUTSIDE_OF_RELATION, labelConstraint.scopedLabel());
        } else if (!variable.is().isEmpty()) {
            throw TypeDBException.of(TYPE_CONSTRAINT_UNACCEPTED, IS);
        } else if (// do nothing
        labelConstraint.scope().isPresent())
            // do nothing
            return null;
        else if (defined.contains(variable))
            return conceptMgr.getThingType(labelConstraint.scopedLabel());
        ThingType type = getThingType(labelConstraint);
        if (variable.sub().isPresent()) {
            type = defineSub(type, variable.sub().get(), variable);
        } else if (variable.valueType().isPresent()) {
            // && variable.sub().size() == 0
            String valueType = variable.valueType().get().valueType().name();
            throw TypeDBException.of(ATTRIBUTE_VALUE_TYPE_MODIFIED, valueType, labelConstraint.label());
        } else if (type == null) {
            throw TypeDBException.of(TYPE_NOT_FOUND, labelConstraint.label());
        }
        if (variable.valueType().isPresent() && !(type.isAttributeType())) {
            throw TypeDBException.of(ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE, labelConstraint.label());
        }
        defined.add(variable);
        if (variable.abstractConstraint().isPresent())
            defineAbstract(type);
        if (variable.regex().isPresent())
            defineRegex(type.asAttributeType().asString(), variable.regex().get());
        if (!variable.relates().isEmpty())
            defineRelates(type.asRelationType(), variable.relates());
        if (!variable.owns().isEmpty())
            defineOwns(type, variable.owns());
        if (!variable.plays().isEmpty())
            definePlays(type, variable.plays());
        return type;
    }
}
Also used : LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ThingType(com.vaticle.typedb.core.concept.type.ThingType)

Aggregations

ThingType (com.vaticle.typedb.core.concept.type.ThingType)17 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)11 Then (io.cucumber.java.en.Then)8 ThreadTrace (com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace)6 RoleType (com.vaticle.typedb.core.concept.type.RoleType)6 LabelConstraint (com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint)5 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)3 RelationType (com.vaticle.typedb.core.concept.type.RelationType)3 List (java.util.List)3 FactoryTracingThreadStatic.traceOnThread (com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.traceOnThread)2 TypeDB (com.vaticle.typedb.core.TypeDB)2 TYPE_NOT_FOUND (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_NOT_FOUND)2 OVERRIDDEN_NOT_SUPERTYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE)2 ROLE_DEFINED_OUTSIDE_OF_RELATION (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROLE_DEFINED_OUTSIDE_OF_RELATION)2 ValueType (com.vaticle.typedb.core.concept.type.AttributeType.ValueType)2 EntityType (com.vaticle.typedb.core.concept.type.EntityType)2 Type (com.vaticle.typedb.core.concept.type.Type)2 Consumer (java.util.function.Consumer)2 UNRECOGNISED_VALUE (com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.UNRECOGNISED_VALUE)1 RULE_NOT_FOUND (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleRead.RULE_NOT_FOUND)1