-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathVSequence.java
More file actions
495 lines (443 loc) · 16.6 KB
/
VSequence.java
File metadata and controls
495 lines (443 loc) · 16.6 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*****************************************************************************
* SysML 2 Pilot Implementation, PlantUML Visualization
* Copyright (c) 2021-2024 Mgnite 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 theGNU 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:
* Hisashi Miyashita, Mgnite Inc.
*
*****************************************************************************/
package org.omg.sysml.plantuml;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.omg.sysml.lang.sysml.Annotation;
import org.omg.sysml.lang.sysml.Comment;
import org.omg.sysml.lang.sysml.Connector;
import org.omg.sysml.lang.sysml.Element;
import org.omg.sysml.lang.sysml.EventOccurrenceUsage;
import org.omg.sysml.lang.sysml.Expression;
import org.omg.sysml.lang.sysml.Feature;
import org.omg.sysml.lang.sysml.FeatureChainExpression;
import org.omg.sysml.lang.sysml.FeatureChaining;
import org.omg.sysml.lang.sysml.FeatureDirectionKind;
import org.omg.sysml.lang.sysml.FeatureReferenceExpression;
import org.omg.sysml.lang.sysml.FlowUsage;
import org.omg.sysml.lang.sysml.MetadataFeature;
import org.omg.sysml.lang.sysml.Namespace;
import org.omg.sysml.lang.sysml.OccurrenceDefinition;
import org.omg.sysml.lang.sysml.OccurrenceUsage;
import org.omg.sysml.lang.sysml.Redefinition;
import org.omg.sysml.lang.sysml.ReferenceSubsetting;
import org.omg.sysml.lang.sysml.Succession;
import org.omg.sysml.lang.sysml.Type;
import org.omg.sysml.plantuml.SysML2PlantUMLStyle.StyleRelSwitch;
import org.omg.sysml.plantuml.SysML2PlantUMLStyle.StyleStereotypeSwitch;
import org.omg.sysml.plantuml.SysML2PlantUMLStyle.StyleSwitch;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.SetMultimap;
public class VSequence extends VDefault {
private static final SysML2PlantUMLStyle style
= new SysML2PlantUMLStyle
("VSequence",
null,
"skinparam roundcorner 20\n"
+ "skinparam BoxPadding 20\n"
+ "skinparam SequenceBoxBackgroundColor #white\n"
+ "skinparam style strictuml\n"
+ "skinparam maxMessageSize 100\n",
new StyleSwitch(new StyleRelSwitch() {
@Override
public String caseConnector(Connector c) {
return " ->> ";
}
},
new StyleStereotypeSwitch() {
@Override
public String caseElement(Element e) {
return "";
}
}));
@Override
protected SysML2PlantUMLStyle getStyle() {
return style;
}
// private List<Type> participants = new ArrayList<Type>();
private static boolean isThisOfFeature(Feature f, String qName) {
return qName.equals(f.getQualifiedName());
}
private static boolean isOfFeature(Feature f, String qName) {
if (isThisOfFeature(f, qName)) return true;
for (Redefinition rd: f.getOwnedRedefinition()) {
Feature rf = rd.getRedefinedFeature();
if (isOfFeature(rf, qName)) return true;
}
return false;
}
private static boolean isSource(Feature f) {
return isOfFeature(f, "Transfers::Transfer::source");
}
private static boolean isTarget(Feature f) {
return isOfFeature(f, "Transfers::Transfer::target");
}
private Element next(FeatureChainExpression fce, boolean first) {
List<Expression> ops = fce.getArgument();
int size = ops.size();
if (size == 0) return null;
Expression ex = ops.get(0);
if (first) {
if (ex instanceof FeatureReferenceExpression) {
FeatureReferenceExpression fre = (FeatureReferenceExpression) ex;
return fre.getReferent();
}
} else {
if (ex instanceof FeatureChainExpression) {
return next((FeatureChainExpression) ex, true);
}
return fce.getTargetFeature();
}
if (ex instanceof FeatureReferenceExpression) {
FeatureReferenceExpression fre = (FeatureReferenceExpression) ex;
return fre.getReferent();
}
return null;
}
private static class Pair {
public final Type participant;
public final EventOccurrenceUsage ev;
public boolean isSame(Pair p) {
return participant.equals(p.participant);
}
public static Pair create(Element participant, Element ev) {
if (!(participant instanceof Type)) return null;
if (!(ev instanceof EventOccurrenceUsage)) return null;
return new Pair((Type) participant, (EventOccurrenceUsage) ev);
}
private Pair(Type participant, EventOccurrenceUsage ev2) {
this.participant = participant;
this.ev = ev2;
}
}
private Pair getEventParticipant(Element e) {
if (e == null) return null;
if (e instanceof FeatureChainExpression) {
FeatureChainExpression fce = (FeatureChainExpression) e;
e = next(fce, true);
Element participant = next(fce, false);
if (participant instanceof Type) {
return Pair.create(participant, e);
}
} else if (e instanceof FeatureReferenceExpression) {
FeatureReferenceExpression fre = (FeatureReferenceExpression) e;
e = fre.getReferent();
} else if (e instanceof Feature) {
Feature f = (Feature) e;
List<FeatureChaining> fcs = f.getOwnedFeatureChaining();
int size = fcs.size();
if (size > 0) {
FeatureChaining fc = fcs.get(size - 1);
e = fc.getChainingFeature();
if (size > 1) {
FeatureChaining fc2 = fcs.get(size - 2);
Element participant = fc2.getChainingFeature();
return Pair.create(participant, e);
}
}
}
Namespace participant = e.getOwningNamespace();
return Pair.create(participant, e);
}
private void addParticipant(Type participant) {
if (checkId(participant)) return;
addPUMLLine(participant, "participant ", getNameAnyway(participant));
append('\n');
}
private class Message extends PRelation implements Comparable<Message> {
private final Pair p1;
private final Pair p2;
Message(InheritKey ik, Pair p1, Pair p2, Feature rel, String desc) {
super(ik, p1.participant, p2.participant, rel, desc);
this.p1 = p1;
this.p2 = p2;
}
private boolean consistent(int i1, int i2) {
if (i1 > 0) return i2 >= 0;
if (i1 < 0) return i2 <= 0;
return true;
}
@Override
public int compareTo(Message mes) {
int r[] = new int[4];
if (p1.isSame(mes.p1)) {
r[0] = compare(p1.ev, mes.p1.ev);
}
if (p1.isSame(mes.p2)) {
r[1] = compare(p1.ev, mes.p2.ev);
}
if (p2.isSame(mes.p1)) {
r[2] = compare(p2.ev, mes.p1.ev);
}
if (p2.isSame(mes.p2)) {
r[3] = compare(p2.ev, mes.p2.ev);
}
int ret = 0;
for (int i = 0; i < r.length; i++) {
int rr = r[i];
if (!consistent(ret, rr)) {
setDescription("Disorder error! " + getDescription());
return 0;
}
if ((ret == 0) && (rr != 0)) {
ret = rr;
}
}
return ret;
}
}
private final List<Message> messages;
private void addMessage(Pair p1, Pair p2, FlowUsage fcu) {
addParticipant(p1.participant);
addParticipant(p2.participant);
messages.add(new Message(makeInheritKey(fcu), p1, p2, fcu, fcu.getName()));
}
@Override
protected String getString() {
Collections.sort(messages);
outputPRelations(messages);
messages.clear();
return super.getString();
}
private Feature getEventOccurrenceRef(Feature p) {
if (!(p instanceof OccurrenceUsage)) return null;
if (p.getDirection() != FeatureDirectionKind.IN) return null;
ReferenceSubsetting rs = p.getOwnedReferenceSubsetting();
if (rs == null) return null;
return rs.getSubsettedFeature();
}
@Override
public String caseFlowUsage(FlowUsage fcu) {
List<Feature> params = fcu.getParameter();
int size = params.size();
for (int i = 0; i < size; i++) {
Feature from = getEventOccurrenceRef(params.get(i));
if (from != null) {
Pair p1 = getEventParticipant(from);
if (p1 == null) return "";
for (int j = i + 1; j < size; j++) {
Feature to = getEventOccurrenceRef(params.get(j));
if (to != null) {
Pair p2 = getEventParticipant(to);
if (p2 == null) continue;
addMessage(p1, p2, fcu);
}
}
}
}
return "";
}
private SetMultimap<OccurrenceUsage, OccurrenceUsage> successionMM = MultimapBuilder.hashKeys().hashSetValues().build();
private boolean fwdCompare(OccurrenceUsage oc1, OccurrenceUsage oc2) {
Set<OccurrenceUsage> ocs = successionMM.get(oc1);
if (ocs.contains(oc2)) return true;
for (OccurrenceUsage oc11: ocs) {
if (fwdCompare(oc11, oc2)) return true;
}
return false;
}
private int compare(OccurrenceUsage oc1, OccurrenceUsage oc2) {
if (fwdCompare(oc1, oc2)) return -1;
if (fwdCompare(oc2, oc1)) return 1;
return 0;
}
@Override
public String caseSuccession(Succession sc) {
for (Element src: sc.getSource()) {
if (!(src instanceof OccurrenceUsage)) continue;
for (Element dest: sc.getTarget()) {
if (!(dest instanceof OccurrenceUsage)) continue;
successionMM.put((OccurrenceUsage) src, (OccurrenceUsage) dest);
}
}
return null;
}
private static class FCMessage {
public final boolean isSource;
public final FlowUsage fcu;
public final Feature[] chains;
public final Type inheriting;
private Element getTarget(int i) {
if (i >= 0) return chains[i];
Element e = chains[0];
for (int k = 0; k > i; k--) {
e = e.getOwner();
if (e == null) return null;
}
return e;
}
@Override
public int hashCode() {
int code = fcu.hashCode();
if (inheriting != null) {
code ^= inheriting.hashCode();
}
for (int i = 0; i < chains.length; i++) {
code ^= chains[i].hashCode();
}
return code;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof FCMessage)) return false;
FCMessage fc = (FCMessage) o;
if (!fcu.equals(fc.fcu)) return false;
if (inheriting == null) {
if (fc.inheriting != null) return false;
int i = chains.length - 1;
int j = fc.chains.length - 1;
for (; ; i--, j--) {
if ((i < 0) && (j < 0)) break;
Element e1 = getTarget(i);
if (e1 == null) return false;
Element e2 = fc.getTarget(j);
if (e2 == null) return false;
if (!e1.equals(e2)) return false;
}
} else {
if (!inheriting.equals(fc.inheriting)) return false;
int len = chains.length;
if (len != fc.chains.length) return false;
for (int i = 0; i < len; i++) {
if (!chains[i].equals(fc.chains[i])) return false;
}
}
return true;
}
private FCMessage(boolean isSource,
FlowUsage fcu,
Feature[] chains,
Type inheriting) {
this.isSource = isSource;
this.fcu = fcu;
this.chains = chains;
this.inheriting = inheriting;
}
private static List<FeatureChaining> getFC(EventOccurrenceUsage ocu) {
ReferenceSubsetting rs = ocu.getOwnedReferenceSubsetting();
if (rs == null) return null;
Feature sf = rs.getSubsettedFeature();
List<FeatureChaining> fcs = sf.getOwnedFeatureChaining();
if (fcs.isEmpty()) return null;
return fcs;
}
public static FCMessage create(EventOccurrenceUsage eou) {
List<FeatureChaining> fcs = getFC(eou);
if (fcs == null) return null;
int j = fcs.size() - 1;
if (j < 1) return null;
Feature tf = fcs.get(j).getChainingFeature();
boolean isSource;
if (isSource(tf)) {
isSource = true;
} else if (isTarget(tf)) {
isSource = false;
} else {
return null;
}
j--;
Feature mf = fcs.get(j).getChainingFeature();
if (!(mf instanceof FlowUsage)) return null;
FlowUsage fcu = (FlowUsage) mf;
Feature[] chains = new Feature[j];
for (int i = 0; i < j; i++) {
chains[i] = fcs.get(i).getChainingFeature();
}
/* To checke the equi
*/
Type inheriting = null;
if (j > 0) {
Type t = eou.getOwningType();
Feature f = chains[0];
if (t.getInheritedFeature().contains(f)) {
inheriting = t;
}
}
return new FCMessage(isSource, fcu, chains, inheriting);
}
}
private Map<FCMessage, Pair> messageMap = new HashMap<>();
private boolean processEventFeatureChaining(EventOccurrenceUsage eou) {
FCMessage fcm = FCMessage.create(eou);
if (fcm == null) return false;
Pair p1 = Pair.create(eou.getOwner(), eou);
if (p1 == null) return false;
Pair p2 = messageMap.get(fcm);
if (p2 == null) {
messageMap.put(fcm, p1);
} else {
if (fcm.isSource) {
addMessage(p1, p2, fcm.fcu);
} else {
addMessage(p2, p1, fcm.fcu);
}
}
return true;
}
@Override
public String caseEventOccurrenceUsage(EventOccurrenceUsage eou) {
if (!processEventFeatureChaining(eou)) return null;
return "";
}
private final boolean isInBox;
@Override
public String caseOccurrenceDefinition(OccurrenceDefinition od) {
if (isInBox) return null;
VSequence vseq = new VSequence(this);
vseq.traverse(od);
if (vseq.isEmpty()) return "";
String name = od.getName();
if (name != null) {
append("box ");
quote(name);
append('\n');
vseq.flush();
append("end box\n");
} else {
vseq.flush();
}
return "";
}
/* Tentatively disable comments and annotations in sequence diagrams. */
@Override
public String caseAnnotation(Annotation a) { return ""; }
@Override
public String caseMetadataFeature(MetadataFeature af) { return ""; }
@Override
public String caseComment(Comment c) { return ""; }
private VSequence(VSequence vseq) {
super(vseq);
this.isInBox = true;
this.messages = vseq.messages;
}
public VSequence() {
setMultiplicityStyle(MultiplicityStyle.NONE);
this.isInBox = false;
this.messages = new ArrayList<Message>();
}
}