def change_edge_src( graph: dace.graph.graph.OrderedDiGraph, node_a: Union[dace.graph.nodes.Node, dace.graph.graph.OrderedMultiDiConnectorGraph], node_b: Union[dace.graph.nodes.Node, dace.graph.graph.OrderedMultiDiConnectorGraph]): """ Changes the sources of edges from node A to node B. The function finds all edges in the graph that have node A as their source. It then creates a new edge for each one found, using the same destination nodes and data, but node B as the source. Afterwards, it deletes the edges found and inserts the new ones into the graph. :param graph: The graph upon which the edge transformations will be applied. :param node_a: The original source of the edges to be transformed. :param node_b: The new source of the edges to be transformed. """ # Create new outgoing edges from node B, by copying the outgoing edges from # node A and setting their source to node B. edges = list(graph.out_edges(node_a)) for e in edges: # Delete the outgoing edges from node A from the graph. graph.remove_edge(e) # Insert the new edges to the graph. if isinstance(e, gr.MultiConnectorEdge): # src_conn = e.src_conn # if e.src_conn is not None: # # Remove connector from node A. # node_a.remove_out_connector(e.src_conn) # # Insert connector to node B. # if (not node_b.add_out_connector(src_conn) and isinstance( # node_b, (dace.graph.nodes.CodeNode, # dace.graph.nodes.MapExit))): # while not node_b.add_out_connector(src_conn): # src_conn = src_conn + '_' # graph.add_edge(node_b, src_conn, e.dst, e.dst_conn, e.data) graph.add_edge(node_b, e.src_conn, e.dst, e.dst_conn, e.data) else: graph.add_edge(node_b, e.dst, e.data)
def replace_subgraph(graph: dace.graph.graph.OrderedDiGraph, old: dace.graph.graph.OrderedDiGraph, new: dace.graph.graph.OrderedDiGraph): """ Replaces a subgraph of a graph with a new one. If replacement is not possible, it returns False. The function replaces the 'old' subgraph of the input graph with the 'new' subgraph. Both the 'old' and the 'new' subgraphs must have unique source and sink nodes. Graph edges incoming to the source of the 'old' subgraph have their destination changed to the source of the 'new subgraph. Likewise, graph edges outgoing from the sink of the 'old subgraph have their source changed to the sink of the 'new' subgraph. @param graph: The graph upon which the replacement will be applied. @param old: The subgraph to be replaced. @param new: The replacement subgraph. @return: True if the replacement succeeded, otherwise False. """ # 1. Find the source node of 'old' subgraph. # 1.1. Retrieve the source nodes of the 'old' subgraph. old_source_nodes = find_source_nodes(old) # 1.2. Verify the existence of a unique source in the 'old' subgraph. if len(old_source_nodes) != 1: return False old_source = old_source_nodes[0] # 2. Find the sink node of the 'old' subgraph. # 2.1. Retrieve the sink nodes of the 'old' subgraph. old_sink_nodes = find_sink_nodes(old) # 2.2. Verify the existence of a unique sink in the 'old' subgraph. if len(old_sink_nodes) != 1: return False old_sink = old_sink_nodes[0] # 3. Find the source node of 'new' subgraph. # 3.1. Retrieve the source nodes of the 'new' subgraph. new_source_nodes = find_source_nodes(new) # 3.2. Verify the existence of a unique source in the 'new' subgraph. if len(new_source_nodes) != 1: return False new_source = new_source_nodes[0] # 4. Find the sink node of the 'new' subgraph. # 4.1. Retrieve the sink nodes of the 'new' subgraph. new_sink_nodes = find_sink_nodes(new) # 4.2. Verify the existence of a unique sink in the 'new' subgraph. if len(new_sink_nodes) != 1: return False new_sink = new_sink_nodes[0] # 5. Add the 'new' subgraph to the graph. # 5.1. Add the nodes of the 'new' subgraph to the graph. graph.add_nodes_from(new.nodes()) # 5.2. Add the edges of the 'new' subgraph to the graph. for e in new.edges(): graph.add_edge(*e) # 6. Create new incoming edges to the source of the 'new' subgraph. change_edge_dest(graph, old_source, new_source) # 7. Create new outgoing edges from the sink of the 'new' subgraph. change_edge_src(graph, old_sink, new_sink) # 8. Remove all nodes of the 'old' subgraph from the graph. graph.remove_nodes_from(old.nodes()) # 10. Subgraph replacement has succeeded. Return true. return True