Search in sources :

Example 1 with ElementCardinalityContext

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;
}
Also used : RuleEvaluationContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleEvaluationContextBuilder) HashMap(java.util.HashMap) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Function(java.util.function.Function) View(org.kie.workbench.common.stunner.core.graph.content.view.View) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState) GraphUtils(org.kie.workbench.common.stunner.core.graph.util.GraphUtils) Inject(javax.inject.Inject) ElementCardinalityContext(org.kie.workbench.common.stunner.core.rule.context.ElementCardinalityContext) Map(java.util.Map) Element(org.kie.workbench.common.stunner.core.graph.Element) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) StreamSupport(java.util.stream.StreamSupport) CardinalityContext(org.kie.workbench.common.stunner.core.rule.context.CardinalityContext) RuleEvaluationHandler(org.kie.workbench.common.stunner.core.rule.RuleEvaluationHandler) Collection(java.util.Collection) Set(java.util.Set) Occurrences(org.kie.workbench.common.stunner.core.rule.impl.Occurrences) Collectors(java.util.stream.Collectors) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collections(java.util.Collections) Node(org.kie.workbench.common.stunner.core.graph.Node) GraphEvaluationHandlerUtils.addViolationsSourceUUID(org.kie.workbench.common.stunner.core.rule.handler.impl.GraphEvaluationHandlerUtils.addViolationsSourceUUID) Element(org.kie.workbench.common.stunner.core.graph.Element) View(org.kie.workbench.common.stunner.core.graph.content.view.View) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState)

Example 2 with ElementCardinalityContext

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);
}
Also used : NodeImpl(org.kie.workbench.common.stunner.core.graph.impl.NodeImpl) ElementCardinalityContext(org.kie.workbench.common.stunner.core.rule.context.ElementCardinalityContext) Node(org.kie.workbench.common.stunner.core.graph.Node) Test(org.junit.Test)

Aggregations

Node (org.kie.workbench.common.stunner.core.graph.Node)2 ElementCardinalityContext (org.kie.workbench.common.stunner.core.rule.context.ElementCardinalityContext)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 Test (org.junit.Test)1 DefinitionManager (org.kie.workbench.common.stunner.core.api.DefinitionManager)1 Element (org.kie.workbench.common.stunner.core.graph.Element)1 View (org.kie.workbench.common.stunner.core.graph.content.view.View)1 NodeImpl (org.kie.workbench.common.stunner.core.graph.impl.NodeImpl)1 GraphUtils (org.kie.workbench.common.stunner.core.graph.util.GraphUtils)1 RuleEvaluationHandler (org.kie.workbench.common.stunner.core.rule.RuleEvaluationHandler)1