dsh-model-everything 0.5.0 → 0.5.1
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 +2437 -216
- 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 +764 -67
- package/lib/render/place.js +393 -121
- package/lib/render/route.js +225 -0
- package/lib/render/svg.js +143 -21
- package/lib/render/symbol.js +108 -0
- package/lib/render/text.js +74 -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 +42 -9
- package/lib/types/render/place.d.ts +35 -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/wrap.d.ts +27 -0
- package/package.json +148 -148
- package/skills/model-everything-diagram/SKILL.md +84 -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 +1006 -290
- package/src/render/place.ts +527 -234
- package/src/render/route.ts +269 -0
- package/src/render/svg.ts +270 -127
- package/src/render/symbol.ts +137 -0
- package/src/render/text.ts +86 -0
- package/src/render/wrap.ts +103 -0
|
@@ -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;
|
|
@@ -1,22 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The graph the layout stages agree on, and the order they start from.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* A layout stage has no business knowing what a diagram is: it sees boxes with a size and edges
|
|
5
|
+
* between them. That is what `LayoutGraph` is, and it is the whole vocabulary the pipeline shares -
|
|
6
|
+
* cycle breaking reads it, layering writes layers, ordering and coordinate assignment read those.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* `rankStates` stays: it is the breadth-first order a source's own reading suggests, and it seeds
|
|
9
|
+
* the ordering stage with the discovery order instead of whatever order the parser happened to
|
|
10
|
+
* collect. It no longer decides which row a state is in - `layer.ts` does that now - so the rows it
|
|
11
|
+
* returns are only a first guess at the left-to-right order.
|
|
12
12
|
*
|
|
13
|
-
* Pure, and asserted on the
|
|
13
|
+
* Pure, and asserted on the answer itself.
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
/** A box to place: an id, and the space its text needs. */
|
|
16
|
+
export interface LayoutNode {
|
|
17
|
+
id: string;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
}
|
|
21
|
+
/** An edge to draw: which two boxes it joins, and which diagram edge it came from. */
|
|
22
|
+
export interface LayoutEdge {
|
|
23
|
+
/** The index in the diagram's own edge list, so the drawing can report a lane back for it. */
|
|
24
|
+
index: number;
|
|
25
|
+
from: string;
|
|
26
|
+
to: string;
|
|
27
|
+
}
|
|
28
|
+
/** One level of a nested drawing: what is placed together in a single pass of the pipeline. */
|
|
29
|
+
export interface LayoutGraph {
|
|
30
|
+
nodes: LayoutNode[];
|
|
31
|
+
edges: LayoutEdge[];
|
|
32
|
+
/** The box that must stay on layer zero, when the source named one. */
|
|
33
|
+
init?: string;
|
|
34
|
+
}
|
|
16
35
|
export interface Ranked {
|
|
17
36
|
/** The state ids of each row, top to bottom, already ordered left to right. */
|
|
18
37
|
rows: string[][];
|
|
19
38
|
/** States no edge reaches from the initial one. */
|
|
20
39
|
unreachable: string[];
|
|
21
40
|
}
|
|
22
|
-
export declare function rankStates(nodes:
|
|
41
|
+
export declare function rankStates(nodes: ReadonlyArray<{
|
|
42
|
+
id: string;
|
|
43
|
+
}>, edges: ReadonlyArray<{
|
|
44
|
+
from: string;
|
|
45
|
+
to: string;
|
|
46
|
+
}>, init: string | undefined): Ranked;
|
|
47
|
+
/**
|
|
48
|
+
* The left-to-right seed the ordering stage starts from.
|
|
49
|
+
*
|
|
50
|
+
* Breadth-first discovery is what a reader expects a drawing to follow, so it is a better first
|
|
51
|
+
* guess than the order the parser collected. A state nothing reaches has no discovery order and
|
|
52
|
+
* keeps the order it was written in, after everything that was reached.
|
|
53
|
+
*/
|
|
54
|
+
export declare function seedOrder(graph: LayoutGraph): Map<string, number>;
|
|
@@ -9,18 +9,35 @@
|
|
|
9
9
|
*/
|
|
10
10
|
/** What a node is, as far as colour is concerned. */
|
|
11
11
|
export type NodeRole = 'normal' | 'error' | 'warning' | 'muted';
|
|
12
|
+
/** The symbol a node is drawn as. UML and Mermaid both distinguish these; the notation decides which one applies. */
|
|
13
|
+
export type NodeShape = 'rect' | 'diamond' | 'cylinder' | 'stadium' | 'circle' | 'subroutine' | 'hexagon';
|
|
12
14
|
export interface DiagramNode {
|
|
13
15
|
id: string;
|
|
14
16
|
/** The text drawn inside the box. Defaults to the id when a notation labelled it. */
|
|
15
17
|
label?: string;
|
|
16
18
|
role: NodeRole;
|
|
19
|
+
/** Defaults to a rectangle: a node the source gave no shape has no shape to draw. */
|
|
20
|
+
shape?: NodeShape;
|
|
17
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* What an edge's head means, when the notation distinguished it.
|
|
24
|
+
*
|
|
25
|
+
* The painter draws the meaning: a plain arrow for an association, a hollow triangle for a generalisation or a
|
|
26
|
+
* realisation, a filled diamond at the source for a composition, a hollow one for an aggregation, and a dashed
|
|
27
|
+
* line for the two dependency-flavoured kinds.
|
|
28
|
+
*/
|
|
29
|
+
export type EdgeArrow = 'association' | 'dependency' | 'generalization' | 'realization' | 'composition' | 'aggregation';
|
|
18
30
|
export interface DiagramEdge {
|
|
19
31
|
from: string;
|
|
20
32
|
to: string;
|
|
21
33
|
label: string;
|
|
22
34
|
/** A dashed edge, for notations that distinguish one. */
|
|
23
35
|
dashed?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The arrow the notation stated. Absent means the plain arrow every edge has been drawn with: a Mermaid edge
|
|
38
|
+
* states no arrow type, so it carries no field and is drawn exactly as before.
|
|
39
|
+
*/
|
|
40
|
+
arrow?: EdgeArrow;
|
|
24
41
|
}
|
|
25
42
|
export interface DiagramGroup {
|
|
26
43
|
id: string;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimum-length ranking: the network simplex of Gansner, Koutsofios, North and Vo (1993).
|
|
3
|
+
*
|
|
4
|
+
* The ranking problem: give every node a layer such that each edge spans at least its minimum length, and make the
|
|
5
|
+
* total of all the slack - the layers every edge spans beyond its minimum - as small as it can be. Slack is what a
|
|
6
|
+
* reader pays for: an edge that spans three layers has to be drawn through two of them.
|
|
7
|
+
*
|
|
8
|
+
* Longest paths are one feasible answer and they are not the best one. They put every node as high as it can
|
|
9
|
+
* possibly go, which leaves a node whose edges leave it with nothing to do above the boxes it points at. The best
|
|
10
|
+
* answer is the solution of a linear program, and network simplex is how the mature layering implementations solve
|
|
11
|
+
* it: hold a *feasible tree* - a spanning tree every edge of which is tight - and a ranking that tightens it, attach
|
|
12
|
+
* a *cut value* to each tree edge (the weight flowing one way across that cut minus the weight flowing the other),
|
|
13
|
+
* and while some tree edge has a negative cut value, take it out, let the cheapest edge across its cut in, and move
|
|
14
|
+
* the ranks so the new edge is tight. The cut value is the rate at which the total length falls as the tree edge is
|
|
15
|
+
* left out, so every exchange shortens the drawing, and a tree with no negative cut value is the optimum: no single
|
|
16
|
+
* exchange is left that would shorten it, and for this program that is the definition of optimal.
|
|
17
|
+
*
|
|
18
|
+
* Three things are done differently from the paper, each because this graph is not the paper's graph:
|
|
19
|
+
*
|
|
20
|
+
* - **Components are solved one at a time.** The ranking is translation invariant, so two components that share no
|
|
21
|
+
* edge can be laid out apart from each other; each is solved on its own and its top row is then numbered zero.
|
|
22
|
+
* - **A node may be pinned to the top.** A state machine has an initial state and a reader expects it first, so the
|
|
23
|
+
* stage can name one node that must be on the topmost layer: a zero-length, zero-weight constraint edge runs from
|
|
24
|
+
* it to every other node of its component (`rank(v) >= rank(pinned)`), and the ranking is minimised subject to that.
|
|
25
|
+
* The constraint costs length when the free optimum would have put the pinned node below another source, and that
|
|
26
|
+
* price is what "the initial state is first" means.
|
|
27
|
+
* - **The iteration is capped.** A degenerate exchange (one that moves nothing) is legal and in theory can go round
|
|
28
|
+
* in circles. The cap turns that into a ranking that is feasible and no worse than where it started.
|
|
29
|
+
*
|
|
30
|
+
* Pure, and generic in the ranking program: no ids, no sizes, no drawing.
|
|
31
|
+
*/
|
|
32
|
+
/** One edge of the ranking program: two layers, how far apart they may be, and what one layer of slack costs. */
|
|
33
|
+
export interface RankEdge {
|
|
34
|
+
from: number;
|
|
35
|
+
to: number;
|
|
36
|
+
/** The least number of layers this edge may span. One for an edge that is drawn, zero for a constraint. */
|
|
37
|
+
minLength: number;
|
|
38
|
+
/** What one extra layer on this edge costs. Zero for a constraint, which must hold but is not worth shortening. */
|
|
39
|
+
weight: number;
|
|
40
|
+
}
|
|
41
|
+
export interface RankProblem {
|
|
42
|
+
count: number;
|
|
43
|
+
edges: RankEdge[];
|
|
44
|
+
/** A feasible ranking to start from: it already gives every edge at least its minimum length. */
|
|
45
|
+
start: number[];
|
|
46
|
+
/**
|
|
47
|
+
* The node that has to sit on the topmost layer, when the source named one.
|
|
48
|
+
*
|
|
49
|
+
* It has to be a node nothing points at, or no ranking can hold it at the top - a predecessor forces it down. The
|
|
50
|
+
* constraint is dropped when the start ranking cannot satisfy it.
|
|
51
|
+
*/
|
|
52
|
+
pinned?: number;
|
|
53
|
+
}
|
|
54
|
+
export declare function minimumLength(problem: RankProblem): number[];
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordering: the left-to-right order of each layer.
|
|
3
|
+
*
|
|
4
|
+
* The layer a box is in is fixed by now; the only freedom left is the order inside each layer, and that order decides
|
|
5
|
+
* how many edges cross. A crossing is counted on a pair of adjacent layers: two edges between the same two layers
|
|
6
|
+
* cross when their end points are in opposite order on the top and the bottom.
|
|
7
|
+
*
|
|
8
|
+
* The method is the one every layered layout uses. A median sweep takes each layer in turn and sorts it by the median
|
|
9
|
+
* position of its neighbours in the layer just visited - down the drawing, then up. A down sweep alone settles each
|
|
10
|
+
* layer against the one above it and can be undone by the next; alternating is what makes the passes converge.
|
|
11
|
+
*
|
|
12
|
+
* The median is a good guess, not a search. Greedy switch is the search: try swapping two neighbours in a layer, keep
|
|
13
|
+
* the swap if the total crossing count falls, and stop when a whole pass finds nothing. That is where a crossing the
|
|
14
|
+
* median cannot see gets removed.
|
|
15
|
+
*
|
|
16
|
+
* Pure: layers of ids in, layers of ids out, plus the crossing count that justified them.
|
|
17
|
+
*/
|
|
18
|
+
import type { Chain } from './dummy.js';
|
|
19
|
+
export interface Ordered {
|
|
20
|
+
/** The same layers, each in its final order. */
|
|
21
|
+
layers: string[][];
|
|
22
|
+
/** The number of crossings between adjacent layers left by the ordering. */
|
|
23
|
+
crossings: number;
|
|
24
|
+
}
|
|
25
|
+
/** How many pairs of edges between adjacent layers cross. */
|
|
26
|
+
export declare function countCrossings(layers: string[][], chains: Chain[]): number;
|
|
27
|
+
export declare function order(seedLayers: string[][], chains: Chain[], seed?: Map<string, number>): Ordered;
|