Search in sources :

Example 46 with AttributeType

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

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

the class ReasonerTest method test_offset_limit.

@Test
public void test_offset_limit() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            EntityType milk = conceptMgr.putEntityType("milk");
            AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
            milk.setOwns(ageInDays);
            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)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertEquals("age-in-days", a.get("a").asThing().getType().getLabel().scopedName());
                assertEquals("milk", a.get("x").asThing().getType().getLabel().scopedName());
            });
            assertEquals(2, ans.size());
            List<ConceptMap> ansLimited = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a; limit 1;").asMatch()).toList();
            assertEquals(1, ansLimited.size());
            List<ConceptMap> ansLimitedOffsetted = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a; offset 1; limit 1;").asMatch()).toList();
            assertEquals(1, ansLimitedOffsetted.size());
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) 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 48 with AttributeType

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

the class ReasonerTest method test_has_explicit_rule.

@Test
public void test_has_explicit_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 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.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)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has is-still-good $a;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertFalse(a.get("a").asAttribute().asBoolean().getValue());
                assertEquals("is-still-good", a.get("a").asThing().getType().getLabel().scopedName());
                assertEquals("milk", a.get("x").asThing().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) 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 49 with AttributeType

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

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

the class ReasonerTest method test_no_rules.

@Test
public void test_no_rules() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            EntityType milk = conceptMgr.putEntityType("milk");
            AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
            milk.setOwns(ageInDays);
            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)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertEquals("age-in-days", a.get("a").asThing().getType().getLabel().scopedName());
                assertEquals("milk", a.get("x").asThing().getType().getLabel().scopedName());
            });
            assertEquals(2, ans.size());
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) 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)

Aggregations

AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)53 EntityType (com.vaticle.typedb.core.concept.type.EntityType)27 Test (org.junit.Test)24 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)21 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 Then (io.cucumber.java.en.Then)16 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)13 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)11 ThingType (com.vaticle.typedb.core.concept.type.ThingType)10 LogicManager (com.vaticle.typedb.core.logic.LogicManager)9 When (io.cucumber.java.en.When)7 TypeDB (com.vaticle.typedb.core.TypeDB)4 Options (com.vaticle.typedb.core.common.parameters.Options)4 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)4 RoleType (com.vaticle.typedb.core.concept.type.RoleType)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)3 Variable (com.vaticle.typedb.core.pattern.variable.Variable)3 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)3