@enspirit/opspicy 0.4.0 → 0.6.0

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.
@@ -0,0 +1,139 @@
1
+ // What an area depends on: the citizens of another area that its own name, and the states another
2
+ // holds that its formaldefs read. Derived from what the suite says, and from nothing else
3
+ // (doc/derived.md): no file says who depends on whom, nor who holds a state. Pure.
4
+ //
5
+ // a name a formaldef names a predicate or a function at home in another area
6
+ // a condition an operation says a sentence that a condition of another area decides
7
+ // a type an operation exchanges, or a type is made of, a type another area declares
8
+ // a state a formaldef reads a state another area holds, with no name in between
9
+ //
10
+ // An operation that says a condition also reaches what the formaldef of that condition names. That
11
+ // is told apart, as what it reaches through the condition: the condition depends on it, and the
12
+ // operation on the condition.
13
+ import { sameSentence } from '../suite.js';
14
+ import { namedIn } from './elo.js';
15
+ import { idOf } from './wiki.js';
16
+ const once = (all, key) => [...new Map(all.map((one) => [key(one), one])).values()];
17
+ /** The formaldefs an operation says itself: those of its clauses and of what its examples also show, where no condition gives them, and the inputs a case computes. */
18
+ function ownOf(operation) {
19
+ const clauses = [...operation.clauses, ...operation.cases.flatMap((kase) => kase.and)];
20
+ const computed = operation.cases.flatMap((kase) => (typeof kase.input === 'string' ? [kase.input] : []));
21
+ return [...clauses.filter((clause) => clause.formaldef && clause.from !== 'shared').map((clause) => clause.formaldef ?? ''), ...computed];
22
+ }
23
+ /** The conditions an operation says: those that give one of its clauses its formaldef. */
24
+ function saidBy(operation, conditions) {
25
+ const clauses = [...operation.clauses, ...operation.cases.flatMap((kase) => kase.and)];
26
+ const found = clauses.filter((clause) => clause.from === 'shared').flatMap((clause) => conditions.filter((one) => sameSentence(one.description, clause.description)));
27
+ return once(found, (one) => one.name);
28
+ }
29
+ /**
30
+ * Who holds each state. The area whose predicates and functions read it by its name, when those
31
+ * of one area alone do; where no predicate or function reads it, the area whose operations and
32
+ * conditions do, when one alone does. Otherwise nobody: the suite does not decide.
33
+ */
34
+ function holdersOf(model, words, others, named) {
35
+ const reading = (all, state) => [...new Set(all.filter((one) => named(one).includes(idOf('state', state))).map((one) => one.who.area))];
36
+ const readers = new Map(model.state.map((one) => [one.name, [reading(words, one.name), reading(others, one.name)].find((areas) => areas.length) ?? []]));
37
+ const holders = new Map(model.state.map((one) => [one.name, readers.get(one.name)?.length === 1 ? (readers.get(one.name)?.[0] ?? null) : null]));
38
+ return { holders, readers };
39
+ }
40
+ /** Every way to put a few things in a row. */
41
+ const rows = (all) => (all.length < 2 ? [all] : all.flatMap((one, at) => rows([...all.slice(0, at), ...all.slice(at + 1)]).map((rest) => [one, ...rest])));
42
+ /**
43
+ * The areas in a row, those that depend on others first. Areas that depend on each other, in a
44
+ * circle, are kept together, in the order that leaves the fewest citizens depending against it.
45
+ */
46
+ function orderOf(areas, between) {
47
+ const on = (area) => between.filter((one) => one.from === area).map((one) => one.to);
48
+ const reached = new Map(areas.map((area) => [area, new Set()]));
49
+ const reach = (from, area) => {
50
+ for (const next of on(area))
51
+ if (!reached.get(from)?.has(next))
52
+ (reached.get(from)?.add(next), reach(from, next));
53
+ };
54
+ for (const area of areas)
55
+ reach(area, area);
56
+ const circles = once(areas.map((area) => areas.filter((other) => other === area || (reached.get(area)?.has(other) && reached.get(other)?.has(area)))), (circle) => circle.join(' '));
57
+ // A circle is as far from the ground as the longest way down from it.
58
+ const height = new Map();
59
+ const heightOf = (circle) => {
60
+ const key = circle.join(' ');
61
+ if (!height.has(key)) {
62
+ height.set(key, 0);
63
+ const under = circles.filter((other) => other !== circle && circle.some((area) => other.some((below) => on(area).includes(below))));
64
+ height.set(key, under.length ? 1 + Math.max(...under.map(heightOf)) : 0);
65
+ }
66
+ return height.get(key) ?? 0;
67
+ };
68
+ const weights = new Map(between.map((one) => [`${one.from}>${one.to}`, new Set(one.uses.map((use) => use.from.anchor)).size]));
69
+ const weight = (from, to) => weights.get(`${from}>${to}`) ?? 0;
70
+ const againstIn = (row) => row.reduce((sum, area, at) => sum + row.slice(0, at).reduce((more, above) => more + weight(area, above), 0), 0);
71
+ const better = (best, row) => (againstIn(row) < againstIn(best) ? row : best);
72
+ // A few areas are tried in every order. More are put where most depends the right way, then each is moved wherever that leaves less against the order, until no move does.
73
+ const moved = (row) => row.flatMap((area, from) => row.map((_other, to) => [...row.slice(0, from), ...row.slice(from + 1)].toSpliced(to, 0, area))).reduce(better, row);
74
+ const improved = (row) => (againstIn(moved(row)) < againstIn(row) ? improved(moved(row)) : row);
75
+ const leaning = (area, circle) => circle.reduce((sum, other) => sum + weight(area, other) - weight(other, area), 0);
76
+ const settled = (circle) => (circle.length > 7 ? improved([...circle].sort((a, b) => leaning(b, circle) - leaning(a, circle) || a.localeCompare(b))) : rows(circle).reduce(better));
77
+ return [...circles].sort((a, b) => heightOf(b) - heightOf(a) || (a[0] ?? '').localeCompare(b[0] ?? '')).flatMap(settled);
78
+ }
79
+ /** What the areas of a suite depend on, each other. */
80
+ export function dependenciesOf(model, operations, refs) {
81
+ const operation = (one) => ({ anchor: idOf('op', one.name), kind: 'operation', name: one.name, area: one.area });
82
+ const citizens = [
83
+ ...model.definitions.map((one) => ({ anchor: idOf(one.kind, one.name), kind: one.kind, name: one.name, area: one.area })),
84
+ ...model.conditions.map((one) => ({ anchor: idOf('condition', one.name), kind: 'condition', name: one.description, area: one.area })),
85
+ ...model.types.map((one) => ({ anchor: idOf('type', one.name), kind: 'type', name: one.name, area: one.area })),
86
+ ];
87
+ const at = new Map(citizens.map((one) => [one.anchor, one]));
88
+ const citizen = (anchor) => at.get(anchor) ?? { anchor, kind: 'type', name: anchor, area: '' };
89
+ const asked = new Map();
90
+ const named = ({ who, formaldef, given }) => {
91
+ const key = `${who.anchor}\n${formaldef}`;
92
+ if (!asked.has(key))
93
+ asked.set(key, namedIn(formaldef, refs, { self: who.anchor, given }));
94
+ return asked.get(key) ?? [];
95
+ };
96
+ const words = model.definitions.map((one) => ({ who: citizen(idOf(one.kind, one.name)), formaldef: one.formaldef, given: one.parameters }));
97
+ const conditions = model.conditions.map((one) => ({ who: citizen(idOf('condition', one.name)), formaldef: one.formaldef }));
98
+ const own = operations.flatMap((one) => ownOf(one).map((formaldef) => ({ who: operation(one), formaldef })));
99
+ const types = model.types.map((one) => ({ who: citizen(idOf('type', one.name)), formaldef: `${one.name} = ${one.formaldef}` }));
100
+ const { holders, readers } = holdersOf(model, words, [...conditions, ...own], named);
101
+ const state = new Map(model.state.map((one) => [idOf('state', one.name), one.name]));
102
+ /** What a formaldef names of another area: a predicate, a function or a type at home there, or a state held there. */
103
+ const usedBy = (one) => named(one).flatMap((anchor) => {
104
+ const held = state.get(anchor);
105
+ if (held !== undefined)
106
+ return holders.get(held) ? [{ from: one.who, to: { anchor, kind: 'state', name: held, area: holders.get(held) ?? '' }, through: 'state' }] : [];
107
+ const to = at.get(anchor);
108
+ return to ? [{ from: one.who, to, through: to.kind === 'type' ? 'type' : 'name' }] : [];
109
+ });
110
+ const exchanged = operations.flatMap((one) => [one.input, one.output].flatMap((type) => (type && at.has(idOf('type', type)) ? [{ from: operation(one), to: citizen(idOf('type', type)), through: 'type' }] : [])));
111
+ const said = operations.flatMap((one) => saidBy(one, model.conditions).map((condition) => ({ from: operation(one), to: citizen(idOf('condition', condition.name)), through: 'condition' })));
112
+ // A state is told as a type: the area that holds it depends on the one that declares the type.
113
+ const told = model.state.flatMap((one) => [one.web?.type, one.web?.one?.type].flatMap((type) => (type && at.has(idOf('type', type)) && holders.get(one.name) ? [{ from: { anchor: idOf('state', one.name), kind: 'state', name: one.name, area: holders.get(one.name) ?? '' }, to: citizen(idOf('type', type)), through: 'type' }] : [])));
114
+ const elsewhere = (one) => one.from.area !== one.to.area;
115
+ const uses = once([...[...words, ...conditions, ...own, ...types].flatMap(usedBy), ...exchanged, ...said, ...told].filter(elsewhere), (one) => `${one.from.anchor} ${one.to.anchor} ${one.through}`);
116
+ const through = operations.flatMap((one) => saidBy(one, model.conditions).flatMap((condition) => {
117
+ const by = citizen(idOf('condition', condition.name));
118
+ return usedBy({ who: by, formaldef: condition.formaldef }).filter((use) => use.through === 'name').map((use) => ({ from: operation(one), to: use.to, by }));
119
+ }));
120
+ const direct = new Set(uses.map((one) => `${one.from.anchor} ${one.to.anchor}`));
121
+ const reaches = once(through.filter(elsewhere).filter((one) => !direct.has(`${one.from.anchor} ${one.to.anchor}`)), (one) => `${one.from.anchor} ${one.to.anchor}`);
122
+ const pairs = once(uses, (one) => `${one.from.area}>${one.to.area}`).map((one) => [one.from.area, one.to.area]);
123
+ const between = pairs.map(([from, to]) => ({ from, to, uses: uses.filter((one) => one.from.area === from && one.to.area === to) }));
124
+ const areas = [...new Set([...model.areas.map((one) => one.name), ...citizens.map((one) => one.area), ...operations.map((one) => one.area)])].filter(Boolean);
125
+ const order = orderOf(areas, between);
126
+ const against = new Set(between.filter((one) => order.indexOf(one.from) > order.indexOf(one.to)).map((one) => `${one.from}>${one.to}`));
127
+ return { holders, readers, uses, reaches, between, order, against };
128
+ }
129
+ /**
130
+ * What is around one area: the dependencies it is an end of, and the areas at their other end, in
131
+ * the order all of them are in. What goes against that order still does: a part is drawn as the whole is.
132
+ */
133
+ export function aroundOf(all, area) {
134
+ const between = all.between.filter((one) => one.from === area || one.to === area);
135
+ const near = new Set([area, ...between.flatMap((one) => [one.from, one.to])]);
136
+ const joined = new Set(between.map((one) => `${one.from}>${one.to}`));
137
+ const ofIt = (one) => one.from.area === area || one.to.area === area;
138
+ return { ...all, between, uses: all.uses.filter(ofIt), reaches: all.reaches.filter(ofIt), order: all.order.filter((one) => near.has(one)), against: new Set([...all.against].filter((one) => joined.has(one))) };
139
+ }
@@ -0,0 +1,176 @@
1
+ import { esc, plural } from './html.js';
2
+ import { idOf } from './wiki.js';
3
+ const fixed = (value) => String(Math.round(value * 10) / 10);
4
+ const ROW = 58;
5
+ const BOX = { width: 270, height: 42 };
6
+ /** How far an arc goes out, by how many rows it spans: each span has a width of its own. */
7
+ const outBy = (span) => 22 + 24 * span;
8
+ /** How many of the citizens of an area depend on another. */
9
+ const countOf = (uses) => new Set(uses.map((one) => one.from.anchor)).size;
10
+ /**
11
+ * The areas and what each depends on: an arc from the one that depends to the one it depends on.
12
+ * Given one area, the drawing is of what is around it (`aroundOf`), and says which it is.
13
+ */
14
+ export function areasDrawn(areas, { between, order, against }, here) {
15
+ const drawn = order.map((name) => areas.find((one) => one.name === name)).filter((one) => one !== undefined);
16
+ const at = new Map(drawn.map((one, row) => [one.name, row]));
17
+ const arcs = between.filter((one) => at.has(one.from) && at.has(one.to));
18
+ const span = (one) => Math.abs((at.get(one.to) ?? 0) - (at.get(one.from) ?? 0));
19
+ const widest = (up) => Math.max(0, ...arcs.filter((one) => against.has(`${one.from}>${one.to}`) === up).map((one) => outBy(span(one)))) + 28;
20
+ const [left, right] = [widest(true), widest(false)];
21
+ const width = left + BOX.width + right;
22
+ const height = drawn.length * ROW + 6;
23
+ const middle = (name) => (at.get(name) ?? 0) * ROW + ROW / 2;
24
+ // Where the count of an arc is written: half way along it, or further along where another count is already there.
25
+ const written = [];
26
+ const along = (x, from, to, out) => {
27
+ const at = (t) => ({ x: x + 3 * out * t * (1 - t), y: from + (to - from) * t * t * (3 - 2 * t) });
28
+ const free = [0.5, 0.36, 0.64, 0.24, 0.76].map(at).find((place) => written.every((other) => Math.abs(other.x - place.x) > 18 || Math.abs(other.y - place.y) > 13)) ?? at(0.5);
29
+ written.push(free);
30
+ return free;
31
+ };
32
+ const arc = (one) => {
33
+ const up = against.has(`${one.from}>${one.to}`);
34
+ const [side, x] = up ? [-1, left] : [1, left + BOX.width];
35
+ // It leaves under the middle of what depends, and arrives above the middle of what is depended on; the other way round where it goes up.
36
+ const [from, to] = [middle(one.from) + (up ? -8 : 8), middle(one.to) + (up ? 8 : -8)];
37
+ const out = outBy(span(one)) * side;
38
+ const state = one.uses.every((use) => use.through === 'state');
39
+ const path = `M${fixed(x)},${fixed(from)} C${fixed(x + out)},${fixed(from)} ${fixed(x + out)},${fixed(to)} ${fixed(x + 7 * side)},${fixed(to)}`;
40
+ const tip = `M${fixed(x)},${fixed(to)} l${7 * side},-3.5 v7 z`;
41
+ const count = along(x, from, to, out);
42
+ const said = `${one.from} depends on ${one.to}: ${plural(countOf(one.uses), 'citizen')}${state ? ', by reading its state alone' : ''}${up ? ', against the order: a cycle' : ''}`;
43
+ return `<g class="dep-arc${state ? ' state' : ''}${up ? ' against' : ''}" data-from="${esc(one.from)}" data-to="${esc(one.to)}"><title>${esc(said)}</title><path class="wire" d="${path}"/><path class="tip" d="${tip}"/><text class="dep-n" x="${fixed(count.x)}" y="${fixed(count.y + 4)}" text-anchor="middle">${countOf(one.uses)}</text></g>`;
44
+ };
45
+ const box = (one) => {
46
+ const y = middle(one.name) - BOX.height / 2;
47
+ const held = one.holds.length ? ` · holds ${one.holds.slice(0, 3).join(', ')}${one.holds.length > 3 ? `, +${one.holds.length - 3}` : ''}` : '';
48
+ return `<a class="dep-node${one.name === here ? ' here' : ''}" href="#${esc(idOf('area', one.name))}" data-area="${esc(one.name)}"${one.name === here ? ' aria-current="page"' : ''}><rect class="dep-box" x="${left}" y="${fixed(y)}" width="${BOX.width}" height="${BOX.height}" rx="8"/><text class="dep-name" x="${left + 14}" y="${fixed(y + 18)}">${esc(one.title)}</text><text class="dep-sub" x="${left + 14}" y="${fixed(y + 33)}">${esc(`${plural(one.operations, 'operation')}${held}`)}</text></a>`;
49
+ };
50
+ const subject = here === undefined ? 'The areas' : `${areas.find((one) => one.name === here)?.title ?? here} and the areas it is joined to`;
51
+ const label = `${subject}, those that depend on others first: ${arcs.length ? `${plural(arcs.length, 'dependency')}, ${against.size ? `${against.size} against the order` : 'none against the order'}` : 'none depends on another'}.`;
52
+ return `<svg class="dep dep-areas" viewBox="0 0 ${fixed(width)} ${height}" width="${fixed(width)}" role="img" aria-label="${esc(label)}">${[...arcs].sort((a, b) => span(a) - span(b)).map(arc).join('')}${drawn.map(box).join('')}</svg>`;
53
+ }
54
+ const LINE = 21;
55
+ const HEAD = 32;
56
+ const GAP = 14;
57
+ /** What a part takes over its rows: its line, and its name under it. */
58
+ const PART = 24;
59
+ /**
60
+ * The parts of a box, as an object diagram has them, one under the other with a line between: what
61
+ * the area holds, what it says of it, what it says with it, and what it does.
62
+ */
63
+ const PARTS = [
64
+ ['state', 'State'],
65
+ ['predicate', 'Predicates'],
66
+ ['function', 'Functions'],
67
+ ['condition', 'Conditions'],
68
+ ['type', 'Types'],
69
+ ['operation', 'Operations'],
70
+ ];
71
+ /** How wide a box of citizens is. */
72
+ const WIDE = 320;
73
+ /** How many characters a row has room for: a sentence that is longer is cut, and told whole to who points at it. */
74
+ const ROOM = 36;
75
+ /** The boxes of one side, one under the other, and where each of their rows is. */
76
+ function placed(boxes, rows) {
77
+ let y = 4;
78
+ for (const box of boxes) {
79
+ box.y = y;
80
+ let at = y + HEAD;
81
+ // A part is there when the box has a row of its kind, and the rows of a part keep the order they were given.
82
+ box.parts = PARTS.map(([kind, title]) => ({ title, of: box.rows.filter((row) => row.kind === kind) }))
83
+ .filter((part) => part.of.length)
84
+ .map((part) => {
85
+ const top = at;
86
+ part.of.forEach((row, n) => rows.set(row.anchor, top + PART + n * LINE + LINE / 2));
87
+ at += PART + part.of.length * LINE;
88
+ return { title: part.title, y: top };
89
+ });
90
+ box.height = at + 7 - y;
91
+ y += box.height + GAP;
92
+ }
93
+ return y - GAP;
94
+ }
95
+ /**
96
+ * The citizens that depend and those they depend on, each area in one box and each citizen in one
97
+ * row of it, whatever depends on it and whatever it depends on. The boxes are one under the other
98
+ * in the order of the areas, so a wire goes down, at the right of the boxes, from the row of what
99
+ * depends to the row of what it depends on; one that goes against the order goes up, at the left.
100
+ *
101
+ * A box is in parts, as that of an object diagram: the states, the predicates, the functions, the
102
+ * conditions, the types and the operations, each under a line and its name. A row goes where what
103
+ * it is joined to is, as far as its part lets it. A wire is as wide as it is long, so that two of
104
+ * different lengths never lie on each other.
105
+ */
106
+ function joinedDrawn(wires, order, { title, written }, { cls, label, here }) {
107
+ if (!wires.length)
108
+ return '';
109
+ const said = (one) => {
110
+ const text = one.kind === 'predicate' || one.kind === 'function' ? written(one.anchor) : one.name;
111
+ return text.length > ROOM ? { label: `${text.slice(0, ROOM - 1).trimEnd()}…`, whole: text } : { label: text };
112
+ };
113
+ const ends = wires.flatMap((one) => [one.from, one.to]);
114
+ const depending = (anchor) => wires.filter((wire) => wire.to.anchor === anchor).length;
115
+ const boxes = order
116
+ .map((area) => {
117
+ const all = [...new Map(ends.filter((one) => one.area === area).map((one) => [one.anchor, one])).values()];
118
+ const rows = all.map((one) => ({ anchor: one.anchor, kind: one.kind, ...said(one), ...(depending(one.anchor) ? { count: depending(one.anchor) } : {}) }));
119
+ return { area, title: title(area), rows, y: 0, height: 0, parts: [] };
120
+ })
121
+ .filter((box) => box.rows.length);
122
+ const y = new Map();
123
+ let height = placed(boxes, y);
124
+ // A row goes where what it is joined to is, on average: a few turns are enough to untangle most.
125
+ const mean = (values) => values.reduce((sum, value) => sum + value, 0) / Math.max(1, values.length);
126
+ const towards = (anchor) => mean(wires.flatMap((one) => (one.from.anchor === anchor ? [y.get(one.to.anchor) ?? 0] : one.to.anchor === anchor ? [y.get(one.from.anchor) ?? 0] : [])));
127
+ for (let turn = 0; turn < 6; turn++) {
128
+ for (const box of boxes)
129
+ box.rows.sort((a, b) => towards(a.anchor) - towards(b.anchor));
130
+ height = placed(boxes, y);
131
+ }
132
+ const at = new Map(boxes.map((box, row) => [box.area, row]));
133
+ const up = (one) => (at.get(one.from.area) ?? 0) > (at.get(one.to.area) ?? 0);
134
+ const outOf = (one) => 14 + 8 * Math.sqrt(Math.abs((y.get(one.to.anchor) ?? 0) - (y.get(one.from.anchor) ?? 0)));
135
+ const widest = (side) => Math.max(0, ...wires.filter((one) => up(one) === side).map(outOf)) + 12;
136
+ const [left, right] = [widest(true), widest(false)];
137
+ const width = left + WIDE + right;
138
+ const wire = (one) => {
139
+ const [from, to] = [y.get(one.from.anchor) ?? 0, y.get(one.to.anchor) ?? 0];
140
+ const [side, x] = up(one) ? [-1, left] : [1, left + WIDE];
141
+ const out = outOf(one) * side;
142
+ return `<g class="dep-wire${one.through === 'state' ? ' state' : ''}${up(one) ? ' against' : ''}" data-from="${esc(one.from.anchor)}" data-to="${esc(one.to.anchor)}"><path class="wire" d="M${fixed(x)},${fixed(from)} C${fixed(x + out)},${fixed(from)} ${fixed(x + out)},${fixed(to)} ${fixed(x + 7 * side)},${fixed(to)}"/><path class="tip" d="M${fixed(x)},${fixed(to)} l${7 * side},-3.5 v7 z"/></g>`;
143
+ };
144
+ const box = (one) => {
145
+ const x = left;
146
+ const rows = one.rows
147
+ .map((row) => {
148
+ const middle = y.get(row.anchor) ?? 0;
149
+ const count = row.count === undefined ? '' : `<text class="dep-count" x="${fixed(x + WIDE - 12)}" y="${fixed(middle + 4)}" text-anchor="end">${row.count}</text>`;
150
+ return `<a class="dep-row" href="#${esc(row.anchor)}" data-dep="${esc(row.anchor)}">${row.whole ? `<title>${esc(row.whole)}</title>` : ''}<rect class="dep-hit" x="${fixed(x + 2)}" y="${fixed(middle - LINE / 2)}" width="${WIDE - 4}" height="${LINE}" rx="4"/><text class="dep-word ${row.kind}" x="${fixed(x + 12)}" y="${fixed(middle + 4)}">${esc(row.label)}</text>${count}</a>`;
151
+ })
152
+ .join('');
153
+ const parts = one.parts.map((part) => `<line class="dep-rule" x1="${fixed(x)}" y1="${fixed(part.y)}" x2="${fixed(x + WIDE)}" y2="${fixed(part.y)}"/><text class="dep-part" x="${fixed(x + 12)}" y="${fixed(part.y + 15)}">${esc(part.title)}</text>`).join('');
154
+ return `<rect class="dep-box${one.area === here ? ' here' : ''}" x="${fixed(x)}" y="${fixed(one.y)}" width="${WIDE}" height="${fixed(one.height)}" rx="8"/>${parts}<a href="#${esc(idOf('area', one.area))}"><text class="dep-name" x="${fixed(x + 12)}" y="${fixed(one.y + 22)}">${esc(one.title)}</text></a>${rows}`;
155
+ };
156
+ return `<svg class="dep ${cls}" viewBox="0 0 ${fixed(width)} ${fixed(height + 4)}" width="${fixed(width)}" role="img" aria-label="${esc(label)}">${wires.map(wire).join('')}${boxes.map(box).join('')}</svg>`;
157
+ }
158
+ /**
159
+ * The operations that name a predicate or a function of another area in a formaldef of their own,
160
+ * each joined to that name. What an operation reaches through a condition is not drawn: it is the
161
+ * condition that names it.
162
+ */
163
+ export function namesDrawn({ uses, order }, naming) {
164
+ const wires = uses.filter((one) => one.from.kind === 'operation' && one.through === 'name');
165
+ const label = `${plural(new Set(wires.map((one) => one.from.anchor)).size, 'operation')} joined to the ${plural(new Set(wires.map((one) => one.to.anchor)).size, 'name')} of another area that their formaldefs say.`;
166
+ return joinedDrawn(wires, order, naming, { cls: 'dep-names', label });
167
+ }
168
+ /**
169
+ * What is around one area, in detail (`aroundOf`): every citizen that depends, joined to what it
170
+ * depends on, whatever it goes through: a name, a condition, a type, or a state, which is dashed.
171
+ * Each arc of the drawing of the areas is here as many wires as it has reasons.
172
+ */
173
+ export function detailDrawn({ uses, order }, naming, here) {
174
+ const label = `${naming.title(here)} in detail: ${plural(new Set(uses.map((one) => one.from.anchor)).size, 'citizen')} joined to the ${plural(new Set(uses.map((one) => one.to.anchor)).size, 'citizen')} of another area they depend on.`;
175
+ return joinedDrawn(uses, order, naming, { cls: 'dep-detail', label, here });
176
+ }
@@ -33,6 +33,7 @@ const navOf = (wiki, about) => `<nav class="side" aria-label="Explorer">
33
33
  <div class="nav">
34
34
  ${link('#definitions', 'Formal definitions')}
35
35
  ${link('#types', 'Types')}
36
+ ${link('#dependencies', 'Dependencies')}
36
37
  </div>
37
38
  <p class="chap">Testing</p>
38
39
  <div class="nav">
@@ -93,3 +93,21 @@ export function eloHtml(text, refs, about = {}) {
93
93
  })
94
94
  .join('');
95
95
  }
96
+ /**
97
+ * The citizens a text of Elo names, by their anchors, each once: what `eloHtml` would make a link
98
+ * of, and nothing else. A function of Elo's own, called, names no citizen.
99
+ */
100
+ export function namedIn(text, refs, about = {}) {
101
+ const tokens = tokensOf(text);
102
+ const links = refsIn(text, wordsOf(tokens), refs, about);
103
+ const found = new Set();
104
+ let at = 0;
105
+ for (const token of tokens) {
106
+ const end = at + token.text.length;
107
+ for (const ref of token.kind === 'fn' ? links.filter((one) => !one.called) : links)
108
+ if (ref.at >= at && ref.at + ref.length <= end)
109
+ found.add(ref.anchor);
110
+ at = end;
111
+ }
112
+ return [...found];
113
+ }
@@ -2,6 +2,7 @@
2
2
  // every citizen at home on one of them, under an anchor of its own. What the suite says, and
3
3
  // nothing of a run: that is in tests.ts. Pure: the wiki is given, markup is what comes out.
4
4
  import { isA } from '../suite.js';
5
+ import { aroundShown, dependenciesPage } from './dependencies.js';
5
6
  import { citizens, described, elo, esc, link, list, mono, plural, said } from './html.js';
6
7
  import { areaTitle, crumbs, operationPage } from './operation.js';
7
8
  import { idOf } from './wiki.js';
@@ -71,12 +72,14 @@ const typeShown = (one, refs) => {
71
72
  const id = idOf('type', one.name);
72
73
  return { id, source: one.source, name: mono(one.name), description: said(one.description), formaldef: elo(`${one.name} = ${one.formaldef}`, refs, { self: id }) };
73
74
  };
74
- function areaPage(area, refs) {
75
+ function areaPage(area, wiki) {
76
+ const { refs } = wiki;
75
77
  const id = idOf('area', area.name);
76
78
  const operations = area.operations.map((one) => operationListed(id, one, [mono(`${one.entry.method} ${one.entry.path}`), esc(one.observes ? 'Observation' : 'Operation'), link(`#${idOf('agent', one.agent)}`, one.agent)]));
77
79
  const body = `${crumbs(link('#home', 'Specification'))}<h1>${esc(areaTitle(area.name))}</h1>
78
80
  <p class="lead">${said(area.description)}</p>
79
81
  <p class="sub">${esc(plural(area.operations.length, 'operation'))}, from ${mono(`${area.name}/`)}.</p>
82
+ ${aroundShown(wiki, area.name)}
80
83
  ${citizens('Operations', operations, { columns: ['Operation', '', 'Route', 'Kind', 'Performed by'], widths: ['20%', '40%', '22%', '9%', '9%'] })}
81
84
  ${citizens('Predicates', area.predicates.map((one) => definitionShown(one, refs)), { widths: WIDTHS })}
82
85
  ${citizens('Functions', area.functions.map((one) => definitionShown(one, refs)), { widths: WIDTHS })}
@@ -150,5 +153,5 @@ function backgroundsPage(wiki) {
150
153
  /** The pages of the specification, in the order the navigation shows them. */
151
154
  export function specPagesOf(wiki) {
152
155
  const around = { types: new Map(wiki.areas.flatMap((area) => area.types).map((one) => [one.name, one])), conditions: wiki.areas.flatMap((area) => area.conditions), state: wiki.state, refs: wiki.refs };
153
- return [overviewPage(wiki), agentsPage(wiki), statePage(wiki), ...wiki.areas.map((area) => areaPage(area, wiki.refs)), ...wiki.operations.map((one) => operationPage(one, around)), ...wiki.agents.map((one) => agentPage(one, wiki)), definitionsPage(wiki), typesPage(wiki), worldPage(wiki), backgroundsPage(wiki)];
156
+ return [overviewPage(wiki), agentsPage(wiki), statePage(wiki), ...wiki.areas.map((area) => areaPage(area, wiki)), ...wiki.operations.map((one) => operationPage(one, around)), ...wiki.agents.map((one) => agentPage(one, wiki)), definitionsPage(wiki), typesPage(wiki), dependenciesPage(wiki), worldPage(wiki), backgroundsPage(wiki)];
154
157
  }
@@ -0,0 +1,41 @@
1
+ // The drawings of what the areas depend on, on the Dependencies page and on the page of each area:
2
+ // an area, an operation or a name that is pointed at, or reached by the keyboard, shows what it is
3
+ // joined to, and the rest steps back. The drawings read without this: every arc is there.
4
+
5
+ /** What an element of a drawing is joined by: the arcs of an area, the wires of an operation or of a name. */
6
+ function joinedTo(drawing, element) {
7
+ const { area, dep } = element.dataset;
8
+ return $$('[data-from]', drawing).filter((wire) => (area ? wire.dataset.from === area || wire.dataset.to === area : wire.dataset.from === dep || wire.dataset.to === dep));
9
+ }
10
+
11
+ /** Lights what an element is joined to, and what is at the other end; nothing, when no element is given. */
12
+ function lightDependencies(drawing, element) {
13
+ for (const lit of $$('.lit', drawing)) lit.classList.remove('lit');
14
+ drawing.classList.toggle('lighting', Boolean(element));
15
+ if (!element) return;
16
+ element.classList.add('lit');
17
+ for (const wire of joinedTo(drawing, element)) {
18
+ wire.classList.add('lit');
19
+ for (const end of [wire.dataset.from, wire.dataset.to]) for (const other of $$('[data-area], [data-dep]', drawing)) if ((other.dataset.area || other.dataset.dep) === end) other.classList.add('lit');
20
+ }
21
+ }
22
+
23
+ for (const drawing of $$('svg.dep')) {
24
+ const pointed = (event) => event.target.closest('[data-area], [data-dep]');
25
+ drawing.addEventListener('pointerover', (event) => lightDependencies(drawing, pointed(event)));
26
+ drawing.addEventListener('pointerleave', () => lightDependencies(drawing, null));
27
+ drawing.addEventListener('focusin', (event) => lightDependencies(drawing, pointed(event)));
28
+ drawing.addEventListener('focusout', () => lightDependencies(drawing, null));
29
+ }
30
+
31
+ // The switch of the drawing of an area shows it in detail with no script. With it, the choice is
32
+ // that of every area, and is remembered.
33
+ const detailed = $$('.dep-detail-on');
34
+
35
+ function setDetailed(on) {
36
+ for (const one of detailed) one.checked = on;
37
+ local.set('opspicy.detailed', on);
38
+ }
39
+
40
+ for (const one of detailed) one.addEventListener('change', () => setDetailed(one.checked));
41
+ setDetailed(local.get('opspicy.detailed', false) === true);
@@ -41,6 +41,7 @@ function route() {
41
41
  hidePeek();
42
42
  showNotes(page);
43
43
  addCall(page);
44
+ addRunner(page);
44
45
  if (named && named.matches('tr.has-fd')) toggleRow(named, true);
45
46
  if (page.id === 'review') renderReview();
46
47
  $('#pnote').hidden = formalButton.hidden = !page.dataset.kind;
@@ -83,7 +83,7 @@ function downloadReview() {
83
83
 
84
84
  /** Has the server write the review to its disk, and says where; false, after saying why, when it did not. */
85
85
  async function writeReview() {
86
- const answer = await serverFetch('/api/review', { method: 'PUT', headers: { 'content-type': 'text/markdown' }, body: reviewText() }, WAITS.review);
86
+ const answer = await serverFetch('api/review', { method: 'PUT', headers: { 'content-type': 'text/markdown' }, body: reviewText() }, WAITS.review);
87
87
  const data = answer && answer.data;
88
88
  if (answer && answer.status === 200 && data && data.path) {
89
89
  toast(`Review written to ${data.path}`);
@@ -114,7 +114,10 @@ async function sendRun(box, ref, health) {
114
114
  button.disabled = true;
115
115
  out.innerHTML = '<p class="m">Running…</p>';
116
116
  try {
117
- out.innerHTML = answeredHtml(await serverFetch('/api/run', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ case: ref, mode: modeOfRun(health, mode) }) }, WAITS.run));
117
+ const answer = await serverFetch('api/run', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ case: ref, mode: modeOfRun(health, mode) }) }, WAITS.run);
118
+ out.innerHTML = answeredHtml(answer);
119
+ // The table of the tests says it too: the case has a row there, which tells its last run.
120
+ if (answer && answer.status === 200 && isRecord(answer.data)) tellRows({ is: 'ran', ref, ...answer.data });
118
121
  } catch {
119
122
  out.innerHTML = '<p class="err">What the server answered is not the telling of a run.</p>';
120
123
  } finally {