/// <summary> /// Copy the node to another root graph. /// Copies the attributes as well, as far as the attributes have been /// introduced in the destination graph. /// </summary> public Node CopyToOtherRoot(RootGraph destination) { Node result = destination.GetOrAddNode(GetName()); _ = CopyAttributesTo(result); return(result); }
public void CloneInto(RootGraph target) { // Copy all nodes and edges foreach (var node in Nodes()) { string nodename = node.GetName(); Node newnode = target.GetOrAddNode(nodename); foreach (var edge in node.EdgesOut(this)) { Node head = edge.Head(); Debug.Assert(Contains(head)); Node tail = edge.Tail(); Debug.Assert(node.Equals(tail)); string headname = head.GetName(); Node newhead = target.GetOrAddNode(headname); string tailname = tail.GetName(); Node newtail = target.GetNode(tailname); string edgename = edge.GetName(); Edge newedge = target.GetOrAddEdge(newtail, newhead, edgename); edge.CopyAttributesTo(newedge); } node.CopyAttributesTo(newnode); } // Copy all subgraphs foreach (var subgraph in Descendants()) { string subgraphname = subgraph.GetName(); Graph parent = subgraph.Parent(); Graph newparent; if (parent.Equals(this)) { newparent = target; } else { string parentname = parent.GetName(); newparent = target.GetDescendantByName(parentname); Debug.Assert(newparent != null); } SubGraph newsubgraph = newparent.GetOrAddSubgraph(subgraphname); subgraph.CopyAttributesTo(newsubgraph); // Add the (already created) nodes and edges to newly created subgraph foreach (var node in subgraph.Nodes()) { string nodename = node.GetName(); Node newnode = target.GetNode(nodename); Debug.Assert(newnode != null); newsubgraph.AddExisting(newnode); foreach (var edge in node.EdgesOut(subgraph)) { Node head = edge.Head(); Node tail = edge.Tail(); Debug.Assert(node.Equals(tail)); string headname = head.GetName(); Node newhead = target.GetNode(headname); string tailname = tail.GetName(); Node newtail = target.GetNode(tailname); string edgename = edge.GetName(); Edge newedge = target.GetEdge(newtail, newhead, edgename); newsubgraph.AddExisting(newedge); } node.CopyAttributesTo(newnode); } } }