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