Search in sources :

Example 1 with CardinalityMinRuleViolation

use of org.kie.workbench.common.stunner.core.rule.violations.CardinalityMinRuleViolation in project kie-wb-common by kiegroup.

the class CardinalityEvaluationHandler method evaluate.

@Override
public RuleViolations evaluate(final Occurrences rule, final CardinalityContext context) {
    final DefaultRuleViolations results = new DefaultRuleViolations();
    final int minOccurrences = rule.getMinOccurrences();
    final int maxOccurrences = rule.getMaxOccurrences();
    final int candidatesCount = context.getCandidateCount();
    final Optional<CardinalityContext.Operation> operation = context.getOperation();
    final Violation.Type type = operation.filter(CardinalityContext.Operation.ADD::equals).isPresent() ? Violation.Type.ERROR : Violation.Type.WARNING;
    final int count = !operation.isPresent() ? candidatesCount : (operation.get().equals(CardinalityContext.Operation.ADD) ? candidatesCount + 1 : candidatesCount - 1);
    if (count < minOccurrences) {
        results.addViolation(new CardinalityMinRuleViolation(context.getRoles().toString(), minOccurrences, candidatesCount, type));
    } else if (maxOccurrences > -1 && count > maxOccurrences) {
        results.addViolation(new CardinalityMaxRuleViolation(context.getRoles().toString(), maxOccurrences, candidatesCount, type));
    }
    return results;
}
Also used : CardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation) CardinalityMinRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMinRuleViolation) Violation(org.kie.workbench.common.stunner.core.validation.Violation) CardinalityContext(org.kie.workbench.common.stunner.core.rule.context.CardinalityContext) CardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) CardinalityMinRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMinRuleViolation)

Aggregations

CardinalityContext (org.kie.workbench.common.stunner.core.rule.context.CardinalityContext)1 CardinalityMaxRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation)1 CardinalityMinRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.CardinalityMinRuleViolation)1 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)1 Violation (org.kie.workbench.common.stunner.core.validation.Violation)1