-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.js
152 lines (150 loc) · 4.33 KB
/
process.js
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
const fs = require('fs')
const input = require('./input.json');
const output = {elements:[], deletes: [], source: 'magicdraw'};
const toRemove = ['_appliedStereotypeIds', 'appliedStereotypeInstanceId', 'stereotypedElementId', '_modified', '_modifier', '_commitId',
'_docId', '_creator', '_created', '_projectId', '_refId', '_inRefIds'];
const ASIDS = '_appliedStereotypeIds';
const ASID = 'appliedStereotypeInstanceId';
const asiMapping = {};
function getType(values) {
if (!values || values.length < 1) {
return 'StringTaggedValue';
}
switch (values[0].type) {
case 'LiteralBoolean':
return 'BooleanTaggedValue';
case 'LiteralInteger':
return 'IntegerTaggedValue';
case 'LiteralReal':
return 'RealTaggedValue';
case 'ElementValue':
case 'InstanceValue':
return 'ElementTaggedValue';
default:
return 'StringTaggedValue';
}
}
function getValues(values) {
if (!values || values.length < 1) {
return [];
}
const res = [];
for (const v of values) {
if (v.hasOwnProperty('value')) {
res.push({value: v.value});
} else if (v.instanceId) {
res.push(v.instanceId);
} else if (v.elementId) {
res.push(v.elementId);
}
}
return res;
}
function cleanValue(val, skip) {
if (!skip) {
val.taggedValueIds = [];
val.appliedStereotypeIds = [];
}
if (val.hasOwnProperty(ASIDS)) {
delete val[ASIDS];
}
if (val.hasOwnProperty(ASID)) {
delete val[ASID];
}
if (val.visibility) {
val.visibility = null; //value specs should have null visibility?
}
if (val.operand) {
for (let o of val.operand) {
cleanValue(o);
}
}
if (val.min) {
cleanValue(val.min);
}
if (val.max) {
cleanValue(val.max);
}
if (val.expr) {
cleanValue(val.expr);
}
}
for (const e of input.elements) {
if (e.type === 'InstanceSpecification' && e.id.endsWith('_asi')) {
asiMapping[e.id] = e;
}
}
for (const e of input.elements) {
const type = e.type;
const id = e.id;
if (type === 'InstanceSpecification' && id.endsWith('_asi')) {
output.deletes.push({id: id});
continue;
}
const newe = JSON.parse(JSON.stringify(e));
if (id.indexOf('_asi-slot-') > 0 && type === 'Slot') {
const index = id.indexOf('_asi-slot-');
newe.ownerId = id.substring(0, index);
newe.taggedValueOwnerId = id.substring(0, index);
newe.tagDefinitionId = e.definingFeatureId ? e.definingFeatureId : null;
newe.type = getType(e.value);
if (newe.type === 'ElementTaggedValue') {
newe.valueIds = getValues(e.value);
delete newe.value;
} else {
newe.value = getValues(e.value);
for (const val of newe.value) {
cleanValue(val, true);
}
}
if (newe.hasOwnProperty('definingFeatureId')) {
delete newe['definingFeatureId'];
}
if (newe.hasOwnProperty('owningInstanceId')) {
delete newe['owningInstanceId'];
}
}
if (e.hasOwnProperty(ASIDS)) {
newe.appliedStereotypeIds = e[ASIDS];
}
newe.taggedValueIds = [];
if (e[ASID] && asiMapping[e[ASID]]) {
newe.taggedValueIds = asiMapping[e[ASID]].slotIds;
}
for (const key of toRemove) {
if (newe.hasOwnProperty(key)) {
delete newe[key];
}
}
if ((newe.type === 'Slot') && newe.value) { //fix keys in embedded value spec objects
for (const val of newe.value) {
cleanValue(val);
}
}
if (newe.value && !Array.isArray(newe.value)) {
cleanValue(newe.value);
}
if (newe.defaultValue) {
cleanValue(newe.defaultValue);
}
if (newe.upperValue) {
cleanValue(newe.upperValue);
}
if (newe.lowerValue) {
cleanValue(newe.lowerValue);
}
if (newe.specification) {
cleanValue(newe.specification);
}
if (newe.when) {
cleanValue(newe.when);
}
if (newe._contents) {
cleanValue(newe._contents);
}
if (newe.weight) {
cleanValue(newe.weight);
}
output.elements.push(newe);
}
fs.writeFileSync('output.json', JSON.stringify(output));