Search in sources :

Example 1 with Explanation

use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.

the class ExplanationProducer method requestAnswered.

// note: root resolver calls this single-threaded, so is threads safe
private void requestAnswered(Explain.Finished explainedAnswer) {
    if (options.traceInference())
        ResolutionTracer.get().finish();
    Explanation explanation = explainedAnswer.explanation();
    explainablesManager.setAndRecordExplainables(explanation.conditionAnswer());
    queue.put(explanation);
    if (required.decrementAndGet() > 0)
        requestExplanation();
    else
        processing.decrementAndGet();
}
Also used : Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation)

Example 2 with Explanation

use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.

the class ExplanationTest method assertSingleExplainableExplanations.

private List<Explanation> assertSingleExplainableExplanations(ConceptMap ans, int anonymousConcepts, int explainablesCount, int explanationsCount, CoreTransaction txn) {
    List<ConceptMap.Explainable> explainables = ans.explainables().iterator().toList();
    assertEquals(anonymousConcepts, iterate(ans.concepts().keySet()).filter(Identifier::isAnonymous).count());
    assertEquals(explainablesCount, explainables.size());
    ConceptMap.Explainable explainable = explainables.iterator().next();
    assertNotEquals(NOT_IDENTIFIED, explainable.id());
    List<Explanation> explanations = txn.query().explain(explainable.id()).toList();
    assertEquals(explanationsCount, explanations.size());
    explanations.forEach(explanation -> {
        Map<Retrievable, Set<Retrievable>> mapping = explanation.variableMapping();
        ConceptMap projected = applyMapping(mapping, ans);
        projected.concepts().forEach((var, concept) -> {
            assertTrue(explanation.conclusionAnswer().contains(var));
            assertEquals(explanation.conclusionAnswer().get(var), concept);
        });
    });
    return explanations;
}
Also used : Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Set(java.util.Set) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Example 3 with Explanation

use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.

the class ExplanationTest method test_all_transitive_explanations.

@Test
public void test_all_transitive_explanations() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            LogicManager logicMgr = txn.logic();
            txn.query().define(TypeQL.parseQuery("define " + "location sub entity, " + "  plays location-hierarchy:superior, " + "  plays location-hierarchy:subordinate; " + "location-hierarchy sub relation," + "  relates superior," + "  relates subordinate;").asDefine());
            logicMgr.putRule("transitive-location", TypeQL.parsePattern("{ (subordinate: $x, superior: $y) isa location-hierarchy;" + "(subordinate: $y, superior: $z) isa location-hierarchy; }").asConjunction(), TypeQL.parseVariable("(subordinate: $x, superior: $z) isa location-hierarchy").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 " + "(subordinate: $a, superior: $b) isa location-hierarchy; " + "(subordinate: $b, superior: $c) isa location-hierarchy; " + "(subordinate: $c, superior: $d) isa location-hierarchy; " + "(subordinate: $d, superior: $e) isa location-hierarchy; " + "$a isa location; $b isa location; $c isa location;" + "$d isa location; $e isa location;").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 $r isa location-hierarchy;").asMatch()).toList();
            assertEquals(10, ans.size());
            List<ConceptMap> explainableMaps = iterate(ans).filter(answer -> !answer.explainables().isEmpty()).toList();
            assertEquals(6, explainableMaps.size());
            Map<Pair<ConceptMap, ConceptMap.Explainable>, List<Explanation>> allExplanations = new HashMap<>();
            for (ConceptMap explainableMap : explainableMaps) {
                List<ConceptMap.Explainable> explainables = explainableMap.explainables().iterator().toList();
                assertEquals(1, explainables.size());
                List<Explanation> explanations = txn.query().explain(explainables.get(0).id()).toList();
                allExplanations.put(new Pair<>(explainableMap, explainables.get(0)), explanations);
            }
            int oneExplanation = 0;
            int twoExplanations = 0;
            int threeExplanations = 0;
            for (Map.Entry<Pair<ConceptMap, ConceptMap.Explainable>, List<Explanation>> entry : allExplanations.entrySet()) {
                List<Explanation> explanations = entry.getValue();
                if (explanations.size() == 1)
                    oneExplanation++;
                else if (explanations.size() == 2)
                    twoExplanations++;
                else if (explanations.size() == 3)
                    threeExplanations++;
                else
                    fail();
            }
            assertEquals(3, oneExplanation);
            assertEquals(2, twoExplanations);
            assertEquals(1, threeExplanations);
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) NOT_IDENTIFIED(com.vaticle.typedb.core.concept.answer.ConceptMap.Explainable.NOT_IDENTIFIED) Pair(com.vaticle.typedb.common.collection.Pair) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) HashMap(java.util.HashMap) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Concept(com.vaticle.typedb.core.concept.Concept) LogicManager(com.vaticle.typedb.core.logic.LogicManager) After(org.junit.After) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) Before(org.junit.Before) TypeQL(com.vaticle.typeql.lang.TypeQL) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) CoreSession(com.vaticle.typedb.core.database.CoreSession) List(java.util.List) NamedThreadFactory(com.vaticle.typedb.common.concurrent.NamedThreadFactory) Paths(java.nio.file.Paths) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) Assert.assertFalse(org.junit.Assert.assertFalse) ActorExecutorGroup(com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) Options(com.vaticle.typedb.core.common.parameters.Options) HashMap(java.util.HashMap) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) LogicManager(com.vaticle.typedb.core.logic.LogicManager) List(java.util.List) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Pair(com.vaticle.typedb.common.collection.Pair) Test(org.junit.Test)

Example 4 with Explanation

use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.

the class ExplanationTest method test_nested_explanations.

@Test
public void test_nested_explanations() {
    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);
            AttributeType gender = conceptMgr.putAttributeType("gender", AttributeType.ValueType.STRING);
            EntityType city = conceptMgr.putEntityType("city");
            person.setOwns(name);
            person.setOwns(gender);
            RelationType friendship = conceptMgr.putRelationType("friendship");
            friendship.setRelates("friend");
            RelationType marriage = conceptMgr.putRelationType("marriage");
            marriage.setRelates("husband");
            marriage.setRelates("wife");
            RelationType wedding = conceptMgr.putRelationType("wedding");
            wedding.setRelates("male");
            wedding.setRelates("female");
            wedding.setRelates("location");
            person.setPlays(friendship.getRelates("friend"));
            person.setPlays(marriage.getRelates("husband"));
            person.setPlays(marriage.getRelates("wife"));
            person.setPlays(wedding.getRelates("male"));
            person.setPlays(wedding.getRelates("female"));
            city.setPlays(wedding.getRelates("location"));
            logicMgr.putRule("wedding-implies-marriage", TypeQL.parsePattern("{ $x isa person, has gender \"male\"; $y isa person, has gender \"female\"; " + "$l isa city; (male: $x, female: $y, location: $l) isa wedding; }").asConjunction(), TypeQL.parseVariable("(husband: $x, wife: $y) isa marriage").asThing());
            logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $a isa person; $b isa person; (husband: $a, wife: $b) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $a, friend: $b) 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 " + "(male: $x, female: $y, location: $l) isa wedding;" + "$x isa person, has gender \"male\";" + "$y isa person, has gender \"female\";" + "$l isa city;").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) isa friendship;").asMatch()).toList();
            assertEquals(2, ans.size());
            assertFalse(ans.get(0).explainables().isEmpty());
            List<Explanation> explanations = assertSingleExplainableExplanations(ans.get(0), 1, 1, 1, txn);
            Explanation explanation = explanations.get(0);
            assertEquals(txn.logic().getRule("marriage-is-friendship"), explanation.rule());
            assertEquals(2, explanation.variableMapping().size());
            assertEquals(3, explanation.conclusionAnswer().concepts().size());
            ConceptMap marriageIsFriendshipAnswer = explanation.conditionAnswer();
            assertEquals(1, marriageIsFriendshipAnswer.explainables().iterator().count());
            assertSingleExplainableExplanations(marriageIsFriendshipAnswer, 1, 1, 1, txn);
        }
    }
}
Also used : Options(com.vaticle.typedb.core.common.parameters.Options) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) 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) Test(org.junit.Test)

Aggregations

Explanation (com.vaticle.typedb.core.reasoner.resolution.answer.Explanation)4 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)3 Options (com.vaticle.typedb.core.common.parameters.Options)2 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)2 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)2 EntityType (com.vaticle.typedb.core.concept.type.EntityType)2 RelationType (com.vaticle.typedb.core.concept.type.RelationType)2 CoreSession (com.vaticle.typedb.core.database.CoreSession)2 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)2 LogicManager (com.vaticle.typedb.core.logic.LogicManager)2 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)2 Retrievable (com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable)2 Set (java.util.Set)2 Test (org.junit.Test)2 Pair (com.vaticle.typedb.common.collection.Pair)1 NamedThreadFactory (com.vaticle.typedb.common.concurrent.NamedThreadFactory)1 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)1 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)1 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)1 Concept (com.vaticle.typedb.core.concept.Concept)1