Search in sources :

Example 1 with TreeWalkTraverseProcessor

use of org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor 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 TreeWalkTraverseProcessor

use of org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor in project kie-wb-common by kiegroup.

the class GraphImpl method hashCode.

@Override
public int hashCode() {
    // dirty trick to allow inner class to modify variable
    int[] hashArr = { 0 };
    final TreeWalkTraverseProcessor treeWalkTraverseProcessor = new TreeWalkTraverseProcessorImpl();
    treeWalkTraverseProcessor.traverse(this, new AbstractTreeTraverseCallback<Graph, Node, Edge>() {

        int[] myHashArr = hashArr;

        @Override
        public boolean startEdgeTraversal(final Edge edge) {
            super.startEdgeTraversal(edge);
            final Object content = edge.getContent();
            myHashArr[0] = HashUtil.combineHashCodes(myHashArr[0], content.hashCode());
            return true;
        }

        @Override
        public boolean startNodeTraversal(final Node node) {
            super.startNodeTraversal(node);
            myHashArr[0] = HashUtil.combineHashCodes(myHashArr[0], node.hashCode());
            if (!(node.getContent() instanceof DefinitionSet) && node.getContent() instanceof Definition) {
                Object def = ((Definition) (node.getContent())).getDefinition();
                myHashArr[0] = HashUtil.combineHashCodes(myHashArr[0], def.hashCode());
            }
            return true;
        }
    });
    // Get the hash from the graph traversal
    return hashArr[0];
}
Also used : TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Node(org.kie.workbench.common.stunner.core.graph.Node) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) Edge(org.kie.workbench.common.stunner.core.graph.Edge) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl)

Example 3 with TreeWalkTraverseProcessor

use of org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor in project kie-wb-common by kiegroup.

the class CanvasHighlightVisitor method prepareVisit.

@SuppressWarnings("unchecked")
private void prepareVisit(final Command command) {
    final Graph graph = canvasHandler.getDiagram().getGraph();
    final TreeWalkTraverseProcessor treeWalkTraverseProcessor = new TreeWalkTraverseProcessorImpl().useStartNodePredicate(this::isStartNode);
    new ViewTraverseProcessorImpl(treeWalkTraverseProcessor).traverse(graph, new ContentTraverseCallback<View<?>, Node<View, Edge>, Edge<View<?>, Node>>() {

        @Override
        public void startGraphTraversal(final Graph<DefinitionSet, Node<View, Edge>> graph) {
        }

        @Override
        public void startEdgeTraversal(final Edge<View<?>, Node> edge) {
            addShape(edge.getUUID());
        }

        @Override
        public void endEdgeTraversal(final Edge<View<?>, Node> edge) {
        }

        @Override
        public void startNodeTraversal(final Node<View, Edge> node) {
            addShape(node.getUUID());
        }

        @Override
        public void endNodeTraversal(final Node<View, Edge> node) {
        }

        @Override
        public void endGraphTraversal() {
            command.execute();
        }

        private void addShape(final String uuid) {
            final Shape shape = canvasHandler.getCanvas().getShape(uuid);
            if (null != shape) {
                shapes.add(shape);
            }
        }
    });
}
Also used : Shape(org.kie.workbench.common.stunner.core.client.shape.Shape) Node(org.kie.workbench.common.stunner.core.graph.Node) ViewTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ViewTraverseProcessorImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) Graph(org.kie.workbench.common.stunner.core.graph.Graph) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Aggregations

Edge (org.kie.workbench.common.stunner.core.graph.Edge)3 Graph (org.kie.workbench.common.stunner.core.graph.Graph)3 Node (org.kie.workbench.common.stunner.core.graph.Node)3 TreeWalkTraverseProcessor (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor)3 DefinitionSet (org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)2 View (org.kie.workbench.common.stunner.core.graph.content.view.View)2 TreeWalkTraverseProcessorImpl (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl)2 HashSet (java.util.HashSet)1 Shape (org.kie.workbench.common.stunner.core.client.shape.Shape)1 Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)1 ViewTraverseProcessorImpl (org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ViewTraverseProcessorImpl)1 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)1 RuleViolationImpl (org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl)1