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);
}
}
}
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());
}
}
}
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());
}
}
}
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();
}
}
}
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());
}
}
}
Aggregations