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
|
#!/usr/bin/env python3
"""draw_fsm_diagram.py: Create dot code and dokuwiki table from a state transition table
convert dot code to png using graphviz:
dot fsm.dot -Tpng -o fsm.png
"""
import argparse
import re
__author__ = "Julian Oes"
def get_dot_header():
return """digraph finite_state_machine {
graph [ dpi = 300 ];
ratio = 1.5
node [shape = circle];"""
def get_dot_footer():
return """}\n"""
def main():
# parse input arguments
parser = argparse.ArgumentParser(description='Create dot code and dokuwiki table from a state transition table.')
parser.add_argument("-i", "--input-file", default=None, help="choose file to parse")
parser.add_argument("-d", "--dot-file", default=None, help="choose file for output dot file")
parser.add_argument("-t", "--table-file", default=None, help="choose file for output of table")
args = parser.parse_args()
# open source file
if args.input_file == None:
exit('please specify file')
f = open(args.input_file,'r')
source = f.read()
# search for state transition table and extract the table itself
# first look for StateTable::Tran
# then accept anything including newline until {
# but don't accept the definition (without ;)
# then extract anything inside the brackets until };
match = re.search(r'StateTable::Tran(?:.|\n!;)*\{((?:.|\n)*?)\};', source)
if not match:
exit('no state transition table found')
table_source = match.group(1)
# bookkeeping for error checking
num_errors_found = 0
states = []
events = []
# first get all states and events
for table_line in table_source.split('\n'):
match = re.search(r'/\*\s+STATE_(\w+)\s+\*/', table_line)
if match:
states.append(str(match.group(1)))
# go to next line
continue
if len(states) == 1:
match = re.search(r'/\*\s+EVENT_(\w+)\s+\*/', table_line)
if match:
events.append(str(match.group(1)))
print('Found %d states and %d events' % (len(states), len(events)))
# keep track of origin state
state = None
# fill dot code in here
dot_code = ''
# create table len(states)xlen(events)
transition_table = [[[] for x in range(len(states))] for y in range(len(events))]
# now fill the transition table and write the dot code
for table_line in table_source.split('\n'):
# get states
# from: /* STATE_NONE */
# extract only "NONE"
match = re.search(r'/\*\s+STATE_(\w+)\s+\*/', table_line)
if match:
state = match.group(1)
state_index = states.index(state)
# go to next line
continue
# can't advance without proper state
if state == None:
continue
# get event and next state
# from /* EVENT_READY_REQUESTED */ {ACTION(&Navigator::start_ready), STATE_READY}
# extract "READY_REQUESTED" and "READY" if there is ACTION
match_action = re.search(r'/\*\s+EVENT_(\w+)\s+\*/\s+\{ACTION\((?:.|\n)*STATE_(\w+)', table_line)
# get event and next state
# from /* EVENT_NONE_REQUESTED */ {NO_ACTION, STATE_NONE},
# extract "NONE_REQUESTED" and "STATE_NONE" if there is NO_ACTION
match_no_action = re.search(r'/\*\s+EVENT_(\w+)\s+\*/\s+\{NO_ACTION(?:.|\n)*STATE_(\w+)', table_line)
# ignore lines with brackets only
if match_action or match_no_action:
# only write arrows for actions
if match_action:
event = match_action.group(1)
new_state = match_action.group(2)
dot_code += ' ' + state + ' -> ' + new_state + '[ label = "' + event + '"];\n'
elif match_no_action:
event = match_no_action.group(1)
new_state = match_no_action.group(2)
# check for state changes without action
if state != new_state:
print('Error: no action but state change:')
print('State: ' + state + ' changed to: ' + new_state)
print(table_line)
num_errors_found += 1
# check for wrong events
if event not in events:
print('Error: unknown event: ' + event)
print(table_line)
num_errors_found += 1
# check for wrong new states
if new_state not in states:
print('Error: unknown new state: ' + new_state)
print(table_line)
num_errors_found += 1
# save new state in transition table
event_index = events.index(event)
# bold for action
if match_action:
transition_table[event_index][state_index] = '**' + new_state + '**'
else:
transition_table[event_index][state_index] = new_state
# assemble dot code
dot_code = get_dot_header() + dot_code + get_dot_footer()
# write or print dot file
if args.dot_file:
f = open(args.dot_file,'w')
f.write(dot_code)
print('Wrote dot file')
else:
print('##########Dot-start##########')
print(dot_code)
print('##########Dot-end############')
# assemble doku wiki table
table_code = '| ^ '
# start with header of all states
for state in states:
table_code += state + ' ^ '
table_code += '\n'
# add events and new states
for event, row in zip(events, transition_table):
table_code += '^ ' + event + ' | '
for new_state in row:
table_code += new_state + ' | '
table_code += '\n'
# write or print wiki table
if args.table_file:
f = open(args.table_file,'w')
f.write(table_code)
print('Wrote table file')
else:
print('##########Table-start########')
print(table_code)
print('##########Table-end##########')
# report obvous errors
if num_errors_found:
print('Obvious errors found: %d' % num_errors_found)
else:
print('No obvious errors found')
if __name__ == '__main__':
main()
|