Search in sources :

Example 1 with Node

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

the class AcyclicDirectedGraphRule method evaluate.

@Override
@SuppressWarnings("unchecked")
public RuleViolations evaluate(final RuleExtension rule, final GraphConnectionContext context) {
    final Graph<?, Node> graph = (Graph<?, Node>) context.getGraph();
    final Optional<Node<? extends View<?>, ? extends Edge>> oSource = context.getSource();
    final Optional<Node<? extends View<?>, ? extends Edge>> oTarget = context.getTarget();
    final Edge<? extends View<?>, ? extends Node> oConnector = context.getConnector();
    final DefaultRuleViolations result = new DefaultRuleViolations();
    // Only validate DAG when source and target nodes are set
    if (!(oSource.isPresent() && oTarget.isPresent())) {
        return result;
    }
    final Node<?, Edge> source = (Node<?, Edge>) oSource.get();
    final Node<?, Edge> target = (Node<?, Edge>) oTarget.get();
    final Edge<?, Node> connector = (Edge<?, Node>) oConnector;
    try {
        final TreeWalkTraverseProcessor walker = getTreeWalker(source, target, connector);
        walker.traverse(graph, new TreeTraverseCallback<Graph, Node, Edge>() {

            final Set<Node> inProgress = new HashSet<>();

            @Override
            public void startGraphTraversal(final Graph graph) {
            }

            @Override
            public boolean startNodeTraversal(final Node node) {
                if (inProgress.contains(node)) {
                    throw new DirectedAcrylicGraphViolationException();
                }
                inProgress.add(node);
                return true;
            }

            @Override
            public boolean startEdgeTraversal(final Edge edge) {
                return true;
            }

            @Override
            public void endNodeTraversal(final Node node) {
                inProgress.remove(node);
            }

            @Override
            public void endEdgeTraversal(final Edge edge) {
            }

            @Override
            public void endGraphTraversal() {
            }
        });
    } catch (DirectedAcrylicGraphViolationException e) {
        result.addViolation(new RuleViolationImpl(ERROR_MESSAGE));
    }
    return result;
}
Also used : RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) Node(org.kie.workbench.common.stunner.core.graph.Node) View(org.kie.workbench.common.stunner.core.graph.content.view.View) TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) Graph(org.kie.workbench.common.stunner.core.graph.Graph) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Edge(org.kie.workbench.common.stunner.core.graph.Edge) HashSet(java.util.HashSet)

Example 2 with Node

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

the class AcyclicDirectedGraphRuleTest method checkMissingConnectionTargetNodeDoesNotTriggerGraphWalk.

@Test
@SuppressWarnings("unchecked")
public void checkMissingConnectionTargetNodeDoesNotTriggerGraphWalk() {
    final Node source = mock(Node.class);
    when(context.getSource()).thenReturn(Optional.of(source));
    when(context.getTarget()).thenReturn(Optional.empty());
    when(context.getConnector()).thenReturn(connector);
    when(check.getTreeWalker(any(Node.class), any(Node.class), any(Edge.class))).thenReturn(walker);
    final RuleViolations result = check.evaluate(rule, context);
    assertNotNull(result);
    assertFalse(result.violations().iterator().hasNext());
    verify(walker, never()).traverse(any(Graph.class), any(TreeTraverseCallback.class));
}
Also used : Graph(org.kie.workbench.common.stunner.core.graph.Graph) Node(org.kie.workbench.common.stunner.core.graph.Node) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) TreeTraverseCallback(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeTraverseCallback) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Test(org.junit.Test)

Example 3 with Node

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

the class AcyclicDirectedGraphRuleTest method checkCyclicalConnection.

@Test
@SuppressWarnings("unchecked")
public void checkCyclicalConnection() {
    final Node node1 = new NodeImpl<>("node1");
    final Node node2 = new NodeImpl<>("node2");
    final Edge c1 = new EdgeImpl<>("edge1");
    node1.getOutEdges().add(c1);
    node2.getInEdges().add(c1);
    c1.setSourceNode(node1);
    c1.setTargetNode(node2);
    graph.addNode(node1);
    graph.addNode(node2);
    when(context.getSource()).thenReturn(Optional.of(node2));
    when(context.getTarget()).thenReturn(Optional.of(node1));
    when(context.getConnector()).thenReturn(connector);
    final RuleViolations result = check.evaluate(rule, context);
    assertNotNull(result);
    assertTrue(result.violations().iterator().hasNext());
    final RuleViolation violation = result.violations().iterator().next();
    assertNotNull(violation);
    assertTrue(violation.getArguments().isPresent());
    assertEquals(1, violation.getArguments().get().length);
    assertEquals(AcyclicDirectedGraphRule.ERROR_MESSAGE, violation.getArguments().get()[0]);
}
Also used : NodeImpl(org.kie.workbench.common.stunner.core.graph.impl.NodeImpl) Node(org.kie.workbench.common.stunner.core.graph.Node) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Test(org.junit.Test)

Example 4 with Node

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

the class AcyclicDirectedGraphRuleTest method checkCompleteConnectionDefinitionTriggersGraphWalk.

@Test
@SuppressWarnings("unchecked")
public void checkCompleteConnectionDefinitionTriggersGraphWalk() {
    final Node source = mock(Node.class);
    final Node target = mock(Node.class);
    when(context.getSource()).thenReturn(Optional.of(source));
    when(context.getTarget()).thenReturn(Optional.of(target));
    when(context.getConnector()).thenReturn(connector);
    when(check.getTreeWalker(any(Node.class), any(Node.class), any(Edge.class))).thenReturn(walker);
    final RuleViolations result = check.evaluate(rule, context);
    assertNotNull(result);
    assertFalse(result.violations().iterator().hasNext());
    verify(walker).traverse(eq(graph), any(TreeTraverseCallback.class));
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) TreeTraverseCallback(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeTraverseCallback) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Test(org.junit.Test)

Example 5 with Node

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

the class BaseGraphRuleTest method checkNoViolationsWhenNoExistingConnections.

@Test
@SuppressWarnings("unchecked")
public void checkNoViolationsWhenNoExistingConnections() {
    final Node node1 = new NodeImpl<>("node1");
    final Node node2 = new NodeImpl<>("node2");
    graph.addNode(node1);
    graph.addNode(node2);
    when(context.getSource()).thenReturn(Optional.of(node1));
    when(context.getTarget()).thenReturn(Optional.of(node2));
    when(context.getConnector()).thenReturn(connector);
    final RuleViolations result = check.evaluate(rule, context);
    assertNotNull(result);
    assertFalse(result.violations().iterator().hasNext());
}
Also used : NodeImpl(org.kie.workbench.common.stunner.core.graph.impl.NodeImpl) Node(org.kie.workbench.common.stunner.core.graph.Node) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Test(org.junit.Test)

Aggregations

Node (org.kie.workbench.common.stunner.core.graph.Node)153 Edge (org.kie.workbench.common.stunner.core.graph.Edge)85 View (org.kie.workbench.common.stunner.core.graph.content.view.View)59 Test (org.junit.Test)38 Graph (org.kie.workbench.common.stunner.core.graph.Graph)32 ViewConnector (org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector)24 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)21 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)17 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)15 Element (org.kie.workbench.common.stunner.core.graph.Element)14 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)13 DefinitionSet (org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)13 EdgeImpl (org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl)13 Matchers.anyString (org.mockito.Matchers.anyString)13 List (java.util.List)12 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)12 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)12 Before (org.junit.Before)11 Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)11 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)11