Search in sources :

Example 6 with EntityType

use of com.vaticle.typedb.core.concept.type.EntityType in project grakn by graknlabs.

the class PlannerTest method test_planner_multiple_dependencies.

@Test
public void test_planner_multiple_dependencies() {
    transaction.query().define(TypeQL.parseQuery("define employment sub relation, relates employee, relates employer;" + "person plays employment:employee;" + "company sub entity, plays employment:employer, owns name;"));
    transaction.commit();
    session.close();
    initialise(Arguments.Session.Type.DATA, Arguments.Transaction.Type.READ);
    EntityType person = conceptMgr.putEntityType("person");
    AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
    person.setOwns(name);
    EntityType company = conceptMgr.putEntityType("company");
    company.setOwns(name);
    conceptMgr.putRelationType("employment");
    Retrievable retrievable = new Retrievable(resolvedConjunction("{ $p isa person; }", logicMgr));
    Concludable concludable = Concludable.create(resolvedConjunction("{ $p has name $n; }", logicMgr)).iterator().next();
    Retrievable retrievable2 = new Retrievable(resolvedConjunction("{ $c isa company, has name $n; }", logicMgr));
    Concludable concludable2 = Concludable.create(resolvedConjunction("{ $e($c, $p2) isa employment; }", logicMgr)).iterator().next();
    Set<Resolvable<?>> resolvables = set(retrievable, retrievable2, concludable, concludable2);
    List<Resolvable<?>> plan = Planner.plan(resolvables, new HashMap<>(), set());
    assertEquals(list(retrievable, concludable, retrievable2, concludable2), plan);
}
Also used : EntityType(com.vaticle.typedb.core.concept.type.EntityType) Concludable(com.vaticle.typedb.core.logic.resolvable.Concludable) Retrievable(com.vaticle.typedb.core.logic.resolvable.Retrievable) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) Resolvable(com.vaticle.typedb.core.logic.resolvable.Resolvable) Test(org.junit.Test)

Example 7 with EntityType

use of com.vaticle.typedb.core.concept.type.EntityType 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 8 with EntityType

use of com.vaticle.typedb.core.concept.type.EntityType 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)

Example 9 with EntityType

use of com.vaticle.typedb.core.concept.type.EntityType 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 10 with EntityType

use of com.vaticle.typedb.core.concept.type.EntityType 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)

Aggregations

EntityType (com.vaticle.typedb.core.concept.type.EntityType)32 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)28 Test (org.junit.Test)28 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)24 CoreSession (com.vaticle.typedb.core.database.CoreSession)22 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)22 RelationType (com.vaticle.typedb.core.concept.type.RelationType)20 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)16 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)14 LogicManager (com.vaticle.typedb.core.logic.LogicManager)10 TypeDB (com.vaticle.typedb.core.TypeDB)6 Options (com.vaticle.typedb.core.common.parameters.Options)5 RoleType (com.vaticle.typedb.core.concept.type.RoleType)5 Entity (com.vaticle.typedb.core.concept.thing.Entity)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)4 Variable (com.vaticle.typedb.core.pattern.variable.Variable)4 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)4 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)4 Map (java.util.Map)4 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)3