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;
}
Aggregations