Search in sources :

Example 11 with RuleViolation

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

the class SetConnectionSourceNodeCommand method check.

@SuppressWarnings("unchecked")
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
    final GraphCommandResultBuilder resultBuilder = new GraphCommandResultBuilder();
    final Node<View<?>, Edge> sourceNode = (Node<View<?>, Edge>) getSourceNode(context);
    final Edge<View<?>, Node> edge = (Edge<View<?>, Node>) getEdge(context);
    final Node<? extends View<?>, Edge> lastSourceNode = edge.getSourceNode();
    // Only check for rules in case the connector's source node is a different one.
    if ((null == lastSourceNode && null != sourceNode) || (null != lastSourceNode && (!lastSourceNode.equals(sourceNode)))) {
        // New connection being made
        final Collection<RuleViolation> connectionRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.connection(getGraph(context), edge, Optional.ofNullable(sourceNode), Optional.ofNullable(targetNode)));
        resultBuilder.addViolations(connectionRuleViolations);
        final Node<View<?>, Edge> currentSource = edge.getSourceNode();
        // If the edge has an outoutgoing source node, check cardinality for removing it.
        if (null != currentSource) {
            final Collection<RuleViolation> cardinalityRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.edgeCardinality(getGraph(context), currentSource, edge, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.DELETE)));
            resultBuilder.addViolations(cardinalityRuleViolations);
        }
        // If the new source node exist, evaluate cardinality rules for this edge.
        if (null != sourceNode) {
            final Collection<RuleViolation> cardinalityRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.edgeCardinality(getGraph(context), sourceNode, edge, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD)));
            resultBuilder.addViolations(cardinalityRuleViolations);
        }
    }
    return resultBuilder.build();
}
Also used : GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) Node(org.kie.workbench.common.stunner.core.graph.Node) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 12 with RuleViolation

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

the class SetParentNodeCommand method execute.

@Override
@SuppressWarnings("unchecked")
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
    final CommandResult<RuleViolation> results = allow(context);
    if (!results.getType().equals(CommandResult.Type.ERROR)) {
        final Node<?, Edge> parent = getParent(context);
        final Node<?, Edge> candidate = getCandidate(context);
        // TODO: Create a ParentEdgeFactory iface extending EdgeFactory using as content generics type Relationship
        final String uuid = UUID.uuid();
        final Edge<Parent, Node> edge = new EdgeImpl<>(uuid);
        edge.setContent(new Parent());
        edge.setSourceNode(parent);
        edge.setTargetNode(candidate);
        parent.getOutEdges().add(edge);
        candidate.getInEdges().add(edge);
        getMutableIndex(context).addEdge(edge);
    }
    return results;
}
Also used : Parent(org.kie.workbench.common.stunner.core.graph.content.relationship.Parent) Node(org.kie.workbench.common.stunner.core.graph.Node) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 13 with RuleViolation

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

the class GraphValidatorImpl method validate.

/**
 * Performs the validation for the <code>graph</code> instance.
 * @param graph The instance to validate.
 * @param aRuleSet An optional rule set instance to validate against it. If not present, the default
 * rule set for the the graph will be used.
 * @param graphValidatorConsumer An optional consumer for the graph instance when is being validated.
 * @param nodeValidatorConsumer An optional consumer each node instance when being validated.
 * @param edgeValidatorConsumer An optional consumer each edge instance when being validated.
 * @param resultConsumer The consumer for all the resulting validation violations produced during the
 * validator for the graph, and all of its nodes and edges. It's being called once the
 * validation has been completed.
 */
@SuppressWarnings("unchecked")
void validate(final Graph graph, final Optional<RuleSet> aRuleSet, final Optional<BiConsumer<Graph, Collection<RuleViolation>>> graphValidatorConsumer, final Optional<BiConsumer<Node, Collection<RuleViolation>>> nodeValidatorConsumer, final Optional<BiConsumer<Edge, Collection<RuleViolation>>> edgeValidatorConsumer, Consumer<Collection<RuleViolation>> resultConsumer) {
    final RuleSet ruleSet = aRuleSet.orElse(getRuleSet(graph));
    final ViolationsSet violations = new ViolationsSet();
    treeWalkTraverseProcessor.traverse(graph, new AbstractTreeTraverseCallback<org.kie.workbench.common.stunner.core.graph.Graph, Node, Edge>() {

        private final Stack<Node> currentParents = new Stack<Node>();

        @Override
        public void startGraphTraversal(final org.kie.workbench.common.stunner.core.graph.Graph graph) {
            super.startGraphTraversal(graph);
            currentParents.clear();
            // Evaluate the graph's cardinality rules.
            final Set<RuleViolation> graphCardinalityViolations = violations.addViolations(evaluateCardinality(ruleSet, graph));
            graphValidatorConsumer.ifPresent(g -> g.accept(graph, graphCardinalityViolations));
        }

        @Override
        public boolean startEdgeTraversal(final Edge edge) {
            super.startEdgeTraversal(edge);
            final Object content = edge.getContent();
            final ViolationsSet edgeViolations = new ViolationsSet();
            if (content instanceof Child) {
                this.currentParents.push(edge.getSourceNode());
            } else if (content instanceof View) {
                final Optional<Node<? extends View<?>, ? extends Edge>> sourceOpt = Optional.ofNullable(edge.getSourceNode());
                final Optional<Node<? extends View<?>, ? extends Edge>> targetOpt = Optional.ofNullable(edge.getTargetNode());
                // Check not empty connections.
                final Optional<RuleViolation> emptyConnectionViolation = evaluateNotEmptyConnections(graph, edge, sourceOpt, targetOpt);
                emptyConnectionViolation.ifPresent(edgeViolations::add);
                // Evaluate connection rules.
                edgeViolations.addViolations(evaluateConnection(ruleSet, graph, edge, sourceOpt, targetOpt));
                // Evaluate connector cardinality rules for this edge.
                if (null != edge.getTargetNode()) {
                    edgeViolations.addViolations(evaluateIncomingEdgeCardinality(ruleSet, graph, edge));
                }
                if (null != edge.getSourceNode()) {
                    edgeViolations.addViolations(evaluateOutgoingEdgeCardinality(ruleSet, graph, edge));
                }
            } else if (content instanceof Dock) {
                final Node parent = edge.getSourceNode();
                final Node docked = edge.getTargetNode();
                // Evaluate docking rules for the source & target nodes.
                edgeViolations.addViolations(evaluateDocking(ruleSet, graph, parent, docked));
            }
            edgeValidatorConsumer.ifPresent(c -> c.accept(edge, edgeViolations));
            violations.addAll(edgeViolations);
            return true;
        }

        @Override
        public void endEdgeTraversal(final Edge edge) {
            super.endEdgeTraversal(edge);
            if (edge.getContent() instanceof Child) {
                this.currentParents.pop();
            }
        }

        @Override
        public boolean startNodeTraversal(final Node node) {
            super.startNodeTraversal(node);
            final Collection<RuleViolation> nodeViolations = evaluateNode(node, currentParents.isEmpty() ? null : currentParents.peek());
            nodeValidatorConsumer.ifPresent(c -> c.accept(node, nodeViolations));
            return true;
        }

        @Override
        public void endGraphTraversal() {
            super.endGraphTraversal();
            // Finished - feed the consumer instance.
            resultConsumer.accept(violations);
        }

        private Collection<RuleViolation> evaluateNode(final Node node, final Node parent) {
            // Evaluate containment rules for this node.
            return violations.addViolations(evaluateContainment(ruleSet, graph, null != parent ? parent : graph, node));
        }
    });
}
Also used : Edge(org.kie.workbench.common.stunner.core.graph.Edge) Stack(java.util.Stack) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Inject(javax.inject.Inject) EdgeCardinalityContext(org.kie.workbench.common.stunner.core.rule.context.EdgeCardinalityContext) TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) BiConsumer(java.util.function.BiConsumer) Element(org.kie.workbench.common.stunner.core.graph.Element) AbstractTreeTraverseCallback(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.AbstractTreeTraverseCallback) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) LinkedHashSet(java.util.LinkedHashSet) Collection(java.util.Collection) Set(java.util.Set) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) Logger(java.util.logging.Logger) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) RuleContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) EmptyConnectionViolation(org.kie.workbench.common.stunner.core.rule.violations.EmptyConnectionViolation) Consumer(java.util.function.Consumer) Graph(org.kie.workbench.common.stunner.core.graph.Graph) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Optional(java.util.Optional) Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) ApplicationScoped(javax.enterprise.context.ApplicationScoped) RuleManager(org.kie.workbench.common.stunner.core.rule.RuleManager) Node(org.kie.workbench.common.stunner.core.graph.Node) GraphValidator(org.kie.workbench.common.stunner.core.validation.GraphValidator) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) Node(org.kie.workbench.common.stunner.core.graph.Node) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Stack(java.util.Stack) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) Collection(java.util.Collection) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 14 with RuleViolation

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

the class CompositeCommandTest method testExecuteFailedThenUndo.

@Test
@SuppressWarnings("unchecked")
public void testExecuteFailedThenUndo() {
    Command c1 = mock(Command.class);
    when(c1.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c1.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    Command c2 = mock(Command.class);
    when(c2.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c2.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    CommandResult<RuleViolation> failed = new CommandResultImpl<>(CommandResult.Type.ERROR, Collections.singletonList(new RuleViolationImpl("failed")));
    Command c3 = mock(Command.class);
    when(c3.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c3.execute(eq(commandExecutionContext))).thenReturn(failed);
    CompositeCommand composite = new CompositeCommand.Builder<>().addCommand(c1).addCommand(c2).addCommand(c3).build();
    CommandResult result = composite.execute(commandExecutionContext);
    assertEquals(CommandResult.Type.ERROR, result.getType());
    verify(c1, times(1)).undo(eq(commandExecutionContext));
    verify(c2, times(1)).undo(eq(commandExecutionContext));
    verify(c3, times(1)).undo(eq(commandExecutionContext));
}
Also used : RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 15 with RuleViolation

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

the class CanvasHighlightTest method testViolations.

@Test
@SuppressWarnings("unchecked")
public void testViolations() {
    final List<RuleViolation> violations = new LinkedList<>();
    final RuleViolationImpl v1 = new RuleViolationImpl("m1");
    v1.setUUID(ID1);
    final RuleViolationImpl v2 = new RuleViolationImpl("m2");
    v2.setUUID(ID2);
    violations.add(v1);
    violations.add(v2);
    tested.invalid(violations);
    verify(shape1, times(1)).applyState(eq(ShapeState.INVALID));
    verify(shape2, times(1)).applyState(eq(ShapeState.INVALID));
    verify(canvasView, times(2)).setCursor(eq(AbstractCanvas.Cursors.NOT_ALLOWED));
    verify(canvas, times(1)).draw();
}
Also used : RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)122 Test (org.junit.Test)81 GraphCommandExecutionContext (org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext)59 Node (org.kie.workbench.common.stunner.core.graph.Node)27 Edge (org.kie.workbench.common.stunner.core.graph.Edge)26 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)21 LiteralExpression (org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression)18 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)18 InformationItem (org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem)16 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)16 RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)15 ContainmentRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation)15 GraphCommandResultBuilder (org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder)13 DecisionRule (org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule)12 AbstractGraphCommand (org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand)11 List (org.kie.workbench.common.dmn.api.definition.v1_1.List)10 Collection (java.util.Collection)8 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)8 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)8 Binding (org.kie.workbench.common.dmn.api.definition.v1_1.Binding)7