@bpmnkit/core 1.7.0 → 1.9.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.
Files changed (52) hide show
  1. package/AGENTS.md +19 -0
  2. package/README.md +2 -0
  3. package/agent-cards/bpmn.md +88 -0
  4. package/agent-cards/dmn.md +105 -0
  5. package/agent-cards/form.md +72 -0
  6. package/dist/bpmn/auto-layout.js +2 -1
  7. package/dist/bpmn/bpmn-builder.d.ts +2 -2
  8. package/dist/bpmn/bpmn-builder.js +27 -6
  9. package/dist/bpmn/camunda7/migrate.d.ts +1 -1
  10. package/dist/bpmn/camunda7/migrate.js +1 -1
  11. package/dist/bpmn/compact.js +3 -0
  12. package/dist/bpmn/full-operations.js +2 -0
  13. package/dist/bpmn/index.d.ts +9 -1
  14. package/dist/bpmn/index.js +18 -23
  15. package/dist/bpmn/optimize/deploy.js +14 -0
  16. package/dist/bpmn/optimize/index.d.ts +2 -0
  17. package/dist/bpmn/optimize/index.js +5 -0
  18. package/dist/bpmn/optimize/types.d.ts +7 -0
  19. package/dist/bpmn/stable-ids.d.ts +15 -0
  20. package/dist/bpmn/stable-ids.js +26 -0
  21. package/dist/bpmn/svg.js +5 -6
  22. package/dist/bpmn/xsd-guards.d.ts +8 -0
  23. package/dist/bpmn/xsd-guards.js +56 -0
  24. package/dist/check-finding.d.ts +19 -0
  25. package/dist/check-finding.js +5 -0
  26. package/dist/dmn/compact.js +14 -5
  27. package/dist/dmn/dmn-builder.js +3 -3
  28. package/dist/dmn/drd-builder.d.ts +53 -0
  29. package/dist/dmn/drd-builder.js +100 -0
  30. package/dist/dmn/index.d.ts +35 -0
  31. package/dist/dmn/index.js +36 -0
  32. package/dist/dmn/lint.d.ts +19 -0
  33. package/dist/dmn/lint.js +414 -0
  34. package/dist/form/form-builder.d.ts +9 -1
  35. package/dist/form/form-builder.js +34 -1
  36. package/dist/form/form-model.d.ts +7 -0
  37. package/dist/form/form-parser.js +22 -0
  38. package/dist/form/form-serializer.js +10 -0
  39. package/dist/form/index.d.ts +11 -2
  40. package/dist/form/index.js +10 -1
  41. package/dist/form/lint.d.ts +19 -0
  42. package/dist/form/lint.js +356 -0
  43. package/dist/index.d.ts +14 -3
  44. package/dist/index.js +10 -2
  45. package/dist/layout/grid/edge-labels.d.ts +7 -1
  46. package/dist/layout/grid/edge-labels.js +62 -1
  47. package/dist/layout/semantic/index.js +78 -8
  48. package/dist/layout/semantic/place.d.ts +19 -4
  49. package/dist/layout/semantic/place.js +232 -34
  50. package/dist/layout/semantic/route.js +47 -14
  51. package/llms.txt +9 -0
  52. package/package.json +5 -1
@@ -36,6 +36,7 @@ grid) {
36
36
  const childResults = new Map();
37
37
  const planes = [];
38
38
  const sizes = new Map();
39
+ const axes = new Map();
39
40
  for (const el of graph.nodes) {
40
41
  const child = childLayoutOf(el, collapsed, grids);
41
42
  if (!child) {
@@ -51,12 +52,14 @@ grid) {
51
52
  childResults.set(el.id, child.result);
52
53
  planes.push(...(child.result.planes ?? []));
53
54
  sizes.set(el.id, child.size);
55
+ if (child.axis !== undefined)
56
+ axes.set(el.id, child.axis);
54
57
  }
55
58
  }
56
59
  // A named expanded sub-process draws its title along its top border.
57
60
  const titled = new Set([...childResults.keys()].filter((id) => graph.byId.get(id)?.name !== undefined));
58
61
  const bandLayout = assignBands(graph, titled);
59
- const { bounds, lanes, gutterX } = place(graph, bandLayout, sizes, laneSet, inContainer, titled, grid);
62
+ const { bounds, lanes, gutterX } = place(graph, bandLayout, sizes, laneSet, inContainer, titled, grid, axes);
60
63
  const besideStem = boundaryLabels(graph, bounds);
61
64
  const nodes = [];
62
65
  for (const el of flowElements) {
@@ -113,7 +116,11 @@ grid) {
113
116
  result.lanes = lanes;
114
117
  if (planes.length > 0)
115
118
  result.planes = planes;
116
- placeEdgeLabels(edges, new Map(nodes.map((n) => [n.id, n])));
119
+ clearExternalLabels(nodes, edges);
120
+ // A gateway's branch labels read as the answer to its question, so they go
121
+ // right at its exit rather than halfway along the flow.
122
+ const gateways = new Set(nodes.filter((n) => n.type.endsWith("Gateway")).map((n) => n.id));
123
+ placeEdgeLabels(edges, new Map(nodes.map((n) => [n.id, n])), new Set(edges.filter((e) => gateways.has(e.sourceRef)).map((e) => e.id)));
117
124
  return result;
118
125
  }
119
126
  /** Lay out a container's children and report the size its border needs. */
@@ -131,13 +138,18 @@ function childLayoutOf(el, collapsed, grids) {
131
138
  if (!extent)
132
139
  return null;
133
140
  const titleBand = el.name !== undefined ? TITLE_BAND : 0;
134
- return {
135
- result,
136
- size: {
137
- width: extent.width + 2 * SUB_PADDING,
138
- height: extent.height + 2 * SUB_PADDING + titleBand,
139
- },
141
+ // The inner spine is band 0; its first node sets the line the parent row continues.
142
+ const spineNode = result.edges.length > 0
143
+ ? result.nodes.find((n) => n.position === 0 && n.type !== "boundaryEvent")
144
+ : undefined;
145
+ const size = {
146
+ width: extent.width + 2 * SUB_PADDING,
147
+ height: extent.height + 2 * SUB_PADDING + titleBand,
140
148
  };
149
+ if (!spineNode)
150
+ return { result, size };
151
+ const axis = spineNode.bounds.y + spineNode.bounds.height / 2 - extent.y + SUB_PADDING + titleBand;
152
+ return { result, size, axis };
141
153
  }
142
154
  function extentOf(result) {
143
155
  let minX = Number.POSITIVE_INFINITY;
@@ -261,6 +273,64 @@ function boundaryLabels(graph, bounds) {
261
273
  }
262
274
  return labels;
263
275
  }
276
+ /**
277
+ * Move an event's or gateway's label off the flows that leave or pass it.
278
+ *
279
+ * The label starts centred below the shape, where a branch leaving through the
280
+ * bottom would cut through it. It then tries above, and beside the vertical
281
+ * stems on either side, keeping the first spot clear of every flow, shape and
282
+ * label; with none clear it stays below.
283
+ */
284
+ function clearExternalLabels(nodes, edges) {
285
+ const segments = [];
286
+ for (const edge of edges) {
287
+ for (let i = 0; i + 1 < edge.waypoints.length; i++) {
288
+ segments.push([edge.waypoints[i], edge.waypoints[i + 1]]);
289
+ }
290
+ }
291
+ const contains = (outer, inner) => outer.x <= inner.x &&
292
+ outer.y <= inner.y &&
293
+ outer.x + outer.width >= inner.x + inner.width &&
294
+ outer.y + outer.height >= inner.y + inner.height;
295
+ const labels = nodes.filter((n) => n.labelBounds).map((n) => n.labelBounds);
296
+ for (const node of nodes) {
297
+ const label = node.labelBounds;
298
+ if (!label || node.type === "boundaryEvent")
299
+ continue;
300
+ const clear = (b) => !segments.some(([p, q]) => segmentCuts(p, q, b)) &&
301
+ !nodes.some((other) => other !== node && !contains(other.bounds, node.bounds) && overlapsBox(other.bounds, b)) &&
302
+ !labels.some((other) => other !== label && overlapsBox(other, b));
303
+ if (clear(label))
304
+ continue;
305
+ const b = node.bounds;
306
+ const cx = b.x + b.width / 2;
307
+ const below = b.y + b.height + LABEL_OFFSET;
308
+ const above = b.y - LABEL_OFFSET - label.height;
309
+ const beside = 2 * LABEL_OFFSET;
310
+ const candidates = [
311
+ { ...label, y: above },
312
+ { ...label, x: cx + beside, y: below },
313
+ { ...label, x: cx - beside - label.width, y: below },
314
+ { ...label, x: cx + beside, y: above },
315
+ { ...label, x: cx - beside - label.width, y: above },
316
+ ];
317
+ const spot = candidates.find(clear);
318
+ if (spot) {
319
+ label.x = spot.x;
320
+ label.y = spot.y;
321
+ }
322
+ }
323
+ }
324
+ function overlapsBox(a, b) {
325
+ return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
326
+ }
327
+ /** True when the axis-parallel segment p→q runs through the box. */
328
+ function segmentCuts(p, q, b) {
329
+ return (Math.max(p.x, q.x) > b.x &&
330
+ Math.min(p.x, q.x) < b.x + b.width &&
331
+ Math.max(p.y, q.y) > b.y &&
332
+ Math.min(p.y, q.y) < b.y + b.height);
333
+ }
264
334
  /** Events and gateways carry their name outside the shape; activities do not. */
265
335
  function externalLabel(type, name, bounds) {
266
336
  const width = externalLabelWidth(type, name);
@@ -2,9 +2,18 @@ import type { BpmnFlowElement, BpmnLaneSet } from "../../bpmn/bpmn-model.js";
2
2
  import type { Bounds, GridHint } from "../types.js";
3
3
  import { type BandLayout } from "./bands.js";
4
4
  import type { SemanticGraph } from "./graph.js";
5
- /** Geometry constants. Layout may add space beyond these; it never takes any back. */
6
- export declare const H_GAP = 100;
7
- export declare const V_GAP = 80;
5
+ /**
6
+ * Geometry constants. Layout may add space beyond these; it never takes any back.
7
+ * Hand-drawn diagrams keep about 50-60 px between neighbouring shapes; wider
8
+ * gaps only where a flow label or a long event label needs the room.
9
+ */
10
+ export declare const H_GAP = 60;
11
+ export declare const V_GAP = 60;
12
+ /**
13
+ * Gap beside a column holding an expanded container. The container's border,
14
+ * its title and the notes on its children crowd that gap, so it keeps more air.
15
+ */
16
+ export declare const CONTAINER_GAP = 100;
8
17
  export declare const COMPONENT_GAP = 80;
9
18
  export declare const LANE_PADDING = 40;
10
19
  export declare const BOUNDARY_SIZE = 36;
@@ -56,5 +65,11 @@ packLoose?: boolean,
56
65
  */
57
66
  titled?: ReadonlySet<string>,
58
67
  /** Shape of the `packLoose` grid, when the builder asked for one. */
59
- grid?: GridHint): Placement;
68
+ grid?: GridHint,
69
+ /**
70
+ * For expanded containers with a flow inside: the distance from the
71
+ * container's top to its inner spine. The container is hung so that spine
72
+ * continues the row's line, and the flow runs straight through it.
73
+ */
74
+ axes?: ReadonlyMap<string, number>): Placement;
60
75
  //# sourceMappingURL=place.d.ts.map
@@ -1,8 +1,21 @@
1
1
  import { ELEMENT_SIZES, LABEL_CHAR_WIDTH, LABEL_HEIGHT, LABEL_MIN_WIDTH, LABEL_VERTICAL_OFFSET, } from "../types.js";
2
2
  import { boundarySide } from "./bands.js";
3
- /** Geometry constants. Layout may add space beyond these; it never takes any back. */
4
- export const H_GAP = 100;
5
- export const V_GAP = 80;
3
+ /**
4
+ * Geometry constants. Layout may add space beyond these; it never takes any back.
5
+ * Hand-drawn diagrams keep about 50-60 px between neighbouring shapes; wider
6
+ * gaps only where a flow label or a long event label needs the room.
7
+ */
8
+ export const H_GAP = 60;
9
+ export const V_GAP = 60;
10
+ /**
11
+ * Gap beside a column holding an expanded container. The container's border,
12
+ * its title and the notes on its children crowd that gap, so it keeps more air.
13
+ */
14
+ export const CONTAINER_GAP = 100;
15
+ /** Clear space either side of a flow label inside a column gap. */
16
+ const LABEL_MARGIN = 10;
17
+ /** Gap between an event or gateway and its external label (mirrors `index.ts`). */
18
+ const EXTERNAL_LABEL_OFFSET = 4;
6
19
  const GRID_GAP_X = 40;
7
20
  const GRID_GAP_Y = 20;
8
21
  export const COMPONENT_GAP = 80;
@@ -57,7 +70,13 @@ packLoose = false,
57
70
  */
58
71
  titled = new Set(),
59
72
  /** Shape of the `packLoose` grid, when the builder asked for one. */
60
- grid) {
73
+ grid,
74
+ /**
75
+ * For expanded containers with a flow inside: the distance from the
76
+ * container's top to its inner spine. The container is hung so that spine
77
+ * continues the row's line, and the flow runs straight through it.
78
+ */
79
+ axes = new Map()) {
61
80
  const bounds = new Map();
62
81
  const componentOf = new Map();
63
82
  for (let i = 0; i < graph.components.length; i++) {
@@ -71,15 +90,27 @@ grid) {
71
90
  const size = sizes.get(node.id) ?? DEFAULT_SIZE;
72
91
  colWidth.set(rank, Math.max(colWidth.get(rank) ?? 0, size.width));
73
92
  }
93
+ const ranks = [...colWidth.keys()].sort((a, b) => a - b);
94
+ const gaps = columnGaps(graph, sizes, colWidth, ranks);
74
95
  const colX = new Map();
75
96
  const gutterX = new Map();
76
97
  let x = 0;
77
- for (const rank of [...colWidth.keys()].sort((a, b) => a - b)) {
98
+ for (const [i, rank] of ranks.entries()) {
99
+ const gap = gaps.get(rank) ?? H_GAP;
100
+ if (i > 0)
101
+ x += gap;
78
102
  colX.set(rank, x);
79
- gutterX.set(rank, x - H_GAP / 2);
80
- x += (colWidth.get(rank) ?? 0) + H_GAP;
103
+ gutterX.set(rank, x - gap / 2);
104
+ x += colWidth.get(rank) ?? 0;
81
105
  }
82
- // ── Rows: one per band, laid out per component then stacked ──
106
+ const xOf = (id) => {
107
+ const size = sizes.get(id) ?? DEFAULT_SIZE;
108
+ const rank = graph.ranks.get(id) ?? 0;
109
+ const cw = colWidth.get(rank) ?? size.width;
110
+ const left = (colX.get(rank) ?? 0) + (cw - size.width) / 2;
111
+ return { left, right: left + size.width, width: size.width };
112
+ };
113
+ // ── Rows: one line per band, laid out per component then stacked ──
83
114
  const loose = packLoose ? looseNodes(graph) : [];
84
115
  const looseSet = new Set(loose);
85
116
  let cursorY = 0;
@@ -93,47 +124,214 @@ grid) {
93
124
  cursorY = placeGrid(graph, loose, sizes, cursorY, bounds, grid);
94
125
  continue;
95
126
  }
96
- const rowHeight = new Map();
127
+ const lines = bandLines(graph, bandLayout, members, sizes, xOf, axes, titled);
128
+ let top = Number.POSITIVE_INFINITY;
129
+ let bottom = Number.NEGATIVE_INFINITY;
97
130
  for (const id of members) {
98
- const band = bandLayout.bands.get(id) ?? 0;
99
- const size = sizes.get(id) ?? DEFAULT_SIZE;
100
- rowHeight.set(band, Math.max(rowHeight.get(band) ?? 0, size.height));
101
- }
102
- const rowY = new Map();
103
- let y = cursorY;
104
- for (const band of [...rowHeight.keys()].sort((a, b) => a - b)) {
105
- rowY.set(band, y);
106
- y += (rowHeight.get(band) ?? 0) + V_GAP;
131
+ const line = lines.line.get(bandLayout.bands.get(id) ?? 0) ?? 0;
132
+ const extent = lines.extent.get(id);
133
+ if (!extent)
134
+ continue;
135
+ top = Math.min(top, line - extent.above);
136
+ bottom = Math.max(bottom, line + extent.below);
107
137
  }
138
+ const dy = cursorY - top;
108
139
  for (const id of members) {
109
140
  const size = sizes.get(id) ?? DEFAULT_SIZE;
110
- const rank = graph.ranks.get(id) ?? 0;
111
- const band = bandLayout.bands.get(id) ?? 0;
112
- const cw = colWidth.get(rank) ?? size.width;
113
- const rh = rowHeight.get(band) ?? size.height;
114
- // A container taller than the row's ordinary nodes hangs from the task
115
- // line (top = line - half a task) instead of being centred, so the
116
- // flows into and out of it stay straight.
117
- const top = rowY.get(band) ?? 0;
118
- const y = rh > DEFAULT_SIZE.height
119
- ? size.height > DEFAULT_SIZE.height
120
- ? top
121
- : top + (DEFAULT_SIZE.height - size.height) / 2
122
- : top + (rh - size.height) / 2;
141
+ const line = lines.line.get(bandLayout.bands.get(id) ?? 0) ?? 0;
142
+ const { left } = xOf(id);
123
143
  bounds.set(id, {
124
- x: (colX.get(rank) ?? 0) + (cw - size.width) / 2,
125
- y,
144
+ x: left,
145
+ y: line + dy - centreOffset(id, size, axes),
126
146
  width: size.width,
127
147
  height: size.height,
128
148
  });
129
149
  }
130
- cursorY = y - V_GAP + COMPONENT_GAP;
150
+ cursorY = bottom + dy + COMPONENT_GAP;
131
151
  }
132
152
  separateSameCell(graph, bandLayout, bounds);
133
153
  const lanes = laneSet ? applyLanes(graph, laneSet, bounds) : [];
134
154
  dockBoundaryEvents(graph, bounds, titled);
135
155
  return { bounds, lanes, gutterX };
136
156
  }
157
+ /**
158
+ * The gap in front of each rank. At least {@link H_GAP}; wider where a flow
159
+ * label sits between two neighbouring columns, or where events' and gateways'
160
+ * labels, centred below shapes narrower than themselves, would meet.
161
+ */
162
+ function columnGaps(graph, sizes, colWidth, ranks) {
163
+ const rankOf = (id) => {
164
+ const el = graph.byId.get(id);
165
+ const host = el?.type === "boundaryEvent" ? el.attachedToRef : id;
166
+ return graph.ranks.get(host) ?? 0;
167
+ };
168
+ const position = new Map(ranks.map((rank, i) => [rank, i]));
169
+ const tall = new Set();
170
+ for (const node of graph.nodes) {
171
+ if ((sizes.get(node.id) ?? DEFAULT_SIZE).height > DEFAULT_SIZE.height) {
172
+ tall.add(graph.ranks.get(node.id) ?? 0);
173
+ }
174
+ }
175
+ const overhang = new Map();
176
+ for (const node of graph.nodes) {
177
+ if (!node.name)
178
+ continue;
179
+ const width = externalLabelWidth(node.type, node.name);
180
+ if (width === undefined)
181
+ continue;
182
+ const rank = graph.ranks.get(node.id) ?? 0;
183
+ const cw = colWidth.get(rank) ?? (sizes.get(node.id) ?? DEFAULT_SIZE).width;
184
+ overhang.set(rank, Math.max(overhang.get(rank) ?? 0, (width - cw) / 2));
185
+ }
186
+ const gaps = new Map();
187
+ for (const [i, rank] of ranks.entries()) {
188
+ if (i === 0)
189
+ continue;
190
+ const previous = ranks[i - 1];
191
+ const labels = Math.max(0, overhang.get(previous) ?? 0) + Math.max(0, overhang.get(rank) ?? 0);
192
+ const base = tall.has(rank) || tall.has(previous) ? CONTAINER_GAP : H_GAP;
193
+ gaps.set(rank, Math.max(base, labels + 2 * EXTERNAL_LABEL_OFFSET + LABEL_MARGIN));
194
+ }
195
+ for (const flows of graph.outgoing.values()) {
196
+ for (const flow of flows) {
197
+ if (!flow.name || graph.backEdges.has(flow.id))
198
+ continue;
199
+ const target = rankOf(flow.targetRef);
200
+ const at = position.get(target);
201
+ if (at === undefined || at === 0)
202
+ continue;
203
+ // Only a flow between neighbouring columns has nowhere else to put its label.
204
+ if (position.get(rankOf(flow.sourceRef)) !== at - 1)
205
+ continue;
206
+ const need = Math.max(flow.name.length * LABEL_CHAR_WIDTH, LABEL_MIN_WIDTH) + 2 * LABEL_MARGIN;
207
+ gaps.set(target, Math.max(gaps.get(target) ?? H_GAP, need));
208
+ }
209
+ }
210
+ return gaps;
211
+ }
212
+ /** How far a node's centre line sits below its top. */
213
+ function centreOffset(id, size, axes) {
214
+ if (size.height <= DEFAULT_SIZE.height)
215
+ return size.height / 2;
216
+ // A tall container carries its inner spine on the row's line; one without an
217
+ // inner flow (a tool grid) hangs from the task line instead.
218
+ return axes.get(id) ?? DEFAULT_SIZE.height / 2;
219
+ }
220
+ /**
221
+ * Space a node needs above and below its band's line: its shape, the label
222
+ * below an event or gateway, and the boundary events docked on its borders
223
+ * together with their labels.
224
+ */
225
+ function extentOf(graph, id, size, axes, titled) {
226
+ const offset = centreOffset(id, size, axes);
227
+ let above = offset;
228
+ let below = size.height - offset;
229
+ const el = graph.byId.get(id);
230
+ if (el?.name && externalLabelWidth(el.type, el.name) !== undefined) {
231
+ below = Math.max(below, size.height / 2 + EXTERNAL_LABEL_OFFSET + LABEL_HEIGHT);
232
+ }
233
+ for (const event of graph.attachers.get(id) ?? []) {
234
+ const reach = BOUNDARY_SIZE / 2 + (event.name ? EXTERNAL_LABEL_OFFSET + LABEL_HEIGHT : 0);
235
+ if (boundarySide(event, titled.has(id)) === -1)
236
+ above = Math.max(above, offset + reach);
237
+ else
238
+ below = Math.max(below, size.height - offset + reach);
239
+ }
240
+ return { above, below };
241
+ }
242
+ /**
243
+ * The y of each band's line, relative to the spine at 0.
244
+ *
245
+ * Bands keep their order, but a band only clears what it actually sits under
246
+ * or over: the shapes above it in its own columns, and those its connecting
247
+ * flows run past. A tall sub-process therefore pushes down only the branches
248
+ * beneath it, not every branch in the diagram.
249
+ */
250
+ function bandLines(graph, bandLayout, members, sizes, xOf, axes, titled) {
251
+ const extent = new Map();
252
+ const byBand = new Map();
253
+ for (const id of members) {
254
+ extent.set(id, extentOf(graph, id, sizes.get(id) ?? DEFAULT_SIZE, axes, titled));
255
+ const band = bandLayout.bands.get(id) ?? 0;
256
+ const list = byBand.get(band);
257
+ if (list)
258
+ list.push(id);
259
+ else
260
+ byBand.set(band, [id]);
261
+ }
262
+ const line = new Map();
263
+ /** Placed boxes: x-range and how far they reach above / below their line. */
264
+ const placed = [];
265
+ const place = (band, y) => {
266
+ line.set(band, y);
267
+ for (const id of byBand.get(band) ?? []) {
268
+ const { left, right } = xOf(id);
269
+ const e = extent.get(id);
270
+ placed.push({ left, right, top: y - e.above, bottom: y + e.below });
271
+ }
272
+ };
273
+ /** Centre x of every node a band's members connect to, so the band clears its own flows. */
274
+ const span = (band) => {
275
+ let left = Number.POSITIVE_INFINITY;
276
+ let right = Number.NEGATIVE_INFINITY;
277
+ const memberSet = new Set(byBand.get(band) ?? []);
278
+ const take = (id) => {
279
+ const el = graph.byId.get(id);
280
+ const host = el?.type === "boundaryEvent" ? el.attachedToRef : id;
281
+ if (!graph.byId.has(host))
282
+ return;
283
+ const b = xOf(host);
284
+ const mid = b.left + b.width / 2;
285
+ left = Math.min(left, memberSet.has(host) ? b.left : mid);
286
+ right = Math.max(right, memberSet.has(host) ? b.right : mid);
287
+ };
288
+ for (const id of memberSet) {
289
+ take(id);
290
+ for (const f of graph.outgoing.get(id) ?? [])
291
+ if (!graph.backEdges.has(f.id))
292
+ take(f.targetRef);
293
+ for (const f of graph.incoming.get(id) ?? [])
294
+ if (!graph.backEdges.has(f.id))
295
+ take(f.sourceRef);
296
+ }
297
+ return { left, right };
298
+ };
299
+ const xOverlap = (a, b) => a.left < b.right && b.left < a.right;
300
+ const bands = [...byBand.keys()];
301
+ if (byBand.has(0))
302
+ place(0, 0);
303
+ const below = bands.filter((b) => b > 0).sort((a, b) => a - b);
304
+ const above = bands.filter((b) => b < 0).sort((a, b) => b - a);
305
+ for (const [list, dir] of [
306
+ [below, 1],
307
+ [above, -1],
308
+ ]) {
309
+ let previous = 0;
310
+ for (const band of list) {
311
+ const wide = span(band);
312
+ let y = previous + dir;
313
+ for (const box of placed) {
314
+ if (xOverlap(box, wide)) {
315
+ y = dir === 1 ? Math.max(y, box.bottom + V_GAP) : Math.min(y, box.top - V_GAP);
316
+ }
317
+ for (const id of byBand.get(band) ?? []) {
318
+ const b = xOf(id);
319
+ const e = extent.get(id);
320
+ const reach = { left: b.left - H_GAP / 2, right: b.right + H_GAP / 2 };
321
+ if (!xOverlap(box, reach))
322
+ continue;
323
+ y =
324
+ dir === 1
325
+ ? Math.max(y, box.bottom + V_GAP + e.above)
326
+ : Math.min(y, box.top - V_GAP - e.below);
327
+ }
328
+ }
329
+ place(band, y);
330
+ previous = y;
331
+ }
332
+ }
333
+ return { line, extent };
334
+ }
137
335
  /**
138
336
  * Nodes with no incoming or outgoing flow, in declaration order — only when
139
337
  * there are at least two of them, since one has nothing to be packed with.
@@ -98,7 +98,7 @@ export function routeFlows(graph, flows, bounds, bandLayout, gutterX) {
98
98
  : forwardCandidates(ctx, source, target, sourceRank, targetRank);
99
99
  const clear = pick(direct, ctx, flow.sourceRef, flow.targetRef);
100
100
  if (clear && clear.crossings <= CROSSING_TOLERANCE) {
101
- commit(ctx, routed, flow.id, clear.waypoints);
101
+ commit(ctx, routed, flow, clear.waypoints);
102
102
  continue;
103
103
  }
104
104
  pending.push({
@@ -119,12 +119,12 @@ export function routeFlows(graph, flows, bounds, bandLayout, gutterX) {
119
119
  const around = detour(ctx, item.source, item.target, item.sourceRank, item.targetRank, item.below, item.flow.sourceRef, item.flow.targetRef);
120
120
  // Going around costs bends and length, so it has to save more than one
121
121
  // crossing to be worth taking.
122
- const cost = crossingCount(around, ctx) + DETOUR_PENALTY;
122
+ const cost = crossingCount(around, ctx, item.flow.sourceRef, item.flow.targetRef) + DETOUR_PENALTY;
123
123
  const chosen = item.direct && item.direct.crossings <= cost ? item.direct.waypoints : around;
124
124
  const waypoints = blocked(ctx, chosen, item.flow.sourceRef, item.flow.targetRef)
125
125
  ? (searchRoute(ctx, item.flow, item.source, item.target) ?? chosen)
126
126
  : chosen;
127
- commit(ctx, routed, item.flow.id, waypoints);
127
+ commit(ctx, routed, item.flow, waypoints);
128
128
  }
129
129
  // Emit in declaration order, whichever pass produced the route.
130
130
  const edges = [];
@@ -180,13 +180,19 @@ function searchRoute(ctx, flow, source, target) {
180
180
  });
181
181
  }
182
182
  /** Record a chosen route so later edges can steer around it. */
183
- function commit(ctx, routed, id, waypoints) {
184
- routed.set(id, waypoints);
183
+ function commit(ctx, routed, flow, waypoints) {
184
+ routed.set(flow.id, waypoints);
185
185
  for (let i = 0; i + 1 < waypoints.length; i++) {
186
186
  const a = waypoints[i];
187
187
  const b = waypoints[i + 1];
188
- if (a && b)
189
- ctx.routed.insert(Math.min(a.x, b.x), Math.max(a.x, b.x), [a, b]);
188
+ if (a && b) {
189
+ ctx.routed.insert(Math.min(a.x, b.x), Math.max(a.x, b.x), [
190
+ a,
191
+ b,
192
+ flow.sourceRef,
193
+ flow.targetRef,
194
+ ]);
195
+ }
190
196
  }
191
197
  }
192
198
  function hostOf(graph, eventId) {
@@ -204,7 +210,7 @@ function pick(candidates, ctx, sourceId, targetId) {
204
210
  // the two endpoints are exempt from the obstacle check.
205
211
  if (blocked(ctx, candidate, sourceId, targetId))
206
212
  continue;
207
- const crossings = crossingCount(candidate, ctx);
213
+ const crossings = crossingCount(candidate, ctx, sourceId, targetId);
208
214
  if (crossings === 0)
209
215
  return { waypoints: candidate, crossings };
210
216
  if (!best || crossings < best.crossings)
@@ -217,22 +223,45 @@ function intersects(p, q, r, t) {
217
223
  const side = (o, a, b) => Math.sign((a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x));
218
224
  return side(p, q, r) !== side(p, q, t) && side(r, t, p) !== side(r, t, q);
219
225
  }
220
- /** How many already-routed edges a candidate would cut across. */
221
- function crossingCount(waypoints, ctx) {
226
+ /**
227
+ * How many already-routed edges a candidate would cut across, or run along:
228
+ * a stretch drawn on top of an unrelated flow reads as one line and hides
229
+ * where either goes. Flows sharing an endpoint may merge, as a join's do.
230
+ */
231
+ function crossingCount(waypoints, ctx, sourceId, targetId) {
222
232
  let count = 0;
223
233
  for (let i = 0; i + 1 < waypoints.length; i++) {
224
234
  const a = waypoints[i];
225
235
  const b = waypoints[i + 1];
226
236
  if (!a || !b)
227
237
  continue;
228
- ctx.routed.query(Math.min(a.x, b.x), Math.max(a.x, b.x), ([c, d]) => {
238
+ ctx.routed.query(Math.min(a.x, b.x), Math.max(a.x, b.x), ([c, d, from, to]) => {
229
239
  if (intersects(a, b, c, d))
230
240
  count++;
241
+ else if (sourceId !== undefined &&
242
+ from !== sourceId &&
243
+ to !== targetId &&
244
+ collinearOverlap(a, b, c, d)) {
245
+ // Worse than a clean crossing: the reader cannot tell the two apart.
246
+ count += 2;
247
+ }
231
248
  return false;
232
249
  });
233
250
  }
234
251
  return count;
235
252
  }
253
+ /** True when two axis-parallel segments lie on one line and share more than a point. */
254
+ function collinearOverlap(a, b, c, d) {
255
+ const shared = (p, q, r, t) => Math.min(Math.max(p, q), Math.max(r, t)) - Math.max(Math.min(p, q), Math.min(r, t)) >
256
+ HIT_TOLERANCE;
257
+ if (Math.abs(a.x - b.x) < 1 && Math.abs(c.x - d.x) < 1 && Math.abs(a.x - c.x) < 1) {
258
+ return shared(a.y, b.y, c.y, d.y);
259
+ }
260
+ if (Math.abs(a.y - b.y) < 1 && Math.abs(c.y - d.y) < 1 && Math.abs(a.y - c.y) < 1) {
261
+ return shared(a.x, b.x, c.x, d.x);
262
+ }
263
+ return false;
264
+ }
236
265
  /** True when the segment a→b passes through `o`, grazing allowed up to HIT_TOLERANCE. */
237
266
  function segmentHits(a, b, o) {
238
267
  const minX = Math.min(a.x, b.x) + HIT_TOLERANCE;
@@ -295,8 +324,9 @@ function forwardCandidates(ctx, source, target, sourceRank, targetRank) {
295
324
  if (rightOf) {
296
325
  const inTarget = from.y > target.y && from.y < target.y + target.height;
297
326
  const inSource = to.y > source.y && to.y < source.y + source.height;
298
- if (inTarget !== inSource) {
299
- const y = inTarget ? from.y : to.y;
327
+ if (inTarget || inSource) {
328
+ // Both centres inside each other: the smaller shape's line is the row's.
329
+ const y = inTarget && (!inSource || source.height <= target.height) ? from.y : to.y;
300
330
  return [
301
331
  [
302
332
  { x: source.x + source.width, y },
@@ -382,8 +412,10 @@ function fromBoundaryCandidates(ctx, source, host, target, targetRank) {
382
412
  { x: gutter, y: to.y },
383
413
  { x: target.x, y: to.y },
384
414
  ];
415
+ // The gutter can already carry another flow's vertical run; beside it is clear.
416
+ const beside = [-1, 1].map((side) => around.map((wp, i) => i === 2 || i === 3 ? { x: gutter + (side * CORRIDOR_SPACING) / 2, y: wp.y } : wp));
385
417
  if (!ahead)
386
- return [around];
418
+ return [around, ...beside];
387
419
  return [
388
420
  [
389
421
  { x: from.x, y: exitY },
@@ -391,6 +423,7 @@ function fromBoundaryCandidates(ctx, source, host, target, targetRank) {
391
423
  { x: target.x, y: to.y },
392
424
  ],
393
425
  around,
426
+ ...beside,
394
427
  ];
395
428
  }
396
429
  return [
package/llms.txt ADDED
@@ -0,0 +1,9 @@
1
+ # @bpmnkit/core
2
+
3
+ > BPMN 2.0, DMN and Camunda Form SDK for TypeScript. Agents: read the card for your type, not the README.
4
+
5
+ - [AGENTS.md](AGENTS.md): what to read and the rules for all types
6
+ - [BPMN card](agent-cards/bpmn.md): process build script and traps
7
+ - [DMN card](agent-cards/dmn.md): decision table and DRD script and traps
8
+ - [Form card](agent-cards/form.md): form script and traps
9
+ - [Builder reference](builder-reference.md): every builder option
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bpmnkit/core",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -21,10 +21,14 @@
21
21
  "files": [
22
22
  "LICENSE",
23
23
  "README.md",
24
+ "AGENTS.md",
25
+ "llms.txt",
26
+ "agent-cards",
24
27
  "builder-reference.md",
25
28
  "dist/**/*.js",
26
29
  "dist/**/*.d.ts"
27
30
  ],
31
+ "agents": "./AGENTS.md",
28
32
  "engines": {
29
33
  "node": ">=20"
30
34
  },