-
Notifications
You must be signed in to change notification settings - Fork 1
/
EADBWrapper.py
432 lines (326 loc) · 15.1 KB
/
EADBWrapper.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
import sys
import numpy as np
from matplotlib import pyplot as plt
from lsst.sims.catalogs.generation.db import DBObject
__all__ = ["EADBWrapper", "SysMLObject", "SysMLObjectList"]
class SysMLObject(object):
def __init__(self):
self._name = None
self._objid = None
self._parentid = None
self._version = None
self._author = None
self._daughters = None
self._properties = {}
self._attributes = {}
def getData(self, dbo, objid):
"""
Get the data for this SysMLObject
@param [in] dbo is an EADBWrapper
@param [in] objid is an int denoting the objectID of this object
"""
if self._objid is not None:
raise RuntimeError("You just tried to populate a SysMLObject"
"That is already populated")
self._objid=objid
dtype = np.dtype([
('Name', str, 300),
('Object_ID', np.int),
('ParentID', np.int),
('Version', str, 300),
('Author', str, 300)
])
query = "select t.Name, t.Object_ID, t.ParentID, t.Version, t.Author " \
+ "from t_object t where t.Object_ID=%d" % objid
results = dbo._dbo.execute_arbitrary(query, dtype=dtype)
if len(results)!=1:
raise RuntimeError("The ObjectID you passed to SysMLObject produced"
"%d objects; not 1" % len(results))
self._name = results['Name'][0]
self._author = results['Author'][0]
self._version = results['Version'][0]
self._parentid = results['ParentID'][0]
# get daughters
dtype = np.dtype([('Object_ID', np.int)])
query = "select t.Object_ID from t_object t where t.ParentID=%d" % objid
results = dbo._dbo.execute_arbitrary(query, dtype=dtype)
self._daughters = list(results['Object_ID'])
# query t_attribute
dtypeList = [
('Object_ID', np.int),
('Name', str, 300),
('Scope', str, 300),
('Stereotype', str, 300),
('Containment', str, 300),
('IsStatic', str, 300),
('IsCollection', str, 300),
('IsOrdered', str, 300),
('AllowDuplicates', str, 300),
('LowerBound', np.float),
('UpperBound', np.float),
('Container', str, 300),
('Notes', str, 300),
('Derived', str, 300),
('ID', np.int),
('Pos', str, 300),
('GenOption', str, 300),
('Length', str, 300),
('Precision', str, 300),
('Scale', str, 300),
('Const', str, 300),
('Style', str, 300),
('Classifier', str, 300),
('Default', str, 300),
('Type', str, 300),
('ea_guid', str, 300),
('StyleEx', str, 300)
]
dtype = np.dtype(dtypeList)
query = "select * from t_attribute t where t.Object_ID=%d" % objid
results = dbo._dbo.execute_arbitrary(query, dtype=dtype)
for ix in range(len(results)):
attName = results['Name'][ix]
self._attributes[attName] = {}
for column in dtypeList:
if column[0] != 'Name':
self._attributes[attName][column[0]] = results[column[0]][ix]
# query t_objectproperties
dtypeList = [
('PropertyID', np.int),
('Object_ID', np.int),
('Property', str, 300),
('Value', str, 300),
('Notes', str, 300),
('ea_guid', str, 300)
]
dtype = np.dtype(dtypeList)
query = "select * from t_objectproperties t where t.Object_ID=%d" % objid
results = dbo._dbo.execute_arbitrary(query, dtype=dtype)
for ix in range(len(results)):
pName = results['Property'][ix]
self._properties[pName] = {}
for column in dtypeList:
if column[0]!='Property':
self._properties[pName][column[0]] = results[column[0]][ix]
@property
def name(self):
"""a string carrying the name of the object"""
return self._name
@property
def objid(self):
"""an int carrying the ID of the object"""
return self._objid
@property
def parent(self):
"""an int carrying the ID of the object's parent"""
return self._parentid
@property
def version(self):
"""a string carrying the version of the object"""
return self._version
@property
def author(self):
"""a string carrying the author of the object"""
return self._author
@property
def daughters(self):
"""a list carrying the IDs of daughter objects"""
return self._daughters
@property
def properties(self):
"""
A dict carrying properties of the object.
The dict will be keyed to the property's name.
The dict will store all of the values associated with
that property in SysML
"""
return self._properties
@property
def attributes(self):
"""
A dict carrying the attributes of the object.
The dict will be keyed to the attribute's name.
The dict will carry a dict of all of the values
associated with that attribute in SysML.
"""
return self._attributes
def printObject(self, file_handle=sys.stdout):
file_handle.write('********\n')
file_handle.write('Name: %s\n' % self._name)
file_handle.write('Version: %s\n' % self._version)
file_handle.write('Author: %s\n' % self._author)
self.printProperties(file_handle=file_handle)
self.printAttributes(file_handle=file_handle)
file_handle.write('********\n')
def printProperties(self, file_handle=sys.stdout):
for pp in self._properties:
file_handle.write('Property: %s\n' % pp)
file_handle.write('Value: %s\n' % self._properties[pp]['Value'])
if self._properties[pp]['Notes'] != 'None':
file_handle.write('%s\n' % self._properties[pp]['Notes'])
def printAttributes(self, file_handle=sys.stdout):
if(len(self._attributes)>0):
file_handle.write('Attributes:\n')
for aa in self._attributes:
file_handle.write(' ------------\n')
file_handle.write(' %s = %s %s\n' % (aa,self._attributes[aa]['Default'], self._attributes[aa]['Type']))
if self._attributes[aa]['Notes'] != 'None':
file_handle.write(' %s\n' % self._attributes[aa]['Notes'])
class SysMLObjectList(object):
def __init__(self, dbo, objIdList):
self._objectList = []
for objid in objIdList:
obj = SysMLObject()
obj.getData(dbo, objid)
self._objectList.append(obj)
self._relationship_dict = {}
parents_of_objects = []
id_list = []
self._id_dict = {}
self._name_dict = {}
for ix, obj in enumerate(self._objectList):
if obj.objid in self._id_dict:
raise RuntimeError("ObjID %d repeated in SysMLObjectList" % obj.objid)
if obj.name in self._name_dict:
raise RuntimeError("Name %s repeated in SysMLObjectList" % obj.name)
self._id_dict[obj.objid] = ix
self._name_dict[obj.name] = ix
parents_of_objects.append(obj.parent)
id_list.append(obj.objid)
self._list_of_parents = [] # list of objects contained that are parents
self._list_of_children = [] # list of objects contained that are not parents
for obj in self._objectList:
if obj.objid in parents_of_objects:
self._list_of_parents.append(obj.objid)
else:
self._list_of_children.append(obj.objid)
self._list_of_grandparents = [] # list of objects contained whose parents are not contained
for ix in self._list_of_parents:
if self[ix].parent not in id_list:
self._list_of_grandparents.append(self[ix].objid)
self._getRelationships(dbo)
def __len__(self):
return len(self._objectList)
def __getitem__(self, value):
if isinstance(value, str):
dex = self._name_dict[value]
else:
dex = self._id_dict[value]
return self._objectList[dex]
def __iter__(self):
for val in self._objectList:
yield val
def _getRelationships(self, dbo):
dtype = np.dtype([('label', str, 300)])
baseQuery = "select t.Btm_Mid_Label from t_connector t "
for daughterObj in self._objectList:
if daughterObj.parent in self._id_dict:
parent = daughterObj.parent
daughter = daughterObj.objid
query = baseQuery + "where t.Start_Object_ID=%d " % daughter \
+ "and t.End_Object_ID=%d" % parent
results = dbo._dbo.execute_arbitrary(query, dtype=dtype)
if len(results)>1:
raise RuntimeError("Your relationship query between %d and %d yielded %d results." \
%(daughter, parent, len(results)))
if len(results)>0:
ans = results['label'][0].replace("\r\n","")
if daughter not in self._relationship_dict:
self._relationship_dict[daughter] = {}
self._relationship_dict[daughter][parent] = ans
def writeFamilyTree(self, file_handle=sys.stdout):
for obj in self._objectList:
file_handle.write('========\n')
file_handle.write('Name: %s\n' % obj.name)
file_handle.write('Version: %s\n' % obj.version)
file_handle.write('Author: %s\n' % obj.author)
obj.printProperties(file_handle=file_handle)
if obj.objid in self._relationship_dict:
file_handle.write('Relationships:\n')
for parent in self._relationship_dict[obj.objid]:
file_handle.write(' ')
file_handle.write(self._relationship_dict[obj.objid][parent])
file_handle.write(' %s\n' % self[parent].name)
file_handle.write('\n')
obj.printAttributes(file_handle=file_handle)
file_handle.write('========\n\n')
class EADBWrapper(object):
def __init__(self):
self._dbo = DBObject(database='sysarch', host='terminal.lsst.org',
port='3306', driver='mysql')
self._object_list = [('Name', str, 300), ('Object_ID', np.int),
('ParentID', np.int), ('Object_type', str, 300),
('Author', str, 300), ('Version', str, 300),
('Note', str, 300), ('Package_ID', np.int)]
self._object_dtype = np.dtype(self._object_list)
self._object_query = 'select'
for datum in self._object_list:
if self._object_query!='select':
self._object_query += ','
self._object_query += ' t.%s' % datum[0]
self._object_query += ' from t_object t'
def objectIdFromName(self, name):
dtype = np.dtype([('name', str, 300), ('ObjectID', np.int)])
query = "select t.name, t.Object_ID from t_object t where t.name = '%s'" % name
results = self._dbo.execute_arbitrary(query, dtype=dtype)
return results['ObjectID']
def _getDaughtersFromObjID(self, objid):
"""
Recursively return a list of Object_IDs corresponding to the objects
descended (either directly or indirectly) from an object specified by an
Object_ID
"""
query = self._object_query + " where t.ParentID=%d" % objid
results = self._dbo.execute_arbitrary(query, dtype=self._object_dtype)
ans = list(results["Object_ID"])
for aa in results["Object_ID"]:
new_results = self._getDaughtersFromObjID(aa)
ans += new_results
return ans
def writeFamilyTree(self, name, author=None, version=None, file_handle=sys.stdout):
"""
Write out all of the objects descended from <name>
@param[in] name is a string denoting the name of the desired object
@param[in] author is an optional string denoting the author of the
desired object (in case there are multiple versions)
@param[in] version is an optional string denoting the version
of the desired object (in case there are multiple versions)
@param[in] file_handle points to the output file (default is stdout)
This method writes its results to the destination specified by file_handle
"""
objIdList = self.getFamilyIDs(name=name, author=author, version=version)
objList = SysMLObjectList(self, objIdList)
objList.writeFamilyTree(file_handle=file_handle)
def writeFamilyTreeFromID(self, objid, file_handle=sys.stdout):
objIdList = self.getFamilyIDs(objid=objid)
objList = SysMLObjectList(self, objIdList)
objList.writeFamilyTree(file_handle=file_handle)
def getFamilyIDs(self, name=None, author=None, version=None, objid=None):
"""
Get a list of the Object_IDs of all objects descended (directly or
indirectly) from a specified object
@param[in] name is a string denoting the name of the desired object
@param[in] author is an optional string denoting the author of the
desired object (in case there are multiple versions)
@param[in] version is an optional string denoting the version
of the desired object (in case there are multiple versions)
@param[in] objid is an optional integer denoting the Object_ID of the
object whose tree you want to write out (if you already know it)
@param[out] a list of ints denoting the Object_IDs of all of
the objects beneath the desired object in its family tree.
"""
if objid is None:
query = self._object_query + " where t.name='%s'" % name
if author is not None:
query += " and t.Author='%s'" % author
if version is not None:
query += " and t.Version='%s'" % version
else:
query = self._object_query + " where t.Object_ID=%d" % objid
results = self._dbo.execute_arbitrary(query, dtype=self._object_dtype)
if len(results)>1:
raise RuntimeError('More than one object match the name you gave. '
'Try specifying an author or a version')
if len(results)==0:
raise RuntimeError('No objects matched the name you gave.')
return list(results["Object_ID"]) + self._getDaughtersFromObjID(results["Object_ID"][0])