Successfully break cycle.
Chris Pressey
4 years ago
65 | 65 |
fa.analyze_program(program)
|
66 | 66 |
|
67 | 67 |
if options.dump_fallthru_info:
|
68 | |
fallthru_map = dict(fa.fallthru_map)
|
69 | |
sys.stdout.write(json.dumps(fallthru_map, indent=4, sort_keys=True))
|
|
68 |
sys.stdout.write(json.dumps(fa.fallthru_map, indent=4, sort_keys=True))
|
70 | 69 |
sys.stdout.write("\n")
|
71 | 70 |
|
72 | |
fa.find_ancestors()
|
73 | |
|
74 | |
if options.debug and options.dump_fallthru_info:
|
75 | |
sys.stdout.write("*** ancestors:\n")
|
76 | |
sys.stdout.write(json.dumps(fa.ancestor_map, indent=4, sort_keys=True))
|
77 | |
sys.stdout.write("\n")
|
78 | |
|
79 | 71 |
fa.find_cycles()
|
80 | 72 |
|
81 | |
if fa.cycles_found:
|
|
73 |
while fa.cycles_found:
|
82 | 74 |
if options.dump_fallthru_info:
|
|
75 |
|
|
76 |
if options.debug:
|
|
77 |
sys.stdout.write("*** ancestors:\n")
|
|
78 |
sys.stdout.write(json.dumps(fa.ancestor_map, indent=4, sort_keys=True))
|
|
79 |
sys.stdout.write("\n")
|
|
80 |
|
83 | 81 |
sys.stdout.write("*** cycles found:\n")
|
84 | 82 |
sys.stdout.write(json.dumps(sorted(fa.cycles_found), indent=4, sort_keys=True))
|
85 | 83 |
sys.stdout.write("\n")
|
86 | 84 |
|
87 | |
fa.break_cycles()
|
88 | |
|
89 | |
if options.dump_fallthru_info and fallthru_map != fa.fallthru_map:
|
90 | |
sys.stdout.write("*** after breaking cycles:\n")
|
|
85 |
fa.break_cycle()
|
|
86 |
|
|
87 |
if options.dump_fallthru_info:
|
|
88 |
sys.stdout.write("*** after breaking cycle:\n")
|
91 | 89 |
sys.stdout.write(json.dumps(fa.fallthru_map, indent=4, sort_keys=True))
|
92 | 90 |
sys.stdout.write("\n")
|
|
91 |
|
|
92 |
fa.find_cycles()
|
93 | 93 |
|
94 | 94 |
if options.analyze_only:
|
95 | 95 |
return
|
23 | 23 |
self.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()])
|
24 | 24 |
return self.fallthru_map
|
25 | 25 |
|
26 | |
def find_ancestors(self):
|
|
26 |
def find_cycles(self):
|
27 | 27 |
self.ancestor_map = {}
|
28 | 28 |
for key in self.fallthru_map:
|
29 | 29 |
ancestors = set()
|
30 | 30 |
make_transitive_closure(self.fallthru_map, key, ancestors)
|
31 | 31 |
self.ancestor_map[key] = sorted(ancestors)
|
32 | |
return self.ancestor_map
|
33 | 32 |
|
34 | |
def find_cycles(self):
|
35 | 33 |
self.cycles_found = set()
|
36 | 34 |
for key in self.ancestor_map:
|
37 | 35 |
if key in self.ancestor_map[key]:
|
38 | 36 |
self.cycles_found.add(key)
|
|
37 |
|
39 | 38 |
return self.cycles_found
|
40 | 39 |
|
41 | |
def break_cycles(self):
|
|
40 |
def break_cycle(self):
|
|
41 |
cycle_to_break = sorted(self.cycles_found)[0]
|
|
42 |
cycles_to_break = set([cycle_to_break])
|
|
43 |
|
42 | 44 |
new_fallthru_map = {}
|
43 | 45 |
for key in self.fallthru_map:
|
44 | |
values = set(self.fallthru_map[key]) - self.cycles_found
|
|
46 |
values = set(self.fallthru_map[key]) - cycles_to_break
|
45 | 47 |
if values:
|
46 | 48 |
new_fallthru_map[key] = sorted(values)
|
47 | 49 |
self.fallthru_map = new_fallthru_map
|