-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflattener.py
executable file
·813 lines (532 loc) · 23.2 KB
/
flattener.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#!/usr/bin/python
'''
This is the ANML flattener. It accepts a hierarchical ANML file using
macros, and returns a flattened version using only elements.
*So far only support STEs, Counters, and Inverters
usage: flattener.py <input anml filename> <output anml filename>
Author: Tom Tracy II ([email protected])
26 March 2016
v1.7
UPDATE
======
#Added this to allow flattener to work with latest version of ANML
if not macro_use.endswith('_macro.anml'):
macro_use += '_macro.anml'
'''
import sys
import xml.etree.ElementTree as ET
import copy
import os.path
VERBOSITY = False
STEP_THROUGH = False
reference_addresses = {}
delimiter = "___"
# Flatten the macro
def flatten(root, macro_subtree, macro, parent_id, connection_dictionary):
macro_id, macro_use, activations, substitutions = grab_macro_details(macro)
# The new id is (path-to-macro)<delimiter>(macro id)
new_id = parent_id + delimiter + macro_id
if VERBOSITY:
print "new_id = ", new_id
try:
if VERBOSITY:
print "Parsing: ", macro_use
# Added this to allow flattener to work with latest version of ANML
if not macro_use.endswith('.anml'):
macro_use += '_macro.anml'
macro_subtree = ET.parse(macro_use).getroot()
except IOError:
print "Failed to open Macro: ", macro_use
exit()
# Pause execution
if STEP_THROUGH:
raw_input("Press Enter to continue...")
header = macro_subtree.find('header')
inner_inter = header.find('interface-declarations')
inner_params = header.find('parameter-declarations')
inner_interface_declarations = None
inner_parameters = None
if inner_inter is not None:
if VERBOSITY:
print "Found a <interface-declarations> tag in the macro header"
inner_interface_declarations = grab_inner_declarations(inner_inter)
if inner_params is not None:
if VERBOSITY:
print "Found a <interface-declarations> tag in the macro header"
inner_parameters = grab_inner_parameters(inner_params)
if VERBOSITY:
print "Done with macro header; removing from the subtree"
if STEP_THROUGH:
raw_input("Press Enter to continue...")
# Strip out the header; we're done here
macro_subtree.remove(header)
if inner_parameters is not None:
# If default value not over-ridden, add to substitutions
for item in inner_parameters:
if item not in substitutions:
if VERBOSITY:
print item, " is not over-ridden, so add to substitutions"
substitutions[item] = inner_parameters[item]
if VERBOSITY:
print "Updated substitutions: ", substitutions
if STEP_THROUGH:
raw_input("Press Enter to continue...")
body = macro_subtree.find('body')
port_defs = body.find('port-definitions')
# Grab the port definitions
ports_in, ports_out = grab_port_definitions(port_defs, new_id)
if VERBOSITY:
print "Done with port defs; removing from the subtree"
if STEP_THROUGH:
raw_input("Press Enter to continue...")
# Strip out the port-defs; we're done here
body.remove(port_defs)
# Merge dictionaries
connection_dictionary.update(ports_in)
if VERBOSITY:
print "Connection Dictionary Updated with ports_ins:",
connection_dictionary
if VERBOSITY:
print "Time to replace Substitutions"
replace_substitutions(body, substitutions)
if VERBOSITY:
print "Done replacing Substitutions"
if STEP_THROUGH:
raw_input("Press Enter to continue...")
# Activation tags for different elements
activation_dictionary = {
'state-transition-element': 'activate-on-match',
'counter': 'activate-on-target',
'inverter': 'activate-on-high'}
if VERBOSITY:
print "Time to scan the body"
# For elements in the body
for child in body:
if child.tag == 'macro-reference':
if VERBOSITY:
print "Found a macro reference; time to flatten:"
root, connection_dictionary = flatten(root, macro_subtree, child, new_id, connection_dictionary)
elif child.tag in ['state-transition-element', 'counter', 'inverter']:
if VERBOSITY:
print "Found an STE/COUNTER/INVERTER"
# Update the id of the element
temp_id = new_id + delimiter + child.attrib['id']
child.set('id', temp_id)
if VERBOSITY:
print "Looking for temp_id = ", temp_id
if temp_id in ports_out:
if VERBOSITY:
print "Found temp_id in ports_out"
for out_connection in ports_out[temp_id]:
if VERBOSITY:
print "ports out:", ports_out[temp_id]
print "activations: ", activations
if len(activations.keys()) > 0:
if VERBOSITY:
print out_connection
# Get the port
out_port = out_connection.split(delimiter)[-1]
print "Got out port: ", out_port
# We're inside the macro, but our macro is linking out, so outside the macro
for activation in activations[out_port]:
activation_string = activation_dictionary[child.tag]
temp_element = ET.Element(activation_string)
if activation[1] is not None:
print "Found a port in activation[1]"
if activation[1] is not 'cnt':
link_to = parent_id + delimiter + activation[0] + delimiter + activation[1]
else:
link_to = parent_id + delimiter + activation[0] + ":" + activation[1]
else:
print "Couldnt find a port in activation[1]"
link_to = parent_id + delimiter + activation[0]
if VERBOSITY:
print "Created new temp element to link to: ",
link_to
temp_element.set('element', link_to)
child.append(temp_element)
root.append(child)
else:
for link in child.findall('activate-on-match'):
new_value = new_id + delimiter + link.attrib['element']
link.set('element', new_value)
root.append(child)
else:
print "Oh shit; we found something we shouldnt have"
print child.tag
exit()
else:
if VERBOSITY:
print "We have no macros!"
print "There are no macro-references in ", parent_id
return root, connection_dictionary
def grab_activations(activate_out):
# Empty dictionary of activations
activations = {}
for a_f_m in activate_out.findall('activate-from-macro'):
source = a_f_m.attrib['source']
element = a_f_m.attrib['element'].split(':')
element_id = element[0]
if VERBOSITY:
print "----------"
print "Found <activate-from-macro> tag"
print "source = ", source, ", element = ",
element, ", element_id = ", element_id
if len(element) == 2:
element_port = element[1]
if VERBOSITY:
print "Found an element port = ", element_port
else:
element_port = None
if VERBOSITY:
print "Could not find an element port"
destination = (element_id, element_port)
if source not in activations:
activations[source] = [destination, ]
if VERBOSITY:
print "Destination not found in activations; adding"
else:
activations[source].append(destination)
if VERBOSITY:
print "Found Activation in Activations; appending Destination"
print "Activate source = ", source, " -> destination = ",
activations[source]
if VERBOSITY:
print "----------"
return activations
def grab_substitutions(substitute):
# Empty dictionary of substitutions
substitutions = {}
for replace in substitute.findall('replace'):
if VERBOSITY:
print "----------"
print "Found <replace> tag"
parameter_name = replace.attrib['parameter-name']
replace_with = replace.attrib['replace-with']
if VERBOSITY:
print "parameter-name = ", parameter_name
print "replace_with = ", replace_with
if replace_with:
substitutions[parameter_name] = replace_with
print "Adding to subtitutions dictionary"
else:
print "ERROR: Found a parameter... but we're not replacing... huh"
if VERBOSITY:
print "----------"
return substitutions
def grab_inner_parameters(parameter_declarations):
inner_parameters = {}
for child in parameter_declarations:
inner_parameters[child.attrib['parameter-name']] = child.attrib['default-value']
if VERBOSITY:
print "Adding parameter-name = ", child.attrib['parameter-name'],
", default-value = ", child.attrib['default-value'],
"to inner parameters dictionary"
if VERBOSITY:
print "Found Inner Parameters: ", inner_parameters
return inner_parameters
def grab_inner_declarations(inner_interface):
inner_interface_declarations = {}
for child in inner_interface:
inner_interface_declarations[child.attrib['id']] = child.attrib['type']
if VERBOSITY:
print "Adding child id = ", child.attrib['id'], ", type = ",
child.attrib['type'], "to inner interface declations"
if VERBOSITY:
print "Found Inner Interface Declarations: ",
inner_interface_declarations
return inner_interface_declarations
def grab_port_definitions(port_defs, new_id):
ports_in = {}
ports_out = {}
if VERBOSITY:
print "----------"
print "Time to grab the port definitions"
# Iterate through all ports in port definitions
for child in port_defs:
# If a port-in
if child.tag == 'port-in':
if VERBOSITY:
print "Found a <port-in>"
print "-----------------"
for element in child:
# Macro_id + external port name
activate_from_name = new_id + delimiter + child.attrib['id']
# Macro_id + STE name
new_ste_name = new_id + delimiter + element.attrib['element']
if VERBOSITY:
print "From port: ", activate_from_name
print "New Element Name: ", new_ste_name
# Another hack to disable translation for counter
if (':cnt' not in new_ste_name) and (':rst' not in new_ste_name):
new_ste_name = new_ste_name.replace(':', delimiter)
else:
if VERBOSITY:
print "Found a :cnt or :rst port!", new_ste_name
if VERBOSITY:
print "Active from: ", activate_from_name
print "New Element Name: ", new_ste_name
if activate_from_name in ports_in:
ports_in[activate_from_name].append(new_ste_name)
else:
ports_in[activate_from_name] = [new_ste_name, ]
print "Updated ports_in: ", ports_in
# If a port-out
# Does the dictionary value also include the macro id?
#
elif child.tag == 'port-out':
if VERBOSITY:
print "Found a <port-out>"
print "------------------"
for element in child:
new_ste_name = new_id + delimiter + element.attrib['element']
to_port = new_id + delimiter + child.attrib['id']
if VERBOSITY:
print "New STE Name: ", new_ste_name
print "To Port: ", to_port
if new_ste_name in ports_out:
ports_out[new_ste_name].append(to_port)
else:
ports_out[new_ste_name] = [to_port, ]
if VERBOSITY:
print "Updated ports_out: ", ports_out
print "----------"
return ports_in, ports_out
def replace_substitutions(body, substitutions):
for child in body:
if 'symbol-set' in child.attrib:
if child.attrib['symbol-set'] in substitutions:
if VERBOSITY:
print "Replacing ", child.attrib['symbol-set'], " with ",
substitutions[child.attrib['symbol-set']]
child.set('symbol-set',
substitutions[child.attrib['symbol-set']])
if VERBOSITY:
print child.tag, "...", child.attrib
return 0
def grab_macro_details(macro):
activations = {}
substitutions = {}
macro_id = macro.attrib['id']
macro_use = macro.attrib['use']
if VERBOSITY:
print "Grabbing macro details for (id =", macro_id, ", use =",
macro_use, ")"
# Replace macro_use with actual address
if macro_use in reference_addresses:
macro_use = reference_addresses[macro_use]
if VERBOSITY:
print "Replacing macro_use = ", macro_use,
" with reference address = ", macro.attrib['use']
else:
print "Couldnt replace ", macro_use
# Not necessarily used
activate_out = macro.find('activate-out')
subs = macro.find('substitutions')
# Grab outward activations
if activate_out is not None:
if VERBOSITY:
print "Found <activate-out> tags; time to grab activations"
activations = grab_activations(activate_out)
# Grab substitutions
if subs is not None:
if VERBOSITY:
print "Found <substitutions> tags; time to grab substitutions"
substitutions = grab_substitutions(subs)
if VERBOSITY:
print ""
print "Macro id: ", macro_id, " Macro use: ", macro_use
print "Activations: ", activations
print "Substitutions: ", substitutions
print "-------------"
return macro_id, macro_use, activations, substitutions
# Print children
def print_children(root):
for child in root:
print child.tag, child.attrib
return
# Load a library of macro definitions
def load_library(library):
if VERBOSITY:
print "Loading Library Dictionary"
print "----------"
library_dictionary = {}
library_filename = library.attrib['ref'].strip()
if not os.path.isfile(library_filename):
print "Error: %s cannot be found as a valid library file" % library_filename
exit()
else:
if VERBOSITY:
print "Loading Dictionary of Macro Definitions"
library_tree = ET.parse(library_filename)
root = library_tree.getroot()
library_defs = root.findall('library-definition')
for library_def in library_defs:
library_id = library_def.attrib['id']
for include_macro in library_def.findall('include-macro'):
macro_ref = include_macro.attrib['ref']
key = library_id + '.' + macro_ref.split('.')[0]
library_dictionary[key] = macro_ref
if VERBOSITY:
print key + " -> ", macro_ref
if VERBOSITY:
print "----------"
print "Done loading dictionary"
return library_dictionary
# Main()
if __name__ == "__main__":
# Verify argument count
if len(sys.argv) != 3 or ['-h', '--help'] in sys.argv:
print "Usage: flattener.py <input anml> <flattened output>"
exit()
else:
# Grab input and output file names
input_filename = sys.argv[1]
output_filename = sys.argv[2]
# Parse the root XML file
try:
if VERBOSITY:
print "Parsing input file: ", input_filename
tree = ET.parse(input_filename)
except IOError:
print "Failed to open Input Filename: ", input_filename
exit()
# Grab the root node
root = tree.getroot()
# --------------------------------------------------------------------------
# If include-macros is defined at root level (older version of ANML)
include_macros = root.findall('include-macro')
if len(include_macros) > 0:
if VERBOSITY:
print "Found <include-macro> tag"
for include_macro in include_macros:
macro_filename = include_macro.attrib['ref']
if not os.path.isfile(macro_filename):
print "ERROR: %s cannot be found as a valid macro " % macro_filename
exit()
else:
reference_key = (macro_filename[0:macro_filename.find('_macro.anml')]).strip()
reference_addresses[reference_key] = macro_filename
# We don't need it anymore
root.remove(include_macro)
else:
if VERBOSITY:
print "Could not find <include-macro> tag"
# -----------------------------------------------------------------------------
# If include-library is defined at the root level (ANML v1.0)
include_library = root.find('include-library')
if include_library is not None:
if VERBOSITY:
print "Found <include-library> tag"
reference_addresses = load_library(include_library)
root.remove(include_library)
else:
if VERBOSITY:
print "Could not find <include-library> tag"
# Pause execution
if STEP_THROUGH:
raw_input("Press Enter to continue...")
# -----------------------------------------------------------------------------
# Check if in automata level of anml
automata_network = root.find('automata-network')
if automata_network is not None:
root = automata_network
if VERBOSITY:
print "Found the <automata-network> tag and set root"
parent_id = 'root'
# Empty dictionary to populate with new names and connection
connection_dictionary = {}
if VERBOSITY:
print "Iterate through all <macro-reference> tags"
# Iterate through each macro
for macro in root.findall('macro-reference'):
if VERBOSITY:
print "----------"
print "Flattening: (tag=", macro.tag,
", attrib=", macro.attrib, ")"
# Flatten macro (root is root and the current subtree we're into)
root, connection_dictionary = flatten(root, root, macro, parent_id, connection_dictionary)
# We're done with the macro; remove it
root.remove(macro)
if VERBOSITY:
print " Done Flattening: ", macro.tag
print "----------"
# -----------------------------------------------------------------------------
if VERBOSITY:
print "Done with all of the macros."
print "Connection Dictionary: ", connection_dictionary
# Pause execution
if STEP_THROUGH:
raw_input("Press Enter to continue...")
# Activation tags for different elements
activation_dictionary = {
'state-transition-element': 'activate-on-match',
'counter': 'activate-on-target',
'inverter': 'activate-on-high'}
if VERBOSITY:
print "Now time to go through child elements"
# print all children of root
for child in root:
# ---------------------------------------------------------------------
# If any root elements have not been translated
if 'id' in child.attrib:
# We haven't prefixed then name with root yet
if 'root' not in child.attrib['id']:
child.set('id', 'root' + delimiter + child.attrib['id'])
if VERBOSITY:
print "Converting id = ", child.attrib['id']
if child.tag in activation_dictionary:
activation_string = activation_dictionary[child.tag]
if VERBOSITY:
print "Found an activation for the child: ",
activation_string
else:
print "Could not find child in the activation dictionary: ",
child.attrib, child.tag
continue
# ----------------------------------------------------------------------
activation_links = child.findall(activation_string)
if VERBOSITY:
print "Find all links with activation string = ",
activation_string
print "Activation Links: ", activation_links
for link in activation_links:
if 'root' not in link.attrib['element']:
old_value = "root" + delimiter + link.attrib['element']
else:
old_value = link.attrib['element']
if VERBOSITY:
print "Old value: ", old_value
# If old element value used ':', substitute for '_'; but not for counter
if (':cnt' not in old_value) and (':rst' not in old_value):
old_value = old_value.replace(':', delimiter)
if VERBOSITY:
print "This element is a: ", child.tag
print "Found that child is not counter, so replace : with delimiter"
print old_value
else:
if VERBOSITY:
print "Found a counter! (tag: ", child.tag
if old_value in connection_dictionary:
# Make shallow copy for this link; may need several times
dests = copy.copy(connection_dictionary[old_value])
if VERBOSITY:
print "Found destinations in connection dictionary: ",
dests
if dests:
link.set('element', dests.pop(0))
while len(dests) > 0:
temp_link = copy.copy(link)
temp_link.set('element', dests.pop(0))
child.append(temp_link)
print "Adding link to child. Link: ",
temp_link, ", child: ", child
else:
link.set('element', old_value)
else:
if VERBOSITY:
print old_value, " not in the dictionary"
link.set('element', old_value)
if STEP_THROUGH:
raw_input("Press Enter to continue...")
tree.write(output_filename)