Search in sources :

Example 21 with CoreTransaction

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

the class ExplanationTest method test_has_explicit_explainable_two_ways.

@Test
public void test_has_explicit_explainable_two_ways() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            LogicManager logicMgr = txn.logic();
            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);
            logicMgr.putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days <= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good true").asThing());
            logicMgr.putRule("all-milk-is-good", TypeQL.parsePattern("{ $x isa milk; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good true").asThing());
            txn.commit();
        }
    }
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 5;").asInsert());
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 15;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ, (new Options.Transaction().explain(true)))) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has is-still-good $a;").asMatch()).toList();
            assertEquals(3, ans.size());
            assertFalse(ans.get(0).explainables().isEmpty());
            assertFalse(ans.get(1).explainables().isEmpty());
            assertFalse(ans.get(2).explainables().isEmpty());
            AttributeType ageInDays = txn.concepts().getAttributeType("age-in-days");
            if (ans.get(0).get("x").asThing().getHas(ageInDays).first().get().asLong().getValue().equals(15L)) {
                assertSingleExplainableExplanations(ans.get(0), 0, 1, 1, txn);
            } else
                assertSingleExplainableExplanations(ans.get(0), 0, 1, 2, txn);
            if (ans.get(1).get("x").asThing().getHas(ageInDays).first().get().asLong().getValue().equals(15L)) {
                assertSingleExplainableExplanations(ans.get(1), 0, 1, 1, txn);
            } else
                assertSingleExplainableExplanations(ans.get(1), 0, 1, 2, txn);
            if (ans.get(2).get("x").asThing().getHas(ageInDays).first().get().asLong().getValue().equals(15L)) {
                assertSingleExplainableExplanations(ans.get(2), 0, 1, 1, txn);
            } else
                assertSingleExplainableExplanations(ans.get(2), 0, 1, 2, txn);
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Options(com.vaticle.typedb.core.common.parameters.Options) LogicManager(com.vaticle.typedb.core.logic.LogicManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 22 with CoreTransaction

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

the class ExplanationTest method test_relation_explainable_multiple_ways.

@Test
public void test_relation_explainable_multiple_ways() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            LogicManager logicMgr = txn.logic();
            EntityType person = conceptMgr.putEntityType("person");
            AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
            person.setOwns(name);
            RelationType friendship = conceptMgr.putRelationType("friendship");
            friendship.setRelates("friend");
            RelationType marriage = conceptMgr.putRelationType("marriage");
            marriage.setRelates("husband");
            marriage.setRelates("wife");
            person.setPlays(friendship.getRelates("friend"));
            person.setPlays(marriage.getRelates("husband"));
            person.setPlays(marriage.getRelates("wife"));
            logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (husband: $x, wife: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
            logicMgr.putRule("everyone-is-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; not { $x is $y; }; }").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 = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            txn.query().insert(TypeQL.parseQuery("insert $x isa person, has name 'Zack'; $y isa person, has name 'Yasmin'; (husband: $x, wife: $y) isa marriage;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ, (new Options.Transaction().explain(true)))) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match (friend: $p1, friend: $p2) isa friendship; $p1 has name $na;").asMatch()).toList();
            assertEquals(2, ans.size());
            assertFalse(ans.get(0).explainables().isEmpty());
            assertFalse(ans.get(1).explainables().isEmpty());
            assertSingleExplainableExplanations(ans.get(0), 1, 1, 3, txn);
            assertSingleExplainableExplanations(ans.get(1), 1, 1, 3, txn);
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Options(com.vaticle.typedb.core.common.parameters.Options) LogicManager(com.vaticle.typedb.core.logic.LogicManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 23 with CoreTransaction

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

the class ExplanationTest method singleThreadElgTransaction.

private CoreTransaction singleThreadElgTransaction(CoreSession session, Arguments.Transaction.Type transactionType, Options.Transaction options) {
    CoreTransaction transaction = session.transaction(transactionType, options.infer(true));
    ActorExecutorGroup service = new ActorExecutorGroup(1, new NamedThreadFactory("typedb-actor"));
    transaction.reasoner().resolverRegistry().setExecutorService(service);
    return transaction;
}
Also used : NamedThreadFactory(com.vaticle.typedb.common.concurrent.NamedThreadFactory) ActorExecutorGroup(com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction)

Example 24 with CoreTransaction

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

the class EncodingTest method encoded_keys_are_correct.

@Test
public void encoded_keys_are_correct() {
    try (CoreTransaction transaction = session.transaction(WRITE)) {
        Storage.Data storage = transaction.traversal().graph().data().storage();
        assertEquals(0, vertexElements(storage).size());
        assertEquals(0, variableStartEdgeElements(storage).size());
        assertEquals(0, fixedStartEdgeElements(storage).size());
        assertEquals(0, optimisationEdgeElements(storage).size());
        transaction.query().insert(TypeQL.parseQuery("insert " + "$p isa person, has name 'Alice';" + "$c isa company, has company-id 10;" + "(employer: $c, employee: $p) isa employment;").asInsert());
        transaction.commit();
    }
    try (CoreTransaction transaction = session.transaction(WRITE)) {
        GraphManager graph = transaction.traversal().graph();
        Storage.Data storage = graph.data().storage();
        List<KeyValue<RawKey, ByteArray>> vertexKVs = vertexElements(storage);
        assertTrue(iterate(vertexKVs).allMatch(kv -> kv.value().isEmpty()));
        List<ByteArray> vertices = iterate(vertexKVs).map(kv -> kv.key().bytes()).toList();
        // we must have exactly the expected set of vertex bytes
        assertEquals(7, vertices.size());
        ByteArray personIID = expectedIID(graph, Label.of("person"), 0);
        assertTrue(vertices.contains(personIID));
        ByteArray companyIID = expectedIID(graph, Label.of("company"), 0);
        assertTrue(vertices.contains(companyIID));
        ByteArray employmentIID = expectedIID(graph, Label.of("employment"), 0);
        assertTrue(vertices.contains(employmentIID));
        ByteArray employeeRoleIID = expectedIID(graph, Label.of("employee", "employment"), 0);
        assertTrue(vertices.contains(employeeRoleIID));
        ByteArray employerRoleIID = expectedIID(graph, Label.of("employer", "employment"), 0);
        assertTrue(vertices.contains(employerRoleIID));
        ByteArray nameAliceIID = expectedAttributeIID(graph, Label.of("name"), "Alice");
        assertTrue(vertices.contains(nameAliceIID));
        ByteArray companyId10IID = expectedAttributeIID(graph, Label.of("company-id"), 10);
        assertTrue(vertices.contains(companyId10IID));
        // we must have exactly the right set of fixed length edges: relates, plays, and forward has
        List<KeyValue<RawKey, ByteArray>> fixedKVs = fixedStartEdgeElements(storage);
        assertTrue(iterate(fixedKVs).allMatch(kv -> kv.value().isEmpty()));
        List<ByteArray> fixed = iterate(fixedKVs).map(kv -> kv.key().bytes()).toList();
        assertEquals(10, fixed.size());
        assertTrue(fixed.contains(join(personIID, HAS.forward().bytes(), nameAliceIID)));
        assertTrue(fixed.contains(join(personIID, PLAYING.forward().bytes(), employeeRoleIID)));
        assertTrue(fixed.contains(join(employeeRoleIID, PLAYING.backward().bytes(), personIID)));
        assertTrue(fixed.contains(join(employeeRoleIID, RELATING.backward().bytes(), employmentIID)));
        assertTrue(fixed.contains(join(employmentIID, RELATING.forward().bytes(), employeeRoleIID)));
        assertTrue(fixed.contains(join(employmentIID, RELATING.forward().bytes(), employerRoleIID)));
        assertTrue(fixed.contains(join(employerRoleIID, RELATING.backward().bytes(), employmentIID)));
        assertTrue(fixed.contains(join(employerRoleIID, PLAYING.backward().bytes(), companyIID)));
        assertTrue(fixed.contains(join(companyIID, PLAYING.forward().bytes(), employerRoleIID)));
        assertTrue(fixed.contains(join(companyIID, HAS.forward().bytes(), companyId10IID)));
        // we must have exactly the right set of variable length edges: backward has
        List<KeyValue<RawKey, ByteArray>> variableKVs = variableStartEdgeElements(storage);
        assertTrue(iterate(variableKVs).allMatch(kv -> kv.value().isEmpty()));
        List<ByteArray> variable = iterate(variableKVs).map(kv -> kv.key().bytes()).toList();
        assertEquals(2, variable.size());
        assertTrue(variable.contains(join(nameAliceIID, HAS.backward().bytes(), personIID)));
        assertTrue(variable.contains(join(companyId10IID, HAS.backward().bytes(), companyIID)));
        // we must have exactly the right set of optimisation edges: role player
        List<KeyValue<RawKey, ByteArray>> optimisationKVs = optimisationEdgeElements(storage);
        assertTrue(iterate(optimisationKVs).allMatch(kv -> kv.value().isEmpty()));
        List<ByteArray> optimisation = iterate(optimisationKVs).map(kv -> kv.key().bytes()).toList();
        assertEquals(4, optimisation.size());
        TypeVertex employerRoleType = graph.schema().getType(Label.of("employer", "employment"));
        TypeVertex employeeRoleType = graph.schema().getType(Label.of("employee", "employment"));
        assertTrue(optimisation.contains(join(personIID, ROLEPLAYER.backward().bytes(), employeeRoleType.iid().bytes(), employmentIID, employeeRoleIID.view(1 + employeeRoleType.iid().bytes().length()))));
        assertTrue(optimisation.contains(join(employmentIID, ROLEPLAYER.forward().bytes(), employeeRoleType.iid().bytes(), personIID, employeeRoleIID.view(1 + employeeRoleType.iid().bytes().length()))));
        assertTrue(optimisation.contains(join(companyIID, ROLEPLAYER.backward().bytes(), employerRoleType.iid().bytes(), employmentIID, employerRoleIID.view(1 + employerRoleType.iid().bytes().length()))));
        assertTrue(optimisation.contains(join(employmentIID, ROLEPLAYER.forward().bytes(), employerRoleType.iid().bytes(), companyIID, employerRoleIID.view(1 + employerRoleType.iid().bytes().length()))));
    }
}
Also used : Factory(com.vaticle.typedb.core.database.Factory) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) ByteArray.encodeLong(com.vaticle.typedb.core.common.collection.ByteArray.encodeLong) VERTEX_ROLE(com.vaticle.typedb.core.graph.common.Encoding.Prefix.VERTEX_ROLE) After(org.junit.After) CoreFactory(com.vaticle.typedb.core.database.CoreFactory) ByteArray.join(com.vaticle.typedb.core.common.collection.ByteArray.join) ByteArray(com.vaticle.typedb.core.common.collection.ByteArray) Path(java.nio.file.Path) VERTEX_ENTITY(com.vaticle.typedb.core.graph.common.Encoding.Prefix.VERTEX_ENTITY) TypeQL(com.vaticle.typeql.lang.TypeQL) AfterClass(org.junit.AfterClass) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) ROLE(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Thing.ROLE) ENTITY(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Thing.ENTITY) RELATING(com.vaticle.typedb.core.graph.common.Encoding.Edge.Thing.Base.RELATING) TypeDB(com.vaticle.typedb.core.TypeDB) List(java.util.List) PLAYING(com.vaticle.typedb.core.graph.common.Encoding.Edge.Thing.Base.PLAYING) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) TypeDBCheckedException(com.vaticle.typedb.core.common.exception.TypeDBCheckedException) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) RELATION(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Thing.RELATION) ByteArray.encodeLongAsSorted(com.vaticle.typedb.core.common.collection.ByteArray.encodeLongAsSorted) BeforeClass(org.junit.BeforeClass) KeyValue(com.vaticle.typedb.core.common.collection.KeyValue) Collections.set(com.vaticle.typedb.common.collection.Collections.set) STRING_ENCODING(com.vaticle.typedb.core.graph.common.Encoding.ValueType.STRING_ENCODING) WRITE(com.vaticle.typedb.core.common.parameters.Arguments.Transaction.Type.WRITE) ByteArray.encodeStringAsSorted(com.vaticle.typedb.core.common.collection.ByteArray.encodeStringAsSorted) Storage(com.vaticle.typedb.core.graph.common.Storage) Label(com.vaticle.typedb.core.common.parameters.Label) Encoding(com.vaticle.typedb.core.graph.common.Encoding) Before(org.junit.Before) ILLEGAL_ARGUMENT(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_ARGUMENT) HAS(com.vaticle.typedb.core.graph.common.Encoding.Edge.Thing.Base.HAS) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) CoreSession(com.vaticle.typedb.core.database.CoreSession) VERTEX_RELATION(com.vaticle.typedb.core.graph.common.Encoding.Prefix.VERTEX_RELATION) Paths(java.nio.file.Paths) ROLEPLAYER(com.vaticle.typedb.core.graph.common.Encoding.Edge.Thing.Optimised.ROLEPLAYER) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) VERTEX_ATTRIBUTE(com.vaticle.typedb.core.graph.common.Encoding.Prefix.VERTEX_ATTRIBUTE) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) ATTRIBUTE(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Thing.ATTRIBUTE) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) Storage(com.vaticle.typedb.core.graph.common.Storage) KeyValue(com.vaticle.typedb.core.common.collection.KeyValue) ByteArray(com.vaticle.typedb.core.common.collection.ByteArray) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 25 with CoreTransaction

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

the class CorrectnessVerifierTest method testSoundnessThrowsWhenRuleTriggersTooOftenEmployableExample.

@Test
public void testSoundnessThrowsWhenRuleTriggersTooOftenEmployableExample() {
    TypeQLMatch inferenceQuery = parseQuery("match $x has employable true;").asMatch();
    CorrectnessVerifier correctnessVerifier;
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        correctnessVerifier = CorrectnessVerifier.initialise(session);
        try (CoreTransaction tx = session.transaction(Arguments.Transaction.Type.WRITE)) {
            tx.query().insert(parseQuery("insert $p isa person;"));
            tx.commit();
        }
        assertThrows(() -> correctnessVerifier.verifySoundness(inferenceQuery), SoundnessException.class);
        assertNotThrows(() -> correctnessVerifier.verifyCompleteness(inferenceQuery));
    }
}
Also used : CorrectnessVerifier(com.vaticle.typedb.core.test.behaviour.reasoner.verification.CorrectnessVerifier) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Aggregations

CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)56 CoreSession (com.vaticle.typedb.core.database.CoreSession)46 Test (org.junit.Test)45 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)22 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)20 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)19 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)16 RelationType (com.vaticle.typedb.core.concept.type.RelationType)13 Util.resolvedConjunction (com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction)12 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)12 LogicManager (com.vaticle.typedb.core.logic.LogicManager)10 Options (com.vaticle.typedb.core.common.parameters.Options)9 NamedThreadFactory (com.vaticle.typedb.common.concurrent.NamedThreadFactory)8 ActorExecutorGroup (com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup)8 Variable (com.vaticle.typedb.core.pattern.variable.Variable)7 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)6 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)6 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)6