use of com.vaticle.typedb.core.concept.type.Type 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.concept.type.Type in project grakn by graknlabs.
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());
}
use of com.vaticle.typedb.core.concept.type.Type 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.concept.type.Type in project grakn by graknlabs.
the class TypeService method execute.
public void execute(Transaction.Req req) {
ConceptProto.Type.Req typeReq = req.getTypeReq();
String label = typeReq.getLabel();
String scope = typeReq.getScope();
Type type = scope != null && !scope.isEmpty() ? notNull(conceptMgr.getRelationType(scope)).getRelates(label) : notNull(conceptMgr.getThingType(label));
UUID reqID = byteStringAsUUID(req.getReqId());
switch(typeReq.getReqCase()) {
case TYPE_DELETE_REQ:
delete(type, reqID);
return;
case TYPE_SET_LABEL_REQ:
setLabel(type, typeReq.getTypeSetLabelReq().getLabel(), reqID);
return;
case TYPE_IS_ABSTRACT_REQ:
isAbstract(type, reqID);
return;
case TYPE_GET_SUPERTYPE_REQ:
getSupertype(type, reqID);
return;
case TYPE_SET_SUPERTYPE_REQ:
setSupertype(type, typeReq.getTypeSetSupertypeReq().getType(), reqID);
return;
case TYPE_GET_SUPERTYPES_REQ:
getSupertypes(type, reqID);
return;
case TYPE_GET_SUBTYPES_REQ:
getSubtypes(type, reqID);
return;
case ROLE_TYPE_GET_RELATION_TYPES_REQ:
getRelationTypes(type.asRoleType(), reqID);
return;
case ROLE_TYPE_GET_PLAYERS_REQ:
getPlayers(type.asRoleType(), reqID);
return;
case THING_TYPE_SET_ABSTRACT_REQ:
setAbstract(type.asThingType(), reqID);
return;
case THING_TYPE_UNSET_ABSTRACT_REQ:
unsetAbstract(type.asThingType(), reqID);
return;
case THING_TYPE_SET_OWNS_REQ:
setOwns(type.asThingType(), typeReq.getThingTypeSetOwnsReq(), reqID);
return;
case THING_TYPE_SET_PLAYS_REQ:
setPlays(type.asThingType(), typeReq.getThingTypeSetPlaysReq(), reqID);
return;
case THING_TYPE_UNSET_OWNS_REQ:
unsetOwns(type.asThingType(), typeReq.getThingTypeUnsetOwnsReq().getAttributeType(), reqID);
return;
case THING_TYPE_UNSET_PLAYS_REQ:
unsetPlays(type.asThingType(), typeReq.getThingTypeUnsetPlaysReq().getRole(), reqID);
return;
case THING_TYPE_GET_INSTANCES_REQ:
getInstances(type.asThingType(), reqID);
return;
case THING_TYPE_GET_OWNS_REQ:
getOwns(type.asThingType(), typeReq, reqID);
return;
case THING_TYPE_GET_PLAYS_REQ:
getPlays(type.asThingType(), reqID);
return;
case ENTITY_TYPE_CREATE_REQ:
create(type.asEntityType(), reqID);
return;
case RELATION_TYPE_CREATE_REQ:
create(type.asRelationType(), reqID);
return;
case RELATION_TYPE_GET_RELATES_FOR_ROLE_LABEL_REQ:
String roleLabel = typeReq.getRelationTypeGetRelatesForRoleLabelReq().getLabel();
getRelatesForRoleLabel(type.asRelationType(), roleLabel, reqID);
return;
case RELATION_TYPE_SET_RELATES_REQ:
setRelates(type.asRelationType(), typeReq.getRelationTypeSetRelatesReq(), reqID);
return;
case RELATION_TYPE_UNSET_RELATES_REQ:
unsetRelates(type.asRelationType(), typeReq.getRelationTypeUnsetRelatesReq(), reqID);
return;
case RELATION_TYPE_GET_RELATES_REQ:
getRelates(type.asRelationType(), reqID);
return;
case ATTRIBUTE_TYPE_PUT_REQ:
put(type.asAttributeType(), typeReq.getAttributeTypePutReq().getValue(), reqID);
return;
case ATTRIBUTE_TYPE_GET_REQ:
get(type.asAttributeType(), typeReq.getAttributeTypeGetReq().getValue(), reqID);
return;
case ATTRIBUTE_TYPE_GET_REGEX_REQ:
getRegex(type.asAttributeType(), reqID);
return;
case ATTRIBUTE_TYPE_SET_REGEX_REQ:
setRegex(type.asAttributeType(), typeReq.getAttributeTypeSetRegexReq().getRegex(), reqID);
return;
case ATTRIBUTE_TYPE_GET_OWNERS_REQ:
getOwners(type.asAttributeType(), typeReq.getAttributeTypeGetOwnersReq().getOnlyKey(), reqID);
return;
case REQ_NOT_SET:
default:
throw TypeDBException.of(UNKNOWN_REQUEST_TYPE);
}
}
use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.
the class TypeService method setSupertype.
private void setSupertype(Type type, ConceptProto.Type supertype, UUID reqID) {
Type sup = getThingType(supertype);
if (type.isEntityType()) {
type.asEntityType().setSupertype(sup.asEntityType());
} else if (type.isRelationType()) {
type.asRelationType().setSupertype(sup.asRelationType());
} else if (type.isAttributeType()) {
type.asAttributeType().setSupertype(sup.asAttributeType());
} else {
throw TypeDBException.of(ILLEGAL_SUPERTYPE_ENCODING, className(type.getClass()));
}
transactionSvc.respond(setSupertypeRes(reqID));
}
Aggregations