graph.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:cobol-sharp 作者: petli 项目源码 文件源码
def from_cobol_graph(cls, cobol_graph):
        """Identify loops in a CobolStructureGraph and break them by adding Loop
        and ContinueLoop nodes.  Returns the resulting AcyclicStructureGraph.
        """
        dag = cls()

        # Copy by way of edges, to avoid getting copies of the node objects
        dag.graph.add_edges_from(cobol_graph.graph.edges(keys=True, data=True))

        # Loops are strongly connected components, i.e. a set of nodes
        # which can all reach the other ones via some path through the
        # component.

        # Since loops can contain loops, this is done repeatedly until all
        # loops have been broken.  At this stage single-node loops are ignored,
        # since nx.strongly_connected_components() returns components also
        # consisting of a single nodes without any self-looping edge.
        while True:
            components = [c for c in nx.strongly_connected_components(dag.graph)
                          if len(c) > 1]
            if not components:
                break

            for component in components:
                dag._break_component_loop(component)

        # Finally find any remaining single-node loops
        for node in list(dag.graph):
            if dag.graph[node].get(node) is not None:
                dag._break_component_loop({node})

        return dag
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号