-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathSysML2OWL.qvto
More file actions
388 lines (326 loc) · 12.4 KB
/
SysML2OWL.qvto
File metadata and controls
388 lines (326 loc) · 12.4 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
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
/*****************************************************************************
* SysML 2 Pilot Implementation
* Copyright (c) 2018 Model Driven Solutions, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
*
* Contributors:
* Ed Seidewitz
*
*****************************************************************************/
modeltype SysML uses "https://www.omg.org/spec/SysML/20250201";
modeltype OWL uses "http://www.omg.org/spec/SysML/2.0/OWL";
modeltype Ecore uses "http://www.eclipse.org/emf/2002/Ecore";
transformation SysML2OWL(in sysml: SysML, out owl: OWL);
main() {
var roots := sysml.rootObjects()[SysML::Package];
assert fatal (roots->notEmpty()) with log('No root Packages');
var file = object OntologyFile { };
roots->forEach(root) {
log('Transforming ' + root.name + ' to ' + root.URL());
root.mapToDeclarations();
file.documents += new OntologyDocument(root.URL(), owl.rootObjects()[OWL::Axiom]);
};
}
// Utility
query valueOf(s: String): String {
return
if s = null then "<null>"
else if s.oclIsInvalid() then "<invalid>"
else s endif endif;
}
// Constructors
constructor OWL::OntologyDocument::OntologyDocument(prefixDeclarations_: List(OWL::PrefixDeclaration), ontology_: OWL::Ontology) {
prefixDeclarations := prefixDeclarations_;
ontology := ontology_;
}
constructor OWL::OntologyDocument::OntologyDocument(ontologyURL: String, axioms: Set(OWL::Axiom)) {
prefixDeclarations += new PrefixDeclaration(":", "<" + ontologyURL + "#" + ">");
ontology := new Ontology("<" + ontologyURL + ">", axioms);
}
constructor OWL::PrefixDeclaration::PrefixDeclaration(prefixName_: String, fullIRI_: String) {
prefixName := prefixName_;
fullIRI := fullIRI_;
}
constructor OWL::Ontology::Ontology(ontologyIRI_: String, axioms_: Set(OWL::Axiom)) {
ontologyIRI := ontologyIRI_;
axioms := axioms_;
}
constructor OWL::Declaration::Declaration(entity_: OWL::Entity) {
entity := entity_;
}
constructor OWL::ClassReference::ClassReference(referent: OWL::Class) {
_class := referent.entityIRI;
}
constructor OWL::EquivalentClasses::EquivalentClasses(classExpression1: OWL::ClassExpression, classExpression2: OWL::ClassExpression) {
classExpressions += classExpression1;
classExpressions += classExpression2;
}
constructor OWL::ObjectPropertyReference::ObjectPropertyReference(referent: OWL::ObjectProperty) {
objectProperty := referent.entityIRI;
}
constructor OWL::ObjectPropertyDomain::ObjectPropertyDomain(objectProperty: OWL::ObjectPropertyExpression, domain: OWL::ClassExpression) {
objectPropertyExpression := objectProperty;
classExpression := domain;
}
constructor OWL::ObjectPropertyRange::ObjectPropertyRange(objectProperty: OWL::ObjectPropertyExpression, domain: OWL::ClassExpression) {
objectPropertyExpression := objectProperty;
classExpression := domain;
}
constructor OWL::ObjectMinCardinality::ObjectMinCardinality(lower: Integer, objectProperty: OWL::ObjectPropertyExpression) {
cardinality := lower;
objectPropertyExpression := objectProperty;
}
constructor OWL::ObjectMaxCardinality::ObjectMaxCardinality(upper: Integer, objectProperty: OWL::ObjectPropertyExpression) {
cardinality := upper;
objectPropertyExpression := objectProperty;
}
constructor OWL::SubClassOf::SubClassOf(subClass: OWL::ClassExpression, superClass: OWL::ClassExpression) {
subClassExpression := subClass;
superClassExpression := superClass;
}
// Element
query ElementIRI(element: SysML::Element): String {
return ':' + element.qualifiedName();
}
query SysML::Element::name(): String {
return self.name;
}
query SysML::Element::qualifiedName(): String {
var owner := self.owner();
return
if owner = null then self.name()
else owner.qualifiedName() + "." + self.name()
endif;
}
abstract query SysML::Element::IRI(): String;
query SysML::Element::owner(): SysML::Element {
// log('Element ' + self.toString());
var container := self.container();
// log (' container: ' + container.toString());
if container <> null and container.oclIsKindOf(SysML::Membership) then {
container := container.container();
} endif;
// log(' owner: ' + if container = null then "<none>" else container.toString() endif);
return if container = null then null else container.oclAsType(SysML::Element) endif;
}
helper SysML::Element::mapToDeclarations() {
}
mapping SysML::Element::toEntity(): OWL::Entity disjuncts
SysML::Feature::toObjectProperty,
SysML::Type::toClass;
// Namespace
query SysML::Namespace::URL(): String {
return 'http://www.omg.org/SysML/ontology/' + self.qualifiedName().replace(".", "/");
}
helper SysML::Namespace::mapToDeclarations() {
self.ownedMember->mapToDeclarations();
}
//mapping SysML::Package::toDeclaration(): Declaration {
// log('Mapping Namespace ' + valueOf(self.name) + ' to Declaration');
// entity := self.map toEntity();
// log(' entity: ' + entity.toString());
//}
mapping SysML::Namespace::toDeclaration(): OWL::Declaration {
entity := self.map toEntity();
// log('Mapping Namespace ' + valueOf(self.name) + ' to Declaration');
// log(' entity: ' + entity.toString());
}
abstract mapping SysML::Namespace::toEntityBase(): OWL::Entity {
entityIRI := self.IRI();
}
// Class
query SysML::Type::owningFeature(): SysML::Feature {
var owner = self.container();
return
if owner <> null and owner.oclIsKindOf(SysML::Feature) then
owner.oclAsType(SysML::Feature)
else
null
endif;
}
query SysML::Type::name(): String {
var typeName := self.name;
if typeName = null then {
var owningFeature := self.owningFeature();
if owningFeature <> null then {
typeName := '_' + owningFeature.name()
} else {
typeName := '_anonymous_';
} endif;
} endif;
return typeName;
}
query SysML::Type::IRI(): String {
return ElementIRI(self) + '-Class';
}
query SysML::Type::ownedFeatures(): OrderedSet(SysML::Feature) {
return self.ownedFeature;
}
query SysML::Type::implicitSuperclass(): SysML::Type {
var owningFeature := self.owningFeature();
return
if owningFeature = null then null
else owningFeature.type()
endif
}
query SysML::Type::superclasses(): Sequence(SysML::Type) {
var superclasses := self.ownedSpecialization.general;
var implicitSuperclass := self.implicitSuperclass();
if implicitSuperclass <> null then {
superclasses += implicitSuperclass;
} endif;
return superclasses;
}
query SysML::Type::feature(featureName: String): SysML::Feature {
var members := self.ownedMembership.memberElement;
var namedFeature := members[SysML::Feature]![name = featureName];
if namedFeature = null then {
namedFeature := self.superclasses().feature(featureName)![true];
} endif;
return namedFeature;
}
helper SysML::Type::mapToDeclarations() {
self.map toDeclaration();
self.ownedMember->forEach(element) {
// log(' ownedMember: ' + valueOf(element.name));
element.mapToDeclarations();
};
}
mapping SysML::Type::toClass(): OWL::Class
inherits SysML::Namespace::toEntityBase {
log('Mapping Class ' + valueOf(self.name()) + ' to Class ' + valueOf(entityIRI));
self.ownedSpecialization->map toSubClassOf();
var implicitSuperclass := self.implicitSuperclass();
if implicitSuperclass <> null then {
new SubClassOf(
new ClassReference(result),
implicitSuperclass.mapToClassExpression());
} endif;
}
helper SysML::Type::mapToClassExpression(): OWL::ClassExpression {
// log('Mapping Class ' + valueOf(self.name) + ' to ClassExpression');
var entity := self.map toClass();
// log(' entity: ' + entity.toString());
return new OWL::ClassReference(entity);
}
// Feature
query SysML::Feature::name(): String {
var redefinedFeature := self.redefinedFeature();
return
if redefinedFeature <> null then redefinedFeature.name()
else valueOf(self.name)
endif;
}
query SysML::Feature::IRI(): String {
return ElementIRI(self) + '-Feature';
}
query SysML::Feature::redefinedFeature(): SysML::Feature {
log('Feature ' + valueOf(self.name));
var redefinedFeature := self.ownedElement[Redefinition].redefinedFeature![true];
if redefinedFeature = null and self.name <> null then {
var owner := self.owner();
if (owner <> null and owner.oclIsKindOf(SysML::Type)) then {
redefinedFeature :=
owner.oclAsType(SysML::Type).superclasses().feature(self.name)![true];
} endif;
} endif;
log(' redefinedFeature: ' +
if redefinedFeature.oclIsInvalid() then "<invalid>"
else if redefinedFeature = null then "<none>"
else valueOf(redefinedFeature.name) endif endif);
return redefinedFeature;
}
query SysML::Feature::type(): SysML::Type {
var featureType := self.type![true];
if featureType = null then {
var redefinedFeature := self.redefinedFeature();
if redefinedFeature <> null then {
featureType := redefinedFeature.type();
} endif;
} endif;
return featureType;
}
query SysML::Feature::range(): SysML::Type {
return self.type![true]
}
helper SysML::Feature::mapToDeclarations() {
self.map toDeclaration();
self.type->mapToDeclarations();
self.ownedMember->mapToDeclarations();
}
helper newMinCardinality(cardinality: Integer, objectProperty: OWL::ObjectProperty, domain: OWL::Class) {
new EquivalentClasses(
new OWL::ClassReference(domain),
new ObjectMinCardinality(cardinality, new ObjectPropertyReference(objectProperty)));
}
helper newMaxCardinality(cardinality: Integer, objectProperty: OWL::ObjectProperty, domain: OWL::Class) {
new EquivalentClasses(
new OWL::ClassReference(domain),
new ObjectMaxCardinality(cardinality, new ObjectPropertyReference(objectProperty)));
}
helper SysML::Feature::mapMultiplicity(objectProperty: OWL::ObjectProperty, domain: OWL::Class) {
var lower: Expression = null;
var upper: Expression = null;
if self.multiplicity <> null and self.multiplicity.oclIsKindOf(SysML::MultiplicityRange) then {
var range = self.multiplicity.oclAsType(SysML::MultiplicityRange);
lower := range.lowerBound;
upper := range.upperBound;
} endif;
if lower <> null then {
if lower.oclIsKindOf(SysML::LiteralInteger) then {
newMinCardinality(lower.oclAsType(SysML::LiteralInteger).value, objectProperty, domain);
} endif;
} else if upper <> null then {
if upper.oclIsKindOf(SysML::LiteralInteger) then {
newMinCardinality(upper.oclAsType(SysML::LiteralInteger).value, objectProperty, domain);
} endif
} else {
newMinCardinality(1, objectProperty, domain);
} endif endif;
if upper <> null and upper.oclIsKindOf(SysML::LiteralInteger) then {
newMaxCardinality(upper.oclAsType(SysML::LiteralInteger).value, objectProperty, domain);
} else if upper = null then {
newMaxCardinality(1, objectProperty, domain);
} endif endif;
}
mapping SysML::Feature::toObjectProperty(): OWL::ObjectProperty
inherits SysML::Namespace::toEntityBase {
log('Mapping Feature ' + valueOf(self.name) + ' to ObjectProperty ' + valueOf(entityIRI));
var owner = self.owner();
if owner <> null and owner.oclIsKindOf(SysML::Type) then {
var domain := owner.oclAsType(SysML::Type).map toClass();
log(' domain: ' + if domain = null then '<null>' else domain.toString() endif);
new OWL::ObjectPropertyDomain(new ObjectPropertyReference(result), new OWL::ClassReference(domain));
self.mapMultiplicity(result, domain);
} endif;
if self.type->notEmpty() then {
new OWL::ObjectPropertyRange(
new ObjectPropertyReference(result),
self.range().mapToClassExpression());
} endif;
}
helper SysML::Feature::mapToObjectPropertyExpression(): OWL::ObjectPropertyExpression {
return new OWL::ObjectPropertyReference(self.map toObjectProperty());
}
// Generalization
mapping SysML::Specialization::toSubClassOf(): OWL::SubClassOf {
log('Mapping Generalization');
subClassExpression := self.container().oclAsType(SysML::Type).mapToClassExpression();
superClassExpression := self.general.mapToClassExpression();
log(' subClassExpression: ' + valueOf(subClassExpression.oclAsType(ClassReference)._class));
log(' superClassExpression: ' + valueOf(superClassExpression.oclAsType(ClassReference)._class));
}