-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.js
410 lines (343 loc) · 12.7 KB
/
converter.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/bin/env node
//TODO: Start on this line of the diagram
// uml:Diagram diagramType="InteractionDiagram" documentation="" name="VC Issuance - Sequence" toolName="Visual Paradigm" xmi:id="VjyV8t6GAqBwAQlL"
// walk through the diagrams to get the lifeline names/messages then construct from there
// order operations by Y value, order lifelines/actors by X values and combine actors/lifelines
const fs = require('fs');
const { XMLParser } = require('fast-xml-parser');
//TODO: this should be a config argument
const inFileName = "aug_23.xml";
const outFileName = "test.json";
const outputFormat = "txt";
project = loadXml(inFileName);
var diagrams = extractDiagrams(project)
var mermaid = generateMermaid(inFileName, project);
if(outputFormat == "json")
{
writeJsonOutput(mermaid, outFileName);
}
else if(outputFormat == "txt")
{
writeTxtOutput(mermaid, inFileName);
}
// writeTxtOutput takes the convered mermaid and names array and
// writes each diagram to its own .txt file
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function writeTxtOutput(mermaid, jsonTopLevel) {
//console.log(mermaid[jsonTopLevel].length)
for(var i = 0; i < mermaid[jsonTopLevel].length; i++)
{
var fileName = mermaid[jsonTopLevel][i]['name'] + '.txt';
mermaid[jsonTopLevel][i]['mermaid'] = mermaid[jsonTopLevel][i]['mermaid'].replace(/ /g, '<br/>');
var data = mermaid[jsonTopLevel][i]['mermaid'];
//console.log(fileName, data)
fs.writeFileSync(fileName, data);
}
}
// writeJsonOutput takes the convered mermaid and names array and
// writes it to a .json file
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function writeJsonOutput(mermaid, file) {
var jsonStr = JSON.stringify(mermaid, null, 4);
var fs = require('fs');
fs.writeFileSync(file, jsonStr);
}
// loadXml loads and parses an xmi formatted serialization of a sequence
// diagram.
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function loadXml(inFile) {
const pOptions = {
ignoreAttributes: false,
attributeNamePrefix: '@_'
};
var parser = new XMLParser(pOptions);
var xml = fs.readFileSync(inFile, 'utf8');
var result = parser.parse(xml);
//console.log(JSON.stringify(result, null, 2));
return result;
}
// Extract diagrams finds all of the diagrams in the uml and
// returns them in a consise, ordered JSON array
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function extractDiagrams(result) {
var xmiDiagrams = result['xmi:XMI']['uml:Diagram'];
let diagrams = [];
xmiDiagrams.forEach(d => {
//console.log(d)
var xmiElements = d['uml:Diagram.element']['uml:DiagramElement']
let elements = [];
xmiElements.forEach(e => {
let elem = {
geometry: e['@_geometry'],
preferredShapeType: e['@_preferredShapeType'],
subject: e['@_subject'],
xmiId: e['@_xmi:id'],
fromDiagramElement: e['@_fromDiagramElement'],
toDiagramElement: e['@_toDiagramElement']
}
elements.push(elem)
})
let dgm = {
diagramType: d['@_diagramType'],
documentation: d['@_documentation'],
name: d['@_name'],
toolName: d['@_toolName'],
xmiId: d['@_xmi:id'],
diagramElements: elements
}
diagrams.push(dgm);
})
//console.log(diagrams)
return diagrams;
}
// find collaborators owned member finds the index of the
// ownedMember array that has type uml:Collaboration
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function findCollaborationOwnedMember(umlModel) {
var index = 0;
let ownedMemberArr = [];
ownedMemberArr = umlModel['ownedMember']
ownedMemberArr.forEach(member => {
//console.log(member)
if(member['@_xmi:type'] == 'uml:Collaboration')
index = ownedMemberArr.indexOf(member)
})
return index;
}
// Extract lifelines finds all of the lifelines in the diagram and
// returns them in a concise, ordered JSON array
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function extractLifelines(umlCollaborators, ownedBehaviorIndex) {
let xmlLifelines = umlCollaborators.ownedBehavior[ownedBehaviorIndex].lifeline;
let lifelines = [];
xmlLifelines.forEach(l => {
// console.log(l);
let lfl = {
name: l['@_name'],
id: l['@_xmi:id'],
type: l['@_xmi:type']
}
lifelines.push(lfl);
// console.log(lfl);
})
return lifelines;
}
// Extract fragments finds all of the lifelines in the diagram and
// returns them in a concise, ordered JSON array.
// Fragments include the link that specifies which lifeline
// is associated with the send/receive properties of a message
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function extractFragments(umlCollaborators, ownedBehaviorIndex) {
let xmlFragments = umlCollaborators.ownedBehavior[ownedBehaviorIndex].fragment;
let frags = {};
xmlFragments.forEach(f => {
let frag = {
id: f['@_xmi:id'],
msg: f['@_message'],
type: f['@_xmi:type'],
covered: f['@_covered']
};
frags[frag.id] = frag;
});
return frags;
}
// lookupEventTarget takes the id used to specify send/receive
// targets in messages and returns the human-readable name
// of that target.
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function lookupEventTarget(targetId, fragments, lifelines) {
if (targetId in fragments) {
let target = fragments[targetId].covered;
let lifeline = lifelines.find(l => l.id == target);
if(!lifeline) {
console.log("One of your diagrams has hidden lifelines, delete them!")
return "Lifeline not found"
}
console.log('looking up lifeline ' + target + ':' + lifeline );
return lifeline.name;
}
else {
console.log('fragment not found ' + targetId);
return null;
}
}
// extractOwnedOperationsMsg takes a msg that has an owned
// operation and find the actual msgText
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function extractOwnedOperationsMsg(msg, umlModel) {
let msgText = '';
umlModel.ownedMember.forEach(member => {
var ownedOp = member.ownedOperation;
//console.log(ownedOp);
if(ownedOp) {
if(!ownedOp[0])
ownedOp = [ownedOp];
ownedOp.forEach(param => {
let modelTransition = {
'from': param['xmi:Extension']['modelTransition']['@_from'],
'to': param['xmi:Extension']['modelTransition']['@_to']
};
var idFrom = -1;
var idTo = -1;
if(modelTransition['from'])
idFrom = modelTransition['from'].indexOf(msg.id);
if(modelTransition['to'])
idTo = modelTransition['to'].indexOf(msg.id);
//console.log(idFrom, idTo);
if(idFrom != -1 || idTo != -1) {
msgText = param['@_name'];
}
})
}
})
return msgText;
}
// findOwnedBehaviorIndex finds the index in the ownedBehavior[]
// of the diagram with the specified diagName
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function findOwndedBehaviorIndex(umlCollaborators, diagName) {
var index = -1;
let ownedBehavior = [];
ownedBehavior = umlCollaborators['ownedBehavior'];
ownedBehavior.forEach(behav => {
if(behav['@_name'] == diagName)
index = ownedBehavior.indexOf(behav)
})
return index;
}
// Extract messages finds all of the messages in the specified diagram and
// returns them in a concise, ordered JSON array.
// This includes dereferencing the send/receive targets using
// the fragments variable.
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function extractMessages(umlCollaborators, umlModel, ownedBehaviorIndex, fragments, lifelines) {
let xmlMessages = umlCollaborators.ownedBehavior[ownedBehaviorIndex].message;
let ms = [];
xmlMessages.forEach(m => {
//console.log(m);
// start with the properties that every message has
let msg = {
text: m['@_name'], // Is this actually always set?
from: lookupEventTarget(m['@_sendEvent'], fragments, lifelines),
to: lookupEventTarget(m['@_receiveEvent'], fragments, lifelines),
id: m['@_xmi:id'],
async: m['xmi:Extension'].asynshronous['@_xmi:value'], // Is this actually always set?
type: m['@_xmi:type'],
number: Number(m['xmi:Extension'].number['@_xmi:value'])
}
// now grab the two properties that may or may not be in the data set
if ('fromActivation' in m['xmi:Extension']) {
msg.fromActivation = m['xmi:Extension'].fromActivation['activation']['@_xmi:value'];
}
if ('toActivation' in m['xmi:Extension']) {
msg.toActivation = m['xmi:Extension'].toActivation['activation']['@_xmi:value'];
}
if(!msg['text']){
var msgText = extractOwnedOperationsMsg(msg, umlModel)
msg.text = msgText
}
ms.push(msg);
});
ms = ms.sort((a, b) => a.number - b.number);
return ms;
}
function getDiagramLifelines(diagramElements) {
let lifelines = []
diagramElements.forEach(elem => {
if(elem['preferredShapeType'] == 'InteractionLifeLine' ||
elem['preferredShapeType'] == 'InteractionActor')
lifelines.push(elem)
})
return lifelines
}
function getYVal(geometry) {
var xAndY = String(geometry).split(';')
var yVal = Number(String(xAndY).split(',')[1])
return yVal
}
function orderDiagramMessages(diagramElements) {
let messages = []
diagramElements.forEach(elem => {
if(elem['preferredShapeType'] == 'Message')
messages.push(elem)
})
var numMsg = messages.length;
for(let i = 0; i < numMsg; i++) {
for(let j = 0; j < numMsg - 1; j++) {
if(getYVal(messages[j]['geometry']) > getYVal(messages[j + 1]['geometry'])) {
let tmp = messages[j];
messages[j] = messages[j + 1];
messages[j + 1] = tmp;
}
}
}
return messages;
}
function retrieveMessageFromDiagramMessage(orderedDiagramMessages, messages) {
let ordered = [];
orderedDiagramMessages.forEach(msg => {
var subject = msg['subject']
messages.forEach(m => {
var id = m['id']
if(subject == id)
ordered.push(m)
})
});
return ordered
}
function generateInteractionDiagramMermaid(diagram, messages) {
var orderedDiagramMessages = orderDiagramMessages(diagram['diagramElements']);
var orderedMessages = retrieveMessageFromDiagramMessage(orderedDiagramMessages, messages);
//TODO: loop through the orderedMessages and generate the mermaid for each one
//TODO: inject number into before the m.text with a counter and remove autonumber from mer = ;; THIS SHOULD BE A CONFIG
let mer = `sequenceDiagram
autonumber\n`;
orderedMessages.forEach(function (m) {
mer += ` ${m.from}->>${m.to}:${m.text}\n`
})
return mer;
}
function generateJsonOutput(inFile, diagNames, mermaid) {
var json = {[inFile]:[]};
for(var i = 0; i < diagNames.length; i++) {
var curJson = {
"name" : diagNames[i],
"mermaid" : mermaid[i]
};
json[inFile].push(curJson);
}
return json;
}
function generateMermaid(inFile, project) {
let mermaid = [];
let diagNames = [];
diagrams.forEach(diagram => {
if(diagram['diagramType'] == 'InteractionDiagram') {
var diagName = diagram['name']
var umlModel = project['xmi:XMI']['uml:Model'];
var collaboratorsIndex = findCollaborationOwnedMember(umlModel);
var umlCollaborators = project['xmi:XMI']['uml:Model'].ownedMember[collaboratorsIndex];
var ownedBehaviorIndex = findOwndedBehaviorIndex(umlCollaborators, diagName);
var fragments = extractFragments(umlCollaborators, ownedBehaviorIndex);
var lifelines = extractLifelines(umlCollaborators, ownedBehaviorIndex);
var messages = extractMessages(umlCollaborators, umlModel, ownedBehaviorIndex, fragments, lifelines);
diagNames.push(diagName);
mermaid.push(generateInteractionDiagramMermaid(diagram, messages));
}
});
var json = generateJsonOutput(inFile, diagNames, mermaid);
return json;
}
//console.log(lifelines);
//console.log(xmlFragments);