Skip to content

Commit

Permalink
[509078] Fix GraphCopier for nested graphs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwienand committed Dec 12, 2016
1 parent 5d91b42 commit 5349a07
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.eclipse.gef.common.attributes.IAttributeCopier;
import org.eclipse.gef.graph.Edge;
import org.eclipse.gef.graph.Graph;
Expand All @@ -27,6 +29,34 @@ public class GraphCopierTests {

private static final String ID = "id";

@Test
public void buildNestedWithKeys() {
// build source graph containing a nested graph in the second node
Node n = new Node();
Node m = new Node();
Edge nm = new Edge(n, m);
Node ma = new Node();
Node mb = new Node();
Edge mab = new Edge(ma, mb);
Graph mg = new Graph(Arrays.asList(ma, mb), Arrays.asList(mab));
m.setNestedGraph(mg);
Graph sourceGraph = new Graph(Arrays.asList(n, m), Arrays.asList(nm));

// copy source graph
GraphCopier copier = new GraphCopier(IAttributeCopier.NULL_COPY);
Graph copy = copier.copy(sourceGraph);

// check number of nodes and edges
assertEquals(2, copy.getNodes().size());
assertEquals(1, copy.getEdges().size());

// check source and target of first edge
assertEquals(copy.getNodes().get(0),
copy.getEdges().get(0).getSource());
assertEquals(copy.getNodes().get(1),
copy.getEdges().get(0).getTarget());
}

private Graph genGraph(int size) {
Graph.Builder gb = new Graph.Builder();
Node prev = null;
Expand Down
62 changes: 38 additions & 24 deletions org.eclipse.gef.graph/src/org/eclipse/gef/graph/GraphCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,7 @@ public Graph copy(Graph graph) {
// clear input to output maps
inputToOutputNodes.clear();
inputToOutputEdges.clear();
// create new graph to hold the copy
Graph outputGraph = new Graph();
copyAttributes(graph, outputGraph);
// copy nodes, keeping track of copied nodes (so we can relocate them to
// link edges)
for (Node inputNode : graph.getNodes()) {
Node outputNode = copyNode(inputNode);
if (outputNode != null) {
inputToOutputNodes.put(inputNode, outputNode);
outputNode.setGraph(outputGraph);
outputGraph.getNodes().add(outputNode);
}
}
// copy edges
for (Edge inputEdge : graph.getEdges()) {
Edge outputEdge = copyEdge(inputEdge);
if (outputEdge != null) {
inputToOutputEdges.put(inputEdge, outputEdge);
outputEdge.setGraph(outputGraph);
outputGraph.getEdges().add(outputEdge);
}
}
return outputGraph;
return copyGraph(graph);
}

/**
Expand Down Expand Up @@ -117,6 +95,42 @@ protected Edge copyEdge(Edge edge) {
return outputEdge;
}

/**
* Copies the given {@link Graph} using the current
* {@link IAttributeCopier}. Records the copied nodes in the
* {@link #getInputToOutputNodeMap()} and the copied edges in the
* {@link #getInputToOutputEdgeMap()}.
*
* @param graph
* The input {@link Graph} to copy.
* @return The copied result {@link Graph}.
*/
protected Graph copyGraph(Graph graph) {
// create new graph to hold the copy
Graph outputGraph = new Graph();
copyAttributes(graph, outputGraph);
// copy nodes, keeping track of copied nodes (so we can relocate them to
// link edges)
for (Node inputNode : graph.getNodes()) {
Node outputNode = copyNode(inputNode);
if (outputNode != null) {
inputToOutputNodes.put(inputNode, outputNode);
outputNode.setGraph(outputGraph);
outputGraph.getNodes().add(outputNode);
}
}
// copy edges
for (Edge inputEdge : graph.getEdges()) {
Edge outputEdge = copyEdge(inputEdge);
if (outputEdge != null) {
inputToOutputEdges.put(inputEdge, outputEdge);
outputEdge.setGraph(outputGraph);
outputGraph.getEdges().add(outputEdge);
}
}
return outputGraph;
}

/**
* Creates a copy of the given node.
*
Expand All @@ -129,7 +143,7 @@ protected Node copyNode(Node node) {
copyAttributes(node, outputNode);
// convert nested graph
if (node.getNestedGraph() != null) {
Graph nested = copy(node.getNestedGraph());
Graph nested = copyGraph(node.getNestedGraph());
outputNode.setNestedGraph(nested);
}
return outputNode;
Expand Down

0 comments on commit 5349a07

Please sign in to comment.