use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.
the class Unifier method cartesianUnrestrictedNamedTypes.
private static FunctionalIterator<ConceptMap> cartesianUnrestrictedNamedTypes(Map<Retrievable, Concept> initialConcepts, Requirements.Instance instanceRequirements) {
Map<Retrievable, Concept> fixedConcepts = new HashMap<>();
List<Variable.Name> namedTypeNames = new ArrayList<>();
List<FunctionalIterator<Type>> namedTypeSupers = new ArrayList<>();
initialConcepts.forEach((id, concept) -> {
if (id.isName() && concept.isType() && !instanceRequirements.hasRestriction(id)) {
namedTypeNames.add(id.asName());
namedTypeSupers.add(concept.asType().getSupertypes().map(t -> t));
} else
fixedConcepts.put(id, concept);
});
if (namedTypeNames.isEmpty()) {
return Iterators.single(new ConceptMap(fixedConcepts));
} else {
return Iterators.cartesian(namedTypeSupers).map(permutation -> {
Map<Retrievable, Concept> concepts = new HashMap<>(fixedConcepts);
for (int i = 0; i < permutation.size(); i++) {
concepts.put(namedTypeNames.get(i), permutation.get(i));
}
return new ConceptMap(concepts);
});
}
}
use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.
the class RelationTypeImpl method getRelates.
/**
* Get the role type with a given {@code roleLabel} related by this relation type.
*
* First, look up the role type by the given label and it's scope: the relation label.
* If the role type vertex do not exist, then call {@code role()} to get the inherited role types,
* and see if the any of them has the {@code roleLabel} of interest.
*
* @param roleLabel the label of the role
* @return the role type related in this relation
*/
@Override
public RoleTypeImpl getRelates(String roleLabel) {
Optional<RoleTypeImpl> roleType;
TypeVertex roleTypeVertex = graphMgr.schema().getType(roleLabel, vertex.label());
if (roleTypeVertex != null) {
return RoleTypeImpl.of(graphMgr, roleTypeVertex);
} else if ((roleType = getRelates().filter(role -> role.getLabel().name().equals(roleLabel)).first()).isPresent()) {
return roleType.get();
} else
return null;
}
use of com.vaticle.typedb.core.concept.type.Type 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.concept.type.Type 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.concept.type.Type 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);
}
Aggregations