use of ai.grakn.kb.internal.concept.RelationshipTypeImpl 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;
}
Aggregations