Search in sources :

Example 41 with Edge

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

the class ConnectorParentsMatchConnectionHandler method evaluateConnection.

private RuleViolations evaluateConnection(final RuleExtension rule, final GraphConnectionContext context) {
    LOGGER.log(Level.INFO, "Evaluating rule handler [" + getClass().getName() + "]...");
    final Optional<Node<? extends View<?>, ? extends Edge>> sourceNode = context.getSource();
    final Optional<Node<? extends View<?>, ? extends Edge>> targetNode = context.getTarget();
    final Class<?>[] typeArguments = rule.getTypeArguments();
    final Class<?> parentType = null != typeArguments ? typeArguments[0] : null;
    final DefaultRuleViolations result = new DefaultRuleViolations();
    boolean isValid = true;
    if (sourceNode.isPresent() && targetNode.isPresent()) {
        isValid = new ParentsTypeMatcher(definitionManager).forParentType(parentType).test(sourceNode.get(), targetNode.get());
    }
    if (!isValid) {
        addViolation(context.getConnector().getUUID(), rule, result);
    }
    return result;
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge) ParentsTypeMatcher(org.kie.workbench.common.stunner.core.graph.util.ParentsTypeMatcher)

Example 42 with Edge

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

the class StunnerLogger method log.

@SuppressWarnings("unchecked")
public static void log(final Node<View, Edge> node) {
    if (null == node) {
        log("Node is null");
    } else {
        log("(View) Node UUID: " + node.getUUID());
        final View view = node.getContent();
        final String nId = getDefinitionId(view.getDefinition());
        final Bounds bounds = view.getBounds();
        log("(View) Node Id: " + nId);
        log("(View) Node Bounds: " + bounds);
        final Node parent = getParent(node);
        if (null != parent) {
            log("(View) Node Parent is: " + parent.getUUID());
        }
        Set<Edge> outEdges = new HashSet<>(node.getOutEdges());
        if (outEdges.isEmpty()) {
            log("No outgoing edges found");
        } else {
            log("Outgoing edges");
            log("==============");
            for (final Edge edge : outEdges) {
                log(edge);
            }
        }
        Set<Edge> inEdges = new HashSet<>(node.getInEdges());
        if (inEdges.isEmpty()) {
            log("No incoming edges found");
        } else {
            log("incoming edges");
            log("==============");
            for (final Edge edge : inEdges) {
                log(edge);
            }
        }
    }
}
Also used : Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) Node(org.kie.workbench.common.stunner.core.graph.Node) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge) HashSet(java.util.HashSet)

Example 43 with Edge

use of org.kie.workbench.common.stunner.core.graph.Edge 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 44 with Edge

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

the class ToolboxControlImplTest method testNotSupportedElement.

@Test
public void testNotSupportedElement() {
    tested.enable(canvasHandler);
    final Edge edge = mock(Edge.class);
    tested.register(edge);
    assertFalse(tested.isRegistered(edge));
    verify(shapeEventHandler, never()).supports(any(ViewEventType.class));
    verify(shapeEventHandler, never()).addHandler(any(ViewEventType.class), any(MouseClickHandler.class));
}
Also used : MouseClickHandler(org.kie.workbench.common.stunner.core.client.shape.view.event.MouseClickHandler) Edge(org.kie.workbench.common.stunner.core.graph.Edge) ViewEventType(org.kie.workbench.common.stunner.core.client.shape.view.event.ViewEventType) Test(org.junit.Test)

Example 45 with Edge

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

the class CanvasLayoutUtilsTest method getNextFromNewTaskWithNonEmptyPositionWithParent.

// TODO (AlessioP & Roger):
@Test
@Ignore
@SuppressWarnings("unchecked")
public void getNextFromNewTaskWithNonEmptyPositionWithParent() {
    when(ruleManager.evaluate(eq(ruleSet), any(RuleEvaluationContext.class))).thenReturn(ruleViolations);
    when(ruleViolations.violations(Violation.Type.ERROR)).thenReturn(ruleViolationIterable);
    when(ruleViolations.violations(Violation.Type.ERROR).iterator()).thenReturn(ruleViolationIterator);
    when(ruleViolations.violations(Violation.Type.ERROR).iterator().hasNext()).thenReturn(false);
    this.graphTestHandlerParent = new TestingGraphMockHandler();
    graphInstanceParent = TestingGraphInstanceBuilder.newGraph2(graphTestHandlerParent);
    Node newNode = mock(Node.class);
    Bounds boundsNewNode = new BoundsImpl(new BoundImpl(100d, 200d), new BoundImpl(300d, 300d));
    View viewNewNode = mock(View.class);
    when(newNode.getContent()).thenReturn(viewNewNode);
    when(viewNewNode.getBounds()).thenReturn(boundsNewNode);
    when(canvasHandler.getDiagram().getGraph()).thenReturn(graphInstanceParent.graph);
    when(graphBoundsIndexer.getAt(280.0, 100.0, 100.0, 100.0, graphInstanceParent.parentNode)).thenReturn(graphInstanceParent.intermNode);
    graphInstanceParent.startNode.getOutEdges().clear();
    Point2D next = canvasLayoutUtils.getNext(canvasHandler, graphInstanceParent.startNode, newNode);
    Node<View<?>, Edge> intermNode = (Node<View<?>, Edge>) graphInstanceParent.intermNode;
    double[] size = GraphUtils.getNodeSize(intermNode.getContent());
    assertTrue(next.getX() == CanvasLayoutUtils.getPaddingX());
    assertTrue(next.getY() > size[1]);
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Node(org.kie.workbench.common.stunner.core.graph.Node) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) TestingGraphMockHandler(org.kie.workbench.common.stunner.core.TestingGraphMockHandler) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Edge (org.kie.workbench.common.stunner.core.graph.Edge)154 View (org.kie.workbench.common.stunner.core.graph.content.view.View)101 Node (org.kie.workbench.common.stunner.core.graph.Node)84 Test (org.junit.Test)45 Documentation (org.kie.workbench.common.stunner.bpmn.definition.property.general.Documentation)30 Name (org.kie.workbench.common.stunner.bpmn.definition.property.general.Name)30 Graph (org.kie.workbench.common.stunner.core.graph.Graph)28 BPMNGeneralSet (org.kie.workbench.common.stunner.bpmn.definition.property.general.BPMNGeneralSet)25 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)25 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)19 ViewConnector (org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector)19 List (java.util.List)17 EventPropertyReader (org.kie.workbench.common.stunner.bpmn.backend.converters.tostunner.properties.EventPropertyReader)16 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)16 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)15 Element (org.kie.workbench.common.stunner.core.graph.Element)14 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)13 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)12 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)12 ArrayList (java.util.ArrayList)11