use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyIsaConcludableTest method isa_predicates_can_filter_answers.
// TODO: enable when we have query rewriting to handle all predicates at once
@Ignore
@Test
public void isa_predicates_can_filter_answers() {
String conjunction = "{ $a isa first-name; $a > 'b'; $a < 'y'; $a contains 'j'; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Isa queryConcludable = concludables.iterator().next().asIsa();
Rule rule = createRule("isa-rule", "{ $x isa person; }", "$x has first-name \"john\"", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
// test requirements
assertEquals(0, unifier.requirements().types().size());
assertEquals(1, unifier.requirements().isaExplicit().size());
assertEquals(3, unifier.requirements().predicates().size());
// test filter allows a valid answer
assertEquals(set(Label.of("first-name")), unifier.requirements().isaExplicit().get(Identifier.Variable.name("a")));
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("first-name", "johnny")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(1, unified.next().concepts().size());
// filter out using >
concepts = map(pair(Identifier.Variable.name("x"), instanceOf("first-name", "abe")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
// filter out using <
concepts = map(pair(Identifier.Variable.name("x"), instanceOf("first-name", "zack")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
// filter out using contains
concepts = map(pair(Identifier.Variable.name("x"), instanceOf("first-name", "carol")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap 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);
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap 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);
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap 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);
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap 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);
}
}
}
Aggregations