forked from oasys-elettra-kit/WISEr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelatify.py
434 lines (361 loc) · 10.6 KB
/
Relatify.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
433
434
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 25 00:10:35 2021
@author: Mike
"""
#===========================================================================
# CLASS: TreeItem
#===========================================================================
class TreeItem(object):
''' Protected attributes = Friend attributes.
Friend class is Tree
'''
def __init__(self):
self.Parents = []
self.Parent = None
self.Children = []
self.Name = None
def __str__(self):
return self.Name
def __disp__(self):
__HardcodedStyle = 1
NameChildren= ','.join([Child.Name for Child in self.Children])
NameParent = ('' if self.Parent == None else self.Parent.Name)
if __HardcodedStyle == 0:
Str = '[%s] ---- *[%s]*----[%s]' %( NameParent, self.Name,NameChildren)
else:
Str = '*[%s]*' % self.Name
return Str
@property
def Name(self):
return self._Name
@Name.setter
def Name(self,x):
self._Name = x
# ================================================
# PROP: UpstreamItemList
# ================================================
@property
def UpstreamItemList(self):
oeThis = self
ItemList = []
for Parent in self.Parents:
while oeThis.Parents != []:
oeThis = oeThis.Parent
ItemList.append(oeThis)
return ItemList
#================================================
# PROP: DonwstreamItemList
#================================================
@property
def DonwstreamItemList(self):
oeThis = self
ItemList = []
#improve: 10
while len(oeThis.Children) > 0:
oeThis = oeThis.Children[0]
ItemList.append(oeThis)
return ItemList
#===========================================================================
# CLASS: Tree
#===========================================================================
class Tree( CodeGenerator):
#================================================
# __init__
#================================================
def __init__(self, ItemList = None):
# self._Items = dict()
self._Items = OrderedDict()
self._ActiveItem = None
self._Name = ''
self._FirstItem = None
if ItemList is not None:
for Item in ItemList:
if type(Item) is str:
ItemToAdd = globals()[Item] # does not work
else:
ItemToAdd = Item
self.Append(Item)
#================================================
# __getitem__
#================================================
def __getitem__(self,Key):
'''
Paramters
--------------------
Key : can be EITHER strinf (name) OR integer (position index)
'''
def ItemNotFound(Key = None):
raise Exception('Tree.__getitem__, Item not found. Specified item:\n"%s"' % Key)
# The Key is an object
try:
try:
return self._Items[Key.Name]
except:
ItemNotFound(Key.Name)
except:
pass
# The Key is an integer
if type(Key) == int:
try:
return self._Items.items()[Key][1] # returns the object of the dictionary
except:
ItemNotFound(Key)
# The Key is a STRING
elif type(Key) == str :
try:
return self._Items[Key] # returns the object of the dictionary
except:
ItemNotFound(Key)
else:
print('ERROR: in Tree.getItem! Type mismatch')
return None
# #================================================
# # __setitem__
# #================================================
# def __setitem__(self,Name):
# try:
# return self._Items[Name]
# except:
# return self._Items[Name]
#================================================
# __str__
#================================================
def __str__(self):
"""
Uses the _:_disp__ function of the Item class
# Old code: I should improve __iter__ and __getitem__ for this class :-)
StrList = [itm.__disp__() for itm in self]
return '\n'.join(StrList)
"""
StrList = ['']
Itm0 = self.FirstItem
StrList.append(Itm0.__disp__())
while True:
if len(Itm0.Children) == 0:
break
else:
Itm1 = Itm0.Children[0]
StrList.append(Itm1.__disp__())
Itm0 = Itm1
return 'Name:' + self.Name + 'n'+ 10*'=' + '\n'+ '\n'.join(StrList)
#================================================
# FirstItem
#================================================
@property
def FirstItem(self):
return self._FirstItem
#================================================
# LastItem
#================================================
@property
def LastItem(self):
return self.ItemList[self.NItems-1]
#================================================
# PROP: Name
#================================================
@property
def Name(self):
return self._Name
@Name.setter
def Name(self,x):
self._Name = str(x)
#================================================
# ItemList
#================================================
@property
def ItemList(self):
'''
It is a secondary attribute (its value is built upon that of primary attributes).
It bases on the primary attribute: FirstItem
It should be named GetItemList NOT ItemList
'''
t0 = self.FirstItem
if t0 != None:
List = []
while True:
List.append(t0)
if len(t0.Children) > 0:
t0 = t0.Children[0]
else:
break
return List
else:
return []
@property
def ItemNameList(self):
return [_.Name for _ in self.ItemList]
#================================================
# NItems
#================================================
@property
def NItems(self):
return size(self.ItemList)
'''return size(self._Items.items())''' # why did I do it that way?
#================================================
# Insert
#================================================
def Insert(self,
NewItem,
ExistingName = None,
Mode = INSERT_MODE.After,
NewName = None):
'''
Paramters
---------
NewItem : Fundation.OpticalElement
Object to add (Assignement will be used)
ExistingItem : String | Fundation.OpticalElement
Reference Item
Mode : INSERT_MODE. type
INSERT_MODE.After, INSERT_MODE.Before, INSERT_MODE.Fork
NewName : string
Shorthand for changing NewItem.Name. Maybe I'll remove it
'''
NewName = (NewName if not(NewName==None) else NewItem.Name )
# if NewName == None and NewItem.Name == None):
# print ('ERROR: No Name specified in Tree.Insert method')
# elif NewName == None and NewItem.Name != None:
# pass
# elif NewName != None:
# NewItem.Name
# Update the ParentContainer attribute
NewItem.ParentContainer = self
# This one is the first item of the tree
if len(self._Items) == 0:
NewItem.Parent = None
NewItem.Children = []
self._FirstItem = NewItem
else:
# There are already other items in the tree
if ExistingName == None:
raise ValueError("Tree.Insert ERROR: I don't know where to place this new optical element. You gave me a name I can not find")
return 0
ItemA = self._Items[ExistingName]
# INSERT AFTER (default)
if Mode == INSERT_MODE.After:
# Update NewItem (second, or middle)
NewItem.Parent = ItemA
NewItem.Children = ItemA.Children
# update ItemA (first)
del ItemA.Children
ItemA.Children = [NewItem]
# update ItemB (last)
for Child in NewItem.Children:
Child.Parent = NewItem
# INSERT BEFORE
elif Mode == INSERT_MODE.Before:
# NewItem will be the first one of the tree
if ItemA.Parent == None:
# update NewItem
NewItem.Parent = []
NewItem.Children = ItemA
# update ItemA
ItemA.Parent = NewItem
# update Tree Prop
self.FirstItem = NewItem
# NewItem is a generic item
else:
pass
# codice da fare per 'INSERT BEFORE
# FORK
elif Mode == INSERT_MODE.Fork:
NewItem.Parent = ItemA
ItemA.Children.append(NewItem)
# ASSIGNMENT
# TmpDict = {NewName : NewItem}
self._Items[NewName] = NewItem
self._ActiveItem= NewItem
#================================================
# Insert
#================================================
def Append(self, NewItem, posdirective = None, NewName = None,
AppendAllIfList = True):
'''
Append NewItem to the tree structure.
If NewItem is a list, then all the items in the list are appended.
'''
#Case: NewItem is a list of items
if ((type(NewItem) is list) or (type(NewItem) is tuple)) and AppendAllIfList:
for _ in NewItem:
self.Append(_)
#Case: NewItem is a single item (usual)
else:
NewItem.Name = (NewName if NewName != None else NewItem.Name)
if self._ActiveItem == None:
ExistingItem = None
else:
ExistingItem = self._ActiveItem.Name
self.Insert(NewItem,
ExistingName = ExistingItem,
NewName = NewName,
Mode = INSERT_MODE.After)
#================================================
# Remove
#================================================
def Remove(self, KeyOrItem):
raise Exception("Remove does not work! Fix it to use it")
if type(KeyOrItem) is not list:
KeyOrItem = [KeyOrItem]
for _ in KeyOrItem:
# get the element within the Tree
if type(_) == int:
Item = self.ItemList[_]
else:
Item = _
# cuts and paste the link with parent and children
HasParent = Item.Parent is not None
HasChildren = Item.Children is not None
if HasParent and HasChildren:
#links parent to children
Item.Parent.Children = Item.Children
#links children toparents
for Child in Item.Children:
Child.Parent = Item.Parent
if HasParent and not(HasChildren):
#free the parent from children
Item.Parent = None
if HasChildren and not(HasParent):
#free the children from parent
for Child in Item.Children:
Child.Parent = None
#================================================
# GetFromTo
#================================================
def GetFromTo(self, FromItem = None, ToItem = None):
'''
Returns the item comprised between FromItem and ToItem within self.ItemList
(included)
'''
# Use all the items in the beamline
if FromItem == None and ToItem == None:
return self.ItemList
else:
# Use just a selection between FromItem and ToItem
ItmList = self.ItemList
iStart = ItmList.index(FromItem)
iEnd = ItmList.index(ToItem)
iStart = iStart if iStart != None else 0
iEnd = iEnd +1 if iEnd != None else self.NItems
return ItmList[iStart:iEnd]
class Item(Foundation.TreeItem):
pass
class UnionItem(Foundation.TreeItem):
def __init__(self, Parent1, Parent2):
self._Parent1 = Parent1
self._Parent2 = Parent2
@property
def Parent1(self):
return self._Parent1
@Parent1.setter
def Parent1(self,x):
self._Parent1 =x
@property
def Parent2(self):
return self._Parent2
@Parent2.setter
def Parent2(self,x):
self._Parent2 =x
@property
def Parents(self):
return [self.Parent1, self.Parent2]