Search in sources :

Example 1 with EdgeCardinalityContext

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;
}
Also used : Violation(org.kie.workbench.common.stunner.core.validation.Violation) EdgeCardinalityMinRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.EdgeCardinalityMinRuleViolation) EdgeCardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.EdgeCardinalityMaxRuleViolation) EdgeCardinalityMinRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.EdgeCardinalityMinRuleViolation) EdgeCardinalityContext(org.kie.workbench.common.stunner.core.rule.context.EdgeCardinalityContext) CardinalityContext(org.kie.workbench.common.stunner.core.rule.context.CardinalityContext) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) EdgeCardinalityContext(org.kie.workbench.common.stunner.core.rule.context.EdgeCardinalityContext) EdgeCardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.EdgeCardinalityMaxRuleViolation)

Aggregations

CardinalityContext (org.kie.workbench.common.stunner.core.rule.context.CardinalityContext)1 EdgeCardinalityContext (org.kie.workbench.common.stunner.core.rule.context.EdgeCardinalityContext)1 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)1 EdgeCardinalityMaxRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.EdgeCardinalityMaxRuleViolation)1 EdgeCardinalityMinRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.EdgeCardinalityMinRuleViolation)1 Violation (org.kie.workbench.common.stunner.core.validation.Violation)1