dsh-model-everything 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en-US.md +12 -0
- package/README.md +4 -0
- package/lib/client.js +2829 -224
- package/lib/render/channels.js +243 -0
- package/lib/render/collapse.js +12 -20
- package/lib/render/components.js +99 -0
- package/lib/render/coords.js +288 -0
- package/lib/render/cycle-break.js +147 -0
- package/lib/render/dummy.js +43 -0
- package/lib/render/from-mermaid.js +34 -10
- package/lib/render/from-plantuml.js +318 -46
- package/lib/render/layer.js +91 -0
- package/lib/render/layout.js +27 -11
- package/lib/render/network-simplex.js +272 -0
- package/lib/render/order.js +122 -0
- package/lib/render/paint.js +1227 -69
- package/lib/render/place.js +393 -121
- package/lib/render/route.js +225 -0
- package/lib/render/svg.js +146 -21
- package/lib/render/symbol.js +108 -0
- package/lib/render/text.js +74 -0
- package/lib/render/trunk.js +213 -0
- package/lib/render/wrap.js +87 -0
- package/lib/types/render/channels.d.ts +63 -0
- package/lib/types/render/components.d.ts +57 -0
- package/lib/types/render/coords.d.ts +64 -0
- package/lib/types/render/cycle-break.d.ts +45 -0
- package/lib/types/render/dummy.d.ts +32 -0
- package/lib/types/render/from-plantuml.d.ts +12 -6
- package/lib/types/render/layer.d.ts +28 -0
- package/lib/types/render/layout.d.ts +43 -11
- package/lib/types/render/model.d.ts +17 -0
- package/lib/types/render/network-simplex.d.ts +54 -0
- package/lib/types/render/order.d.ts +27 -0
- package/lib/types/render/paint.d.ts +157 -10
- package/lib/types/render/place.d.ts +40 -19
- package/lib/types/render/route.d.ts +77 -0
- package/lib/types/render/svg.d.ts +1 -1
- package/lib/types/render/symbol.d.ts +73 -0
- package/lib/types/render/text.d.ts +38 -0
- package/lib/types/render/trunk.d.ts +104 -0
- package/lib/types/render/wrap.d.ts +27 -0
- package/package.json +148 -148
- package/skills/model-everything-diagram/SKILL.md +85 -82
- package/src/render/channels.ts +309 -0
- package/src/render/collapse.ts +150 -155
- package/src/render/components.ts +126 -0
- package/src/render/coords.ts +328 -0
- package/src/render/cycle-break.ts +164 -0
- package/src/render/dummy.ts +65 -0
- package/src/render/from-mermaid.ts +261 -236
- package/src/render/from-plantuml.ts +469 -178
- package/src/render/layer.ts +98 -0
- package/src/render/layout.ts +101 -62
- package/src/render/model.ts +86 -67
- package/src/render/network-simplex.ts +290 -0
- package/src/render/order.ts +139 -0
- package/src/render/paint.ts +1570 -290
- package/src/render/place.ts +532 -234
- package/src/render/route.ts +269 -0
- package/src/render/svg.ts +274 -127
- package/src/render/symbol.ts +137 -0
- package/src/render/text.ts +86 -0
- package/src/render/trunk.ts +275 -0
- package/src/render/wrap.ts +103 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fan-in trunks: a batch of arrows into one box from one side, drawn as one bundle.
|
|
3
|
+
*
|
|
4
|
+
* A node that four relations point at is drawn as four ports along its side and four lines converging on it. Each
|
|
5
|
+
* line is right on its own, and the picture is wrong: the bundle reads as a comb, and a reader counts ports to count
|
|
6
|
+
* arrows. The mature layered implementations solve this by merging the edges - ELK's *Merge Edges* with junction
|
|
7
|
+
* points, Graphviz's `concentrate` - and the shape they produce is one straight run outside the node, with a branch
|
|
8
|
+
* joining it wherever a separate edge arrives, and one shared stub into the node.
|
|
9
|
+
*
|
|
10
|
+
* This module decides only which ends may merge, where the shared run sits and which of the branches becomes the
|
|
11
|
+
* shared entry. It is pure geometry over boxes and ports, so the rules can be asserted without a canvas; the painter
|
|
12
|
+
* draws the answer.
|
|
13
|
+
*
|
|
14
|
+
* The rules, and why each one exists:
|
|
15
|
+
*
|
|
16
|
+
* - **One target, one entry side.** Edges that arrive from different sides are travelling in different directions,
|
|
17
|
+
* and a trunk that changed axis would be two trunks.
|
|
18
|
+
* - **The exit faces the trunk.** A branch arrives at the trunk along the direction its own port leaves by, so the
|
|
19
|
+
* trunk is outside the source as well as outside the target. An end that does not face the trunk is not merged: it
|
|
20
|
+
* would have to turn before it got there, which is the bend the merge was supposed to save.
|
|
21
|
+
* - **Distinct sources, three or more.** Two merged edges read as one thick line. A pair of relations about one pair
|
|
22
|
+
* has its own, older rule - they stay visibly apart - and that rule is the distinct-source rule here: two
|
|
23
|
+
* statements about one pair share their source, so they can never both be a branch of one trunk.
|
|
24
|
+
* - **One arrow semantics.** A composition and an association are different statements about the relation; drawn on
|
|
25
|
+
* one trunk they would be read as one statement. Ends that disagree on their arrow are not merged with each other.
|
|
26
|
+
* - **Clear of every box.** A trunk that crosses a box is the same defect as a route that does, and it is worse here
|
|
27
|
+
* because it is drawn once for the whole bundle.
|
|
28
|
+
* - **One trunk per line.** Two groups whose trunks would be drawn on one line would read as one bundle between two
|
|
29
|
+
* unrelated targets, so the later group is refused.
|
|
30
|
+
*
|
|
31
|
+
* The shared entry is the middle of the target's side. A bundle enters the target through one port, and a side that
|
|
32
|
+
* carries one port is centred on it, so the trunk runs to the middle and every branch joins it there: the branches
|
|
33
|
+
* that are not in the middle bend, which is the price of one entry that reads as centred rather than one that leans
|
|
34
|
+
* towards whichever branch happened to arrive straightest.
|
|
35
|
+
*
|
|
36
|
+
* Merging forces every branch but one to bend twice, so an unmerged drawing of `k` straight branches costs
|
|
37
|
+
* `2 * (n - k)` bends and a merged one costs `2 * (n - 1)`; the branch nearest the middle is the lead because it is
|
|
38
|
+
* the only one that can still reach the shared port without a horizontal leg, and on a target whose middle happens
|
|
39
|
+
* to fall on a branch's own line that branch is free, as before.
|
|
40
|
+
*
|
|
41
|
+
* What this module does not decide: the colour of a branch, the text of its label, and how a branch's own route
|
|
42
|
+
* reaches the trunk. Those belong to the painter.
|
|
43
|
+
*/
|
|
44
|
+
import { crossesBox, normalOf, ROUTE_STUB } from './route.js';
|
|
45
|
+
/** The least a trunk is worth drawing for: two merged edges read as one thick line. */
|
|
46
|
+
const TRUNK_MIN_EDGES = 3;
|
|
47
|
+
/** Does this side move along the page's vertical axis? A run on it is horizontal. */
|
|
48
|
+
function isVerticalSide(side) {
|
|
49
|
+
return side === 'top' || side === 'bottom';
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Where the trunk outside a box sits.
|
|
53
|
+
*
|
|
54
|
+
* The same stub the router leaves before it turns, so the bundle starts exactly where a single route would have
|
|
55
|
+
* turned: about `ROUTE_STUB` outside the target, square to the side it enters.
|
|
56
|
+
*/
|
|
57
|
+
function trunkAt(box, side) {
|
|
58
|
+
if (side === 'top')
|
|
59
|
+
return box.y - ROUTE_STUB;
|
|
60
|
+
if (side === 'bottom')
|
|
61
|
+
return box.y + box.height + ROUTE_STUB;
|
|
62
|
+
if (side === 'left')
|
|
63
|
+
return box.x - ROUTE_STUB;
|
|
64
|
+
return box.x + box.width + ROUTE_STUB;
|
|
65
|
+
}
|
|
66
|
+
/** The coordinate a point takes along the side it sits on: where along that side the port is. */
|
|
67
|
+
function alongSide(point, side) {
|
|
68
|
+
return isVerticalSide(side) ? point.x : point.y;
|
|
69
|
+
}
|
|
70
|
+
/** The coordinate a point takes on its side's outward normal: the axis a trunk outside that side moves on. */
|
|
71
|
+
function alongNormal(point, side) {
|
|
72
|
+
return isVerticalSide(side) ? point.y : point.x;
|
|
73
|
+
}
|
|
74
|
+
/** Does the port on `side` travel toward the trunk at `at`? A branch only merges into a trunk it moves toward. */
|
|
75
|
+
function facesTrunk(point, side, at) {
|
|
76
|
+
const normal = normalOf(side);
|
|
77
|
+
const outward = isVerticalSide(side) ? normal.y : normal.x;
|
|
78
|
+
return (at - alongNormal(point, side)) * outward > 0.5;
|
|
79
|
+
}
|
|
80
|
+
/** The group an edge end belongs to: its target, the side it enters by, and the arrow it means. */
|
|
81
|
+
function keyOf(end) {
|
|
82
|
+
return end.to + '\u0000' + end.entry + '\u0000' + (end.arrow ?? '');
|
|
83
|
+
}
|
|
84
|
+
/** Is this axis-aligned segment clear of every box it does not attach to? */
|
|
85
|
+
function clearSegment(boxes, skip, x1, y1, x2, y2) {
|
|
86
|
+
for (const box of boxes) {
|
|
87
|
+
if (skip.includes(box))
|
|
88
|
+
continue;
|
|
89
|
+
if (crossesBox(x1, y1, x2, y2, box))
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/** Would this run lie on the line of an accepted trunk and share a stretch of it? */
|
|
95
|
+
function onOneLine(group, side, at, low, high) {
|
|
96
|
+
if (isVerticalSide(group.side) !== isVerticalSide(side))
|
|
97
|
+
return false;
|
|
98
|
+
if (group.at !== at)
|
|
99
|
+
return false;
|
|
100
|
+
return Math.min(group.to, high) - Math.max(group.from, low) > 0.5;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Decide which ends merge, and where each accepted trunk runs.
|
|
104
|
+
*
|
|
105
|
+
* Ends that cannot merge are simply absent from the plan; the painter draws them as it always has. Groups are
|
|
106
|
+
* ordered by the first member the source wrote, so the answer does not depend on map iteration order.
|
|
107
|
+
*/
|
|
108
|
+
export function planTrunks(input) {
|
|
109
|
+
const groups = [];
|
|
110
|
+
const groupOf = new Map();
|
|
111
|
+
// Per edge first: the cheap rules. An end that fails one of them cannot join any group, so it never reaches the
|
|
112
|
+
// count that decides whether a group is worth drawing.
|
|
113
|
+
const buckets = new Map();
|
|
114
|
+
for (const end of input.ends) {
|
|
115
|
+
if (end.runY === undefined)
|
|
116
|
+
continue;
|
|
117
|
+
// The branch approaches the trunk along its own exit normal, so the two ends must move on one axis: a mixed pair
|
|
118
|
+
// would have to turn before it reached the trunk, which is the bend the merge exists to save.
|
|
119
|
+
if (isVerticalSide(end.exit) !== isVerticalSide(end.entry))
|
|
120
|
+
continue;
|
|
121
|
+
const source = input.boxOf(end.from);
|
|
122
|
+
const target = input.boxOf(end.to);
|
|
123
|
+
if (source === undefined || target === undefined)
|
|
124
|
+
continue;
|
|
125
|
+
if (source === target)
|
|
126
|
+
continue;
|
|
127
|
+
const exit = input.exitPortOf(end.index);
|
|
128
|
+
if (exit === undefined)
|
|
129
|
+
continue;
|
|
130
|
+
const at = trunkAt(target, end.entry);
|
|
131
|
+
if (!facesTrunk(exit, end.exit, at))
|
|
132
|
+
continue;
|
|
133
|
+
const candidate = { end, source, target, at, exit, branch: alongSide(exit, end.exit) };
|
|
134
|
+
const key = keyOf(end);
|
|
135
|
+
const list = buckets.get(key);
|
|
136
|
+
if (list === undefined)
|
|
137
|
+
buckets.set(key, [candidate]);
|
|
138
|
+
else
|
|
139
|
+
list.push(candidate);
|
|
140
|
+
}
|
|
141
|
+
for (const list of buckets.values()) {
|
|
142
|
+
// A pair of relations about one node is not two branches: the pair's own rule keeps them visibly apart, and both
|
|
143
|
+
// of them name the same source, so dropping the duplicated source drops the whole pair. Neither joins, and the
|
|
144
|
+
// group keeps the singles.
|
|
145
|
+
const bySource = new Map();
|
|
146
|
+
for (const candidate of list) {
|
|
147
|
+
const seen = bySource.get(candidate.end.from);
|
|
148
|
+
if (seen === undefined)
|
|
149
|
+
bySource.set(candidate.end.from, [candidate]);
|
|
150
|
+
else
|
|
151
|
+
seen.push(candidate);
|
|
152
|
+
}
|
|
153
|
+
const members = list.filter((candidate) => bySource.get(candidate.end.from).length === 1);
|
|
154
|
+
if (members.length < TRUNK_MIN_EDGES)
|
|
155
|
+
continue;
|
|
156
|
+
const target = members[0].target;
|
|
157
|
+
const side = members[0].end.entry;
|
|
158
|
+
const at = members[0].at;
|
|
159
|
+
// The shared entry is the middle of the target's side. A side that carries one port is centred on it, and a bundle
|
|
160
|
+
// enters through one port, so the run reaches the middle rather than whichever branch asked for the middle first.
|
|
161
|
+
const entryAlong = isVerticalSide(side) ? target.x + target.width / 2 : target.y + target.height / 2;
|
|
162
|
+
// The lead is the branch nearest that middle: the only member that can reach the shared port without a lateral
|
|
163
|
+
// leg. The source's order breaks a tie, so the answer does not depend on map iteration.
|
|
164
|
+
let lead = members[0];
|
|
165
|
+
for (const candidate of members) {
|
|
166
|
+
if (Math.abs(candidate.branch - entryAlong) < Math.abs(lead.branch - entryAlong) - 1e-9)
|
|
167
|
+
lead = candidate;
|
|
168
|
+
}
|
|
169
|
+
// The run: the whole stretch the branches and the one port span, so every branch joins a line that reaches it.
|
|
170
|
+
let low = entryAlong;
|
|
171
|
+
let high = entryAlong;
|
|
172
|
+
for (const candidate of members) {
|
|
173
|
+
low = Math.min(low, candidate.branch);
|
|
174
|
+
high = Math.max(high, candidate.branch);
|
|
175
|
+
}
|
|
176
|
+
// Clearance. The run is checked against every box but the target; each branch's own drop is checked against every
|
|
177
|
+
// box but its source and the target. The drop leaves a port square to its side, so those are the only two boxes it
|
|
178
|
+
// can legitimately touch.
|
|
179
|
+
const vertical = isVerticalSide(side);
|
|
180
|
+
const run = vertical ? [low, at, high, at] : [at, low, at, high];
|
|
181
|
+
if (!clearSegment(input.boxes, [target], run[0], run[1], run[2], run[3]))
|
|
182
|
+
continue;
|
|
183
|
+
let blocked = false;
|
|
184
|
+
for (const candidate of members) {
|
|
185
|
+
// The drop: from the branch's own port to the point where it meets the trunk, along its exit normal.
|
|
186
|
+
const drop = vertical
|
|
187
|
+
? [candidate.branch, candidate.exit.y, candidate.branch, at]
|
|
188
|
+
: [candidate.exit.x, candidate.branch, at, candidate.branch];
|
|
189
|
+
if (!clearSegment(input.boxes, [candidate.source, target], drop[0], drop[1], drop[2], drop[3]))
|
|
190
|
+
blocked = true;
|
|
191
|
+
}
|
|
192
|
+
if (blocked)
|
|
193
|
+
continue;
|
|
194
|
+
// Two trunks on one line would be drawn as one bundle. The first group keeps the line; the later one travels alone.
|
|
195
|
+
if (groups.some((group) => onOneLine(group, side, at, low, high)))
|
|
196
|
+
continue;
|
|
197
|
+
const id = groups.length;
|
|
198
|
+
groups.push({
|
|
199
|
+
id,
|
|
200
|
+
target: members[0].end.to,
|
|
201
|
+
side,
|
|
202
|
+
at,
|
|
203
|
+
entryAlong,
|
|
204
|
+
from: low,
|
|
205
|
+
to: high,
|
|
206
|
+
lead: lead.end.index,
|
|
207
|
+
members: members.map((candidate) => candidate.end.index),
|
|
208
|
+
});
|
|
209
|
+
for (const candidate of members)
|
|
210
|
+
groupOf.set(candidate.end.index, id);
|
|
211
|
+
}
|
|
212
|
+
return { groups, groupOf };
|
|
213
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapping: turning one endless column into a page.
|
|
3
|
+
*
|
|
4
|
+
* A chain of twelve boxes has twelve layers, and layering alone draws it as a single column taller than anything a
|
|
5
|
+
* reader can take in - a strip, not a drawing. Graphviz and ELK both answer this with a wrapping pass: cut the layer
|
|
6
|
+
* sequence into bands and stand the bands side by side, so reading order becomes "down the page, then the next
|
|
7
|
+
* column", the way text wraps.
|
|
8
|
+
*
|
|
9
|
+
* The cut is chosen, not guessed: each candidate number of bands is scored by how close the packed page gets to the
|
|
10
|
+
* aspect ratio a page and a landscape screen share, and the best one wins. A drawing that is already close to that
|
|
11
|
+
* shape is left alone - wrapping exists for the strip, not for every tall drawing.
|
|
12
|
+
*
|
|
13
|
+
* Pure: layer sizes in, band ranges and offsets out. The caller applies them.
|
|
14
|
+
*/
|
|
15
|
+
/** The aspect ratio a page and a landscape screen share. */
|
|
16
|
+
const PAGE_ASPECT = 1.6;
|
|
17
|
+
/** A drawing shorter than this many layers is read better stacked, whatever its proportions. */
|
|
18
|
+
const MIN_LAYERS_TO_WRAP = 4;
|
|
19
|
+
/** A drawing that is not this tall relative to its width is not a strip. */
|
|
20
|
+
const STRIP_RATIO = 2;
|
|
21
|
+
/** The height of a run of layers with the gap between them. */
|
|
22
|
+
function runHeight(layerHeight, first, last, gap) {
|
|
23
|
+
let total = 0;
|
|
24
|
+
for (let index = first; index <= last; index += 1) {
|
|
25
|
+
total += layerHeight[index];
|
|
26
|
+
if (index < last)
|
|
27
|
+
total += gap;
|
|
28
|
+
}
|
|
29
|
+
return total;
|
|
30
|
+
}
|
|
31
|
+
/** The width of a run of layers: the widest layer sets it. */
|
|
32
|
+
function runWidth(layerWidth, first, last) {
|
|
33
|
+
let widest = 0;
|
|
34
|
+
for (let index = first; index <= last; index += 1)
|
|
35
|
+
widest = Math.max(widest, layerWidth[index]);
|
|
36
|
+
return widest;
|
|
37
|
+
}
|
|
38
|
+
/** How far the packed page is from the target shape, in log space so half and double count the same. */
|
|
39
|
+
function shapeError(width, height, aspect) {
|
|
40
|
+
if (width <= 0 || height <= 0)
|
|
41
|
+
return Infinity;
|
|
42
|
+
const ratio = Math.max(width, height) / Math.min(width, height);
|
|
43
|
+
return Math.abs(Math.log(ratio / aspect));
|
|
44
|
+
}
|
|
45
|
+
export function planBands(layerWidth, layerHeight, gap, aspect = PAGE_ASPECT) {
|
|
46
|
+
const count = layerWidth.length;
|
|
47
|
+
if (count === 0)
|
|
48
|
+
return { bands: [], width: 0, height: 0 };
|
|
49
|
+
const single = {
|
|
50
|
+
bands: [{ first: 0, last: count - 1, offsetX: 0 }],
|
|
51
|
+
width: runWidth(layerWidth, 0, count - 1),
|
|
52
|
+
height: runHeight(layerHeight, 0, count - 1, gap),
|
|
53
|
+
};
|
|
54
|
+
if (count < MIN_LAYERS_TO_WRAP || single.height <= single.width * STRIP_RATIO)
|
|
55
|
+
return single;
|
|
56
|
+
const columnGap = gap * 2;
|
|
57
|
+
let best = single;
|
|
58
|
+
let bestError = shapeError(single.width, single.height, aspect);
|
|
59
|
+
for (let bands = 2; bands < count; bands += 1) {
|
|
60
|
+
const base = Math.floor(count / bands);
|
|
61
|
+
const extra = count % bands;
|
|
62
|
+
const ranges = [];
|
|
63
|
+
let cursor = 0;
|
|
64
|
+
for (let band = 0; band < bands; band += 1) {
|
|
65
|
+
const size = base + (band < extra ? 1 : 0);
|
|
66
|
+
ranges.push({ first: cursor, last: cursor + size - 1 });
|
|
67
|
+
cursor += size;
|
|
68
|
+
}
|
|
69
|
+
let width = columnGap * (bands - 1);
|
|
70
|
+
let height = 0;
|
|
71
|
+
for (const range of ranges) {
|
|
72
|
+
width += runWidth(layerWidth, range.first, range.last);
|
|
73
|
+
height = Math.max(height, runHeight(layerHeight, range.first, range.last, gap));
|
|
74
|
+
}
|
|
75
|
+
if (shapeError(width, height, aspect) >= bestError)
|
|
76
|
+
continue;
|
|
77
|
+
const planned = [];
|
|
78
|
+
let offsetX = 0;
|
|
79
|
+
for (const range of ranges) {
|
|
80
|
+
planned.push({ first: range.first, last: range.last, offsetX });
|
|
81
|
+
offsetX += runWidth(layerWidth, range.first, range.last) + columnGap;
|
|
82
|
+
}
|
|
83
|
+
best = { bands: planned, width, height };
|
|
84
|
+
bestError = shapeError(width, height, aspect);
|
|
85
|
+
}
|
|
86
|
+
return best;
|
|
87
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Channel assignment: where the vertical lanes and the horizontal runs between two rows go.
|
|
3
|
+
*
|
|
4
|
+
* The stages before this one decide where the boxes are, and the painter decides what a box looks like. What is left
|
|
5
|
+
* is the space between two rows, and an edge that decides that space alone will collide with the next one: every long
|
|
6
|
+
* edge has to cross a row somewhere, and every edge whose two ends are not vertically aligned needs one horizontal run
|
|
7
|
+
* inside the gap it crosses. Chosen independently, two runs land on one height and read as a junction, and a run
|
|
8
|
+
* sweeps across a vertical another edge is already using.
|
|
9
|
+
*
|
|
10
|
+
* So this stage assigns both, after the boxes have settled and before anything is drawn:
|
|
11
|
+
*
|
|
12
|
+
* - **A lane's x.** A long edge owns one vertical lane through the rows it crosses. The x is a position no box
|
|
13
|
+
* occupies in any of those rows, and lanes keep the order their dummies have in the ordering, so two lanes never
|
|
14
|
+
* share an x inside one gap. Alignment is worth a detour: a lane that lands inside a box's own port span turns a
|
|
15
|
+
* four-bend staircase into a straight drop, so alignment comes before distance from the proposed position.
|
|
16
|
+
* - **A run's y.** The horizontal runs inside one gap are stacked at distinct heights - Graphviz's virtual nodes - so
|
|
17
|
+
* two runs never lie on one line, and a nested run stays near the box it leaves.
|
|
18
|
+
*
|
|
19
|
+
* Pure: rows, chains and coordinates in; lane positions and run heights out.
|
|
20
|
+
*/
|
|
21
|
+
import type { Chain } from './dummy.js';
|
|
22
|
+
/** A box, as far as routing is concerned: only a box can move a lane. */
|
|
23
|
+
export interface ChannelBox {
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
export interface ChannelInput {
|
|
30
|
+
/** The ordered rows, dummies included: a lane keeps its dummy's order. */
|
|
31
|
+
layers: string[][];
|
|
32
|
+
/** Every edge's chain, from the layout source to the layout target. */
|
|
33
|
+
chains: Chain[];
|
|
34
|
+
/** A dummy instance, and the lane it belongs to. */
|
|
35
|
+
chainOf: ReadonlyMap<string, string>;
|
|
36
|
+
/** The boxes that block a lane. A dummy has none. */
|
|
37
|
+
boxes: ReadonlyMap<string, ChannelBox>;
|
|
38
|
+
/** The top of each row, and how tall it is. */
|
|
39
|
+
layerY: number[];
|
|
40
|
+
layerHeight: number[];
|
|
41
|
+
/** What coordinate assignment proposed for each lane. */
|
|
42
|
+
laneX: ReadonlyMap<string, number>;
|
|
43
|
+
}
|
|
44
|
+
export interface ChannelOptions {
|
|
45
|
+
/** How far a lane stays from a box in a row it crosses. */
|
|
46
|
+
clearance: number;
|
|
47
|
+
/** The narrowest two lanes sharing a gap may be. */
|
|
48
|
+
spacing: number;
|
|
49
|
+
}
|
|
50
|
+
/** One long edge's lane: the x of its vertical run, and the height of a run in each gap it crosses. */
|
|
51
|
+
export interface LaneChannel {
|
|
52
|
+
x: number;
|
|
53
|
+
y: number[];
|
|
54
|
+
}
|
|
55
|
+
export interface ChannelPlan {
|
|
56
|
+
/** By diagram edge index: the lane a long edge reserved, with one corridor height per gap it crosses. */
|
|
57
|
+
lanes: Map<number, LaneChannel>;
|
|
58
|
+
/** By diagram edge index: the height of the one run an edge between two adjacent rows needs. */
|
|
59
|
+
runs: Map<number, number>;
|
|
60
|
+
}
|
|
61
|
+
/** How far a port sits from a box's corner. The painter places ports from this same number. */
|
|
62
|
+
export declare const PORT_INSET = 10;
|
|
63
|
+
export declare function assignChannels(input: ChannelInput, options: ChannelOptions): ChannelPlan;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two decompositions a drawing needs before it can be packed.
|
|
3
|
+
*
|
|
4
|
+
* **Weakly connected components.** Two subgraphs with no edge between them are two drawings that happen to share a
|
|
5
|
+
* page. Laying them out together makes them share one set of layers, so the wider one sets every column width and
|
|
6
|
+
* the narrower one is squeezed beside it; moving one moves the other. Split first, lay each out on its own, then pack
|
|
7
|
+
* the rectangles.
|
|
8
|
+
*
|
|
9
|
+
* **Packing.** A pile of rectangles has to become a page. The target is the aspect ratio a page and a screen share -
|
|
10
|
+
* 1.6 - and a shelf packer is enough to get there: sort by height, fill a shelf until the next rectangle would make
|
|
11
|
+
* the drawing wider than the page it is aiming at, then start the next shelf. No overlap, and a deterministic order,
|
|
12
|
+
* so the same rectangles always pack the same way.
|
|
13
|
+
*
|
|
14
|
+
* Pure, and asserted on the rectangles themselves.
|
|
15
|
+
*/
|
|
16
|
+
export interface SizedRectangle {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
export interface PackedRectangle {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The nodes of each weakly connected component, in the order the nodes were written.
|
|
28
|
+
*
|
|
29
|
+
* An isolated node is a component of its own: it is not noise, it is a drawing with nothing joining it, and the
|
|
30
|
+
* packer is what decides where it goes.
|
|
31
|
+
*/
|
|
32
|
+
export declare function weakComponents(nodes: ReadonlyArray<{
|
|
33
|
+
id: string;
|
|
34
|
+
}>, edges: ReadonlyArray<{
|
|
35
|
+
from: string;
|
|
36
|
+
to: string;
|
|
37
|
+
}>): string[][];
|
|
38
|
+
export interface PackOptions {
|
|
39
|
+
/** The empty strip kept between two rectangles, on both axes. */
|
|
40
|
+
gap?: number;
|
|
41
|
+
/** The page the packer aims at; the drawing comes out as close to it as the rectangles allow. */
|
|
42
|
+
aspect?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Pack rectangles into a page of roughly `aspect`, using shelves.
|
|
46
|
+
*
|
|
47
|
+
* The shelf width is derived from the total area and the target aspect: that is the width at which a perfect grid
|
|
48
|
+
* would have the wanted shape, so a shelf that fills it lands near the target. Rectangles are taken tallest first,
|
|
49
|
+
* which is what keeps a shelf from being mostly empty air above a short rectangle.
|
|
50
|
+
*/
|
|
51
|
+
export declare function packRectangles(rectangles: ReadonlyArray<SizedRectangle>, options?: PackOptions): {
|
|
52
|
+
placements: PackedRectangle[];
|
|
53
|
+
width: number;
|
|
54
|
+
height: number;
|
|
55
|
+
};
|
|
56
|
+
/** The aspect ratio of a box, as a number a reader can compare against the page's. */
|
|
57
|
+
export declare function aspectOf(width: number, height: number): number;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coordinate assignment: how far right each box sits. Brandes-Kopf.
|
|
3
|
+
*
|
|
4
|
+
* The layers are rows and the order inside them is settled; the only freedom left is the x of each box. Three
|
|
5
|
+
* properties make a drawing readable:
|
|
6
|
+
*
|
|
7
|
+
* - A box sits over what it is connected to, and a chain of boxes sits on one vertical line. Brandes and Kopf get
|
|
8
|
+
* this by *aligning*: every box is offered the median of its neighbours in the layer just above (or just below) and
|
|
9
|
+
* is joined to that neighbour's block when the join crosses no other edge. A block is then one node of a smaller
|
|
10
|
+
* graph, and a box that aligned downwards through five rows comes out as one straight segment.
|
|
11
|
+
* - Nothing overlaps, and the empty strip between two boxes in a row is the same everywhere. The blocks are compacted
|
|
12
|
+
* against each other by the least distance their rows allow, which is a longest path over the block graph: a block
|
|
13
|
+
* sits as far left as the block to its left permits, and no further.
|
|
14
|
+
* - The drawing is balanced rather than packed to one side. Aligning downwards alone would lean everything left, so
|
|
15
|
+
* the same two passes are run from the bottom up, and each pair is run once more mirrored left to right. The four
|
|
16
|
+
* layouts are then averaged. Averaging is a convex combination, and the separation constraints are linear, so the
|
|
17
|
+
* average of four layouts that keep the boxes apart also keeps them apart - the balance is free of overlaps by
|
|
18
|
+
* construction. A pass that starts at the top and one that starts at the bottom compact their blocks from opposite
|
|
19
|
+
* ends, so each side is first slid onto the origin of the first pass of that side, and only then averaged.
|
|
20
|
+
*
|
|
21
|
+
* Three things this module owes the rest of the pipeline rather than the paper:
|
|
22
|
+
*
|
|
23
|
+
* - A long edge's dummies share one x. They are one lane, not one point per row, so each lane is collapsed to the mean
|
|
24
|
+
* of the x its two ends want - the straight drop a reader follows. Collapsing can put a lane where a box already is,
|
|
25
|
+
* so the last pass spends the lane's space: boxes are pushed right past it and never the other way, and a row keeps
|
|
26
|
+
* the width its placeholders were given, which is the width a corridor needs when the lane has to go around a box.
|
|
27
|
+
* The lane is a proposal either way - the channel stage moves a lane into a free corridor when routing needs it, and
|
|
28
|
+
* a box never moves for a lane.
|
|
29
|
+
* - A lane is not a neighbour to be aligned with. A box that aligned itself to a lane passing through its row would be
|
|
30
|
+
* pulled off the boxes it does connect to, which is how a decision with one long edge comes out as a staircase. Lanes
|
|
31
|
+
* are placed by the compaction and then by the channel stage, which is where a lane belongs.
|
|
32
|
+
* - The empty strip between two rows is the caller's, not this module's: the y of a row is a running sum with one gap.
|
|
33
|
+
*
|
|
34
|
+
* Pure: layers and chains in, coordinates out.
|
|
35
|
+
*/
|
|
36
|
+
import type { Chain } from './dummy.js';
|
|
37
|
+
export interface Sized {
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
}
|
|
41
|
+
export interface CoordInput {
|
|
42
|
+
layers: string[][];
|
|
43
|
+
chains: Chain[];
|
|
44
|
+
sizeOf: (id: string) => Sized;
|
|
45
|
+
/** A dummy instance, and the shared lane it belongs to. */
|
|
46
|
+
chainOf: ReadonlyMap<string, string>;
|
|
47
|
+
}
|
|
48
|
+
export interface CoordOptions {
|
|
49
|
+
/** The empty space between two side-by-side boxes. */
|
|
50
|
+
gap: number;
|
|
51
|
+
/** The empty strip between two layers. */
|
|
52
|
+
layerGap: number;
|
|
53
|
+
}
|
|
54
|
+
export interface Coordinates {
|
|
55
|
+
/** Centre x, keyed by node id and by lane id. A long edge's dummies share one entry. */
|
|
56
|
+
x: Map<string, number>;
|
|
57
|
+
/** The top y of each layer. */
|
|
58
|
+
layerY: number[];
|
|
59
|
+
layerHeight: number[];
|
|
60
|
+
layerWidth: number[];
|
|
61
|
+
width: number;
|
|
62
|
+
height: number;
|
|
63
|
+
}
|
|
64
|
+
export declare function assignCoordinates(input: CoordInput, options: CoordOptions): Coordinates;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cycle breaking, in the order a layered layout does it.
|
|
3
|
+
*
|
|
4
|
+
* A layered drawing is a drawing of a DAG: an edge that runs back up the page has no layer to go to. The fix is to
|
|
5
|
+
* lay such an edge out backwards and draw its arrow the way the source wrote it - but *which* edges to turn around
|
|
6
|
+
* is a decision, and deciding it edge by edge is what makes two back edges of one cycle end up pointing opposite
|
|
7
|
+
* ways.
|
|
8
|
+
*
|
|
9
|
+
* So the graph is decomposed first. Tarjan's algorithm finds the strongly connected components; contracting each to
|
|
10
|
+
* a node gives the condensation, which is a DAG, so every edge between components already points one way and nothing
|
|
11
|
+
* between them needs reversing. Only inside a component - where by definition every edge lies on a cycle - is there
|
|
12
|
+
* an order to choose: the nodes are ranked once, greedily, and every edge inside the component is oriented along that
|
|
13
|
+
* single ranking. One ranking per component is what "consistently" means: after the pass, every surviving edge of a
|
|
14
|
+
* component points from a lower rank to a higher one, so the component cannot contain a cycle any more.
|
|
15
|
+
*
|
|
16
|
+
* The initial state is a special case: it is layer zero by definition, so an edge that runs into it is a back edge
|
|
17
|
+
* before the decomposition even starts. Turning those around leaves the state with no predecessor, so the ranking
|
|
18
|
+
* of its component puts it first and nothing is ever oriented back into it.
|
|
19
|
+
*
|
|
20
|
+
* Pure: the same graph in, the same edge directions out.
|
|
21
|
+
*/
|
|
22
|
+
import type { LayoutEdge, LayoutGraph } from './layout.js';
|
|
23
|
+
/** One strongly connected component: a set of nodes that can all reach each other. */
|
|
24
|
+
export interface Component {
|
|
25
|
+
/** The component's index in the condensation, numbered in topological order. */
|
|
26
|
+
id: number;
|
|
27
|
+
members: string[];
|
|
28
|
+
}
|
|
29
|
+
export interface BrokenGraph {
|
|
30
|
+
/** The same edges, in the same order, with the ones that close a cycle turned around. */
|
|
31
|
+
edges: LayoutEdge[];
|
|
32
|
+
/** The diagram edges whose direction the layout changed. */
|
|
33
|
+
reversed: Set<number>;
|
|
34
|
+
/** The strongly connected components, numbered in topological order. */
|
|
35
|
+
components: Component[];
|
|
36
|
+
componentOf: Map<string, number>;
|
|
37
|
+
/** Each node's rank inside its own component: every intra-component edge points from a lower rank to a higher. */
|
|
38
|
+
rank: Map<string, number>;
|
|
39
|
+
/** The condensation: an edge per pair of components that has at least one edge between them. Always acyclic. */
|
|
40
|
+
condensation: Array<{
|
|
41
|
+
from: number;
|
|
42
|
+
to: number;
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
export declare function breakCycles(graph: LayoutGraph): BrokenGraph;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dummy nodes: the placeholders a long edge stands on.
|
|
3
|
+
*
|
|
4
|
+
* An edge from layer 0 to layer 3 has to cross layers 1 and 2. The router can go around whatever is in the way, but a
|
|
5
|
+
* drawing reads better if the edge owns a lane through those layers: then ordering can keep other edges off it, and
|
|
6
|
+
* coordinate assignment can give the whole lane one x. A dummy node is that lane: one per layer the edge crosses.
|
|
7
|
+
*
|
|
8
|
+
* A lane is one variable, not one variable per layer. The instances keep their own id (`#d<edge>_<layer>`) so the
|
|
9
|
+
* ordering stage can sort them inside their layer, and they all point at one lane id (`#e<edge>`) so coordinate
|
|
10
|
+
* assignment cannot give the same edge three different x values.
|
|
11
|
+
*
|
|
12
|
+
* Pure: the layers, the chains, and the map between the two.
|
|
13
|
+
*/
|
|
14
|
+
import type { LayoutEdge } from './layout.js';
|
|
15
|
+
import type { Layering } from './layer.js';
|
|
16
|
+
/** One edge, as the id it passes through in each layer it crosses. */
|
|
17
|
+
export interface Chain {
|
|
18
|
+
/** The diagram edge's index. */
|
|
19
|
+
index: number;
|
|
20
|
+
/** The ids from the layout source to the layout target: the endpoints plus one dummy per crossed layer. */
|
|
21
|
+
path: string[];
|
|
22
|
+
}
|
|
23
|
+
export interface Augmented {
|
|
24
|
+
/** The layers with the dummies inserted; each dummy sits in the layer it crosses. */
|
|
25
|
+
layers: string[][];
|
|
26
|
+
chains: Chain[];
|
|
27
|
+
/** A dummy instance's lane id: the variable coordinate assignment shares between a long edge's dummies. */
|
|
28
|
+
chainOf: Map<string, string>;
|
|
29
|
+
/** The lane id of every edge that needed dummies. */
|
|
30
|
+
laneOf: Map<number, string>;
|
|
31
|
+
}
|
|
32
|
+
export declare function addDummies(layering: Layering, edges: LayoutEdge[]): Augmented;
|
|
@@ -9,14 +9,20 @@
|
|
|
9
9
|
* Supported:
|
|
10
10
|
*
|
|
11
11
|
* - `@startuml` / `@enduml` (optional) and `title ...`;
|
|
12
|
-
* - `component
|
|
13
|
-
*
|
|
14
|
-
* - `
|
|
15
|
-
* -
|
|
12
|
+
* - elements: `component`, `node`, `rectangle`, `artifact` and `state` (a rectangle), `database` and
|
|
13
|
+
* `entity` (a cylinder), `usecase` (an ellipse), `[text]`, each with a `"text"` label and `as` alias;
|
|
14
|
+
* - boundaries: `package`, `folder`, `node`, `component`, `rectangle` and `artifact` blocks, closed by `}`;
|
|
15
|
+
* - arrows, either direction: association `-->`/`<--`, dependency `..>`/`<..`, generalisation `--|>`/`<|--`,
|
|
16
|
+
* realisation `..|>`/`<|..`, composition `*--`/`--*`, aggregation `o--`/`--o`, labelled after `:`;
|
|
17
|
+
* - an edge end that names a declared block attaches to that block's rectangle, the way an external
|
|
18
|
+
* dependency on a package is drawn; a name that is not a declared block is an implicit element;
|
|
19
|
+
* - `[*] --> X` for a start marker;
|
|
16
20
|
* - `'` comments and `skinparam`/`!theme` directives.
|
|
17
21
|
*
|
|
18
|
-
* Refused, by name:
|
|
19
|
-
* `
|
|
22
|
+
* Refused, by line and by name: the element keywords this renderer has no symbol for (`actor`, `class`,
|
|
23
|
+
* `cloud`, ...), the diagram kinds it does not draw (`sequence`), and an edge it cannot draw as written (a
|
|
24
|
+
* line with no arrow end, a mark at both ends, a dotted diamond, an edge between two boundaries, or an edge
|
|
25
|
+
* into a boundary that is nested or empty).
|
|
20
26
|
*/
|
|
21
27
|
import { type Diagram } from './model.js';
|
|
22
28
|
export interface Parsed {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layering: which row each box is in.
|
|
3
|
+
*
|
|
4
|
+
* The graph is acyclic by the time it gets here. A layered drawing needs every edge to span at least one layer, so it
|
|
5
|
+
* always travels downwards, and it wants the total of the layers every edge spans beyond one to be as small as
|
|
6
|
+
* possible: those are the edges that have to be drawn through a row of placeholders instead of going straight down a
|
|
7
|
+
* gap.
|
|
8
|
+
*
|
|
9
|
+
* Longest paths are the first guess: every source on the top row, every other node one layer below its latest
|
|
10
|
+
* predecessor. It is feasible and it is not optimal, because it puts every node as high as it can go - a node whose
|
|
11
|
+
* edges leave it with slack above the boxes it points at is a node that could have moved down and shortened them.
|
|
12
|
+
* `network-simplex.ts` solves the program the mature layering implementations solve: the ranking that minimises the
|
|
13
|
+
* total length, found by exchanging edges in a tight tree until no exchange is left that shortens it.
|
|
14
|
+
*
|
|
15
|
+
* The initial state is layer zero. It is a node with no predecessor - cycle breaking has turned around every edge
|
|
16
|
+
* that ran into it - so a source, and every source is a candidate for the top row. The pin is passed to the solver as
|
|
17
|
+
* a constraint, so the search never trades it away: an initial state that moved down the page would be a machine
|
|
18
|
+
* diagram read from the wrong end.
|
|
19
|
+
*
|
|
20
|
+
* Pure: the graph in, one layer per node out.
|
|
21
|
+
*/
|
|
22
|
+
import type { LayoutEdge, LayoutGraph } from './layout.js';
|
|
23
|
+
export interface Layering {
|
|
24
|
+
/** The ids of each layer, top to bottom. A layer holds at least one node. */
|
|
25
|
+
layers: string[][];
|
|
26
|
+
layerOf: Map<string, number>;
|
|
27
|
+
}
|
|
28
|
+
export declare function layer(graph: LayoutGraph, edges: LayoutEdge[]): Layering;
|