-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecore2plant.py
89 lines (76 loc) · 2.59 KB
/
ecore2plant.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
# https://gist.github.com/aranega/eca9c8fbbd87b2f9c70317da53676ac6
# Usage
# https://github.com/pyecore/pyecore/issues/104
from pyecore.resources import ResourceSet
from pyecore.utils import dispatch
import pyecore.ecore as ecore
class PlantUMLSwitch(object):
def __init__(self):
self.visited = set()
@dispatch
def generate(self, o):
print('Object kind unsupported', o.eClass.name)
@generate.register(ecore.EPackage)
def epackage_switch(self, p):
print('package', p.name, '{')
for classif in p.eClassifiers:
self.generate(classif)
print('}')
@generate.register(ecore.EClass)
def eclass_switch(self, o):
kind = 'interface' if o.interface else 'class'
print(kind, o.name, '{')
for attrib in o.eAttributes:
self.generate(attrib)
print('}')
for sclass in o.eSuperTypes:
print(sclass.name, '<|--', o.name)
for ref in o.eReferences:
self.generate(ref)
@generate.register(ecore.EAttribute)
def eattribute_switch(self, a):
print('\t', a.name, ':', a.eType.name)
@generate.register(ecore.EReference)
def ereference_switch(self, ref):
if ref in self.visited:
return
self.visited.add(ref)
label = f"{ref.name} {ref.lower}..{'*' if ref.many else ref.upper}"
link = "--"
if ref.containment:
link = '*' + link
o_label = ""
if ref.eOpposite:
o = ref.eOpposite
self.visited.add(o)
o_label = f"{o.name} {o.lower}..{'*' if o.many else o.upper}"
if o.containment:
link += '*'
print(f'{ref.eContainer().name} "{label}" {link} "{o_label}" {ref.eType.name}')
else:
link += '>'
print(f'{ref.eContainer().name} "{label}" {link} {ref.eType.name}')
@generate.register(ecore.EEnum)
def enum_switch(self, o):
print('enum', o.name, '{')
for literal in o.eLiterals:
print(literal.name)
print('}')
@generate.register(ecore.EDataType)
def edatatype_switch(self, o):
print('class', o.name, '<< (D,orchid) EDataType>>')
def generate(mm):
print('@startuml')
print('!theme plain')
switch = PlantUMLSwitch()
switch.generate(mm)
print('@enduml')
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} ecore_file")
sys.exit(1)
rset = ResourceSet()
metamodel_resource = rset.get_resource(sys.argv[1])
root = metamodel_resource.contents[0]
generate(root)