-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (44 loc) · 1.44 KB
/
main.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
import collections
import pydot_ng as pydot
from fsmPygen import *
from fsmClasses import *
graph = pydot.graph_from_dot_file('data/graph2.gv')
dot_edges = graph.get_edges()
def parse_data(edges_p):
states = {}
edges = []
for e in edges_p:
s, d, l = e.get_source(), e.get_destination(), e.get_label()
start = states[s] = states.get(s, State(s))
end = states[d] = states.get(d, State(d))
edge = Edge(start, end, l)
start.add_edge(edge)
edges.append(edge)
return states.values(), edges
def generate_state_table(states, edges):
d = collections.defaultdict(dict)
en = set([e.label for e in edges])
for s in states:
for e in en:
d[s][e] = s
for e in edges:
d[e.start][e.label] = e.end
return d
def create_file(states, events, table):
cfile = create_events(events) + "\n"
cfile += create_states(states) + "\n"
cfile += create_fsm_table(table) + "\n"
cfile += 'unsigned long currentMillis, previousMillis = 0, interval;\n\n'
cfile += create_poll() + "\n"
cfile += create_actions(states) + "\n"
cfile += create_eval_state(states) + "\n"
cfile += create_setup(states[0]) + "\n"
cfile += create_loop() + "\n"
return cfile
def main():
s, e = parse_data(dot_edges)
t = generate_state_table(s, e)
with open('test.c', 'w') as f:
f.write(create_file(s, e, t))
if __name__ == '__main__':
main()