Search in sources :

Example 1 with EntityType

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

the class RuleTest method rule_contains_indexes_prevent_undefining_contained_types.

@Test
public void rule_contains_indexes_prevent_undefining_contained_types() 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 friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                marriage.setRelates("spouse");
                person.setPlays(friendship.getRelates("friend"));
                person.setPlays(marriage.getRelates("spouse"));
                person.setOwns(name);
                Rule marriageFriendsRule = logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
                Conjunction marriageFriendsThen = marriageFriendsRule.then();
                Variable marriageFriendsRelation = getVariable(marriageFriendsThen.variables(), Identifier.Variable.anon(0));
                assertEquals(set(Label.of("friendship")), marriageFriendsRelation.inferredTypes());
                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("name")), nameAttr.inferredTypes());
                txn.commit();
            }
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType person = conceptMgr.getEntityType("person");
                assertThrowsTypeDBException(person::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.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) 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 2 with EntityType

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

the class RuleTest method rule_has_explicit_materialises_when_missing.

@Test
public void rule_has_explicit_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, has age-in-days $a; $a >= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good false").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();
                milkInst.setHas(ageInDays.asLong().put(20L));
                Rule rule = txn.logic().getRule("old-milk-is-not-good");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst)));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(3, materialisations.get(0).size());
                AttributeType isStillGood = conceptMgr.getAttributeType("is-still-good");
                List<? extends Attribute> isStillGoodOwned = milkInst.getHas(isStillGood).toList();
                assertEquals(1, isStillGoodOwned.size());
                assertEquals(isStillGood.asBoolean().getInstances().first().get(), isStillGoodOwned.get(0));
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) 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 3 with EntityType

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

the class RuleTest method rule_then_that_cannot_be_satisfied_throws_an_error.

@Test
public void rule_then_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);
                ThingVariable<?> then = TypeQL.parseVariable("$x has name 'fido'").asThing();
                assertThrowsTypeDBException(() -> txn.logic().putRule("dogs-are-named-fido", TypeQL.parsePattern("{$x isa dog;}").asConjunction(), then), RULE_THEN_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)

Example 4 with EntityType

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

the class RuleTest method rule_relation_materialises_when_missing.

@Test
public void rule_relation_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();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.putEntityType("person");
                RelationType friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                marriage.setRelates("spouse");
                person.setPlays(friendship.getRelates("friend"));
                person.setPlays(marriage.getRelates("spouse"));
                logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").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();
                txn.query().insert(TypeQL.parseQuery("insert $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage;").asInsert());
                EntityType person = conceptMgr.getEntityType("person");
                List<? extends Entity> people = person.getInstances().toList();
                assertEquals(2, people.size());
                Rule rule = txn.logic().getRule("marriage-is-friendship");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), people.get(0)), pair(Identifier.Variable.name("y"), people.get(1))));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(5, materialisations.get(0).size());
                RelationType friendship = conceptMgr.getRelationType("friendship");
                List<? extends Relation> friendshipInstances = friendship.getInstances().toList();
                assertEquals(1, friendshipInstances.size());
                assertEquals(set(people.get(0), people.get(1)), friendshipInstances.get(0).getPlayers().toSet());
            }
        }
    }
}
Also used : 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) RelationType(com.vaticle.typedb.core.concept.type.RelationType) 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 5 with EntityType

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

the class RuleTest method rule_that_cannot_be_inserted_throws_an_error.

@Test
public void rule_that_cannot_be_inserted_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 animal = conceptMgr.putEntityType("animal");
                final EntityType person = conceptMgr.putEntityType("person");
                final EntityType dog = conceptMgr.putEntityType("dog");
                final AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                person.setOwns(name);
                person.setSupertype(animal);
                dog.setSupertype(animal);
                assertThrows(() -> txn.logic().putRule("animals-are-named-fido", TypeQL.parsePattern("{$x isa animal;}").asConjunction(), TypeQL.parseVariable("$x has name 'fido'").asThing()));
            }
        }
    }
}
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

EntityType (com.vaticle.typedb.core.concept.type.EntityType)32 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)28 Test (org.junit.Test)28 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)24 CoreSession (com.vaticle.typedb.core.database.CoreSession)22 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)22 RelationType (com.vaticle.typedb.core.concept.type.RelationType)20 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)16 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)14 LogicManager (com.vaticle.typedb.core.logic.LogicManager)10 TypeDB (com.vaticle.typedb.core.TypeDB)6 Options (com.vaticle.typedb.core.common.parameters.Options)5 RoleType (com.vaticle.typedb.core.concept.type.RoleType)5 Entity (com.vaticle.typedb.core.concept.thing.Entity)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)4 Variable (com.vaticle.typedb.core.pattern.variable.Variable)4 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)4 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)4 Map (java.util.Map)4 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)3