Search in sources :

Example 41 with RuleViolations

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

the class CanvasLayoutUtils method canContain.

private boolean canContain(final CanvasHandler canvasHandler, final Node container, final Node candidate) {
    boolean canContain = true;
    NodeContainmentContext containmentContext = RuleContextBuilder.GraphContexts.containment(canvasHandler.getDiagram().getGraph(), container, candidate);
    String definitionSetId = canvasHandler.getDiagram().getMetadata().getDefinitionSetId();
    Object definitionSet = definitionManager.definitionSets().getDefinitionSetById(definitionSetId);
    RuleSet ruleSet = definitionManager.adapters().forRules().getRuleSet(definitionSet);
    RuleViolations violations = ruleManager.evaluate(ruleSet, containmentContext);
    if (violations.violations(Violation.Type.ERROR).iterator().hasNext()) {
        canContain = false;
    }
    return canContain;
}
Also used : RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) NodeContainmentContext(org.kie.workbench.common.stunner.core.rule.context.NodeContainmentContext) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations)

Example 42 with RuleViolations

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

the class CommonLookups method getAllowedTargetDefinitions.

/**
 * Returns the allowed definition identifiers that can be used as target node for the given source node and
 * the given edge (connector) identifier.
 * <p>
 * TODO: Handle several result pages.
 */
@SuppressWarnings("unchecked")
public <T> Set<Object> getAllowedTargetDefinitions(final String defSetId, final Graph<?, ? extends Node> graph, final Node<? extends Definition<T>, ? extends Edge> sourceNode, final String edgeId, final int page, final int pageSize) {
    final Set<Object> result = new LinkedHashSet<>();
    if (null != defSetId && null != graph && null != sourceNode && null != edgeId) {
        final T definition = sourceNode.getContent().getDefinition();
        final RuleSet ruleSet = getRuleSet(defSetId);
        log(Level.FINEST, "*** Checking the target definitions allowed " + "for [" + definition + "] and using the " + "connector [" + edgeId + "] ***");
        // Check outgoing connectors cardinality for the source node ( plus the new one to be added ).
        final int outConnectorsCount = countOutgoingEdges(sourceNode, edgeId);
        log(Level.FINEST, "The source node has  " + outConnectorsCount + "] outgoing connections.");
        final RuleViolations oev = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(sourceNode.getLabels(), edgeId, outConnectorsCount, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD)));
        final boolean oeCardinalityAllowed = pass(oev);
        log(Level.FINEST, "Outgoing edge cardinality rules evaluation " + "result = [" + oeCardinalityAllowed + "]");
        if (oeCardinalityAllowed) {
            // Obtain allowed target roles that pass connection rules.
            final Set<String> allowedConnectionRoles = getConnectionRulesAllowedTargets(defSetId, definition, edgeId, page, pageSize);
            log(Level.FINEST, "Allowed target roles that pass connection rules " + "ARE [" + allowedConnectionRoles + "]");
            if (null != allowedConnectionRoles) {
                // Obtain a first set of candidate Defintiion identifiers.
                final Set<String> allowedDefinitions = getDefinitions(defSetId, allowedConnectionRoles);
                log(Level.FINEST, "Allowed target definitions that pass connection rules " + "ARE [" + allowedConnectionRoles + "]");
                if (null != allowedDefinitions) {
                    final Map<String, Integer> graphLabelCount = GraphUtils.getLabelsCount(graph, allowedConnectionRoles);
                    final int inConnectorsCount = countIncomingEdges(sourceNode, edgeId);
                    allowedDefinitions.stream().forEach(defId -> {
                        final Object targetDefinition = createDefinition(defId);
                        if (null != targetDefinition) {
                            final Set<String> targetDefinitionRoles = getDefinitionManager().adapters().forDefinition().getLabels(targetDefinition);
                            // Check cardinality for each of the roles for this potential target node.
                            final boolean hasCardinalityViolations = targetDefinitionRoles.stream().filter(role -> {
                                final Integer roleCount = Optional.ofNullable(graphLabelCount.get(role)).orElse(0);
                                final RuleViolations violations = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.cardinality(Collections.singleton(role), roleCount, Optional.of(CardinalityContext.Operation.ADD)));
                                return !pass(violations);
                            }).findFirst().isPresent();
                            log(Level.FINEST, "Cardinality rules evaluation " + "result = [" + hasCardinalityViolations + "]");
                            if (!hasCardinalityViolations) {
                                // Check incoming connector cardinality for each the target node.
                                final RuleViolations iev = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(Collections.singleton(defId), edgeId, inConnectorsCount, EdgeCardinalityContext.Direction.INCOMING, Optional.of(CardinalityContext.Operation.ADD)));
                                final boolean ieCardinalityAllowed = pass(iev);
                                log(Level.FINEST, "Incoming edge cardinality rules evaluation " + "result = [" + ieCardinalityAllowed + "]");
                                if (ieCardinalityAllowed) {
                                    // This potential node can be used as target one, as it passes all rule checks.
                                    result.add(targetDefinition);
                                }
                            }
                        }
                    });
                    return result;
                }
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations)

Example 43 with RuleViolations

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

the class GraphConnectionEvaluationHandler method evaluate.

@Override
public RuleViolations evaluate(final CanConnect rule, final GraphConnectionContext context) {
    final Edge<? extends View<?>, ? extends Node> connector = context.getConnector();
    final Node<? extends View<?>, ? extends Edge> source = context.getSource().orElse(null);
    final Node<? extends View<?>, ? extends Edge> target = context.getTarget().orElse(null);
    if (source == null || target == null) {
        return new DefaultRuleViolations();
    }
    final Set<String> edgeLabels = evalUtils.getLabels(connector);
    final Optional<Set<String>> sourceLabels = Optional.of(evalUtils.getLabels(source));
    final Optional<Set<String>> targetLabels = Optional.of(evalUtils.getLabels(target));
    final DefaultRuleViolations result = new DefaultRuleViolations();
    edgeLabels.stream().filter(pr -> rule.getRole().equals(pr)).forEach(pr -> result.addViolations(connectionEvaluationHandler.evaluate(rule, RuleContextBuilder.DomainContexts.connection(pr, sourceLabels, targetLabels))));
    return GraphEvaluationHandlerUtils.addViolationsSourceUUID(connector.getUUID(), result);
}
Also used : RuleEvaluationHandler(org.kie.workbench.common.stunner.core.rule.RuleEvaluationHandler) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Set(java.util.Set) CanConnect(org.kie.workbench.common.stunner.core.rule.impl.CanConnect) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder) View(org.kie.workbench.common.stunner.core.graph.content.view.View) GraphConnectionContext(org.kie.workbench.common.stunner.core.rule.context.GraphConnectionContext) Inject(javax.inject.Inject) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Optional(java.util.Optional) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Node(org.kie.workbench.common.stunner.core.graph.Node) Set(java.util.Set) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)

Example 44 with RuleViolations

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

the class AddChildNodeCommandTest method testNotAllowed.

@Test
@SuppressWarnings("unchecked")
public void testNotAllowed() {
    final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations().addViolation(new ContainmentRuleViolation(graph.getUUID(), PARENT_UUID));
    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) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) ContainmentRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) ContainmentRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation) Test(org.junit.Test)

Example 45 with RuleViolations

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

the class AddNodeCommandTest method testExecuteCheckFailed.

@Test
@SuppressWarnings("unchecked")
public void testExecuteCheckFailed() {
    final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations().addViolation(new ContainmentRuleViolation(graph.getUUID(), UUID));
    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(graph, times(0)).addNode(eq(node));
    verify(graphIndex, times(0)).addNode(eq(node));
    verify(graphIndex, times(0)).addEdge(any(Edge.class));
    verify(graph, times(0)).removeNode(eq(UUID));
    verify(graphIndex, times(0)).removeNode(eq(node));
    verify(graphIndex, times(0)).removeEdge(any(Edge.class));
}
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) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) ContainmentRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) ContainmentRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Test(org.junit.Test)

Aggregations

RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)76 Test (org.junit.Test)68 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)20 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)19 RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)16 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)14 Edge (org.kie.workbench.common.stunner.core.graph.Edge)13 Graph (org.kie.workbench.common.stunner.core.graph.Graph)13 Node (org.kie.workbench.common.stunner.core.graph.Node)12 HashMap (java.util.HashMap)11 ContainmentRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation)11 HashSet (java.util.HashSet)8 List (java.util.List)7 Collection (java.util.Collection)3 AbstractGraphDefinitionTypesTest (org.kie.workbench.common.stunner.core.AbstractGraphDefinitionTypesTest)3 NodeImpl (org.kie.workbench.common.stunner.core.graph.impl.NodeImpl)3 TreeTraverseCallback (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeTraverseCallback)3 ArrayList (java.util.ArrayList)2 View (org.kie.workbench.common.stunner.core.graph.content.view.View)2 EdgeImpl (org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl)2