def test_argument_unaltered(self): match = nx.MultiDiGraph() match.add_node('a') mapping = {1: 'a'} replacement = nx.MultiDiGraph() replacement.add_nodes_from([1, 2]) expected = (len(GRAPH), len(GRAPH.edges())) replace_subgraph(GRAPH, match, mapping, replacement) result = (len(GRAPH), len(GRAPH.edges())) self.assertEqual(expected, result)
def test_empty_replace(self): match = nx.MultiDiGraph() mapping = {} replacement = nx.MultiDiGraph() result = replace_subgraph(GRAPH, match, mapping, replacement) self.assertGraphEqual(GRAPH, result)
def test_remove_node(self): match = nx.MultiDiGraph() match.add_node('a') mapping = {1: 'a'} replacement = nx.MultiDiGraph() result = replace_subgraph(GRAPH, match, mapping, replacement) expected = nx.MultiDiGraph() expected.add_node('b') self.assertGraphEqual(expected, result)
def test_remove_edge(self): match = nx.MultiDiGraph() match.add_edge('a', 'b', key='s') mapping = {1: 'a', 2: 'b'} replacement = nx.MultiDiGraph() replacement.add_nodes_from([1, 2]) result = replace_subgraph(GRAPH, match, mapping, replacement) expected = nx.MultiDiGraph() expected.add_edge('a', 'b', key='t') self.assertGraphEqual(expected, result)
def test_add_identity_edge(self): match = nx.MultiDiGraph() match.add_node('a') mapping = {1: 'a'} replacement = nx.MultiDiGraph() replacement.add_edge(1, 1, key='id') result = replace_subgraph(GRAPH, match, mapping, replacement) expected = nx.MultiDiGraph() expected.add_edge('a', 'b', key='s') expected.add_edge('a', 'b', key='t') expected.add_edge('a', 'a', key='id') self.assertGraphEqual(expected, result)
def test_add_node_and_edge(self): match = nx.MultiDiGraph() match.add_node('a') mapping = {1: 'a'} replacement = nx.MultiDiGraph() replacement.add_edge(2, 1, key='u') result = replace_subgraph(GRAPH, match, mapping, replacement) expected = nx.MultiDiGraph() expected.add_edge('a', 'b', key='s') expected.add_edge('a', 'b', key='t') expected.add_edge(MockSymbol(), 'a', key='u') self.assertSymbolGraphEqual(expected, result)
def _apply_expression_substitutions(graph, expression, substitutions): for subgraph, mapping in find_all_subgraphs(graph, expression): for substitution in substitutions: yield replace_subgraph(graph, subgraph, mapping, substitution)