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
评论列表
文章目录