Search in sources :

Example 1 with CardinalityMaxRuleViolation

use of org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation 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)

Example 2 with CardinalityMaxRuleViolation

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

the class DeleteNodeCommandTest method testNotAllowed.

@Test
@SuppressWarnings("unchecked")
public void testNotAllowed() {
    final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations().addViolation(new CardinalityMaxRuleViolation("candidate", 1, 2, Violation.Type.ERROR));
    when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(FAILED_VIOLATIONS);
    CommandResult<RuleViolation> result = tested.allow(graphCommandExecutionContext);
    assertEquals(CommandResult.Type.ERROR, result.getType());
}
Also used : RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) CardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) CardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Test(org.junit.Test)

Example 3 with CardinalityMaxRuleViolation

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

the class DeleteNodeCommandTest method testExecuteCheckFailed.

@Test
@SuppressWarnings("unchecked")
public void testExecuteCheckFailed() {
    final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations().addViolation(new CardinalityMaxRuleViolation("candidate", 1, 2, Violation.Type.ERROR));
    when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(FAILED_VIOLATIONS);
    CommandResult<RuleViolation> result = tested.execute(graphCommandExecutionContext);
    assertEquals(CommandResult.Type.ERROR, result.getType());
    verify(graphIndex, times(0)).removeNode(any(Node.class));
    verify(graphIndex, times(0)).removeEdge(any(Edge.class));
    verify(graphIndex, times(0)).addEdge(any(Edge.class));
    verify(graphIndex, times(0)).addNode(any(Node.class));
}
Also used : RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) Node(org.kie.workbench.common.stunner.core.graph.Node) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) CardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) CardinalityMaxRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Test(org.junit.Test)

Aggregations

CardinalityMaxRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.CardinalityMaxRuleViolation)3 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)3 Test (org.junit.Test)2 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)2 RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)2 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)2 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)2 Edge (org.kie.workbench.common.stunner.core.graph.Edge)1 Node (org.kie.workbench.common.stunner.core.graph.Node)1 CardinalityContext (org.kie.workbench.common.stunner.core.rule.context.CardinalityContext)1 CardinalityMinRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.CardinalityMinRuleViolation)1 Violation (org.kie.workbench.common.stunner.core.validation.Violation)1