use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.
the class UnifyIsaConcludableTest method isa_exact_unifies_rule_has_exact.
@Test
public void isa_exact_unifies_rule_has_exact() {
String conjunction = "{ $a isa name; }";
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);
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$a", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(1, unifier.requirements().types().size());
assertEquals(1, unifier.requirements().isaExplicit().size());
assertEquals(set(Label.of("first-name"), Label.of("last-name")), unifier.requirements().isaExplicit().values().iterator().next());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.anon(0), instanceOf("first-name", "john")), pair(Identifier.Variable.label("first-name"), conceptMgr.getThingType("first-name")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(1, unified.next().concepts().size());
// filter out invalid type
concepts = map(pair(Identifier.Variable.anon(0), instanceOf("age")), pair(Identifier.Variable.label("first-name"), conceptMgr.getThingType("age")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.
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.Concept in project typedb by vaticle.
the class UnifyIsaConcludableTest method isa_exact_unifies_rule_relation_exact.
@Test
public void isa_exact_unifies_rule_relation_exact() {
String conjunction = "{ $a isa relation; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Isa queryConcludable = concludables.iterator().next().asIsa();
Rule rule = createRule("isa-rule", "{ $x isa person; }", "(employee: $x) isa employment", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
HashMap<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$a", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(1, unifier.requirements().types().size());
assertEquals(1, unifier.requirements().isaExplicit().size());
assertEquals(set(Label.of("employment")), unifier.requirements().isaExplicit().get(Identifier.Variable.name("a")));
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.anon(0), instanceOf("employment")), pair(Identifier.Variable.label("employment"), conceptMgr.getThingType("employment")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(1, unified.next().concepts().size());
// filter out invalid type
concepts = map(pair(Identifier.Variable.anon(0), instanceOf("age")), pair(Identifier.Variable.label("employment"), conceptMgr.getThingType("age")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.
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.concept.Concept in project typedb by vaticle.
the class UnifyRelationConcludableTest method relation_duplicate_players_unifies_rule_relation_distinct_players.
@Test
public void relation_duplicate_players_unifies_rule_relation_distinct_players() {
List<Unifier> unifiers = unifiers("{ (employee: $p, employee: $p) isa employment; }", rule("($employee: $x, $employee: $y) isa $employment", "{ $x isa person; $y isa person; $employment type employment;$employee type employment:employee; }")).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", "$y"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
Unifier unifier = unifiers.get(0);
// test requirements
assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
assertEquals(2, unifier.requirements().isaExplicit().size());
assertEquals(2, unifier.requirements().types().size());
assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
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> identifiedConcepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("y"), person), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(identifiedConcepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
ConceptMap unifiedAnswer = unified.first().get();
assertEquals(2, unifiedAnswer.concepts().size());
assertEquals(person, unifiedAnswer.get("p"));
// filter out answers with differing role players that must be the same
employment = instanceOf("employment").asRelation();
person = instanceOf("person");
Thing differentPerson = instanceOf("person");
addRolePlayer(employment, "employee", person);
addRolePlayer(employment, "employee", differentPerson);
identifiedConcepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("y"), differentPerson), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
unified = unifier.unUnify(identifiedConcepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
Aggregations