-
Notifications
You must be signed in to change notification settings - Fork 3
/
UmlFragmentRenderer.cs
158 lines (132 loc) · 6.32 KB
/
UmlFragmentRenderer.cs
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
using LivingDocumentation.Uml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PitstopDocumentationRenderer
{
internal class UmlFragmentRenderer
{
private static readonly string[] ExternalTargets = new[] { "[", "]" };
/// <summary>
/// Render a tree of interactions.
/// </summary>
public static void RenderTree(StringBuilder stringBuilder, IEnumerable<InteractionFragment> branch, Interactions tree)
{
RenderTree(stringBuilder, branch, tree, null);
}
/// <summary>
/// Render a tree of interactions, tracking activations.
/// </summary>
private static void RenderTree(StringBuilder stringBuilder, IEnumerable<InteractionFragment> branch, Interactions tree, List<string> activations)
{
if (activations == null) activations = new List<string>();
var branchActivations = new List<string>(activations);
foreach (var leaf in branch)
{
var leafActivations = new List<string>(activations);
switch (leaf)
{
case Interactions statementList:
RenderTree(stringBuilder, statementList.Fragments, tree, leafActivations);
break;
case Arrow arrow:
RenderArrow(stringBuilder, arrow, branch.ToList(), tree, leafActivations);
break;
case Alt alt:
leafActivations = new List<string>(branchActivations);
RenderGroup(stringBuilder, alt, tree, leafActivations);
break;
}
branchActivations.AddRange(leafActivations.Except(branchActivations));
}
foreach (var branchActivation in branchActivations.Except(activations))
{
stringBuilder.Deactivate(branchActivation);
}
}
/// <summary>
/// Renders a group.
/// </summary>
/// <remarks>
/// A group can be if/alt/else/case/etc.
/// </remarks>
private static void RenderGroup(StringBuilder stringBuilder, Alt alt, Interactions tree, List<string> activations)
{
var switchBuilder = new StringBuilder();
foreach (var section in alt.Sections)
{
var sectionBuilder = new StringBuilder();
RenderTree(sectionBuilder, section.Fragments, tree, new List<string>(activations));
if (sectionBuilder.Length > 0)
{
var first = switchBuilder.Length == 0;
if (first)
{
switchBuilder.Space(5);
if (string.IsNullOrWhiteSpace(section.GroupType))
{
switchBuilder.AltStart();
}
else
{
switchBuilder.Append($"group {(section.GroupType == "case" || section.GroupType == "switch" ? "switch" : section.GroupType)}");
if (section.GroupType == "case")
{
switchBuilder.AppendLine();
switchBuilder.ElseStart();
}
}
}
else
{
switchBuilder.ElseStart();
}
switchBuilder.AppendLine(string.IsNullOrWhiteSpace(section.GroupType) || section.GroupType == "case" ? $" {section.Label}" : $" [{section.Label}]");
switchBuilder.Append(sectionBuilder);
switchBuilder.Space(5);
}
}
if (switchBuilder.Length > 0)
{
stringBuilder.Append(switchBuilder);
stringBuilder.GroupEnd();
}
}
/// <summary>
/// Renders an arrow between two participants.
/// </summary>
/// <remarks>
/// Takes scope (if/alt/group/etc.) into account for correctly close activation lines.
/// </remarks>
private static void RenderArrow(StringBuilder stringBuilder, Arrow arrow, IReadOnlyList<InteractionFragment> scope, Interactions tree, List<string> activations)
{
var target = (arrow.Source != "W" && arrow.Target == "A") ? "Q" : arrow.Target;
stringBuilder.Arrow(arrow.Source, $"-{arrow.Color}>", target, label: arrow.Name);
if (!activations.Contains(arrow.Source))
{
// Scope was activated in current scope
if (arrow.Target != "]" && arrow.Source != "A" && arrow.Source != "W" && !arrow.Source.StartsWith("x ", StringComparison.Ordinal) && scope.Descendants<Arrow>().Last(a => a.Source == arrow.Source) == arrow && arrow.Source != arrow.Target)
{
// This is the last arrow from this source
stringBuilder.Deactivate(arrow.Source);
activations.Remove(arrow.Source);
}
}
if (!activations.Contains(arrow.Target))
{
// Scope was not activated by the parent scope
if ((arrow.Target != "A" || arrow.Source == "W") && arrow.Target != "Q" && arrow.Target != "W" && !arrow.Target.StartsWith("x ", StringComparison.Ordinal) && arrow.Source != arrow.Target && scope.OfType<Arrow>().First(a => a.Target == arrow.Target) == arrow)
{
var previousArrows = arrow.Ancestors().SelectMany(a => a.StatementsBeforeSelf()).OfType<Arrow>().ToList();
if (!previousArrows.Any(a => a.Target == arrow.Target) && !ExternalTargets.Contains(arrow.Target))
{
// There was no earlier activation in the current scope
stringBuilder.Activate(arrow.Target);
activations.Add(arrow.Target);
}
}
}
}
}
}