-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-stg
157 lines (111 loc) · 3.52 KB
/
generate-stg
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#########################################
# Mattia Corradi - Dalla Chiara Michele #
#########################################
import graphviz as gv
import functools
import sys
import os
def print_red(skk): print("\033[91m {}\033[00m" .format(skk))
def print_green(skk): print("\033[92m {}\033[00m" .format(skk))
def add_nodes(graph, nodes):
for n in nodes:
if isinstance(n, tuple):
graph.node(n[0], **n[1])
else:
graph.node(n)
return graph
def add_edges(graph, edges):
for e in edges:
if isinstance(e[0], tuple):
graph.edge(*e[0], **e[1])
else:
graph.edge(*e)
return graph
def parse_blif():
try:
with open(blifFileName) as f:
try:
lines = f.read().splitlines()
start_pos = None
stop_pos = None
for i in range(len(lines)):
if lines[i] == "#STG-START":
start_pos = i+1
if lines[i] == "#STG-STOP":
stop_pos = i-1
if start_pos is None:
print_red("Manca il marcatore #STG-START. Assicurati di metterlo nella riga precedente alle transizioni")
os._exit(1)
if stop_pos is None:
print_red("Manca il marcatore #STG-STOP. Assicurati di metterlo nella riga successiva alle transizioni")
os._exit(1)
lines = lines[start_pos:stop_pos]
lines = [x for x in lines if x != '']
lines = [x for x in lines if not x.startswith('#')]
edges = []
nodes = []
for line in lines:
line = line.split(' ')
nodes.append(line[1])
nodes.append(line[2])
line = (line[1], line[2])
edges.append(line)
nodes = set(nodes)
edges = set(edges)
return {'edges': edges, 'nodes': nodes}
except :
print_red("Il file è in un formato sbagliato")
os._exit(1)
except :
print_red("Il file non esiste")
os._exit(1)
def apply_styles(graph, styles):
graph.graph_attr.update(
('graph' in styles and styles['graph']) or {}
)
graph.node_attr.update(
('nodes' in styles and styles['nodes']) or {}
)
graph.edge_attr.update(
('edges' in styles and styles['edges']) or {}
)
return graph
def render_graph():
data = parse_blif()
edges = data['edges']
nodes = list(data['nodes'])
#nodes.sort()
g = add_edges(
add_nodes(digraph(), nodes),
edges
)
g = apply_styles(g, styles)
g.render('fsm')
try:
os.remove('fsm')
except OSError:
pass
print_green("STG generata correttamente!")
styles = {
'graph': {
'fontsize': '16',
'fontcolor': 'black',
'rankdir': 'BT',
'splines': 'true',
'overlap': 'false'
},
'edges': {
'arrowsize': '.5'
}
}
digraph = functools.partial(gv.Digraph, format='png')
if len(sys.argv) == 1:
print_red("Non hai messo come parametro il nome del file blif")
exit(0)
if len(sys.argv) > 2:
print_red("Hai messo troppi parametri!. Serve solo il nome del file blif che contiene la FSM")
exit(0)
blifFileName = sys.argv[1]
render_graph()