-
Notifications
You must be signed in to change notification settings - Fork 1
/
Super_Variant_Definition.py
1507 lines (1224 loc) · 66.1 KB
/
Super_Variant_Definition.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Input_Extraction_Definition as IED
class SummarizedVariant:
'''The data structure of the summarization of object-centric variants'''
object_types = {}
lanes = []
interaction_points = []
frequency = 0
def __init__(self, lanes, object_types, interaction_points, frequency):
self.lanes = lanes
self.object_types = object_types
self.interaction_points = interaction_points
self.frequency = frequency
def __str__(self):
result_string = "Lanes: \n"
for i in range(len(self.lanes)):
result_string += str(self.lanes[i]) + "\n"
result_string += "\nInvolved Objects: \n" + str(self.object_types) +"\n"
result_string += "\nInteraction Points: \n"
for i in range(len(self.interaction_points)):
result_string += str(self.interaction_points[i]) + "\n"
return result_string + "Frequency: " + str(self.frequency)
def get_depth(self):
'''
Determines the nested depth of the Super Variant.
:param self: The summarized variant
:type self: SummarizedVariant
:return: The number of nested structures in the Super Variant
:rtype: int
'''
depth = 0
for lane in self.lanes:
depth = max(depth, lane.get_depth())
return depth
def get_length(self):
'''
Determines the total length of the Super Variant.
:param self: The summarized variant
:type self: SummarizedVariant
:return: The highest index in the lanes
:rtype: int
'''
length = 0
for lane in self.lanes:
if(isinstance(lane.elements[-1], CommonConstruct)):
length = max(length, lane.elements[-1].index)
else:
length = max(length, lane.elements[-1].index_end)
return length
def rename_lane(self, lane_id, new_name):
'''
Replaces the name of the lane in the summarized variant with the given lane id with a new name.
:param self: The summarized variant
:type self: SummarizedVariant
:param lane_id: The id of the lane that should be renamed
:type lane_id: tuple
:param new_name: The new name for the lane
:type new_name: str
'''
for lane in self.lanes:
if(lane.lane_id == lane_id):
lane.lane_name = new_name
break
def equals(self, other):
'''
Checks two summarized variants for equality up to lane naming and frequencies.
:param self: The summarized variant
:type self: SummarizedVariant
:param other: The other summarized variant
:type other: SummarizedVariant
:return: Whether the two summarized variants are equal
:rtype: bool
'''
return ((self.encode_lexicographically()) == (other.encode_lexicographically()))
def encode_lexicographically(self):
'''
Converts the summarized variant into an encoding string.
:param self: The summarized variant
:type self: SummarizedVariant
:return: The corresponding lexicographic encoding
:rtype: str
'''
import copy
# Stores the mapping from original lane_id to the new lane_id based on the enumeration of the encoded lanes
mapping = {}
# The lanes are sorted by their type and encoded in this alphabetical order
total_order_objects = [str(object) for object in list(self.object_types)]
total_order_objects.sort()
encoded_result = []
for object in total_order_objects:
# Determine all lanes for this object type
object_lanes = [l for l in self.lanes if l.object_type == object]
# Determine the maximal number of digits for the new lane_id
power_of_ten = len(str(len(object_lanes)))
encoded_lanes = []
id = 0
# Encode each lane, enumerate the encodings provisionally
for lane in object_lanes:
encoding = lane.encode_lexicographically(1)
# Fill up the current id with 0's such that every id has the same number of characters
full_id = str(id)
while (len(full_id) < power_of_ten):
full_id = str(0) + full_id
encoded_lanes.append(full_id + encoding)
# Store a reference between the temporary id and the original lane_id of the lane
mapping[full_id + encoding] = (lane.lane_id, 0)
id += 1
# Sort the encodings aphabetically, excluding its temporary id
encoded_lanes.sort(key = lambda x: x[power_of_ten:])
# Replace the temporary id with the enumeration of encodings as the new lane_id and add the object type to the prefix
for i in range(len(encoded_lanes)):
full_id = str(i)
while (len(full_id) < power_of_ten):
full_id = str(0) + full_id
lane_id = mapping[encoded_lanes[i]][0]
mapping[encoded_lanes[i]]= (lane_id, object + " " + full_id)
self.rename_lane(lane_id, object + " " + full_id)
encoded_lanes[i] = object + " " + full_id + ": " + encoded_lanes[i][power_of_ten:] + " "
encoded_result.extend(encoded_lanes)
# Create dictionary for easy extraction of the new lane_id when encoding the interactions
mapping = dict((x, y) for x, y in list(mapping.values()))
interactions_result = []
#new_interaction_points = []
for interaction in self.interaction_points:
# Encode references with the new lane_id and sort alphabetically
interacting_lanes = [mapping[lane_id] for lane_id in copy.deepcopy(interaction.interaction_lanes)]
interacting_lanes.sort()
lanes_encoding = "".join(str(x) + ", " for x in interacting_lanes)
lanes_encoding = lanes_encoding[:-2]
encoding = f"IP[{interaction.index_in_lanes},[{lanes_encoding}]] "
#if(encoding not in interactions_result):
interactions_result.append(encoding)
#new_interaction_points.append(interaction)
interactions_result.sort()
# Concatenate all encodings
result = ""
for encoded_lane in encoded_result:
result += encoded_lane
result += "- "
for encoded_ip in interactions_result:
result += encoded_ip
# Additionally sort the Super Lane itself
self.lanes.sort(key = lambda x: x.lane_name)
#self.interaction_points = new_interaction_points
return result
def to_super_variant(self, id):
'''
Converts a generic summarized variant into a Super Variant by assigning an id.
:param self: The summarized variant
:type self: SummarizedVariant
:param id: The id for the Super Variant
:type id: int
:return: The corresponding Super Variant
:rtype: SuperVariant
'''
return SuperVariant(id, self.lanes, self.object_types, self.interaction_points, self.frequency)
def get_lane(self, id):
'''
Finds and returns a lane of the summarized variant with the given id.
:param self: The summarized variant
:type self: SummarizedVariant
:param id: The id of the lane
:type id: int
:return: The corresponding Super Lane
:rtype: SuperLane
'''
for lane in self.lanes:
if (lane.lane_id == id):
return lane
return None
def get_lanes_of_type(self, type_label):
'''
Finds and returns all lanes of the summarized variant with the given object type.
:param self: The summarized variant
:type self: SummarizedVariant
:param type_label: The label of the type
:type type_label: str
:return: The corresponding Super Lanes
:rtype: list of type SuperLane
'''
result = []
for lane in self.lanes:
if (lane.object_type == type_label):
result.append(lane)
return result
def get_number_of_events(self):
'''
Returns the number of events of the variant.
:param self: The summarized variant
:type self: SummarizedVariant
:return: The corresponding count
:rtype: int
'''
result = 0
for lane in self.lanes:
result += lane.get_number_of_events()
result += len(self.interaction_points)
return result
def remove_gaps(self):
'''
Removes unnecessary gaps from the Super Variant for improved visualization.
:param self: The summarized variant
:type self: SummarizedVariant
'''
indices_with_no_chevrons = []
for lane in self.lanes:
indices_with_no_chevrons.append(lane.get_gaps())
result = indices_with_no_chevrons[0]
for k in range(1, len(indices_with_no_chevrons)):
result = list(set(result) & set(indices_with_no_chevrons[k]))
if(result == []):
return
else:
#TODO remove gaps and update positions
return
class SuperVariant(SummarizedVariant):
'''The generalized data structure of the summarization of object-centric variants and Super Variants'''
id = 0
def __init__(self, id, lanes, object_types, interaction_points, frequency):
self.id = id
self.lanes = lanes
self.object_types = object_types
self.interaction_points = interaction_points
self.frequency = frequency
def __str__(self):
result_string = "Super Variant " + str(self.id) + "\nLanes: \n"
for i in range(len(self.lanes)):
result_string += str(self.lanes[i]) + "\n"
result_string += "\nInvolved Objects: \n" + str(self.object_types) +"\n"
result_string += "\nInteraction Points: \n"
for i in range(len(self.interaction_points)):
result_string += str(self.interaction_points[i]) + "\n"
return result_string + "Frequency: " + str(self.frequency)
class SuperLane:
'''The data structure of the summarization of lanes and Super Lanes'''
lane_id = ()
object_type = ""
lane_name = ""
elements = []
cardinality = "0"
frequency = 0
realizations = []
def __init__(self, lane_id, name, object_type, elements, cardinality, frequency, realizations):
self.lane_id = lane_id
self.object_type = object_type
self.lane_name = name
self.elements = elements
self.cardinality = cardinality
self.frequency = frequency
self.realizations = realizations
def __str__(self):
result_string = f"ID: {self.lane_id}, Name: {self.lane_name}, Cardinality: {self.cardinality}: ["
for i in range(len(self.elements)):
result_string += str(self.elements[i]) + ","
result_string = result_string[:-1]
return result_string + "]"
def get_number_of_events(self):
'''
Returns the number of events of the lane, excluding the interaction points.
:param self: The summarizing Super Lane
:type self: SuperLane
:return: The corresponding count
:rtype: int
'''
result = 0
for element in self.elements:
if (isinstance(element, CommonConstruct) and not isinstance(element, InteractionConstruct)):
result += 1
elif(isinstance(element, GeneralChoiceStructure)):
for option in element.choices:
result += option.get_number_of_events()
return result
def get_length(self):
'''
Cmputes the length of the Super Lane.
:param self: The summarizing Super Lane
:type self: SuperLane
:return: The horizontal length of the Super Lane
:rtype: int
'''
length = 0
for element in self.elements:
if (isinstance(element, CommonConstruct)):
length += 1
else:
length += (element.index_end - element.index_start) + 1
return length
def get_gaps(self):
'''
Returns a list of all horizontal_indices that are not occupied by an activity.
:param self: The summarizing Super Lane
:type self: SuperLane
:return: The list of found indices
:rtype: list
'''
indices_with_no_chevrons = []
if(type(self.elements[0]) == InteractionConstruct or type(self.elements[0]) == CommonConstruct):
for j in range(0, self.elements[0].index):
indices_with_no_chevrons.append(j)
elif(type(self.elements[0]) == ChoiceConstruct or type(self.elements[0]) == OptionalConstruct):
for j in range(0, self.elements[0].index_start):
indices_with_no_chevrons.append(j)
for i in range(1, len(self.elements)):
if(type(self.elements[i]) == InteractionConstruct or type(self.elements[i]) == CommonConstruct):
if(type(self.elements[i-1]) == InteractionConstruct or type(self.elements[i-1]) == CommonConstruct):
for j in range(self.elements[i-1].index + 1, self.elements[i].index):
indices_with_no_chevrons.append(j)
elif(type(self.elements[i-1]) == ChoiceConstruct or type(self.elements[i-1]) == OptionalConstruct):
for j in range(self.elements[i-1].index_end + 1, self.elements[i].index):
indices_with_no_chevrons.append(j)
elif(type(self.elements[i]) == ChoiceConstruct or type(self.elements[i]) == OptionalConstruct):
if(type(self.elements[i-1]) == InteractionConstruct or type(self.elements[i-1]) == CommonConstruct):
for j in range(self.elements[i-1].index + 1, self.elements[i].index_start):
indices_with_no_chevrons.append(j)
elif(type(self.elements[i-1]) == ChoiceConstruct or type(self.elements[i-1]) == OptionalConstruct):
for j in range(self.elements[i-1].index_end + 1, self.elements[i].index_start):
indices_with_no_chevrons.append(j)
intermediate_results = []
for option in self.elements[i].choices:
intermediate_results.append(option.get_gaps())
final_intermediate_result = intermediate_results[0]
for k in range(1, len(intermediate_results)):
final_intermediate_result = list(set(final_intermediate_result) & set(intermediate_results[k]))
indices_with_no_chevrons.extend(indices_with_no_chevrons)
return indices_with_no_chevrons
def get_interaction_points(self, interactions, lane_id):
'''
Returns all interaction points in the lane in order of traversal.
:param self: The summarizing Super Lane
:type self: SuperLane
param interactions: The list of interactions of the corresponding Super Variant
:type interactions: list of type InteractionPoint
param lane_id: The original lane_id of the high-level lane
:type lane_id: tuple
:return: The list of found interaction points
:rtype: list of type InteractionPoint
'''
result = []
for elem in self.elements:
if(type(elem) == InteractionConstruct):
interaction_points = IED.get_interaction_points(interactions, lane_id, elem.position)
result.extend(interaction_points)
elif(type(elem) == ChoiceConstruct or type(elem) == OptionalConstruct):
for choice in elem.choices:
result.extend(choice.get_interaction_points(interactions, lane_id))
return result
def encode_lexicographically(self, depth, just_elements = False):
'''
Converts the Super Lane into an encoding string.
:param self: The summarizing Super Lane
:type self: SuperLane
param depth: The current level in the position
:type depth: int
param just_elements: Whether just the lanes elements should be encoded
:type just_elements: bool
:return: The corresponding lexicographic encoding
:rtype: str
'''
encoding = ""
if(not just_elements):
encoding += f"CAR {self.cardinality}: "
for element in self.elements:
if(type(element) == CommonConstruct):
encoding += f"CO[{element.index}:{element.index}, {element.activity}] "
elif(type(element) == InteractionConstruct):
encoding += f"IP[{element.index}:{element.index}, {element.activity}] "
else:
# Determine all choice sequences and sort alphabetically
choices = [choice.encode_lexicographically(depth + 1, just_elements = True) for choice in element.choices]
choices.sort()
# Encode all choices
choices_encoding = ""
for i in range(len(choices)):
choices_encoding += "Option " + str(i) + " "
choices_encoding += choices[i]
choices_encoding += ", "
choices_encoding = choices_encoding[:-2]
if(type(element) == ChoiceConstruct):
encoding += f"CH[{element.index_start}:{element.index_end}, [{choices_encoding}]] "
elif(type(element) == OptionalConstruct):
encoding += f"OP[{element.index_start}:{element.index_end}, [{choices_encoding}]] "
return encoding[:-1]
def update_lane_id(self, new_lane_id, depth):
'''
Updates the positions of a lane after the order of a choice has been changed.
:param self: The summarizing Super Lane
:type self: SuperLane
:param new_lane_id: The new id of this lane.
:type new_lane_id: int
param depth: The current level in the position
:type depth: int
'''
import copy
for elem in self.elements:
if (isinstance(elem, CommonConstruct)):
positions = [copy.deepcopy(elem.position)]
for i in range(depth-1):
positions.append(positions[-1].position)
new_position = positions[-1]
new_position.lane_id = new_lane_id
for i in range(2, depth+1):
current_position = positions[-i]
current_position.position = new_position
new_position = copy.deepcopy(current_position)
elem.position = new_position
else:
positions_start = [copy.deepcopy(elem.position_start)]
positions_end = [copy.deepcopy(elem.position_end)]
for i in range(depth-1):
positions_start.append(positions_start[-1].position)
positions_end.append(positions_end[-1].position)
new_position_start = positions_start[-1]
new_position_end = positions_end[-1]
new_position_start.lane_id = new_lane_id
new_position_end.lane_id = new_lane_id
for i in range(2, depth+1):
current_position_start = positions_start[-i]
current_position_end = positions_end[-i]
current_position_start.position = new_position_start
current_position_end.position = new_position_end
new_position_start = copy.deepcopy(current_position_start)
new_position_end = copy.deepcopy(current_position_end)
elem.position_start = new_position_start
elem.position_end = new_position_end
for choice in elem.choices:
choice.update_lane_id(new_lane_id, depth)
def same_summarization(self, other):
'''
Checks the equality between two Super Lanes.
:param self: The summarizing Super Lane
:type self: SuperLane
:param other: The other summarizing Super Lane
:type other: SuperLane
:return: Whether the Super Lanes have the same object type and elements
:rtype: bool
'''
if(other.object_type != self.object_type or len(self.elements) != len(other.elements)):
return False
else:
result = True
for i in range(len(self.elements)):
result = result and self.elements[i] == other.elements[i]
return result
def subsumed_summarization(self, other):
'''
Checks whether either of the Super Lanes is subsumed in the other.
:param self: The summarizing Super Lane
:type self: SuperLane
:param other: The other summarizing Super Lane
:type other: SuperLane
:return: Whether the Super Lanes have are subsuming each other
:rtype: bool
'''
import copy
if(other.object_type != self.object_type):
return False
result = False
self_contains_choice = any(isinstance(x, ChoiceConstruct) for x in self.elements) or any(isinstance(x, OptionalConstruct) for x in self.elements)
other_contains_choice = any(isinstance(x, ChoiceConstruct) for x in other.elements) or any(isinstance(x, OptionalConstruct) for x in other.elements)
if(self_contains_choice and not other_contains_choice):
realizations = self.get_realizations_normalized()
normalized_other = copy.deepcopy(other).normalize()
for realization in realizations:
result = result or realization.same_summarization(normalized_other)
elif(not self_contains_choice and other_contains_choice):
realizations = other.get_realizations_normalized()
normalized_self = copy.deepcopy(self).normalize()
for realization in realizations:
result = result or realization.same_summarization(normalized_self)
else:
realizations_self = self.get_realizations_normalized()
realizations_other = other.get_realizations_normalized()
subsumed_in_other = True
subsumed_in_self = True
for realization in realizations_self:
subsumed_in_other = subsumed_in_other and any([realization.same_summarization(other) for other in realizations_other])
for realization in realizations_other:
subsumed_in_self = subsumed_in_self and any([realization.same_summarization(other) for other in realizations_self])
result = (subsumed_in_other or subsumed_in_self)
return result
def normalize(self, offset = 0):
'''
Creates an abstraction of the Super Lane, shifting the positions to their normalized values starting from an offset value.
:param self: The summarizing Super Lane
:type self: SuperLane
:param offset: The starting index of the normalized Super Lane, defaulted at 0
:type offset: int
:return: The corresponding normalized Super Lane
:rtype: SuperLane
'''
import copy
elements = copy.deepcopy(self.elements)
index = offset
for element in elements:
if(type(element) == CommonConstruct or type(element) == InteractionConstruct):
index_before = element.index
element.index = index
element.position.apply_shift(index - index_before)
index += 1
elif(type(element) == ChoiceConstruct or type(element) == OptionalConstruct):
normalized_options = []
end_index = index
for option in element.choices:
normalized_options.append(option.normalize(index))
if(isinstance(normalized_options[-1].elements[-1], CommonConstruct)):
end_index = max(end_index, normalized_options[-1].elements[-1].index)
elif(isinstance(normalized_options[-1].elements[-1], GeneralChoiceStructure)):
end_index = max(end_index, normalized_options[-1].elements[-1].index_end)
index_start_before = element.index_start
index_end_before = element.index_start
element.index_start = index
element.index_end = end_index
element.position.apply_shift(index - index_start_before)
element.position.apply_shift(end_index - index_end_before)
index += end_index - index + 1
return SuperLane(0, "normalization", self.object_type, elements, self.cardinality, self.frequency, [])
def normalize_option(self, lane_id, option_id, offset = 0):
'''
Creates an abstraction of a Super Lane that is an option in a choice with normalized positions as well as a mapping from interaction points to their new positions.
:param self: The summarizing Super Lane
:type self: SuperLane
:param offset: The starting index of the normalized Super Lane, defaulted at 0
:type offset: int
:return: The corresponding normalized Super Lane and the mapping
:rtype: SuperLane, dict
'''
import copy
elements = copy.deepcopy(self.elements)
positions_mappings = dict()
index = offset
for element in elements:
if(type(element) == CommonConstruct or type(element) == InteractionConstruct):
position_before = copy.deepcopy(element.position)
index_before = element.index
element.index = index
element.position.apply_shift(index - index_before)
position_after_shift = element.position
if(isinstance(position_after_shift.position, IED.BasePosition)):
element.position = IED.RecursiveLanePosition(lane_id, IED.RecursiveLanePosition(option_id, IED.BasePosition(position_after_shift.position.lane_id, position_after_shift.position.position)))
else:
element.position = IED.RecursiveLanePosition(lane_id, IED.RecursiveLanePosition(option_id, IED.RecursiveLanePosition(position_after_shift.position.lane_id, position_after_shift.position.position)))
index += 1
if(type(element) == InteractionConstruct):
positions_mappings[str(position_before)] = element.position
elif(type(element) == ChoiceConstruct or type(element) == OptionalConstruct):
normalized_options = []
end_index = index
for option in element.choices:
new_option = copy.deepcopy(option)
normalization, mapping = new_option.normalize_option(lane_id, option_id, index)
normalized_options.append(normalization)
for key in mapping.keys():
positions_mappings[key] = mapping[key]
if(isinstance(normalized_options[-1].elements[-1], CommonConstruct)):
end_index = max(end_index, normalized_options[-1].elements[-1].index)
elif(isinstance(normalized_options[-1].elements[-1], GeneralChoiceStructure)):
end_index = max(end_index, normalized_options[-1].elements[-1].index_end)
element.choices = normalized_options
index_start_before = element.index_start
index_end_before = element.index_start
element.index_start = index
element.index_end = end_index
element.position_start.apply_shift(index - index_start_before)
element.position_end.apply_shift(end_index - index_end_before)
position_start_after_shift = element.position_start
position_end_after_shift = element.position_end
if(isinstance(position_start_after_shift.position, IED.BasePosition)):
element.position_start = IED.RecursiveLanePosition(lane_id, IED.RecursiveLanePosition(option_id, IED.BasePosition(position_end_after_shift.position.lane_id, position_start_after_shift.position.position)))
element.position_end = IED.RecursiveLanePosition(lane_id, IED.RecursiveLanePosition(option_id, IED.BasePosition(position_end_after_shift.position.lane_id, position_end_after_shift.position.position)))
else:
element.position_start = IED.RecursiveLanePosition(lane_id, IED.RecursiveLanePosition(option_id, IED.RecursiveLanePosition(position_start_after_shift.position.lane_id, position_start_after_shift.position.position)))
element.position_end = IED.RecursiveLanePosition(lane_id, IED.RecursiveLanePosition(option_id, IED.RecursiveLanePosition(position_start_after_shift.position.lane_id, position_end_after_shift.position.position)))
index += end_index - index + 1
return SuperLane(self.lane_id, self.lane_name, self.object_type, elements, self.cardinality, self.frequency, []), positions_mappings
def extract_option(self, new_frequency = None):
'''
Updates all positions in a lane by removing the first given option_id corresponding to the second lane_id.
:param self: The summarizing Super Lane
:type self: SuperLane
:param new_frequency: The updated frequency for the elements
:type new_frequency: float
:return: The corresponding lane with adjusted positions and it's mapping
:rtype: SuperLane, dict
'''
import copy
mapping = dict()
for element in self.elements:
if(type(element) == CommonConstruct or type(element) == InteractionConstruct):
position_before = copy.deepcopy(element.position)
element.position = IED.RecursiveLanePosition(0, position_before.position.position)
if(new_frequency != None):
element.frequency = new_frequency
if(type(element) == InteractionConstruct):
mapping[str(position_before)] = element.position
elif(type(element) == ChoiceConstruct or type(element) == OptionalConstruct):
for i in range(len(element.choices)):
updated_choice, recursive_mapping = element.choices[i].extract_option()
element.choices[i] = updated_choice
for key in recursive_mapping.keys():
mapping[key] = recursive_mapping[key]
position_start_before = copy.deepcopy(element.position_start)
position_end_before = copy.deepcopy(element.position_end)
element.position_start = IED.RecursiveLanePosition(0, position_start_before.position.position)
element.position_end = IED.RecursiveLanePosition(0, position_end_before.position.position)
return self, mapping
def get_realizations_normalized(self):
'''
Generates all realizations of a Super Lane with normalized positions starting from 0.
:param self: The summarizing Super Lane
:type self: SuperLane
:return: The list of all realizations
:rtype: list of type SuperLane
'''
import copy
realizations = []
realizations.append([])
for elem in self.elements:
element = copy.deepcopy(elem)
if(type(element) == CommonConstruct or type(element) == InteractionConstruct):
realizations = [realization + [element] for realization in realizations]
elif(type(element) == ChoiceConstruct or type(element) == OptionalConstruct):
intermediate_result = []
for i in range(len(element.choices)):
for sublist in element.choices[i].get_realizations_normalized():
intermediate_result.extend([realization + sublist.elements for realization in realizations])
if(type(element) == OptionalConstruct):
intermediate_result.extend([realization + [EmptyConstruct(element.empty_frequency)] for realization in realizations])
realizations = intermediate_result
result = []
for i in range(len(realizations)):
frequency = 1
for elem in realizations[i]:
frequency = frequency * elem.frequency
realization_frequency = frequency * self.frequency
index = 0
elements = []
for elem in realizations[i]:
if(not type(elem) == EmptyConstruct):
elements.append(copy.deepcopy(elem))
elements[-1].frequency = frequency
elements[-1].index = index
elements[-1].position = IED.BasePosition(0, index)
index += 1
result.append(SuperLane(i, "realization " + str(i), self.object_type, elements, "1", realization_frequency, []))
return result
def get_all_realizations(self):
'''
Generates all realizations of a Super Lane.
:param self: The summarizing Super Lane
:type self: SuperLane
:return: The list of all realizations
:rtype: list of type SuperLane
'''
import copy
realizations = []
realizations.append([])
for elem in self.elements:
element = copy.deepcopy(elem)
if(type(element) == CommonConstruct or type(element) == InteractionConstruct):
realizations = [realization + [copy.deepcopy(element)] for realization in realizations]
elif(type(element) == ChoiceConstruct or type(element) == OptionalConstruct):
intermediate_result = []
for i in range(len(element.choices)):
for sublist in element.choices[i].get_all_realizations():
intermediate_result.extend([realization + sublist.elements for realization in realizations])
if(type(element) == OptionalConstruct):
intermediate_result.extend([realization + [EmptyConstruct(element.empty_frequency)] for realization in realizations])
realizations = intermediate_result
result = []
for i in range(len(realizations)):
frequency = 1
for elem in realizations[i]:
frequency = frequency * elem.frequency
realization_frequency = frequency * self.frequency
new_elements = []
for elem in realizations[i]:
if(not type(elem) == EmptyConstruct):
new_elements.append(copy.deepcopy(elem))
new_elements[-1].frequency = frequency
result.append(SuperLane(i, "realization " + str(i), self.object_type, new_elements, self.cardinality, realization_frequency, []))
return result
def get_valid_realizations(self):
'''
Generates all realizations of a Super Lane and cross-references them with the stored realizations.
:param self: The summarizing Super Lane
:type self: SuperLane
:return: The list of all valid realizations
:rtype: list of type SuperLane
'''
all_realizations = self.get_all_realizations()
valid_realizations = []
for realization in all_realizations:
for comparison in self.realizations:
if (len(comparison.elements) != len(realization.elements)):
continue
else:
is_equivalent = True
for i in range(len(realization.elements)):
if(type(realization.elements[i]) == type(comparison.elements[i])):
if(type(realization.elements[i]) == InteractionConstruct and type(comparison.elements[i]) == InteractionConstruct and comparison.elements[i].activity == realization.elements[i].activity):
continue
elif(type(realization.elements[i]) == CommonConstruct and type(comparison.elements[i]) == CommonConstruct and comparison.elements[i].activity == realization.elements[i].activity):
continue
else:
is_equivalent = False
break
else:
is_equivalent = False
break
if(is_equivalent):
valid_realizations.append(realization)
break
return valid_realizations
def check_identical(self, elements):
'''
Checks the elements of the lane againts a list of other elements
:param self: The summarizing Super Lane
:type self: SuperLane
:param elements: The list of elements
:type elements: list of type SummarizationElement
:return: Whether the elements are identical, the newly update lane
:rtype: bool, SuperLane
'''
import copy
if (len(self.elements) != len(elements)):
return False, self
else:
update_self = copy.deepcopy(self)
for i in range(len(elements)):
if(type(elements[i]) == type(self.elements[i])):
if(type(elements[i]) == InteractionConstruct or type(self.elements[i]) == InteractionConstruct):
return False, self
elif(type(elements[i]) == CommonConstruct and type(self.elements[i]) == CommonConstruct and elements[i].activity == self.elements[i].activity):
update_self.elements[i].frequency += elements[i].frequency
continue
elif((type(elements[i]) == OptionalConstruct and type(self.elements[i]) == OptionalConstruct) or (type(elements[i]) == ChoiceConstruct and type(self.elements[i]) == ChoiceConstruct)):
if(len(elements[i].choices) == 1 and len(self.elements[i].choices) == 1 and elements[i].empty_frequency == self.elements[i].empty_frequency):
is_identical, new_choice = self.elements[i].choices[0].check_identical(elements[i].choices[0].elements)
if(is_identical):
self.elements[i].choices[0] = new_choice
self.elements[i].choices[0].cardinality = "n"
self.elements[i].choices[0].frequency += elements[i].choices[0].frequency
# TODO Handling multiple choices
else:
return False, self
else:
return False, self
return True, update_self
def get_element(self, position):
'''
Extracts the element of the Super Lane at the given position.
:param self: The summarizing Super Lane
:type self: SuperLane
:param position: The position of the element
:type position: LanePosition
:return: The element at the given position
:rtype: SummarizationElement
'''
unpacked_position = position.position
is_base_position = isinstance(unpacked_position, int)
for element in self.elements:
if((type(element) == CommonConstruct or type(element) == InteractionConstruct) and is_base_position and element.index == unpacked_position):
return element
elif((type(element) == ChoiceConstruct or type(element) == OptionalConstruct) and not is_base_position and unpacked_position.get_base_index() >= element.index_start and unpacked_position.get_base_index() <= element.index_end):
return element.choices[unpacked_position.lane_id].get_element(unpacked_position)
elif((type(element) == ChoiceConstruct or type(element) == OptionalConstruct) and is_base_position and unpacked_position >= element.index_start and unpacked_position <= element.index_end):
return element
return None
def contains_activity(self, activity_label):
'''
Checks whether the given activity label occurs in the Super Lane.
:param self: The summarizing Super Lane
:type self: SuperLane
:param activity_label: The label of the activity
:type activity_label: str
:return: Whether the activity is contained in the Super Lane
:rtype: bool
'''
for element in self.elements:
if((type(element) == CommonConstruct or type(element) == InteractionConstruct) and element.activity == activity_label):
return True
elif((type(element) == ChoiceConstruct or type(element) == OptionalConstruct)):
for choice in element.choices:
if (choice.contains_activity(activity_label)):
return True
return False
def count_activity(self, activity_label):
'''
Extracts the number of times the given activity label occurs in the Super Lane.
:param self: The summarizing Super Lane
:type self: SuperLane
:param activity_label: The label of the activity
:type activity_label: str
:return: The count of occurences
:rtype: int
'''
count = 0
for element in self.elements:
if((type(element) == CommonConstruct or type(element) == InteractionConstruct) and element.activity == activity_label):
count += 1
elif((type(element) == ChoiceConstruct or type(element) == OptionalConstruct)):
for choice in element.choices:
count += choice.count_activity(activity_label)
return count
def shift_lane(self, start_element, offset, observed_positions, index = None):
'''
Shifts the positions of the Super Lane by an offset starting from a given start element.
:param self: The summarizing Super Lane
:type self: SuperLane
:param start_element: The first element that requires shifting
:type start_element: SummarizationConstruct
:param offset: The offset of the shift
:type offset: int
:param observed_positions: A dictionary with all other interaction point positions relevant for this shift, these are observed and updated accordingly
:type observed_positions: dict
:param index: The index of the starting element in the list of elements and the lane itself
:type index: int, SuperLane
:return: The dictionary with the observed updated positions and the lane
:rtype: dict, SuperLane
'''
import copy
if(index == None):