Search in sources :

Example 41 with CoreSession

use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.

the class RuleTest method rule_contains_indexes_allow_deleting_type_after_deleting_rule.

@Test
public void rule_contains_indexes_allow_deleting_type_after_deleting_rule() 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();
                GraphManager graphMgr = logicMgr.graph();
                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());
                assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("friend", "friendship"), Label.of("friendship")), marriageFriendsRule.getLabel(), graphMgr);
                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());
                assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("name")), marriageSameName.getLabel(), graphMgr);
                txn.commit();
            }
            // check the rule index is still established after commit
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
                assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("friend", "friendship"), Label.of("friendship")), marriageFriendsRule.getLabel(), txn.logic().graph());
                Rule marriageSameName = txn.logic().getRule("marriage-same-name");
                assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("name")), marriageSameName.getLabel(), txn.logic().graph());
            }
            // deleting a relation type used in a rule should throw
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                RelationType friendship = conceptMgr.getRelationType("friendship");
                assertThrowsTypeDBException(friendship::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
                assertTrue(!txn.isOpen());
            }
            // deleting an attribute type used in a rule should throw
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                AttributeType name = conceptMgr.getAttributeType("name");
                assertThrowsTypeDBException(name::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
                assertTrue(!txn.isOpen());
            }
            // deleting a rule, then an attribute type used in the rule is allowed
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                AttributeType name = conceptMgr.getAttributeType("name");
                LogicManager logicMgr = txn.logic();
                Rule marriageSameName = logicMgr.getRule("marriage-same-name");
                marriageSameName.delete();
                assertNotThrows(name::delete);
                txn.commit();
            }
            // deleting a rule, then an entity type used in the rule is allowed
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                RelationType person = conceptMgr.getRelationType("friendship");
                LogicManager logicMgr = txn.logic();
                Rule marriageIsFriendship = logicMgr.getRule("marriage-is-friendship");
                marriageIsFriendship.delete();
                assertNotThrows(person::delete);
                txn.commit();
            }
            // after all rules are deleted, no rules should exist in the index
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                LogicManager logicMgr = txn.logic();
                GraphManager graphMgr = logicMgr.graph();
                // no types should be in the index
                graphMgr.schema().thingTypes().forEachRemaining(type -> {
                    assertFalse(graphMgr.schema().rules().references().get(graphMgr.schema().getType(type.properLabel())).hasNext());
                });
            }
        }
    }
}
Also used : GraphManager(com.vaticle.typedb.core.graph.GraphManager) 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) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) CoreSession(com.vaticle.typedb.core.database.CoreSession) Test(org.junit.Test)

Example 42 with CoreSession

use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.

the class RuleTest method rule_indexes_update_on_rule_delete.

@Test
public void rule_indexes_update_on_rule_delete() 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 allFriendsRule = logicMgr.putRule("all-people-are-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; $t type friendship; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa $t").asThing());
                Conjunction allFriendsThen = allFriendsRule.then();
                Variable allFriendsRelation = getVariable(allFriendsThen.variables(), Identifier.Variable.anon(0));
                assertEquals(set(Label.of("friendship")), allFriendsRelation.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();
            }
            // check index after commit, and delete some rules
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                LogicManager logicMgr = txn.logic();
                Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
                Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
                Rule allFriendsRule = txn.logic().getRule("all-people-are-friends");
                assertEquals(set(marriageFriendsRule, allFriendsRule), friendshipRules);
                allFriendsRule.delete();
                Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
                Rule marriageSameName = txn.logic().getRule("marriage-same-name");
                assertEquals(set(marriageSameName), hasNameRules);
                marriageSameName.delete();
                txn.commit();
            }
        }
        // check indexed types, should only includes rules that are still present
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                LogicManager logicMgr = txn.logic();
                Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
                Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
                assertEquals(set(marriageFriendsRule), friendshipRules);
                Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
                assertEquals(set(), hasNameRules);
            }
        }
    }
}
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 43 with CoreSession

use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.

the class RuleTest method rule_relation_does_not_materialise_when_present.

@Test
public void rule_relation_does_not_materialise_when_present() 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();
                RelationType friendship = conceptMgr.getRelationType("friendship");
                txn.query().insert(TypeQL.parseQuery("insert $x isa person; $y isa person; " + "(spouse: $x, spouse: $y) isa marriage;" + "(friend: $x, friend: $y) isa friendship;").asInsert());
                List<? extends Relation> friendshipInstances = friendship.getInstances().toList();
                assertEquals(1, friendshipInstances.size());
                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());
                friendshipInstances = friendship.getInstances().toList();
                assertEquals(1, friendshipInstances.size());
                assertEquals(friendshipInstances.get(0), materialisations.get(0).get(Identifier.Variable.anon(0)));
                assertEquals(friendship, materialisations.get(0).get(Identifier.Variable.label("friendship")));
            }
        }
    }
}
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 44 with CoreSession

use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.

the class ConsistencyTest method large_loads_end_with_zero_transaction_isolation_sets.

@Test
public void large_loads_end_with_zero_transaction_isolation_sets() throws ExecutionException, InterruptedException {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        TypeDB.Transaction setupTxn = session.transaction(Arguments.Transaction.Type.WRITE);
        setupTxn.query().insert(TypeQL.parseQuery("insert $x isa person, has name 'Bob'; $y isa person, has name 'Alice';"));
        setupTxn.commit();
        Random random = new Random(0);
        List<CompletableFuture<Void>> workers = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            workers.add(CompletableFuture.runAsync(() -> {
                try {
                    Thread.sleep(random.nextInt(200));
                    try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                        for (int j = 0; j < 1; j++) {
                            txn.query().insert(TypeQL.parseQuery("insert $x isa person, has name 'alice-" + random.nextInt(100) + "';"));
                        }
                        txn.commit();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }));
        }
        CompletableFuture.allOf(workers.toArray(new CompletableFuture[0])).get();
        assertEquals(0, session.database().consistencyMgr().committedEventCount());
        TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.WRITE);
        txn.query().insert(TypeQL.parseQuery("match $x isa person, has name 'Bob'; $y isa person, has name 'Alice'; " + "insert $f (friend: $x, friend: $y) isa friendship;"));
        txn.commit();
        assertEquals(0, session.database().consistencyMgr().committedEventCount());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Random(java.util.Random) ArrayList(java.util.ArrayList) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeDB(com.vaticle.typedb.core.TypeDB) Test(org.junit.Test)

Example 45 with CoreSession

use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.

the class ReasonerSteps method schema.

@Given("reasoning schema")
public void schema(String defineQueryStatements) {
    if (correctnessVerifier != null)
        correctnessVerifier.close();
    if (session != null)
        session.close();
    try (CoreSession session = databaseMgr.session(DATABASE, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction tx = session.transaction(Arguments.Transaction.Type.WRITE)) {
            tx.query().define(TypeQL.parseQuery(String.join("\n", defineQueryStatements)).asDefine());
            tx.commit();
        }
    }
}
Also used : CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Given(io.cucumber.java.en.Given)

Aggregations

CoreSession (com.vaticle.typedb.core.database.CoreSession)45 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)42 Test (org.junit.Test)42 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)23 EntityType (com.vaticle.typedb.core.concept.type.EntityType)23 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)21 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)18 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)16 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)14 RelationType (com.vaticle.typedb.core.concept.type.RelationType)13 LogicManager (com.vaticle.typedb.core.logic.LogicManager)10 Util.resolvedConjunction (com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction)10 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)8 Options (com.vaticle.typedb.core.common.parameters.Options)7 Map (java.util.Map)6 Variable (com.vaticle.typedb.core.pattern.variable.Variable)5 TypeQLMatch (com.vaticle.typeql.lang.query.TypeQLMatch)4 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)3 CorrectnessVerifier (com.vaticle.typedb.core.test.behaviour.reasoner.verification.CorrectnessVerifier)3 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)3