Search in sources :

Example 1 with Definition

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

the class SafeDeleteNodeCommand method initialize.

@Override
@SuppressWarnings("unchecked")
protected SafeDeleteNodeCommand initialize(final GraphCommandExecutionContext context) {
    super.initialize(context);
    final Graph<?, Node> graph = getGraph(context);
    final Node<Definition<?>, Edge> candidate = (Node<Definition<?>, Edge>) getCandidate(context);
    new SafeDeleteNodeProcessor(new ChildrenTraverseProcessorImpl(new TreeWalkTraverseProcessorImpl()), graph, candidate).run(new SafeDeleteNodeProcessor.Callback() {

        private final Set<String> processedConnectors = new HashSet<String>();

        @Override
        public void deleteCandidateConnector(final Edge<? extends View<?>, Node> edge) {
        // This command will delete candidate's connectors once deleting the candidate node later on,
        // as it potentially performs the connectors shortcut operation.
        }

        @Override
        public void deleteConnector(final Edge<? extends View<?>, Node> edge) {
            doDeleteConnector(edge);
        }

        @Override
        public void removeChild(final Element<?> parent, final Node<?, Edge> candidate) {
            log("RemoveChildCommand [parent=" + parent.getUUID() + ", candidate=" + candidate.getUUID() + "]");
            addCommand(new RemoveChildCommand((Node<?, Edge>) parent, candidate));
            safeDeleteCallback.ifPresent(c -> c.removeChild(parent, candidate));
        }

        @Override
        public void removeDock(final Node<?, Edge> parent, final Node<?, Edge> candidate) {
            log("UnDockNodeCommand [parent=" + parent.getUUID() + ", candidate=" + candidate.getUUID() + "]");
            addCommand(new UnDockNodeCommand(parent, candidate));
            safeDeleteCallback.ifPresent(c -> c.removeDock(parent, candidate));
        }

        @Override
        public void deleteCandidateNode(final Node<?, Edge> node) {
            processCandidateConnectors();
            deleteNode(node);
        }

        @Override
        public void deleteNode(final Node<?, Edge> node) {
            log("DeregisterNodeCommand [node=" + node.getUUID() + "]");
            addCommand(new DeregisterNodeCommand(node));
            safeDeleteCallback.ifPresent(c -> c.deleteNode(node));
        }

        private void processCandidateConnectors() {
            if (options.isDeleteCandidateConnectors()) {
                if (options.isShortcutCandidateConnectors() && hasSingleIncomingEdge().and(hasSingleOutgoingEdge()).test(candidate)) {
                    final Edge<? extends ViewConnector<?>, Node> in = getViewConnector().apply(candidate.getInEdges());
                    final Edge<? extends ViewConnector<?>, Node> out = getViewConnector().apply(candidate.getOutEdges());
                    shortcut(in, out);
                } else {
                    Stream.concat(candidate.getInEdges().stream(), candidate.getOutEdges().stream()).filter(e -> e.getContent() instanceof ViewConnector).forEach(this::deleteConnector);
                }
            }
        }

        private void shortcut(final Edge<? extends ViewConnector<?>, Node> in, final Edge<? extends ViewConnector<?>, Node> out) {
            final ViewConnector<?> outContent = out.getContent();
            final Node targetNode = out.getTargetNode();
            addCommand(new DeleteConnectorCommand(out));
            safeDeleteCallback.ifPresent(c -> c.deleteCandidateConnector(out));
            addCommand(new SetConnectionTargetNodeCommand(targetNode, in, outContent.getTargetConnection().orElse(null)));
            safeDeleteCallback.ifPresent(c -> c.setEdgeTargetNode(targetNode, in));
        }

        private void doDeleteConnector(final Edge<? extends View<?>, Node> edge) {
            if (!processedConnectors.contains(edge.getUUID())) {
                log("IN DoDeleteConnector [edge=" + edge.getUUID() + "]");
                addCommand(new DeleteConnectorCommand(edge));
                safeDeleteCallback.ifPresent(c -> c.deleteConnector(edge));
                processedConnectors.add(edge.getUUID());
            }
        }
    });
    return this;
}
Also used : Edge(org.kie.workbench.common.stunner.core.graph.Edge) Portable(org.jboss.errai.common.client.api.annotations.Portable) Function(java.util.function.Function) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Level(java.util.logging.Level) HashSet(java.util.HashSet) SafeDeleteNodeProcessor(org.kie.workbench.common.stunner.core.graph.util.SafeDeleteNodeProcessor) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) NonPortable(org.jboss.errai.common.client.api.annotations.NonPortable) ChildrenTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ChildrenTraverseProcessorImpl) Element(org.kie.workbench.common.stunner.core.graph.Element) PortablePreconditions(org.kie.soup.commons.validation.PortablePreconditions) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) CardinalityContext(org.kie.workbench.common.stunner.core.rule.context.CardinalityContext) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) Set(java.util.Set) Logger(java.util.logging.Logger) RuleContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) List(java.util.List) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Stream(java.util.stream.Stream) CommandUtils(org.kie.workbench.common.stunner.core.command.util.CommandUtils) Optional(java.util.Optional) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) MapsTo(org.jboss.errai.common.client.api.annotations.MapsTo) Node(org.kie.workbench.common.stunner.core.graph.Node) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) Node(org.kie.workbench.common.stunner.core.graph.Node) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) SafeDeleteNodeProcessor(org.kie.workbench.common.stunner.core.graph.util.SafeDeleteNodeProcessor) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) ChildrenTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ChildrenTraverseProcessorImpl) Edge(org.kie.workbench.common.stunner.core.graph.Edge) HashSet(java.util.HashSet)

Example 2 with Definition

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

the class SetChildNodeCommand method check.

@SuppressWarnings("unchecked")
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
    final Element<? extends Definition<?>> parent = (Element<? extends Definition<?>>) getParent(context);
    final Node<Definition<?>, Edge> candidate = (Node<Definition<?>, Edge>) getCandidate(context);
    final Collection<RuleViolation> containmentRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.containment(getGraph(context), parent, candidate));
    return new GraphCommandResultBuilder(containmentRuleViolations).build();
}
Also used : GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) Element(org.kie.workbench.common.stunner.core.graph.Element) Node(org.kie.workbench.common.stunner.core.graph.Node) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 3 with Definition

use of org.kie.workbench.common.stunner.core.graph.content.definition.Definition 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 4 with Definition

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

the class ResizeControlImpl method getResizeCommands.

/**
 * It provides the necessary canvas commands in order to update the domain model with new values that will met
 * the new bounding box size.
 * It always updates the element's position, as resize can update it, and it updates as well some of the bean's properties.
 */
private List<Command<AbstractCanvasHandler, CanvasViolation>> getResizeCommands(final Element<? extends Definition<?>> element, final double w, final double h) {
    final Definition content = element.getContent();
    final Object def = content.getDefinition();
    final DefinitionAdapter<Object> adapter = canvasHandler.getDefinitionManager().adapters().registry().getDefinitionAdapter(def.getClass());
    final List<Command<AbstractCanvasHandler, CanvasViolation>> result = new LinkedList<>();
    final Object width = adapter.getMetaProperty(PropertyMetaTypes.WIDTH, def);
    if (null != width) {
        appendCommandForModelProperty(element, width, w, result);
    }
    final Object height = adapter.getMetaProperty(PropertyMetaTypes.HEIGHT, def);
    if (null != height) {
        appendCommandForModelProperty(element, height, h, result);
    }
    final Object radius = adapter.getMetaProperty(PropertyMetaTypes.RADIUS, def);
    if (null != radius) {
        final double r = w > h ? (h / 2) : (w / 2);
        appendCommandForModelProperty(element, radius, r, result);
    }
    return result;
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) LinkedList(java.util.LinkedList)

Example 5 with Definition

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

the class CaseManagementUtilsTest method checkGetFirstDiagramNodeWithEmptyGraph.

@Test
public void checkGetFirstDiagramNodeWithEmptyGraph() {
    final Graph graph = new GraphImpl<>("uuid", new GraphNodeStoreImpl());
    final Node<Definition<CaseManagementDiagram>, ?> fNode = CaseManagementUtils.getFirstDiagramNode(graph);
    assertNull(fNode);
}
Also used : GraphNodeStoreImpl(org.kie.workbench.common.stunner.core.graph.store.GraphNodeStoreImpl) Graph(org.kie.workbench.common.stunner.core.graph.Graph) GraphImpl(org.kie.workbench.common.stunner.core.graph.impl.GraphImpl) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) Test(org.junit.Test)

Aggregations

Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)111 Node (org.kie.workbench.common.stunner.core.graph.Node)61 Test (org.junit.Test)46 Element (org.kie.workbench.common.stunner.core.graph.Element)31 Edge (org.kie.workbench.common.stunner.core.graph.Edge)25 Graph (org.kie.workbench.common.stunner.core.graph.Graph)20 ArrayList (java.util.ArrayList)16 Optional (java.util.Optional)14 DRGElement (org.kie.workbench.common.dmn.api.definition.model.DRGElement)14 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)14 Diagram (org.kie.workbench.common.stunner.core.diagram.Diagram)14 View (org.kie.workbench.common.stunner.core.graph.content.view.View)12 Decision (org.kie.workbench.common.dmn.api.definition.model.Decision)11 QName (org.kie.workbench.common.dmn.api.property.dmn.QName)10 List (java.util.List)9 Before (org.junit.Before)9 DMNDiagram (org.kie.workbench.common.dmn.api.definition.model.DMNDiagram)9 HasContentDefinitionId (org.kie.workbench.common.stunner.core.graph.content.HasContentDefinitionId)9 NodeImpl (org.kie.workbench.common.stunner.core.graph.impl.NodeImpl)9 Stream (java.util.stream.Stream)8