Search in sources :

Example 1 with Dock

use of org.kie.workbench.common.stunner.core.graph.content.relationship.Dock 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 2 with Dock

use of org.kie.workbench.common.stunner.core.graph.content.relationship.Dock in project kie-wb-common by kiegroup.

the class DockNodeCommandTest method testExecute.

@Test
@SuppressWarnings("unchecked")
public void testExecute() {
    CommandResult<RuleViolation> result = tested.execute(graphCommandExecutionContext);
    assertEquals(CommandResult.Type.INFO, result.getType());
    assertFalse(parent.getOutEdges().isEmpty());
    assertFalse(candidate.getInEdges().isEmpty());
    Edge edge = (Edge) parent.getOutEdges().get(0);
    assertTrue(edge.getContent() instanceof Dock);
    assertEquals(parent, edge.getSourceNode());
    assertEquals(candidate, edge.getTargetNode());
    verify(graphIndex, times(1)).addEdge(eq(edge));
    verify(graphIndex, times(0)).addNode(any(Node.class));
}
Also used : Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) Node(org.kie.workbench.common.stunner.core.graph.Node) 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)

Example 3 with Dock

use of org.kie.workbench.common.stunner.core.graph.content.relationship.Dock in project kie-wb-common by kiegroup.

the class UnDockNodeCommandTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    super.init(500, 500);
    this.parent = mockNode(PARENT_UUID);
    this.candidate = mockNode(CANDIDATE_UUID);
    this.edge = new EdgeImpl<>(EDGE_UUID);
    this.edge.setContent(new Dock());
    this.edge.setSourceNode(parent);
    this.edge.setTargetNode(candidate);
    when(graphIndex.getNode(eq(PARENT_UUID))).thenReturn(parent);
    when(graphIndex.getNode(eq(CANDIDATE_UUID))).thenReturn(candidate);
    when(graphIndex.getEdge(eq(EDGE_UUID))).thenReturn(edge);
    when(parent.getOutEdges()).thenReturn(parentOutEdges);
    when(candidate.getInEdges()).thenReturn(candidateInEdges);
    parentOutEdges.add(edge);
    candidateInEdges.add(edge);
    this.tested = new UnDockNodeCommand(PARENT_UUID, CANDIDATE_UUID);
}
Also used : Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) Before(org.junit.Before)

Example 4 with Dock

use of org.kie.workbench.common.stunner.core.graph.content.relationship.Dock in project kie-wb-common by kiegroup.

the class BPMNDirectDiagramMarshallerTest method testUnmarshallBoundaryEvents.

@Test
@SuppressWarnings("unchecked")
public void testUnmarshallBoundaryEvents() throws Exception {
    Diagram<Graph, Metadata> diagram = unmarshall(BPMN_BOUNDARY_EVENTS);
    // Basic assertions.
    assertEquals("Boundary Event", diagram.getMetadata().getTitle());
    assertDiagram(diagram, 6);
    // Assert than the intermediate event is connected using a dock connector,
    // so boundary to the task.
    Node event = diagram.getGraph().getNode("_CB178D55-8DC2-4CAA-8C42-4F5028D4A1F6");
    List<Edge> inEdges = event.getInEdges();
    boolean foundDockConector = false;
    for (Edge e : inEdges) {
        if (e.getContent() instanceof Dock) {
            foundDockConector = true;
        }
    }
    assertTrue(foundDockConector);
    // Assert relative position for the docked node.
    Bounds bounds = ((View) event.getContent()).getBounds();
    Bounds.Bound ul = bounds.getUpperLeft();
    Bounds.Bound lr = bounds.getLowerRight();
    assertEquals(57, ul.getX(), 0);
    assertEquals(70, ul.getY(), 0);
    assertEquals(87, lr.getX(), 0);
    assertEquals(100, lr.getY(), 0);
}
Also used : Graph(org.kie.workbench.common.stunner.core.graph.Graph) Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) Node(org.kie.workbench.common.stunner.core.graph.Node) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Edge(org.kie.workbench.common.stunner.core.graph.Edge) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Test(org.junit.Test)

Example 5 with Dock

use of org.kie.workbench.common.stunner.core.graph.content.relationship.Dock in project kie-wb-common by kiegroup.

the class BPMNDiagramMarshallerTest method testUnmarshallBoundaryEvents.

@Test
@SuppressWarnings("unchecked")
public void testUnmarshallBoundaryEvents() throws Exception {
    Diagram<Graph, Metadata> diagram = unmarshall(BPMN_BOUNDARY_EVENTS);
    // Basic assertions.
    assertEquals("Boundary Event", diagram.getMetadata().getTitle());
    assertDiagram(diagram, 6);
    // Assert than the intermediate event is connected using a dock connector,
    // so boundary to the task.
    Node event = diagram.getGraph().getNode("_CB178D55-8DC2-4CAA-8C42-4F5028D4A1F6");
    List<Edge> inEdges = event.getInEdges();
    boolean foundDockConector = false;
    for (Edge e : inEdges) {
        if (e.getContent() instanceof Dock) {
            foundDockConector = true;
        }
    }
    assertTrue(foundDockConector);
    // Assert relative position for the docked node.
    Bounds bounds = ((View) event.getContent()).getBounds();
    Bounds.Bound ul = bounds.getUpperLeft();
    Bounds.Bound lr = bounds.getLowerRight();
    assertEquals(57, ul.getX(), 0);
    assertEquals(70, ul.getY(), 0);
    assertEquals(87, lr.getX(), 0);
    assertEquals(100, lr.getY(), 0);
}
Also used : Graph(org.kie.workbench.common.stunner.core.graph.Graph) Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) Node(org.kie.workbench.common.stunner.core.graph.Node) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Edge(org.kie.workbench.common.stunner.core.graph.Edge) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Test(org.junit.Test)

Aggregations

Dock (org.kie.workbench.common.stunner.core.graph.content.relationship.Dock)9 Edge (org.kie.workbench.common.stunner.core.graph.Edge)8 Node (org.kie.workbench.common.stunner.core.graph.Node)8 Graph (org.kie.workbench.common.stunner.core.graph.Graph)5 View (org.kie.workbench.common.stunner.core.graph.content.view.View)5 Test (org.junit.Test)4 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)3 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)3 Optional (java.util.Optional)2 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)2 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)2 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)2 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)2 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1 Stack (java.util.Stack)1