use of org.kie.workbench.common.stunner.core.rule.context.ElementCardinalityContext in project kie-wb-common by kiegroup.
the class ElementCardinalityEvaluationHandler method evaluate.
@Override
@SuppressWarnings("unchecked")
public RuleViolations evaluate(final Occurrences rule, final ElementCardinalityContext context) {
final GraphEvaluationState state = context.getState();
final String role = rule.getRole();
final Set<String> roles = Collections.singleton(role);
final Map<String, Integer> graphLabelCount = countLabels(state, roles);
final Collection<Element<? extends View<?>>> candidates = context.getCandidates();
final Collection<Element<? extends View<?>>> filteredCandidates = candidates.stream().filter(candidate -> accepts(rule, context, candidate)).collect(Collectors.toSet());
final int size = filteredCandidates.size();
final int count = graphLabelCount.isEmpty() ? 0 : graphLabelCount.get(role);
final Optional<CardinalityContext.Operation> operation = context.getOperation();
final DefaultRuleViolations results = new DefaultRuleViolations();
// Ensure processing the role even if not used along the graph, so
// cardinality min rules can be evaluated.
final Function<String, RuleViolations> evaluator = uuid -> evaluate(rule, uuid, roles, count, size, operation);
if (size == 0) {
results.addViolations(evaluator.apply(null));
} else {
filteredCandidates.forEach(candidate -> results.addViolations(evaluator.apply(candidate.getUUID())));
}
return results;
}
use of org.kie.workbench.common.stunner.core.rule.context.ElementCardinalityContext in project kie-wb-common by kiegroup.
the class StatefulGraphEvaluationContextsTests method testCardinalityState.
@Test
@SuppressWarnings("unchecked")
public void testCardinalityState() {
Node someNewNode = new NodeImpl("someNewNodeUUID");
// Test cardinality - ADD
ElementCardinalityContext addNewNodeContext = contextBuilder.cardinality(singletonList(someNewNode), CardinalityContext.Operation.ADD);
Object result = StatefulGraphEvaluationContexts.evaluate(addNewNodeContext, EVALUATOR);
assertEquals(RESULT, result);
Iterable<Node> stateNodes = state.getCardinalityState().nodes();
long nodesCount = count(stateNodes);
assertTrue(state.getCardinalityState().getAddedElements().contains(someNewNode));
assertEquals(7, nodesCount);
// Test cardinality - DELETE
ElementCardinalityContext removeNodeAContext = contextBuilder.cardinality(singletonList(graphInstance.nodeA), CardinalityContext.Operation.DELETE);
result = StatefulGraphEvaluationContexts.evaluate(removeNodeAContext, EVALUATOR);
assertEquals(RESULT, result);
stateNodes = state.getCardinalityState().nodes();
nodesCount = count(stateNodes);
assertTrue(state.getCardinalityState().getDeletedElements().contains(graphInstance.nodeA));
assertEquals(6, nodesCount);
}
Aggregations