| 26 | self.name = name |
| 27 | |
| 28 | def apply(self, graph): |
| 29 | # Copy the graph. Don't change the original. |
| 30 | graph = copy.deepcopy(graph) |
| 31 | |
| 32 | while True: |
| 33 | matches, _ = graph.search(self.pattern) |
| 34 | if not matches: |
| 35 | break |
| 36 | |
| 37 | # Replace pattern with new node |
| 38 | if self.op == "__first__": |
| 39 | combo = matches[0] |
| 40 | elif self.op == "__last__": |
| 41 | combo = matches[-1] |
| 42 | else: |
| 43 | combo = Node(uid=graph.sequence_id(matches), |
| 44 | name=self.name or " > ".join([l.title for l in matches]), |
| 45 | op=self.op or self.pattern, |
| 46 | output_shape=matches[-1].output_shape) |
| 47 | combo._caption = "/".join(filter(None, [l.caption for l in matches])) |
| 48 | graph.replace(matches, combo) |
| 49 | return graph |
| 50 | |
| 51 | |
| 52 | class FoldId(): |