-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_duplicate_association_ends.py
More file actions
70 lines (54 loc) · 2.06 KB
/
Copy pathremove_duplicate_association_ends.py
File metadata and controls
70 lines (54 loc) · 2.06 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
"""
Modelio Jython Script to remove duplicate AssociationEnd elements.
Duplicates are identified per owner Class by (target, name).
"""
from org.modelio.metamodel.uml.statik import Class
if elt is None or not isinstance(elt, Class):
print("Error: Please select any node to identify the package.")
else:
parent_package = elt.getOwner()
print("Removing duplicate AssociationEnd elements in package: " + parent_package.getName())
print("=" * 60)
# Collect classes
classes = []
for element in parent_package.getOwnedElement():
if isinstance(element, Class):
classes.append(element)
print("Found " + str(len(classes)) + " classes to process")
transaction = session.createTransaction("Remove Duplicate Association Ends")
try:
removed_count = 0
kept_count = 0
for cls in classes:
owned_ends = cls.getOwnedEnd()
seen = set()
to_delete = []
for end in owned_ends:
# Identify target
target = end.getTarget() if hasattr(end, 'getTarget') else None
target_name = target.getName() if target else "<None>"
# Identify name
end_name = end.getName()
if end_name is None:
end_name = ""
key = (target_name, end_name)
if key in seen:
to_delete.append(end)
else:
seen.add(key)
kept_count += 1
# Delete duplicates
for end in to_delete:
end.delete()
removed_count += 1
transaction.commit()
print("\n" + "=" * 60)
print("SUMMARY")
print(" - AssociationEnd kept: " + str(kept_count))
print(" - AssociationEnd removed: " + str(removed_count))
print("\nSuccess! Duplicate AssociationEnd elements removed.")
except Exception as e:
transaction.rollback()
print("Error: " + str(e))
import traceback
traceback.print_exc()