use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class RelationshipTypeTest method whenMutatingRolesOfRelationType_EnsureRelationTypeRolesAreAlwaysUpdated.
@Test
public void whenMutatingRolesOfRelationType_EnsureRelationTypeRolesAreAlwaysUpdated() {
RelationshipType relationshipType = tx.putRelationshipType("c1");
Role role1 = tx.putRole("c2");
Role role2 = tx.putRole("c3");
assertThat(relationshipType.relates().collect(toSet()), empty());
relationshipType.relates(role1).relates(role2);
assertThat(relationshipType.relates().collect(toSet()), containsInAnyOrder(role1, role2));
relationshipType.deleteRelates(role1);
assertThat(relationshipType.relates().collect(toSet()), containsInAnyOrder(role2));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class RelationshipTypeTest method whenGettingTheRolesOfRelationTypes_AllTheRolesAreReturned.
@Test
public void whenGettingTheRolesOfRelationTypes_AllTheRolesAreReturned() throws Exception {
RelationshipType relationshipType = tx.putRelationshipType("relationTypes");
Role role1 = tx.putRole("role1");
Role role2 = tx.putRole("role2");
Role role3 = tx.putRole("role3");
relationshipType.relates(role1).relates(role2).relates(role3);
assertThat(relationshipType.relates().collect(toSet()), containsInAnyOrder(role1, role2, role3));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class ShortestPathTest method testMultiplePathsSharing1Instance.
@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
ConceptId startId;
ConceptId endId;
Set<List<ConceptId>> correctPaths = new HashSet<>();
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType = graph.putEntityType(thing);
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType.plays(role1).plays(role2);
RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
entityType.plays(role3).plays(role4);
RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
Entity start = entityType.addEntity();
Entity end = entityType.addEntity();
Entity middle = entityType.addEntity();
startId = start.getId();
endId = end.getId();
ConceptId middleId = middle.getId();
ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
assertEquals(correctPaths.size(), allPaths.size());
Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
assertEquals(correctPaths, computedPaths);
}
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class ValidateGlobalRules method validateRelationTypesToRolesSchema.
/**
* @param relationshipType the {@link RelationshipType} to be validated
* @return Error messages if the role type sub structure does not match the {@link RelationshipType} sub structure
*/
static Set<String> validateRelationTypesToRolesSchema(RelationshipType relationshipType) {
RelationshipTypeImpl superRelationType = (RelationshipTypeImpl) relationshipType.sup();
if (Schema.MetaSchema.isMetaLabel(superRelationType.getLabel()) || superRelationType.isAbstract()) {
// If super type is a meta type no validation needed
return Collections.emptySet();
}
Set<String> errorMessages = new HashSet<>();
Collection<Role> superRelates = superRelationType.relates().collect(Collectors.toSet());
Collection<Role> relates = relationshipType.relates().collect(Collectors.toSet());
Set<Label> relatesLabels = relates.stream().map(SchemaConcept::getLabel).collect(Collectors.toSet());
// Check 1) Every role of relationTypes is the sub of a role which is in the relates of it's supers
if (!superRelationType.isAbstract()) {
Set<Label> allSuperRolesPlayed = new HashSet<>();
superRelationType.sups().forEach(rel -> rel.relates().forEach(roleType -> allSuperRolesPlayed.add(roleType.getLabel())));
for (Role relate : relates) {
boolean validRoleTypeFound = SchemaConceptImpl.from(relate).sups().anyMatch(superRole -> allSuperRolesPlayed.contains(superRole.getLabel()));
if (!validRoleTypeFound) {
errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(relate.getLabel(), relationshipType.getLabel(), "super", "super", superRelationType.getLabel()));
}
}
}
// Check 2) Every role of superRelationType has a sub role which is in the relates of relationTypes
for (Role superRelate : superRelates) {
boolean subRoleNotFoundInRelates = superRelate.subs().noneMatch(sub -> relatesLabels.contains(sub.getLabel()));
if (subRoleNotFoundInRelates) {
errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.getLabel(), superRelationType.getLabel(), "sub", "sub", relationshipType.getLabel()));
}
}
return errorMessages;
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class TypeInferenceQueryTest method typedAnswers.
private List<Answer> typedAnswers(List<RelationshipType> possibleTypes, String pattern, EmbeddedGraknTx<?> graph) {
List<Answer> answers = new ArrayList<>();
ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
for (Type type : possibleTypes) {
GetQuery typedQuery = graph.graql().match(ReasonerQueries.atomic(query.getAtom().addType(type)).getPattern()).get();
typedQuery.stream().filter(ans -> !answers.contains(ans)).forEach(answers::add);
}
return answers;
}
Aggregations