-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
297 lines (255 loc) · 10.2 KB
/
utils.py
File metadata and controls
297 lines (255 loc) · 10.2 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
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
#
# Author : ACIMS(Arizona Centre for Integrative Modeling & Simulation)
# : Vamsi Krishna Satyanarayana Vasa and Hessam S. Sarjoughian
# Version : 1.0
# Date : 06-21-25
#
import re
import xml.etree.ElementTree as ET
from xml.dom import minidom
# Convert to string and pretty print
def prettify(elem):
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def parse_and_save_model_atomic(model_text, output_filename):
# Initialize the root XML element
root = ET.Element("statechart:Model", {
"xmi:version": "2.0",
"xmlns:xmi": "http://www.omg.org/XMI",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:statechart": "http://statechart/1.0"
})
# Collecting States
states = []
state_patterns = re.finditer(r'(InitialState|State|FinalState)\s*{([^}]*)}', model_text, re.DOTALL)
for match in state_patterns:
state_type = match.group(1)
state_content = match.group(2).strip()
state_data = {'Type': state_type}
name_match = re.search(r'Name\s*=\s*"([^"]+)"', state_content)
if name_match:
state_data['Name'] = name_match.group(1)
if 'OutputFunction' in state_content:
output_function = {}
for field in ['Action', 'Description', 'Guard', 'Message', 'OutputPort']:
field_match = re.search(rf'{field}\s*=\s*"([^"]*)"', state_content)
if field_match:
output_function[field] = field_match.group(1)
state_data['OutputFunction'] = output_function
states.append(state_data)
# print(states
transitions = []
transition_patterns = re.finditer(r'(Transition|InternalTransition|ExternalTransition)\s*{(.*?)}', model_text, re.DOTALL)
for match in transition_patterns:
transition_type = match.group(1)
transition_content = match.group(2).strip()
transition_data = {'Type': transition_type}
fields = [
('Action', r'Action\s*=\s*\"([^\"]+)\"'),
('Description', r'Description\s*=\s*\"([^\"]+)\"'),
('Source', r'Source\s*=\s*\"([^\"]+)\"'),
('Target', r'Target\s*=\s*\"([^\"]+)\"'),
('TimeAdvanceType', r'TimeAdvanceType\s*=\s*(\w+)'),
('TimeAdvanceValue', r'TimeAdvanceValue\s*=\s*([0-9]+\.?[0-9]*)'),
('Guard', r'Guard\s*=\s*\"([^\"]*)\"'),
('InputPort', r'InputPort\s*=\s*\"([^\"]+)\"'),
('Message', r'Message\s*=\s*\"([^\"]+)\"')
]
for field, pattern in fields:
match = re.search(pattern, transition_content)
if match:
transition_data[field] = match.group(1)
transitions.append(transition_data)
print(transitions)
# XML root setup
root = ET.Element('statechart:Model', attrib={
'xmi:version': '2.0',
'xmlns:xmi': 'http://www.omg.org/XMI',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:statechart': 'http://statechart/1.0'
})
# Track states for reference
state_refs = {}
# Add states
for i, state in enumerate(states):
if state['Type'] == 'InitialState':
elem = ET.SubElement(root, 'initialState', Name=state['Name'], Description='', outgoing=f"//@transitions.{i}")
state_refs[state['Name']] = f"//@initialState"
elif state['Type'] == 'FinalState':
elem = ET.SubElement(root, 'finalState', Name=state['Name'])
state_refs[state['Name']] = f"//@states.{i-1}"
else:
elem = ET.SubElement(root, 'states', attrib={
'xsi:type': 'statechart:State',
'tag': f"//@states.{i-1}",
'Name': state['Name']
})
if 'OutputFunction' in state:
output = ET.SubElement(elem, 'outputFunctions', OutputPort=state['OutputFunction']['OutputPort'])
ET.SubElement(output, 'Message').text = state['OutputFunction']['Message']
ET.SubElement(output, 'Action').text = state['OutputFunction']['Action']
state_refs[state['Name']] = f"//@states.{i-1}"
# Add transitions
for i, trans in enumerate(transitions):
if trans['Type'] == 'Transition':
attrib={
'xsi:type': f"statechart:{trans['Type']}",
'source': state_refs.get(trans['Source'], ''),
'target': state_refs.get(trans['Target'], '')
}
elif trans['Type'] == 'ExternalTransition':
attrib={
'xsi:type': f"statechart:{trans['Type']}",
'TimeAdvanceType': trans['TimeAdvanceType'],
'TimeAdvanceValue': trans['TimeAdvanceValue'],
'source': state_refs.get(trans['Source'], ''),
'target': state_refs.get(trans['Target'], ''),
'InputPort': trans['InputPort'],
}
else:
attrib={
'xsi:type': f"statechart:{trans['Type']}",
'TimeAdvanceType': trans['TimeAdvanceType'],
'TimeAdvanceValue': trans['TimeAdvanceValue'],
'source': state_refs.get(trans['Source'], ''),
'target': state_refs.get(trans['Target'], ''),
}
trans_elem = ET.SubElement(root, 'transitions', attrib)
if 'Action' in trans:
action_list = trans['Action'].split(',')
for action in action_list:
ET.SubElement(trans_elem, 'Action').text = action
if 'Guard' in trans:
guard_list = trans['Guard'].split(',')
for guard in guard_list:
ET.SubElement(trans_elem, 'Guard').text = guard
if 'Message' in trans:
ET.SubElement(trans_elem, 'Message').text = trans['Message']
xml_output = prettify(root)
# Save to file with proper formatting
with open(output_filename, "w", encoding="utf-8") as file:
file.write(xml_output)
def parse_and_save_model_coupled(model_text, output_filename):
# Pattern to match Coupling
coupling_pattern = r'Coupling\s*{\s*M1Name = "(?P<m1_name>.*?)"\s*M1Port = "(?P<m1_port>.*?)"\s*M2Name = "(?P<m2_name>.*?)"\s*M2Port = "(?P<m2_port>.*?)"\s*}'
# Dictionary to store the parsed data
model_dict = {
# 'AtomicModelList': [],
'CouplingList': []
}
couplings = re.findall(coupling_pattern, model_text, re.DOTALL)
for coupling in couplings:
model_dict['CouplingList'].append({
'M1Name': coupling[0],
'M1Port': coupling[1],
'M2Name': coupling[2],
'M2Port': coupling[3]
})
print(model_dict['CouplingList'])
# Create root XML element
root = ET.Element("model")
composite_model = ET.SubElement(root, "compositeModel", name = "CoupledModel")
for coupling in model_dict['CouplingList']:
ET.SubElement(composite_model, "coupling", m1name=coupling['M1Name'], m1port=coupling['M1Port'], m2name=coupling['M2Name'], m2port=coupling['M2Port'])
xml_output = prettify(root)
# Save to file with proper formatting
with open(output_filename, "w", encoding="utf-8") as file:
file.write(xml_output)
def parse_and_save_model_with_error_handling_atomic(model_text, output_filename="model_output.xml"):
try:
# Try parsing the model text into XML format
parse_and_save_model_atomic(model_text, output_filename)
return None
except Exception as e:
# Catch and display any errors encountered during parsing or saving
print(f"Failed to parse and save the model. Error: {str(e)}")
return str(e)
def parse_and_save_model_with_error_handling_coupled(model_text, output_filename="model_output.xml"):
try:
# Try parsing the model text into XML format
parse_and_save_model_coupled(model_text, output_filename)
return None
except Exception as e:
# Catch and display any errors encountered during parsing or saving
print(f"Failed to parse and save the model. Error: {str(e)}")
return str(e)
##--------------------------------------------------------------------------------------------------
# Example usage:
model_text = """
Model {
ConfluentType = FIT
// States
InitialState { Name = "start" }
State {
Name = "off"
OutputFunction {
Action = ""
Description = "Output Function for state off"
Guard = ""
Message = "off"
OutputPort = "signal_out"
}
}
State {
Name = "on"
OutputFunction {
Action = ""
Description = "Output Function for state on"
Guard = ""
Message = "on"
OutputPort = "signal_out"
}
}
// Transitions
Transition {
Action = "state.toggle(), time_advance.set(1)"
Description = "Toggling state from off to on and setting time_advance"
Source = "off"
Target = "on"
TimeAdvanceType = Value
TimeAdvanceValue = 1.0
}
Transition {
Action = "state.toggle(), time_advance.set(1)"
Description = "Toggling state from on to off and setting time_advance"
Source = "on"
Target = "off"
TimeAdvanceType = Value
TimeAdvanceValue = 1.0
}
}
"""
# Example input
coupled_model_text = '''
Coupled Model {
AtomicModelList {
AtomicModel {
Name = "controller"
InPortList {
}
OutPortList {
OutPort { Name = "toggle_out" }
}
}
AtomicModel {
Name = "light"
InPortList {
InPort { Name = "toggle_in" }
}
OutPortList {
}
}
}
CouplingList {
Coupling {
M1Name = "controller"
M1Port = "toggle_out"
M2Name = "light"
M2Port = "toggle_in"
}
}
}
'''
##--------------------------------------------------------------------------------------------------
# parse_and_save_model_with_error_handling_coupled(coupled_model_text, './out.xml')