-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSA.py
More file actions
268 lines (222 loc) · 7.07 KB
/
SA.py
File metadata and controls
268 lines (222 loc) · 7.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
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
#pip install diagrams
#sudo apt install graphviz
import os, fnmatch, sys, json, re, copy
from colorama import Fore
from itertools import chain
import pdb
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
def getHeaders(path):
header_path = []
path_to_file = ""
for i in path.split('/')[:-1]:
path_to_file += i + '/'
try:
fp = open(path)
except PermissionError:
print(Fore.RED + "Not enough permissions")
sys.exit()
except Exception as e:
# print(Fore.RED + "Error reading file:", i, "-", e)
pass
else:
with fp:
headers = []
for i in fp.readlines():
try:
if(i.lstrip()[0] == '#'):
headers.append(i)
except:
pass
for i in headers:
if "<" in i:
header_path.append(path_to_file + (i.split("<")[1]).split(">")[0])
elif "\"" in i:
header_path.append(path_to_file + (i.split("\"")[1]).split("\"")[0])
fp.close()
return header_path
def getUserFiles(path):
returnList = []
key = ""
for i in path:
if('cadmium' in i.split('/')):
if('logger' in i.split('/')):
key = "Main"
elif('coupled.hpp' in i.split('/') and key != "Main"):
key = "Coupled"
elif('atomic.hpp' in i.split('/') and key != "Main"):
key = "Atomic"
print(Fore.YELLOW + "Skipping \"", i, "\"(Internal library file)")
else:
try:
fp = open(i)
except PermissionError:
# print(Fore.RED + "Not enough permissions")
sys.exit()
except Exception as e:
# print(Fore.RED + "Error reading file:", i, "-", e)
pass
else:
returnList.append(i)
fp.close()
return returnList, key
def getAllFiles(files):
return_list = []
for file in files:
header_paths = getHeaders(file)
user_files, key = getUserFiles(header_paths)
if(user_files != []):
return_list.extend([getAllFiles(user_files), {key : file}])
else:
return_list.append({key : file})
return return_list
def printModels(arr, indent = 0):
for item in arr:
if isinstance(item, list):
printModels(item, indent + 1)
else:
print('\t' * indent, item)
model_stack = []
def convertToDictionary(arr, depth = 0):
main_stack = []
global model_stack
for item in arr:
if isinstance(item, list):
convertToDictionary(item, depth + 1)
else:
if("Main" in item.keys()):
main_stack.append({"Main" : item["Main"], "depth" : depth, "Top" : copy.deepcopy(model_stack)})
model_stack.clear()
elif("Coupled" in item.keys()):
tmp = []
for atomic in model_stack:
if atomic['depth'] > depth:
tmp.append(atomic)
model_stack = [x for x in model_stack if x not in tmp]
IC = {}
try:
fp = open(item["Coupled"])
except PermissionError:
print(Fore.RED + "Not enough permissions")
sys.exit()
except Exception as e:
print(Fore.RED + "Error reading file:", atomic, "-", e)
else:
with fp:
for i in fp.readlines():
if(i.lstrip().split('(')[0] == 'addCoupling'):
from_model = i.lstrip().split('(')[1].split(')')[0].strip().split(',')[0].strip()
to_model = i.lstrip().split('(')[1].split(')')[0].strip().split(',')[-1].strip()
IC.update({from_model : to_model})
fp.close()
instance_names = {}
try:
fp = open(item["Coupled"])
except PermissionError:
print(Fore.RED + "Not enough permissions")
sys.exit()
except Exception as e:
print(Fore.RED + "Error reading file:", atomic, "-", e)
else:
with fp:
for i in fp.readlines():
if(i.lstrip().split('=')[-1].split('<')[0].strip() == 'addComponent'):
# atomic_name = i.lstrip().split('=')[-1].split('<', 1)[-1].rsplit('>', 1)[0].strip()
atomic_name = i.lstrip().split('=')[-1].split('<')[1].split('>')[0].strip()
instance_name = i.lstrip().split('=')[0].strip().split(" ")[-1]
instance_names.update({atomic_name : instance_name})
fp.close()
EIC = []
EOC = []
try:
fp = open(item["Coupled"])
except PermissionError:
print(Fore.RED + "Not enough permissions")
sys.exit()
except Exception as e:
print(Fore.RED + "Error reading file:", atomic, "-", e)
else:
with fp:
for i in fp.readlines():
if("addInPort<" in i.lstrip()):
EIC.append(i.lstrip().split('=')[0].strip())
if("addOutPort<" in i.lstrip()):
EOC.append(i.lstrip().split('=')[0].strip())
fp.close()
model_stack.append({"Coupled" : item["Coupled"], "depth" : depth, "Atomics" : tmp, "Instance_names" : instance_names, "IC" : IC, "EIC" : EIC, "EOC": EOC})
elif("Atomic" in item.keys()):
model_stack.append({"Atomic" : item["Atomic"], "depth" : depth})
return main_stack
def getStates(d, target_key):
result = copy.deepcopy(d)
for key, value in d.items():
if key == target_key:
states = parseStates(value)
result.update({"states" : states})
elif (isinstance(value, dict) and not(key == 'IC' or key == 'Instance_names')):
result.update(getStates(value, target_key))
elif (isinstance(value, list) and not(key == "EIC" or key == "EOC")):
listDicts = []
for item in value:
listDicts.append(getStates(item, target_key))
result.update({key : listDicts})
return result
def parseStates(atomic):
flag = False
state_name = ""
state_vars = {}
try:
fp = open(atomic)
except PermissionError:
print(Fore.RED + "Not enough permissions")
sys.exit()
except Exception as e:
print(Fore.RED + "Error reading file:", atomic, "-", e)
else:
with fp:
for i in fp.readlines():
try:
if re.search(r'struct\s+(\w+)\s*{', i.lstrip()):
state_name = re.search(r'struct\s+(\w+)\s*{', i.lstrip()).group(1)
flag = True
if(flag):
pattern = re.compile(fr"{re.escape(state_name)}\s*\(.*?\)\s*:\s*((?:\w+\s*\(.*?\),\s*)*\w+\s*\(.*?\))\s*{{")
if pattern.search(i.lstrip()):
var_and_val_group = pattern.search(i.lstrip()).group(1)
data = list(chain.from_iterable(re.findall(r'(\w+)\s*\(([^)]*)\)', var_and_val_group)))
for i in range(0, len(data) - 1, 2):
state_vars.update({data[i] : (data[i + 1].strip() if data[i + 1] else "NULL")})
flag = False
except:
pass
fp.close()
return state_vars
if __name__ == '__main__':
if 'main' not in os.listdir():
print(Fore.RED + "main directory not found")
sys.exit()
cwd = os.getcwd()
print("Finding entry point file...")
main_wd = cwd + '/main'
main_cpp = find('*main*.cpp', main_wd)
if (main_cpp == []):
print(Fore.RED, "Entry point cpp file not found")
sys.exit()
print("Finding the models...")
models = getAllFiles([main_cpp[-1]])
print(Fore.RESET)
print("Parsed file system structure:- ")
printModels(models)
print("Converting to Dictionary...")
main_stack = convertToDictionary(models) #Also extracts IC
print("Extracting parameters...")
fullyFeaturedModel = getStates(main_stack[0], 'Atomic')
# Convert and write JSON object to file
with open("model_formalism.json", "w") as outfile:
json.dump(fullyFeaturedModel, outfile, indent = 2)
print(Fore.GREEN, "JSON created successfully")