use of com.vaticle.typedb.core.common.iterator.FunctionalIterator in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_duplicate_roles_unifies_rule_relation_distinct_roles.
// [reflexive parent]
// TODO check answer satisfiability
@Test
public void relation_duplicate_roles_unifies_rule_relation_distinct_roles() {
String conjunction = "{ (employee: $p, employee: $p) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("two-people-are-employed", "{ $x isa person; $y isa person; $employment type employment; $employee type employment:employee; }", "($employee: $x, $employee: $y) isa $employment", logicMgr);
FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {
{
put("$p", set("$x", "$y"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
}
use of com.vaticle.typedb.core.common.iterator.FunctionalIterator in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_distinct_roles_unifies_rule_relation_duplicate_roles.
// [reflexive child]
@Test
public void relation_distinct_roles_unifies_rule_relation_duplicate_roles() {
String conjunction = "{ (employee: $p, employee: $q) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("a-person-is-employed-twice", "{ $x isa person; $employment type employment; $employee type employment:employee; }", "($employee: $x, $employee: $x) isa $employment", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
Set<Map<String, Set<String>>> result = iterate(unifiers).map(u -> getStringMapping(u.mapping())).toSet();
Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {
{
put("$p", set("$x"));
put("$q", set("$x"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
Unifier unifier = unifiers.get(0);
// test requirements
assertEquals(2, unifier.requirements().types().size());
assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
assertEquals(3, unifier.requirements().isaExplicit().size());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Relation employment = instanceOf("employment").asRelation();
Thing person = instanceOf("person");
addRolePlayer(employment, "employee", person);
addRolePlayer(employment, "employee", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
ConceptMap unifiedAnswer = unified.first().get();
assertEquals(3, unifiedAnswer.concepts().size());
assertEquals(person, unifiedAnswer.get("p"));
assertEquals(person, unifiedAnswer.get("q"));
assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
}
use of com.vaticle.typedb.core.common.iterator.FunctionalIterator in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_duplicate_roles_unifies_rule_relation_duplicate_roles.
// [reflexive parent, child]
@Test
public void relation_duplicate_roles_unifies_rule_relation_duplicate_roles() {
String conjunction = "{ (employee: $p, employee: $p) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("a-person-is-employed-twice", "{ $x isa person; $employment type employment; $employee type employment:employee; }", "($employee: $x, $employee: $x) isa $employment", logicMgr);
FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {
{
put("$p", set("$x"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
}
use of com.vaticle.typedb.core.common.iterator.FunctionalIterator in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_variables_one_to_many_unifiers.
// TODO tests below do not test for requirements and unifier application to answers
// [Single VARIABLE ROLE in parent]
@Test
public void relation_variables_one_to_many_unifiers() {
String conjunction = "{ ($role: $p) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("three-people-are-employed", "{ $x isa person; $y isa person; $z isa person; }", "(employee: $x, employee: $y, employee: $z) isa employment", logicMgr);
FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {
{
put("$p", set("$x"));
put("$role", set("$_employment:employee"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$y"));
put("$role", set("$_employment:employee"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$z"));
put("$role", set("$_employment:employee"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
}
use of com.vaticle.typedb.core.common.iterator.FunctionalIterator in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_player_role_unifies_rule_relation_repeated_variable_role.
// [many2many]
@Test
public void relation_player_role_unifies_rule_relation_repeated_variable_role() {
String conjunction = "{ ($role: $p) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("two-people-are-employed", "{ $x isa person; $y isa person; $employment type employment; " + "$employee type employment:employee; $employer type employment:employer; }", "($employee: $x, $employee: $y) isa $employment", logicMgr);
FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {
{
put("$p", set("$x"));
put("$role", set("$employee"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$y"));
put("$role", set("$employee"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
}
Aggregations