-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractElements.py
431 lines (319 loc) · 15.2 KB
/
extractElements.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
import csv
import glob
import os
from collections.abc import Iterable
# from org.eclipse.emf import ecore
from pyecore.ecore import EAttribute
from pyecore.ecore import EStructuralFeature
from pyecore.ecore import EOrderedSet
from pyecore.ecore import EClass
# from ecore import EAttribute
# import networkx as nx
import yaml
from pyecore.ecore import EEnum
from pyecore.resources import ResourceSet, URI
class extractElements:
case = "xmi"
ecoreNumber = 0
featureTypes = []
graphNo = 0
nodeLabel = 1
Graphs = []
attributeNames = {}
classes = dict()
directories = 1
filenumber = 0
yamlexistance = 0
mapvalue = {}
yamlcontents = {}
referenceNames = {}
IDs = {}
graphAttributes = {}
visitednodes = []
jsondata = {}
visited = []
queue = []
pathfile = None
yamlFeatures = {}
yamlclasses = {'include': [], 'exclude': []}
modelData = {}
csvData = {}
refFeatures = []
csvEdgesData = {}
folder = ""
ids = {}
graphNumber = 0
def __init__(self, models_path: str, metamodel_path:str):
self.models_path = models_path
self.metamodel_path = metamodel_path
self.load_config()
self.load_metamodel(metamodel_path)
self.process_models(models_path)
#check the format of input models in Yaml
def load_config(self) -> None:
self.current_dir = os.path.dirname(__file__)
config_file = os.path.join(self.current_dir, 'config.yaml')
with open(config_file) as f:
self.yamlcontents = yaml.load(f, Loader=yaml.FullLoader)
self.case = self.yamlcontents['inputmodels']['format']
def load_metamodel(self, metamodel_path) -> None:
rset = ResourceSet()
resource = rset.get_resource(URI(metamodel_path))
self.meta_root = resource.contents[0]
def process_models(self, models_path) -> None:
if self.case == "ecore":
self.load_ecore_models(models_path)
elif self.case == "xmi":
self.load_xmi_models(models_path)
self.create_edges()
self.create_csv()
def load_ecore_models(self, models_path):
os.chdir(models_path)
for file in glob.glob("*.ecore"):
print("file is", file)
ecore_file = os.path.join(models_path, file)
self.rset = ResourceSet()
resource = self.rset.get_resource(URI(ecore_file))
model_root = resource.contents[0]
self.ecoreNumber += 1
self.create_nodes(model_root, self.meta_root)
def load_xmi_models(self, models_path):
os.chdir(models_path)
self.rset.metamodel_registry[self.meta_root.nsURI] = self.meta_root
for file in glob.glob("*.xmi"):
xmi_file = os.path.join(models_path, file)
resource = self.rset.get_resource(URI(xmi_file))
model_root = resource.contents[0]
self.filenumber += 1
self.create_nodes(model_root, self.meta_root)
def checkYaml(self, mmroot, model, elementType, case, element=None, ):
print("checkyaml is", "mmroot=", mmroot, "model is=", model, "elementType=", elementType, "case=", case,
"element=", element)
metamodel_name = mmroot.name
metamodel_nsURI = mmroot.nsURI
modelName = model.eClass.name
print("element is=", element)
if element != None:
elementName = element.name
# print("model in checkyaml:", modelName)
if self.yamlexistance == 0:
print("yaml 0")
return True
elif case == "include" and elementType == "class":
print("checking existance")
print("meta name=", metamodel_name)
print("ns is", metamodel_nsURI)
print("modelName=", modelName)
if ((self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name,
'uri', mmroot.nsURI, 'classes',
'exclude']) != None and modelName in self.deep_get(
self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI, 'classes',
'exclude'])) or (self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri',
mmroot.nsURI, 'classes',
'include']) != None and modelName not in self.deep_get(
self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI, 'classes',
'include']))):
print("model True")
return False
else:
return True
elif elementType == "feature" and case == "existance":
print("here feature existance")
if self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI,
'classes', modelName, 'features', elementName, 'isIncluded']) != None:
if self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI,
'classes', modelName, 'features', elementName, 'isIncluded']) == "False":
print("return False1")
return False
elif (self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI,
'classes', modelName, "include"]) != None and elementName not in self.deep_get(
self.yamlcontents, ['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI,
'classes', modelName, "include"])):
print("return False2")
return False
elif self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI,
'classes', "excludeAllAttributes"]) == "True":
print("return False3")
return False
else:
print("return True")
return True
elif case == "renaming":
print("yamlcontent::", self.yamlcontents)
print("renaming")
print("metaname:", metamodel_name)
print("model", modelName)
print("element:", elementName)
print("rename value==", self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri',
mmroot.nsURI,
'classes', modelName, 'features', elementName, 'renaming']))
if self.deep_get(self.yamlcontents,
['adaptations', 'metamodels', 'packages', metamodel_name, 'uri', mmroot.nsURI,
'classes', modelName, 'features', elementName, 'renaming']) != None:
print("rename exists")
return self.yamlcontents['adaptations']['metamodels']['packages'][metamodel_name]['uri'][mmroot.nsURI][
'classes'][modelName]['features'][elementName]['renaming']
else:
return True
def deep_get(self, d, keys):
if not keys or d is None:
return d
return self.deep_get(d.get(keys[0]), keys[1:])
model_data = {}
attribute_types = []
def create_nodes(self, model, mmroot):
print("creating nodes")
self.graphNumber = self.graphNumber + 1
print("graphNumber issss:", self.graphNumber)
# Data structures
self.model_data = {}
self.attribute_types = []
# Create node data for root model
node_data = self.create_node_data(model, mmroot, self.graphNumber)
# Add root node data to model data
self.add_to_model_data(model, node_data)
# Process child elements
for element in self.get_child_elements(model):
print("child element")
# Create node data for child
child_node_data = self.create_node_data(element, mmroot, self.graphNumber)
# Add child node data to model data
self.add_to_model_data(element, child_node_data)
# Return data structures
return self.model_data, self.attribute_types
def create_node_data(self, element, mmroot, graphNo):
print("creating node data")
graphdata = {}
graphcsvdata = {}
# metamodel_name = mmroot.name
# modelName = element.eClass.name
node_number = 0
if element.eClass.name in self.ids:
node_number = self.ids[element.eClass.name] + 1
self.ids[element.eClass.name] = node_number
for feature in element._isset:
feature_name = feature.name
feature_value = element.eGet(feature)
graphdata[feature_name] = feature_value
# print("graphNo is now:", graphNo)
graphdata["graphNumber"] = graphNo
graphcsvdata["graphNumber"] = graphNo
if not feature.is_reference:
graphcsvdata[feature_name] = feature_value
if feature.is_attribute:
types = {}
types["node"] = element.eClass.name
types["name"] = feature.name
print("ids is:", self.ids)
print("element is", element)
types["nodeNumber"] = self.ids[element.eClass.name]
# node_number = self.ids.get(class_name, 0) + 1
if isinstance(feature.eType, EEnum):
types["type"] = "EString"
else:
types["type"] = feature.eType.name
self.featureTypes.append(types)
types = {}
self.modelData.setdefault(element.eClass.name, []).append(graphdata)
self.modelData[element.eClass.name][-1]["element"] = element
self.csvData.setdefault(element.eClass.name, []).append(graphcsvdata)
return graphdata
def get_node_number(self, element):
print("csvdata", self.csvData)
print(" modeldata", self.modelData)
class_name = element.eClass.name
node_number = self.ids.get(class_name, 0) + 1
self.ids[class_name] = node_number
return node_number
def add_to_model_data(self, element, data):
print("add to model data")
self.model_data.setdefault(element.eClass.name, []).append(data)
data["element"] = element
def get_child_elements(self, element):
return element.eAllContents()
def get_eclass(self, element, mmroot):
return mmroot.getEClassifier(element.eClass.name)
def get_features(self, eclass):
return list(eclass.eStructuralFeatures)
def create_edges(self):
print("Creating edges...")
for keys in self.csvData:
for i in range(0, len(self.csvData[keys])):
id = str(keys) + "Id"
self.csvData[keys][i][id] = i
self.modelData[keys][i][id] = i
reference_properties = {"eClassifiers", "ePackage", "eStructuralFeatures", "eType",
"eContainingClass", "eOpposite", "eSuperTypes", "eModelElement"}
for node_type, nodes in self.modelData.items():
# print("nodes is", nodes)
for node in nodes:
for property, references in node.items():
if property in reference_properties:
# print("references", references)
if isinstance(references, Iterable):
# print("yes iterable")
refs = list(references)
else:
refs = [references]
source_id = node[f"{node_type}Id"]
graph_number = node["graphNumber"]
# print("references", refs)
for reference in refs:
edge_type = reference.eClass.name
if self.csvEdgesData.get(f"{node_type}_{edge_type}") is None:
self.csvEdgesData[f"{node_type}_{edge_type}"] = []
# print("edge type", edge_type)
# print("model data:", self.modelData)
if edge_type in self.modelData:
for dest_node in self.modelData[edge_type]:
if ("element", reference) in dest_node.items() and (
dest_node["graphNumber"], graph_number):
dest_id = dest_node[f"{edge_type}Id"]
edge_data = {}
edge_data[f"sr{node_type}Id"] = source_id
edge_data[f"ds{edge_type}Id"] = dest_id
edge_data["name"] = property
self.csvEdgesData[f"{node_type}_{edge_type}"].append(edge_data)
import csv
import os
def create_csv(self):
print("Creating CSV files...")
# Encapsulate node logic
self._write_nodes_csv()
# Encapsulate edges logic
self._write_edges_csv()
# Encapsulate types logic
self._write_types_csv()
print("CSV files created!")
def _write_nodes_csv(self):
for types, records in self.csvData.items():
self._write_to_csv(records, types, "nodes")
def _write_edges_csv(self):
for types, records in self.csvEdgesData.items():
if records:
self._write_to_csv(records, types, "edges")
def _write_types_csv(self):
print("feature types:", self.featureTypes)
self._write_to_csv(self.featureTypes, "types", folder="types")
def _write_to_csv(self, records, types, folder):
if types == "types":
filename = f"{types}"
else:
filename = f"{types}_{folder}"
dir_path = f"..\\csvfiles\\{folder}"
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
fields = records[0].keys()
with open(os.path.join(dir_path, f'{filename}.csv'), 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=fields, extrasaction='ignore')
writer.writeheader()
writer.writerows(records)