Search in sources :

Example 41 with AttributeType

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

the class ThingTypeSteps method thing_type_set_owns_attribute_as_throws_exception.

@Then("{root_label}\\( ?{type_label} ?) set owns attribute type: {type_label} as {type_label}; throws exception")
public void thing_type_set_owns_attribute_as_throws_exception(RootLabel rootLabel, String typeLabel, String attributeLabel, String overriddenLabel) {
    AttributeType attributeType = tx().concepts().getAttributeType(attributeLabel);
    AttributeType overriddenType = tx().concepts().getAttributeType(overriddenLabel);
    assertThrows(() -> get_thing_type(rootLabel, typeLabel).setOwns(attributeType, overriddenType));
}
Also used : AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) Then(io.cucumber.java.en.Then)

Example 42 with AttributeType

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

the class ThingTypeSteps method thing_type_set_supertype.

@When("{root_label}\\( ?{type_label} ?) set supertype: {type_label}")
public void thing_type_set_supertype(RootLabel rootLabel, String typeLabel, String superLabel) {
    switch(rootLabel) {
        case ENTITY:
            EntityType entitySuperType = tx().concepts().getEntityType(superLabel);
            tx().concepts().getEntityType(typeLabel).setSupertype(entitySuperType);
            break;
        case ATTRIBUTE:
            AttributeType attributeSuperType = tx().concepts().getAttributeType(superLabel);
            tx().concepts().getAttributeType(typeLabel).setSupertype(attributeSuperType);
            break;
        case RELATION:
            RelationType relationSuperType = tx().concepts().getRelationType(superLabel);
            tx().concepts().getRelationType(typeLabel).setSupertype(relationSuperType);
            break;
    }
}
Also used : EntityType(com.vaticle.typedb.core.concept.type.EntityType) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) When(io.cucumber.java.en.When)

Example 43 with AttributeType

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

the class RuleTest method rule_has_variable_materialises_when_missing.

@Test
public void rule_has_variable_materialises_when_missing() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.putEntityType("milk");
                AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
                AttributeType isStillGood = conceptMgr.putAttributeType("is-still-good", AttributeType.ValueType.BOOLEAN);
                milk.setOwns(ageInDays);
                milk.setOwns(isStillGood);
                txn.logic().putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk; $a 10 isa age-in-days; }").asConjunction(), TypeQL.parseVariable("$x has $a").asThing());
                txn.commit();
            }
        }
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.getEntityType("milk");
                AttributeType ageInDays = conceptMgr.getAttributeType("age-in-days");
                Entity milkInst = milk.create();
                Attribute.Long ageInDays10 = ageInDays.asLong().put(10L);
                Rule rule = txn.logic().getRule("old-milk-is-not-good");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst), pair(Identifier.Variable.name("a"), ageInDays10)));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(2, materialisations.get(0).size());
                List<? extends Attribute> ageInDaysOwned = milkInst.getHas(ageInDays).toList();
                assertEquals(1, ageInDaysOwned.size());
                assertEquals(ageInDays10, ageInDaysOwned.get(0));
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 44 with AttributeType

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

the class RuleTest method new_type_updates_rule_conclusion_index.

@Test
public void new_type_updates_rule_conclusion_index() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.putEntityType("person");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                name.setAbstract();
                AttributeType firstName = conceptMgr.putAttributeType("first-name", AttributeType.ValueType.STRING);
                firstName.setSupertype(name);
                marriage.setRelates("spouse");
                person.setPlays(marriage.getRelates("spouse"));
                person.setOwns(firstName);
                Rule marriageSameName = logicMgr.putRule("marriage-same-name", TypeQL.parsePattern("{ $x isa person, has name $a; $y isa person; (spouse:$x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("$y has $a").asThing());
                Conjunction sameName = marriageSameName.then();
                Variable nameAttr = getVariable(sameName.variables(), Identifier.Variable.name("a"));
                assertEquals(set(Label.of("first-name")), nameAttr.inferredTypes());
                txn.commit();
            }
            // add a new subtype of an attribute in a rule
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                AttributeType lastName = conceptMgr.putAttributeType("last-name", AttributeType.ValueType.STRING);
                lastName.setSupertype(conceptMgr.getAttributeType("name"));
                conceptMgr.getEntityType("person").setOwns(lastName);
                txn.commit();
            }
            // check the new attribute type is re-indexed in the conclusions index
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                LogicManager logicMgr = txn.logic();
                Rule marriageSameName = logicMgr.getRule("marriage-same-name");
                assertEquals(set(marriageSameName), logicMgr.rulesConcludingHas(Label.of("last-name")).toSet());
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Variable(com.vaticle.typedb.core.pattern.variable.Variable) ThingVariable(com.vaticle.typeql.lang.pattern.variable.ThingVariable) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 45 with AttributeType

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

the class RuleTest method rule_when_that_cannot_be_satisfied_throws_an_error.

@Test
public void rule_when_that_cannot_be_satisfied_throws_an_error() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                final ConceptManager conceptMgr = txn.concepts();
                final EntityType person = conceptMgr.putEntityType("person");
                final EntityType dog = conceptMgr.putEntityType("dog");
                final AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                person.setOwns(name);
                // a when using an illegal comparator
                assertThrowsTypeDBException(() -> txn.logic().putRule("two-unique-dogs-exist-called-fido", TypeQL.parsePattern("{$x isa dog; $y isa dog; $x != $y;}").asConjunction(), TypeQL.parseVariable("$x has name 'fido'").asThing()), RULE_WHEN_CANNOT_BE_SATISFIED.code());
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Aggregations

AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)53 EntityType (com.vaticle.typedb.core.concept.type.EntityType)27 Test (org.junit.Test)24 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)21 CoreSession (com.vaticle.typedb.core.database.CoreSession)20 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)20 RelationType (com.vaticle.typedb.core.concept.type.RelationType)16 Then (io.cucumber.java.en.Then)16 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)13 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)11 ThingType (com.vaticle.typedb.core.concept.type.ThingType)10 LogicManager (com.vaticle.typedb.core.logic.LogicManager)9 When (io.cucumber.java.en.When)7 TypeDB (com.vaticle.typedb.core.TypeDB)4 Options (com.vaticle.typedb.core.common.parameters.Options)4 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)4 RoleType (com.vaticle.typedb.core.concept.type.RoleType)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)3 Variable (com.vaticle.typedb.core.pattern.variable.Variable)3 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)3