Search in sources :

Example 1 with RelationshipTypeImpl

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;
}
Also used : Role(ai.grakn.concept.Role) RuleImpl(ai.grakn.kb.internal.concept.RuleImpl) Iterables(com.google.common.collect.Iterables) VALIDATION_CASTING(ai.grakn.util.ErrorMessage.VALIDATION_CASTING) VALIDATION_REQUIRED_RELATION(ai.grakn.util.ErrorMessage.VALIDATION_REQUIRED_RELATION) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Type(ai.grakn.concept.Type) Rule(ai.grakn.concept.Rule) Attribute(ai.grakn.concept.Attribute) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Relationship(ai.grakn.concept.Relationship) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) VALIDATION_RELATION_TYPES_ROLES_SCHEMA(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPES_ROLES_SCHEMA) SchemaConceptImpl(ai.grakn.kb.internal.concept.SchemaConceptImpl) Conjunction(ai.grakn.graql.admin.Conjunction) VALIDATION_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPE) Collection(java.util.Collection) Set(java.util.Set) VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE) Collectors(java.util.stream.Collectors) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) VALIDATION_RELATION_CASTING_LOOP_FAIL(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL) Casting(ai.grakn.kb.internal.structure.Casting) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Collections(java.util.Collections) VALIDATION_NOT_EXACTLY_ONE_KEY(ai.grakn.util.ErrorMessage.VALIDATION_NOT_EXACTLY_ONE_KEY) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Label(ai.grakn.concept.Label) HashSet(java.util.HashSet)

Aggregations

GraknTx (ai.grakn.GraknTx)1 Attribute (ai.grakn.concept.Attribute)1 Label (ai.grakn.concept.Label)1 Relationship (ai.grakn.concept.Relationship)1 RelationshipType (ai.grakn.concept.RelationshipType)1 Role (ai.grakn.concept.Role)1 Rule (ai.grakn.concept.Rule)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Thing (ai.grakn.concept.Thing)1 Type (ai.grakn.concept.Type)1 GraknTxOperationException (ai.grakn.exception.GraknTxOperationException)1 Pattern (ai.grakn.graql.Pattern)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1 RelationshipTypeImpl (ai.grakn.kb.internal.concept.RelationshipTypeImpl)1 RuleImpl (ai.grakn.kb.internal.concept.RuleImpl)1 SchemaConceptImpl (ai.grakn.kb.internal.concept.SchemaConceptImpl)1 TypeImpl (ai.grakn.kb.internal.concept.TypeImpl)1