Search in sources :

Example 1 with LogicManager

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

the class ReasonerTest method test_relation_rule.

@Test
public void test_relation_rule() {
    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());
            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)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $f (friend: $p1, friend: $p2) isa friendship; $p1 has name $na;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertEquals("friendship", a.get("f").asThing().getType().getLabel().scopedName());
                assertEquals("person", a.get("p1").asThing().getType().getLabel().scopedName());
                assertEquals("person", a.get("p2").asThing().getType().getLabel().scopedName());
                assertEquals("name", a.get("na").asAttribute().getType().getLabel().scopedName());
            });
            assertEquals(2, ans.size());
        }
    }
}
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) 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 2 with LogicManager

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

the class BasicTest method write_and_retrieve_attribute_ownership_rule.

@Test
public void write_and_retrieve_attribute_ownership_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();
                AttributeType name = conceptMgr.putAttributeType("name", STRING);
                EntityType person = conceptMgr.putEntityType("person");
                RelationType friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                person.setPlays(friendship.getRelates("friend"));
                person.setOwns(name);
                logicMgr.putRule("people-have-names", TypeQL.parsePattern("{$x isa person; }").asConjunction(), TypeQL.parseVariable("$x has name \"i have a name\"").asThing());
                txn.commit();
            }
            try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                Rule rule = logicMgr.getRule("people-have-names");
                Pattern when = rule.getWhenPreNormalised();
                ThingVariable<?> then = rule.getThenPreNormalised();
                assertEquals(TypeQL.parsePattern("{$x isa person;}"), when);
                assertEquals(TypeQL.parseVariable("$x has name \"i have a name\""), 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) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) 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 3 with LogicManager

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

the class ExplanationTest method test_disjunction_explainable.

@Test
public void test_disjunction_explainable() {
    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());
            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 $p1 isa person; { (friend: $p1, friend: $p2) isa friendship;} or { $p1 has name 'Zack'; }; ").asMatch()).toList();
            assertEquals(3, ans.size());
            ConceptMap withExplainable;
            ConceptMap withoutExplainable;
            if (ans.get(0).contains("p2")) {
                withExplainable = ans.get(0);
                if (!ans.get(1).contains("p2"))
                    withoutExplainable = ans.get(1);
                else
                    withoutExplainable = ans.get(2);
            } else if (ans.get(1).contains("p2")) {
                withExplainable = ans.get(1);
                if (!ans.get(0).contains("p2"))
                    withoutExplainable = ans.get(0);
                else
                    withoutExplainable = ans.get(2);
            } else {
                withExplainable = ans.get(2);
                if (!ans.get(0).contains("p2"))
                    withoutExplainable = ans.get(0);
                else
                    withoutExplainable = ans.get(1);
            }
            assertEquals(3, withExplainable.concepts().size());
            assertEquals(2, withoutExplainable.concepts().size());
            assertFalse(withExplainable.explainables().isEmpty());
            assertTrue(withoutExplainable.explainables().isEmpty());
            assertSingleExplainableExplanations(withExplainable, 1, 1, 1, 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 4 with LogicManager

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

the class ExplanationTest method test_has_variable_explainable_two_ways.

@Test
public void test_has_variable_explainable_two_ways() {
    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 " + "user sub entity, " + "  plays group-membership:member, " + "  owns permission; " + "user-group sub entity," + "  plays group-membership:user-group," + "  owns permission," + "  owns name; " + "group-membership sub relation," + "  relates member," + "  relates user-group; " + "permission sub attribute, value string;" + "name sub attribute, value string;").asDefine());
            logicMgr.putRule("admin-group-gives-permissions", TypeQL.parsePattern("{ $x isa user; ($x, $g) isa group-membership; $g isa user-group, has name \"admin\", has permission $p; }").asConjunction(), TypeQL.parseVariable("$x has $p").asThing());
            logicMgr.putRule("writer-group-gives-permissions", TypeQL.parsePattern("{ $x isa user; ($x, $g) isa group-membership; $g isa user-group, has name \"write\", has permission $p; }").asConjunction(), TypeQL.parseVariable("$x has $p").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 user; " + "$wg isa user-group, has name \"write\", has permission \"write\";" + "(member: $x, user-group: $wg) isa group-membership;" + "$admin isa user-group, has name \"admin\", has permission \"write\", has permission \"delete\";" + "(member: $x, user-group: $admin) isa group-membership;").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 user, has permission \"write\";").asMatch()).toList();
            assertEquals(1, ans.size());
            assertFalse(ans.get(0).explainables().isEmpty());
            assertSingleExplainableExplanations(ans.get(0), 1, 1, 2, txn);
        }
    }
}
Also used : Options(com.vaticle.typedb.core.common.parameters.Options) LogicManager(com.vaticle.typedb.core.logic.LogicManager) 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 5 with LogicManager

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

the class ExplanationTest method test_relation_explainable.

@Test
public void test_relation_explainable() {
    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());
            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, 1, txn);
            assertSingleExplainableExplanations(ans.get(1), 1, 1, 1, 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)

Aggregations

LogicManager (com.vaticle.typedb.core.logic.LogicManager)13 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)12 Test (org.junit.Test)12 EntityType (com.vaticle.typedb.core.concept.type.EntityType)11 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)10 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)10 CoreSession (com.vaticle.typedb.core.database.CoreSession)10 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)10 RelationType (com.vaticle.typedb.core.concept.type.RelationType)8 Options (com.vaticle.typedb.core.common.parameters.Options)7 TypeDB (com.vaticle.typedb.core.TypeDB)2 Rule (com.vaticle.typedb.core.logic.Rule)2 Explanation (com.vaticle.typedb.core.reasoner.resolution.answer.Explanation)2 Pattern (com.vaticle.typeql.lang.pattern.Pattern)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 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)1 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)1 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)1