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
298
299
300
301
302
303
304
305
|
#!/usr/bin/env python3
# vim:set et sw=2 ts=2 tw=80:
import argparse
import re
TINA_TRANSITION_REGEX = re.compile('tr t([0-9]+) : {(.*)} \[0,w\[ (.*) -> (.*)')
TINA_PLACE_W_TOKEN_REGEX = re.compile('pl p([0-9]+) : .* \(([0-9]+)\)')
TINA_PLACE_TOKEN_REGEX = re.compile('pl p([0-9]+) : .*')
TINA_NET_REGEX = re.compile('net (.*)')
CONDITION_OP = dict()
CONDITION_OP[''] = 0
CONDITION_OP['='] = 1
CONDITION_OP['<='] = 2
CONDITION_OP['>='] = 3
CONDITION_OP['<'] = 4
CONDITION_OP['>'] = 5
CONDITION = dict()
# (ID, MIN_VALUE, MAX_VALUE)
# MIN_VALUE and MAX_VALUE are included in the acceptance range.
CONDITION[''] = (0, 0, 0)
CONDITION['PRISE'] = (1, 0, 1)
CONDITION['LUMIERE1'] = (3, 0, 100)
CONDITION['FIN_MVMT'] = (11, 0, 1)
CONDITION['ACTION_EN_COURS'] = (12, 0, 1)
CONDITION['DERNIER_WP'] = (13, 0, 1)
CONDITION['BT_1'] = (14, 0, 1)
CONDITION['BT_2'] = (15, 0, 1)
CONDITION['BT_3'] = (16, 0, 1)
CONDITION['BT_4'] = (17, 0, 1)
ACTION = dict()
# (ID, ACCEPTS_PARAM)
ACTION[''] = (0, False)
ACTION['LEVER_BRAS'] = (7, False)
ACTION['BAISSER_BRAS'] = (8, False)
ACTION['BIPER'] = (11, False)
ACTION['EXIT'] = (19, False)
ACTION['INIT_NAVE'] = (20, False)
ACTION['GO_BASE'] = (22, False)
ACTION['GO_LIVRAISON'] = (23, False)
ACTION['GO_LAST_RD_WP'] = (24, False)
ACTION['GO_NEXT_WP'] = (25, False)
ACTION['ETALON_CAPT_LUMIERE'] = (26, False)
ACTION['ENVOI_BT'] = (18, True)
def parse_translation_table (tt_file):
tt = dict()
for line in tt_file:
data = line.replace('\n','').replace('\r', '').split("::")
if (len(data) == 2): # Action
if (data[0] in tt):
print('[W] Multiple definitions for symbol "' + data[0] + '"')
if (data[1] not in ACTION):
print('[E] Unsupported action: "' + data[1] + '" in translation table.')
exit(-1)
(act_id, act_param) = ACTION[data[1]]
if (act_param):
print(
'[E] "'
+ data[0]
+ '" uses "'
+ data[1]
+ '" without a parameter'
)
exit(-1)
tt[data[0]] = ('action', act_id)
elif (len(data) == 3): # Action with param
if (data[0] in tt):
print('[W] Multiple definitions for symbol "' + data[0] + '"')
if (data[1] not in ACTION):
print('[E] Unsupported action: "' + data[1] + '" in translation table.')
exit(-1)
(act_id, act_param) = ACTION[data[1]]
if (not act_param):
print('[E] "' + data[0] + '" uses "' + data[1] + '" with a parameter')
exit(-1)
tt[data[0]] = ('action_w_param', (act_id, data[2]))
elif (len(data) == 4): # Condition
if (data[0] in tt):
print('[W] Multiple definitions for symbol "' + data[0] + '"')
if (data[1] not in CONDITION):
print(
'[E] Unsupported condition: "'
+ data[1]
+ '" in translation table.'
)
exit(-1)
(cond_id, cond_min, cond_max) = CONDITION[data[1]]
if (not data[3].isdigit()):
print(
'[E] "'
+ data[0]
+ '" uses "'
+ data[3]
+ '" as if it was a positive integer.'
)
exit(-1)
cond_val = int(data[3])
if ((cond_val < cond_min) or (cond_val > cond_max)):
print(
'[E] Value "'
+ data[3]
+ '" in translation table entry "'
+ data[0]
+ '" is outside of the expected range (['
+ cond_min
+ ', '
+ cond_max
+ ']).'
)
exit(-1)
if (data[2] not in CONDITION_OP):
print(
'[E] "'
+ data[0]
+ '" uses an unknown operator: "'
+ data[2]
+ '".'
)
exit(-1)
tt[data[0]] = ('condition', (cond_id, CONDITION_OP[data[2]], cond_val))
else:
print('[W] Ignored invalid Translation Table entry: "' + line + '"')
return tt
def handle_link (link):
return (link if '*' in link else (link + '*1')).replace('p', 'X')
def handle_condition (cond, tt):
if (cond not in tt):
print('[E] Token "' + cond + '" not defined in transition table.')
exit(-1)
(d_type, data) = tt[cond]
if (d_type != 'condition'):
print('[E] Token "' + cond + ' is incorrectly used as a condition.')
exit(-1)
(c_id, c_op, c_val) = data
return (str(c_id) + ',' + str(c_op) + ',' + str(c_val))
def handle_action (act, tt):
if (act not in tt):
print('[E] Token "' + act + '" not defined in transition table.')
exit(-1)
(d_type, data) = tt[act]
if (d_type != 'action' and d_type != 'action_w_param'):
print('[E] Token "' + cond + ' is incorrectly used as an action.')
exit(-1)
if (d_type == 'action'):
a_id = data
a_val = 0
else:
(a_id, a_val) = data
return (str(a_id) + ',' + str(a_val))
def handle_transition (transition_id, label, inputs, outputs, tt):
label = label.split('/')
inputs = inputs.split(' ')
outputs = outputs.split(' ')
conditions = label[0].split(',')
actions = label[1].split(',')
if ('' in conditions):
conditions.remove('')
if ('' in actions):
actions.remove('')
if (len(conditions) > 4):
print('[E] Transition "' + transition_id + '" has too many conditions.')
exit(-1)
if (len(actions) > 4):
print('[E] Transition "' + transition_id + '" has too many actions.')
exit(-1)
result = 't' + transition_id + ':'
result += ','.join([handle_link(input_link) for input_link in inputs])
if (len(conditions) == 0):
result += ';?'
else:
result += ';' + '/'.join([handle_condition(cond, tt) for cond in conditions])
result += ';' + ','.join([handle_link(output_link) for output_link in outputs])
if (len(actions) == 0):
result += ';?'
else:
result += ';' + '/'.join([handle_action(act, tt) for act in actions])
# We don't support priorities
result += ';1'
return result
def convert_tina_net (net_file, tt):
first_line = "1,0"
result = ""
places = dict()
for line in net_file:
line = line.replace('\r','').replace('\n','')
if (line == ''):
continue
matched = TINA_TRANSITION_REGEX.search(line)
if (matched):
result += '\n' + (
handle_transition(
matched.group(1),
matched.group(2),
matched.group(3),
matched.group(4),
tt
)
)
continue
matched = TINA_PLACE_W_TOKEN_REGEX.search(line)
if (matched):
places[int(matched.group(1))] = matched.group(2)
continue
matched = TINA_PLACE_TOKEN_REGEX.search(line)
if (matched):
places[int(matched.group(1))] = str(0)
continue
matched = TINA_NET_REGEX.search(line)
if (matched):
continue
print('[P] Program does not understand Tina NET line: "' + line + '".')
print('[P] If this was unexpected, please inform the developer.')
exit(-1)
first_line = (
str(len(places) + 1)
+ ','
+ ','.join([places[i] for i in range(0,len(places))])
) + ',0'
return (first_line + result)
parser = argparse.ArgumentParser(
description='Converts a Tina NET file into a RDP one, using a translation table.'
)
parser.add_argument(
'net_file',
type=argparse.FileType(mode='r', encoding='UTF-8'),
help='The Tinal NET file'
) # required to deal with N -> N transitions.
parser.add_argument(
'translation_table',
type=argparse.FileType(mode='r', encoding='UTF-8'),
help='The translation table file'
)
parser.add_argument(
'output_file',
type=argparse.FileType(mode='w', encoding='UTF-8'),
help='The output RDP file.'
)
args = parser.parse_args()
translation_table = parse_translation_table(args.translation_table)
converted_tn = convert_tina_net(args.net_file, translation_table)
#print("Result:")
print(converted_tn)
|