-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviconc.py
executable file
·297 lines (271 loc) · 8.95 KB
/
viconc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/python2
##/usr/bin/env python2
import os
import sys
import re
import subprocess
# DEBUG = True
DEBUG = False
def debug(info):
if DEBUG:
print info
def file_to_node_list(f):
regex = '(?:(\d+): ' +\
'(?:' +\
'(?:(action) :: "(.+)")' +\
'|' +\
'(?:(backtrack) :: (\w+))' +\
'|' +\
'(error)' +\
'|' +\
'(cycle)' +\
'))'
# print ">>>> Starting findall with regex"
data = re.findall(regex, f.read())
node_list = []
event_n = 1
action_n = 1
for event in data:
node = dict()
node['event_n'] = event_n
explore_n = event[0]
node['explore_n'] = explore_n
if event[1] == 'action': # Check if an action was taken
node['action'] = event[2]
# Replace every " for \" in the texts, if not preceded by \
action = re.sub('[^\\\]"', '\\"', event[2])
label = '"Explore ' + explore_n + " | Action " +\
str(action_n) + '\\n' + action + '"'
node_dot_text = str(event_n) + " [label = " + label + "];\n"
action_n += 1
# Check if there was a backtrack
elif event[3] == 'backtrack':
node['backtrack'] = event[4]
label = '"Explore ' + explore_n + " | event " +\
str(event_n) + '\\n' +\
'Backtrack set explored: ' + event[4] + '"'
node_dot_text = str(event_n) + " [label = " + label + "];\n"
# Check if there was an error
elif event[5] == 'error':
node['error'] = True
label = '"Explore ' + explore_n + " | event " +\
str(event_n) + '\\n' + 'ERROR!"'
node_dot_text = "// Explore " + str(event_n) + "\n" +\
str(event_n) + " [label = " + label + "];\n"
# Check if there was a cycle
elif event[6] == 'cycle':
node['cycle'] = True
label = '"Explore ' + explore_n + " | event " +\
str(event_n) + '\\n' + 'CYCLE!"'
node_dot_text = "// Explore " + str(event_n) + "\n" +\
str(event_n) + " [label = " + label + "];\n"
node['text'] = node_dot_text
node_list.append(node)
event_n += 1
return node_list
def create_clusters(node_list):
# The tracks are separated with the adjacent events
connection_groups = []
adjacent_list = []
created_cluster = False
for node in node_list:
if 'action' in node: # Check if an action was taken
adjacent_list.append(node)
created_cluster = False
else:
if adjacent_list != []:
connection_groups.append(adjacent_list)
adjacent_list = []
created_cluster = True
if not created_cluster:
connection_groups.append(adjacent_list)
return connection_groups
# String with the full content of the dot file
def dot_file_content(node_list):
node_dot_list = ""
for node in node_list:
if 'action' in node: # Only show nodes with an action
node_dot_list += node.get('text')
node_dot_list_str = str(node_dot_list)
node_clusters = create_clusters(node_list)
# Create the dot clusters
cluster_info = ""
cluster_n = 1
# keep track of how they finish for chronological ordering:
ending_cluster_nodes = []
for group in node_clusters:
cluster_info += 'subgraph cluster_'+str(cluster_n) +' {\n' +\
'label = "Track ' + str(cluster_n) + '";\n' +\
'color = invis;\n'
first_node = True
for node in group:
# A straight invisible line connecting all nodes is needed to
# correctly align them.
if not first_node:
cluster_info += ' -> '
else:
first_node = False
cluster_info += str(node.get('event_n'))
# Check how a track ends
last_node_n = group[-1].get('event_n')
# The high weight is added to make a straight line of nodes
cluster_node_style = '[weight=1000]'
# Then the Concuerror was stopped by the user, a warning will show
if last_node_n >= len(node_list):
warning_image = "marmalade_warning.png"
ending_name = 'warning_' + str(cluster_n)
ending_image = warning_image
else:
ending_node = node_list[last_node_n]
if ('backtrack' in ending_node and
ending_node.get('backtrack') != 'normal'):
if ending_node.get('backtrack') == 'deadlock':
lock_image = "marmalade_lock.png"
ending_name = 'lock_' + str(cluster_n)
ending_image = lock_image
elif ending_node.get('backtrack') == 'sleep_set_block':
# warning_image = "marmalade_checkmark.png"
warning_image = "marmalade_warning.png"
ending_name = 'sleep_set_block_' + str(cluster_n)
ending_image = warning_image
elif 'error' in ending_node:
cross_image = "marmalade_cross.png"
ending_name = 'bad_' + str(cluster_n)
ending_image = cross_image
elif 'cycle' in ending_node:
cycle_image = "marmalade_cycle.png"
ending_name = 'cycle_' + str(cluster_n)
ending_image = cycle_image
else:
checkmark_image = "marmalade_checkmark.png"
ending_name = 'ok_' + str(cluster_n)
ending_image = checkmark_image
cluster_info += ' -> ' + ending_name + ' ' + cluster_node_style + ';\n'
cluster_info += ending_name +' [image="'+ending_image+'"' +\
', label="", style=invisible];\n'
cluster_info += '}\n\n'
cluster_n = cluster_n + 1
ending_cluster_nodes.append(ending_name)
# Show arrow to interleavings after a backtrack:
connection_list = "//Interleavings after backtrack\n"
explore_event_dict = dict()
for group in node_clusters:
first_event_n = group[0].get('event_n')
if first_event_n > 1:
first_explore_n = int(group[0].get('explore_n'))
prev_explore = str(first_explore_n - 1)
connection_list += str(explore_event_dict.get(prev_explore)) +\
' -> ' + str(first_event_n) + ';\n'
for node in group:
explore_n = node.get('explore_n')
event_n = node.get('event_n')
explore_event_dict[explore_n] = event_n
# Correct the graph so the track numbers are shown from left to right
chronological_ordering = ""
if len(node_clusters) > 0:
top_order_nodes = []
bottom_order_nodes = []
chronological_ordering = "\n//Chronological track ordering\n"
node_properties = 'node[shape=none, width=0, height=0, label=""];'
edge_properties = 'edge[dir=none, style=invisible];'
chronological_ordering += node_properties + '\n'
chronological_ordering += edge_properties + '\n'
for i in range(1, len(node_clusters) + 1):
top_order_nodes.append('t'+str(i))
bottom_order_nodes.append('b'+str(i))
top_rank_text = '{rank=same;'
bottom_rank_text = '{rank=same;'
for i in range(0, len(node_clusters)):
top_rank_text += top_order_nodes[i]
bottom_rank_text += bottom_order_nodes[i]
if i != len(node_clusters)-1:
top_rank_text += ','
bottom_rank_text += ','
else:
top_rank_text += '}\n'
bottom_rank_text += '}\n'
for i in range(0, len(node_clusters)):
top_rank_text += top_order_nodes[i]
bottom_rank_text += bottom_order_nodes[i]
if i != len(node_clusters)-1:
top_rank_text += ' -> '
bottom_rank_text += ' -> '
else:
top_rank_text += ';\n'
bottom_rank_text += ';\n'
# First the top hidden nodes are displayed in the dot file
chronological_ordering += top_rank_text
for i in range(0, len(node_clusters)):
chronological_ordering += top_order_nodes[i] + ' -> ' +\
str(node_clusters[i][0].get('event_n')) + ';\n'
# After, the bottom hidden nodes are displayed in the dot file
chronological_ordering += bottom_rank_text
for i in range(0, len(ending_cluster_nodes)):
chronological_ordering += str(ending_cluster_nodes[i]) + ' -> ' +\
bottom_order_nodes[i] + ';\n'
# Path where images are located:
pathname = os.path.dirname(sys.argv[0])
image_path = os.path.abspath(pathname)+'/img'
# //splines=line;//maybe?
# //splines=ortho;//maybe?
# //rankdir=LR;
# //rankdir=TB;
# //TODO: look at the following attributes:
# //TODO: rankstep
# //TODO: sep
file_info = 'digraph {\n' +\
'\n' +\
'imagepath="'+image_path+'"\n' +\
'//splines=ortho;\n' +\
'//nodesep=1;\n' +\
'//ranksep=equally;\n' +\
'//node [shape=plaintext]\n' +\
'node [shape=box]\n' +\
'//node [shape=point]\n' +\
'node [style="filled,rounded"]\n' +\
'edge [arrowhead="vee"]\n\n' +\
'// Interaction information nodes\n' +\
node_dot_list_str + '\n' +\
cluster_info +\
connection_list +\
chronological_ordering +\
'\n' +\
'}\n'
return file_info
# Show the different blocks of information and what fields they contain
def print_nodes(node_list):
n = 1
text = ""
for node in node_list:
text += ">>> Info " + str(n)+":\n"
m = 1
for k, v in node.iteritems():
if k == 'text':
continue
text += "> " + str(n)+"."+str(m) + " " + k + ': ' + str(v) + '\n'
m = m + 1
n = n + 1
return text
if __name__ == "__main__":
argLen = len(sys.argv)
if argLen == 1 or argLen > 3 or not os.path.isfile(sys.argv[1]):
print "Use: " + sys.argv[0] + " conquerror_graph_file [output_format]"
else:
filename = sys.argv[1]
f = open(filename, 'r')
node_list = file_to_node_list(f)
f.close()
debug(print_nodes(node_list))
dot_file = filename + '.dot'
f = open(dot_file, 'w')
f.write(dot_file_content(node_list))
f.close()
output_file = filename
# Default output format as pdf file
outFormat = "pdf"
if argLen == 3:
outFormat = sys.argv[2]
# TODO: Check sfdp
subprocess.call(["dot", "-T"+outFormat, "-o" +output_file+'.'+outFormat,
dot_file])
print "Finished"