-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathappend_xmi.py
313 lines (260 loc) · 9.13 KB
/
append_xmi.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
import uuid
import itertools
import functools
from dataclasses import dataclass, field
from itertools import zip_longest as zip_l
from collections import defaultdict
try:
from functools import cached_property
except:
from backports.cached_property import cached_property
import xml_dict
class namespace:
def __init__(self, uri):
self.uri = uri
def __getattr__(self, k):
return f"{{{self.uri}}}{k}"
XMI = namespace("http://schema.omg.org/spec/XMI/2.1")
def new_id():
return str(uuid.uuid4()).upper()
def flatmap(func, *iterable):
return itertools.chain.from_iterable(map(func, *iterable))
@dataclass
class uml_package:
name : str
id : str = field(default_factory=new_id)
@cached_property
def xml(self):
return xml_dict.xml_node(
tag = 'packagedElement',
attributes = {
XMI.type: 'uml:Package',
XMI.id: self.id,
'name': self.name,
'visibility': 'public'
}
)
@dataclass
class uml_class:
name : str
id : str = field(default_factory=new_id)
@cached_property
def xml(self):
return xml_dict.xml_node(
tag = 'packagedElement',
attributes = {
XMI.type: 'uml:Class',
XMI.id: self.id,
'name': self.name,
'visibility': 'public'
}
)
@dataclass
class uml_enumeration:
name : str
values : list
id : str = field(default_factory=new_id)
@cached_property
def xml(self):
return xml_dict.xml_node(
tag = 'packagedElement',
attributes = {
XMI.type: 'uml:Enumeration',
XMI.id: self.id,
'name': self.name,
},
children = [
xml_dict.xml_node(
tag = 'ownedLiteral',
attributes = {
XMI.type: 'uml:EnumerationLiteral',
XMI.id: new_id(),
'name': v,
}
) for v in self.values
]
)
# @todo mutable: inserts into owner.children
def create_connector(assoc_id):
def inner(type_id_connector_id, owner):
# @todo flatstarmap?
type_id, connector_id = type_id_connector_id
li = [
xml_dict.xml_node(
tag = 'memberEnd',
attributes = {
XMI.idref: connector_id,
}
)
]
if owner is None:
li.append(xml_dict.xml_node(
tag = 'ownedEnd',
attributes = {
XMI.type: 'uml:Property',
XMI.id: connector_id,
"association": assoc_id
},
children = [
xml_dict.xml_node(
tag = 'type',
attributes = {
XMI.idref: type_id,
}
)
]
))
else:
owner.children.append(xml_dict.xml_node(
tag = 'ownedAttribute',
attributes = {
XMI.type: 'uml:Property',
XMI.id: connector_id,
"association": assoc_id
},
children = [
xml_dict.xml_node(
tag = 'type',
attributes = {
XMI.idref: type_id,
}
)
]
))
return li
return inner
@dataclass
class uml_assoc_class:
name : str
connector_types : list
id : str = field(default_factory=new_id)
connector_ids : list = None
type : str = "uml:AssociationClass"
owners : list = None
@cached_property
def xml(self):
c_ids = [cid or new_id() for _, cid in zip_l(self.connector_types, self.connector_ids or [])]
owners = self.owners or ([None] * len(c_ids))
return xml_dict.xml_node(
tag = 'packagedElement',
attributes = {
XMI.type: self.type,
XMI.id: self.id,
'name': self.name,
'visibility': 'public'
},
children = list(flatmap(create_connector(self.id), zip(self.connector_types, c_ids), owners))
)
@dataclass
class uml_association:
connector_types : list
id : str = field(default_factory=new_id)
connector_ids : list = None
type : str = "uml:Association"
owners : list = None
@cached_property
def xml(self):
c_ids = [cid or new_id() for _, cid in zip_l(self.connector_types, self.connector_ids or [])]
owners = self.owners or ([None] * len(c_ids))
return xml_dict.xml_node(
tag = 'packagedElement',
attributes = {
XMI.type: self.type,
XMI.id: self.id,
'visibility': 'public'
},
children = list(flatmap(create_connector(self.id), zip(self.connector_types, c_ids), owners))
)
@dataclass
class uml_realization:
supplier : str
client : str
id : str = field(default_factory=new_id)
@cached_property
def xml(self):
return xml_dict.xml_node(
tag = 'packagedElement',
attributes = {
XMI.type: 'uml:Realization',
XMI.id: self.id,
"supplier": self.supplier,
"client": self.client
}
)
class context:
def __init__(self, *, filename=None, content=None):
if filename:
self.content = xml_dict.read(filename)
else:
self.content = content
self.to_node = {}
def v(nd, stack):
if nd.tag == "packagedElement" and "name" in nd.attributes:
self.to_node[(
nd.attributes[XMI.type],
nd.attributes["name"]
)] = nd
self.to_node[
nd.attributes["name"]
] = nd
self.to_node[
nd.attributes[XMI.id]
] = nd
self._recurse(v)
self.superclass = {}
self.subclasses = defaultdict(list)
def v(nd, stack):
if nd.tag == "packagedElement" and nd.attributes.get(XMI.type) == "uml:Class":
gens = [ch for ch in nd.children if ch.tag == "generalization"]
if gens:
gen_name = self.to_node[gens[0].attributes['general']].attributes["name"]
self_name = nd.attributes["name"]
self.superclass[self_name] = gen_name
self.subclasses[gen_name].append(self_name)
self._recurse(v)
self.substitutions = defaultdict(list)
def v(nd, stack):
if nd.tag == "packagedElement" and nd.attributes[XMI.type] == "uml:Substitution":
self.substitutions[nd.attributes["supplier"]].append(nd.attributes["client"])
self._recurse(v)
def write(self, fn):
xml_dict.serialize([self.content], fn)
def package_by_name(self, package_name):
def v(nd, stack):
if nd.tag == "packagedElement" and nd.attributes[XMI.type] == "uml:Package":
if nd.attributes["name"] == package_name:
return nd
return self._recurse(v)
def insert(self, location, element):
nd = element.xml
location.children.append(nd)
nd.parent = location
return nd
def _recurse(self, fn, nd=None, stack=None):
if nd is None: nd = self.content
if stack is None: stack = []
r = fn(nd, stack)
if r is not None:
return r
for ch in nd.children:
r = self._recurse(fn, ch, stack + [nd])
if r is not None:
return r
def print_packages(self):
def v(nd, stack):
if nd.tag == "packagedElement" and nd.attributes[XMI.type] == "uml:Package":
print(" "*len(stack), nd.attributes["name"])
self._recurse(v)
def to_id(self, *args):
return self.to_node[args].attributes[XMI.id]
if __name__ == "__main__":
c = context("..\schemas\IFC.xml")
ifc_package = c.package_by_name("IFC4X3_ADD2")
views_package = c.insert(ifc_package, uml_package("Views"))
gu_package = c.insert(views_package, uml_package("GeneralUsage"))
axis_geom_package = c.insert(gu_package, uml_package("GeneralUsage"))
axis_geom = uml_class("AxisGeometry")
c.insert(axis_geom_package, axis_geom)
c.insert(axis_geom_package, uml_assoc_class("IfcWallAxisGeometryUsage", [axis_geom.id, c.to_id("uml:Class", "IfcWall")]))
c.write("test.xml")
# breakpoint()