| 126 | self.tag(n, tag, graph, conditional=True) |
| 127 | |
| 128 | def apply(self, graph): |
| 129 | # Copy the graph. Don't change the original. |
| 130 | graph = copy.deepcopy(graph) |
| 131 | |
| 132 | while True: |
| 133 | matches, _ = graph.search(self.pattern) |
| 134 | if not matches: |
| 135 | break |
| 136 | # Tag found nodes and their incoming branches |
| 137 | for n in matches: |
| 138 | self.tag(n, "delete", graph) |
| 139 | # Find all tagged nodes and delete them |
| 140 | tagged = [n for n in graph.nodes.values() |
| 141 | if hasattr(n, "__tag__") and n.__tag__ == "delete"] |
| 142 | graph.remove(tagged) |
| 143 | return graph |
| 144 | |
| 145 | |
| 146 | class FoldDuplicates(): |