-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSaveFile.py
More file actions
163 lines (139 loc) · 6.07 KB
/
SaveFile.py
File metadata and controls
163 lines (139 loc) · 6.07 KB
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
from LoadFile import InitialStateSet
import xml.etree.ElementTree as ET
from AutoTile import System
from UniversalClasses import State
from UniversalClasses import AffinityRule
from UniversalClasses import TransitionRule
def main(system, fileName):
# The following variables are lists from the system
temp = str(system.returnTemp())
states = system.returnStates()
initial_states = system.returnInitialStates()
seed_assembly = system.returnSeedStates()
seed_states = system.returnSeedStates()
# The following variables are dictionaries from the system
vertical_affinities = system.returnVerticalAffinityDict()
horizontal_affinities = system.returnHorizontalAffinityDict()
vertical_transitions = system.returnVerticalTransitionDict()
horizontal_transitions = system.returnHorizontalTransitionDict()
tiles = system.returnTiles()
# Establish root and add temperature value
root = ET.Element("System")
root.set('Temp', temp)
# Add all states used in the system
all_states_tag = ET.Element("AllStates")
root.append(all_states_tag)
for state in states:
label = state.returnLabel()
color = state.returnColor()
display_label = state.returnDisplayLabel()
display_label_color = state.returnDisplayLabelColor()
display_label_font = state.returnDisplayLabelFont()
state_tag = ET.SubElement(all_states_tag, "State")
state_tag.set('Label', label)
state_tag.set('Color', color)
state_tag.set('DisplayLabel', display_label)
state_tag.set('DisplayLabelFont', display_label_font)
state_tag.set('DisplayLabelColor', display_label_color)
# Add all inital states
initial_states_tag = ET.Element("InitialStates")
root.append(initial_states_tag)
for state in initial_states:
label = state.returnLabel()
color = state.returnColor()
display_label = state.returnDisplayLabel()
display_label_color = state.returnDisplayLabelColor()
display_label_font = state.returnDisplayLabelFont()
state_tag = ET.SubElement(initial_states_tag, "State")
state_tag.set('Label', label)
state_tag.set('Color', color)
state_tag.set('DisplayLabel', display_label)
state_tag.set('DisplayLabelFont', display_label_font)
state_tag.set('DisplayLabelColor', display_label_color)
# Add all seed states
seed_states_tag = ET.Element("SeedStates")
root.append(seed_states_tag)
for state in seed_states:
label = state.returnLabel()
color = state.returnColor()
display_label = state.returnDisplayLabel()
display_label_color = state.returnDisplayLabelColor()
display_label_font = state.returnDisplayLabelFont()
state_tag = ET.SubElement(seed_states_tag, "State")
state_tag.set('Label', label)
state_tag.set('Color', color)
state_tag.set('DisplayLabel', display_label)
state_tag.set('DisplayLabelFont', display_label_font)
state_tag.set('DisplayLabelColor', display_label_color)
# Add all Tiles
tiles_tag = ET.Element("Tiles")
root.append(tiles_tag)
for tile in tiles:
label = tile.returnLabel()
color = tile.returnColor()
x = str(tile.getX())
y = str(tile.getY())
display_label = tile.returnDisplayLabel()
display_label_color = tile.returnDisplayLabelColor()
display_label_font = tile.returnDisplayLabelFont()
state_tag = ET.SubElement(tiles_tag, "Tile")
state_tag.set('Label', label)
state_tag.set('Color', color)
state_tag.set('x', x)
state_tag.set('y', y)
state_tag.set('DisplayLabel', display_label)
state_tag.set('DisplayLabelFont', display_label_font)
state_tag.set('DisplayLabelColor', display_label_color)
# Add vertical transition rules
vertical_transitions_tag = ET.Element("VerticalTransitions")
root.append(vertical_transitions_tag)
for key, value in vertical_transitions.items():
label1 = key[0]
label2 = key[1]
for i in range(0, len(value), 2):
label1Final = value[i]
label2Final = value[i + 1]
rule_tag = ET.SubElement(vertical_transitions_tag, "Rule")
rule_tag.set('Label1', label1)
rule_tag.set('Label2', label2)
rule_tag.set('Label1Final', label1Final)
rule_tag.set('Label2Final', label2Final)
# Add horizontal transition rules
horizontal_transitions_tag = ET.Element("HorizontalTransitions")
root.append(horizontal_transitions_tag)
for key, value in horizontal_transitions.items():
label1 = key[0]
label2 = key[1]
for i in range(0, len(value), 2):
label1Final = value[i]
label2Final = value[i + 1]
rule_tag = ET.SubElement(horizontal_transitions_tag, "Rule")
rule_tag.set('Label1', label1)
rule_tag.set('Label2', label2)
rule_tag.set('Label1Final', label1Final)
rule_tag.set('Label2Final', label2Final)
# Add vertical affinity rules
vertical_affinities_tag = ET.Element("VerticalAffinities")
root.append(vertical_affinities_tag)
for key, value in vertical_affinities.items():
label1 = key[0]
label2 = key[1]
strength = str(value)
rule_tag = ET.SubElement(vertical_affinities_tag, "Rule")
rule_tag.set('Label1', label1)
rule_tag.set('Label2', label2)
rule_tag.set("Strength", strength)
# Add horizontal affinity rules
horizontal_affinities_tag = ET.Element("HorizontalAffinities")
root.append(horizontal_affinities_tag)
for key, value in horizontal_affinities.items():
label1 = key[0]
label2 = key[1]
strength = str(value)
rule_tag = ET.SubElement(horizontal_affinities_tag, "Rule")
rule_tag.set('Label1', label1)
rule_tag.set('Label2', label2)
rule_tag.set("Strength", strength)
tree = ET.ElementTree(root)
with open(fileName[0], "wb") as file:
tree.write(file, encoding='utf-8', xml_declaration=True)