Search in sources :

Example 1 with TypeImpl

use of ai.grakn.kb.internal.concept.TypeImpl in project grakn by graknlabs.

the class ValidateGlobalRules method validateInstancePlaysAllRequiredRoles.

/**
 * @param thing The thing to be validated
 * @return An error message if the thing does not have all the required resources
 */
static Optional<String> validateInstancePlaysAllRequiredRoles(Thing thing) {
    TypeImpl<?, ?> currentConcept = (TypeImpl) thing.type();
    while (currentConcept != null) {
        Map<Role, Boolean> plays = currentConcept.directPlays();
        for (Map.Entry<Role, Boolean> playsEntry : plays.entrySet()) {
            if (playsEntry.getValue()) {
                Role role = playsEntry.getKey();
                // Assert there is a relationship for this type
                Stream<Relationship> relationships = thing.relationships(role);
                if (!CommonUtil.containsOnly(relationships, 1)) {
                    Label resourceTypeLabel = Schema.ImplicitType.explicitLabel(role.getLabel());
                    return Optional.of(VALIDATION_NOT_EXACTLY_ONE_KEY.getMessage(thing.getId(), resourceTypeLabel));
                }
            }
        }
        currentConcept = (TypeImpl) currentConcept.sup();
    }
    return Optional.empty();
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) Map(java.util.Map) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl)

Example 2 with TypeImpl

use of ai.grakn.kb.internal.concept.TypeImpl in project grakn by graknlabs.

the class ValidateGlobalRules method roleNotAllowedToBePlayed.

/**
 * Checks  if the plays edge has been added between the roleplayer's {@link Type} and
 * the {@link Role} being played.
 *
 * Also checks that required {@link Role} are satisfied
 *
 * @param role The {@link Role} which the role-player is playing
 * @param thing the role-player
 * @return an error if one is found
 */
private static Optional<String> roleNotAllowedToBePlayed(Role role, Thing thing) {
    TypeImpl<?, ?> currentConcept = (TypeImpl<?, ?>) thing.type();
    boolean satisfiesPlays = false;
    while (currentConcept != null) {
        Map<Role, Boolean> plays = currentConcept.directPlays();
        for (Map.Entry<Role, Boolean> playsEntry : plays.entrySet()) {
            Role rolePlayed = playsEntry.getKey();
            Boolean required = playsEntry.getValue();
            if (rolePlayed.getLabel().equals(role.getLabel())) {
                satisfiesPlays = true;
                // Assert unique relationship for this role type
                if (required && !CommonUtil.containsOnly(thing.relationships(role), 1)) {
                    return Optional.of(VALIDATION_REQUIRED_RELATION.getMessage(thing.getId(), thing.type().getLabel(), role.getLabel(), thing.relationships(role).count()));
                }
            }
        }
        currentConcept = (TypeImpl) currentConcept.sup();
    }
    if (satisfiesPlays) {
        return Optional.empty();
    } else {
        return Optional.of(VALIDATION_CASTING.getMessage(thing.type().getLabel(), thing.getId(), role.getLabel()));
    }
}
Also used : Role(ai.grakn.concept.Role) Map(java.util.Map) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl)

Aggregations

Role (ai.grakn.concept.Role)2 RelationshipTypeImpl (ai.grakn.kb.internal.concept.RelationshipTypeImpl)2 TypeImpl (ai.grakn.kb.internal.concept.TypeImpl)2 Map (java.util.Map)2 Label (ai.grakn.concept.Label)1 Relationship (ai.grakn.concept.Relationship)1