use of org.kie.workbench.common.stunner.core.rule.context.EdgeCardinalityContext in project kie-wb-common by kiegroup.
the class EdgeCardinalityEvaluationHandler method evaluate.
@Override
public RuleViolations evaluate(final EdgeOccurrences rule, final EdgeCardinalityContext 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 EdgeCardinalityContext.Direction direction = rule.getDirection();
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 > 0 ? candidatesCount - 1 : 0));
if (_count < minOccurrences) {
results.addViolation(new EdgeCardinalityMinRuleViolation(context.getRoles().toString(), context.getEdgeRole(), minOccurrences, candidatesCount, direction, type));
} else if (maxOccurrences > -1 && _count > maxOccurrences) {
results.addViolation(new EdgeCardinalityMaxRuleViolation(context.getRoles().toString(), context.getEdgeRole(), maxOccurrences, candidatesCount, direction, type));
}
return results;
}
Aggregations