Search in sources :

Example 16 with LogicManager

use of com.vaticle.typedb.core.logic.LogicManager in project grakn by graknlabs.

the class ReasonerTest method test_exception_kills_query.

@Test
public void test_exception_kills_query() {
    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 false").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.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
            txn.reasoner().resolverRegistry().terminateResolvers(new RuntimeException());
            try {
                List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x isa is-still-good;").asMatch()).toList();
            } catch (TypeDBException e) {
                assertEquals(e.code().get(), RESOLUTION_TERMINATED.code());
                return;
            }
            fail();
        }
    }
}
Also used : 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) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 17 with LogicManager

use of com.vaticle.typedb.core.logic.LogicManager 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 18 with LogicManager

use of com.vaticle.typedb.core.logic.LogicManager 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)

Example 19 with LogicManager

use of com.vaticle.typedb.core.logic.LogicManager in project grakn by graknlabs.

the class BasicTest method write_and_retrieve_relation_rule.

@Test
public void write_and_retrieve_relation_rule() throws IOException {
    Util.resetDirectory(dataDir);
    try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
        typedb.create(database);
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
            try (TypeDB.Transaction 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 (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.getEntityType("person");
                RelationType friendship = conceptMgr.getRelationType("friendship");
                RoleType friend = friendship.getRelates("friend");
                RelationType marriage = conceptMgr.getRelationType("marriage");
                RoleType spouse = marriage.getRelates("spouse");
                Rule rule = logicMgr.getRule("marriage-is-friendship");
                Pattern when = rule.getWhenPreNormalised();
                ThingVariable<?> then = rule.getThenPreNormalised();
                assertEquals(TypeQL.parsePattern("{$x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }"), when);
                assertEquals(TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship"), then);
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Pattern(com.vaticle.typeql.lang.pattern.Pattern) LogicManager(com.vaticle.typedb.core.logic.LogicManager) RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Rule(com.vaticle.typedb.core.logic.Rule) TypeDB(com.vaticle.typedb.core.TypeDB) Test(org.junit.Test)

Example 20 with LogicManager

use of com.vaticle.typedb.core.logic.LogicManager in project grakn by graknlabs.

the class CoreTransaction method initialise.

void initialise(GraphManager graphMgr, TraversalCache traversalCache, LogicCache logicCache) {
    traversalEng = new TraversalEngine(graphMgr, traversalCache);
    conceptMgr = new ConceptManager(graphMgr);
    logicMgr = new LogicManager(graphMgr, conceptMgr, traversalEng, logicCache);
    reasoner = new Reasoner(conceptMgr, logicMgr, traversalEng, context);
    queryMgr = new QueryManager(conceptMgr, logicMgr, reasoner, context);
    isOpen = new AtomicBoolean(true);
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogicManager(com.vaticle.typedb.core.logic.LogicManager) Reasoner(com.vaticle.typedb.core.reasoner.Reasoner) TraversalEngine(com.vaticle.typedb.core.traversal.TraversalEngine) QueryManager(com.vaticle.typedb.core.query.QueryManager)

Aggregations

LogicManager (com.vaticle.typedb.core.logic.LogicManager)26 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)24 Test (org.junit.Test)24 EntityType (com.vaticle.typedb.core.concept.type.EntityType)22 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)20 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)20 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 Options (com.vaticle.typedb.core.common.parameters.Options)14 Explanation (com.vaticle.typedb.core.reasoner.resolution.answer.Explanation)4 TypeDB (com.vaticle.typedb.core.TypeDB)3 Rule (com.vaticle.typedb.core.logic.Rule)3 Pattern (com.vaticle.typeql.lang.pattern.Pattern)3 Pair (com.vaticle.typedb.common.collection.Pair)2 NamedThreadFactory (com.vaticle.typedb.common.concurrent.NamedThreadFactory)2 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)2 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)2 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)2 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)2