use of com.vaticle.typedb.core.logic.Rule in project grakn by graknlabs.
the class UnifyRelationConcludableTest method unUnify_produces_cartesian_named_types.
@Test
public void unUnify_produces_cartesian_named_types() {
String conjunction = "{$r ($role: $x) isa $rel;}";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("people-are-self-friends", "{ $x isa person; }", " (friend: $x) isa friendship ", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
// test filter allows a valid answer
Relation friendship = instanceOf("friendship").asRelation();
Thing person = instanceOf("person");
addRolePlayer(friendship, "friend", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("friendship"), friendship.getType()), pair(Variable.label("friendship:friend"), friendship.getType().getRelates("friend")));
List<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map())).toList();
assertEquals(6, unified.size());
Set<Map<String, String>> expected = set(new HashMap<String, String>() {
{
put("$rel", "friendship");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "friendship");
put("$role", "relation:role");
}
}, new HashMap<String, String>() {
{
put("$rel", "relation");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "relation");
put("$role", "relation:role");
}
}, new HashMap<String, String>() {
{
put("$rel", "thing");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "thing");
put("$role", "relation:role");
}
});
Set<Map<String, String>> actual = new HashSet<>();
iterate(unified).forEachRemaining(answer -> {
actual.add(new HashMap<String, String>() {
{
put("$rel", answer.get("rel").asType().getLabel().name());
put("$role", answer.get("role").asType().getLabel().scopedName());
}
});
});
assertEquals(expected, actual);
}
use of com.vaticle.typedb.core.logic.Rule 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);
}
use of com.vaticle.typedb.core.logic.Rule in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_more_players_than_rule_relation_fails_unify.
@Test
public void relation_more_players_than_rule_relation_fails_unify() {
String relationQuery = "{ (part-time-employee: $r, part-time-employer: $p, restriction: $q) isa part-time-employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(relationQuery, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("one-employee-one-employer", "{ $x isa person; $y isa organisation; " + "$employee type employment:employee; $employer type employment:employer; }", "($employee: $x, $employer: $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 = Collections.emptySet();
assertEquals(expected, result);
}
use of com.vaticle.typedb.core.logic.Rule in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_unifies_many_to_many_rule_relation_players.
@Test
public void relation_unifies_many_to_many_rule_relation_players() {
String conjunction = "{ (employee: $p, employer: $p, employee: $q) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("two-people-are-employed-one-is-also-the-employer", "{ $x isa person; $y isa person; }", "(employee: $x, employer: $x, employee: $y) isa employment", logicMgr);
List<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
List<Map<String, Set<String>>> result = iterate(unifier).map(u -> getStringMapping(u.mapping())).toList();
List<Map<String, Set<String>>> expected = list(new HashMap<String, Set<String>>() {
{
put("$p", set("$x"));
put("$q", set("$y"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$x", "$y"));
put("$q", set("$x"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
}
use of com.vaticle.typedb.core.logic.Rule in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_variable_role_unifies_many_to_many_rule_relation_roles.
// [multiple VARIABLE ROLE, many2many]
@Test
public void relation_variable_role_unifies_many_to_many_rule_relation_roles() {
String conjunction = "{ ($role1: $p, $role1: $q, $role2: $q) isa employment; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("two-people-are-employed-one-is-also-the-employer", "{ $x isa person; $y isa person; }", "(employee: $x, employer: $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("$q", set("$x", "$y"));
put("$role1", set("$_employment:employee"));
put("$role2", set("$_employment:employer"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$x"));
put("$q", set("$x", "$y"));
put("$role1", set("$_employment:employee", "$_employment:employer"));
put("$role2", set("$_employment:employee"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$y"));
put("$q", set("$x"));
put("$role1", set("$_employment:employee", "$_employment:employer"));
put("$role2", set("$_employment:employee"));
put("$_0", set("$_0"));
}
}, new HashMap<String, Set<String>>() {
{
put("$p", set("$y"));
put("$q", set("$x"));
put("$role1", set("$_employment:employee"));
put("$role2", set("$_employment:employer"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
}
Aggregations