Пример #1
0
    def _match(self,
               G: GraphView,
               set_identity: bool = True,
               **kwargs) -> bool:
        replaced = True
        has_modified_graph = False
        while replaced:
            replaced = False
            for subgraph in self.match_function(G):
                # TODO - Save in and out edges here since the replace function may modify the
                # subgraph
                in_edges = [
                    in_edge for input_node in subgraph.inputs()
                    for in_edge in G.in_edges(input_node.name)
                ]
                out_edges = [
                    out_edge for output_node in subgraph.outputs()
                    for out_edge in G.out_edges(output_node.name)
                ]
                try:
                    replacement, edge_in_mapping, edge_out_mapping = self.replace_function(
                        G, subgraph)
                    if replacement is None:
                        G.remove_fragment(subgraph)
                        has_modified_graph = True
                    elif isinstance(replacement, Node):
                        # use saved  in and out edges
                        G.replace_fragment(subgraph,
                                           replacement,
                                           frag_in_edges=in_edges,
                                           frag_out_edges=out_edges,
                                           edge_in_mapping=edge_in_mapping,
                                           edge_out_mapping=edge_out_mapping)
                        has_modified_graph = True
                    else:
                        raise TypeError(
                            "unexcepted return value from replace_function")
                    replaced = True
                    break
                except DontReplaceError:
                    pass

        if set_identity:
            self.set_identity(G)

        return has_modified_graph
Пример #2
0
 def match(self, G: GraphView, set_identity: bool = True):
     replaced = True
     while replaced:
         replaced = False
         for subgraph in self.match_function(G):
             replacement = self.replace_function(G, subgraph)
             if not replacement:
                 G.remove_fragment(subgraph)
             elif isinstance(replacement, Node):
                 G.replace_fragment(subgraph, replacement)
             else:
                 raise TypeError(
                     "unexcepted return value from replace_function")
             replaced = True
             break
     if set_identity:
         self.set_identity(G)