@bpmnkit/core 1.1.0 → 1.3.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.
- package/README.md +31 -0
- package/dist/bpmn/bpmn-builder.d.ts +8 -0
- package/dist/bpmn/bpmn-builder.js +6 -2
- package/dist/bpmn/compact.d.ts +37 -5
- package/dist/bpmn/compact.js +218 -30
- package/dist/bpmn/connector-line.d.ts +46 -0
- package/dist/bpmn/connector-line.js +135 -0
- package/dist/bpmn/optimize/patterns.js +4 -3
- package/dist/bpmn/process-delta.d.ts +91 -0
- package/dist/bpmn/process-delta.js +259 -0
- package/dist/bpmn/process-text-writer.d.ts +63 -0
- package/dist/bpmn/process-text-writer.js +202 -0
- package/dist/bpmn/process-text.d.ts +200 -0
- package/dist/bpmn/process-text.js +1568 -0
- package/dist/bpmn/rest-connector.d.ts +6 -0
- package/dist/bpmn/rest-connector.js +6 -0
- package/dist/bpmn/secrets.d.ts +15 -0
- package/dist/bpmn/secrets.js +59 -0
- package/dist/bpmn/utils.js +4 -0
- package/dist/bpmn/zeebe-extensions.d.ts +10 -0
- package/dist/bpmn/zeebe-extensions.js +7 -0
- package/dist/connectors/aliases.d.ts +21 -0
- package/dist/connectors/aliases.js +272 -0
- package/dist/connectors/api.d.ts +118 -0
- package/dist/connectors/api.js +280 -0
- package/dist/connectors/apply-element.d.ts +59 -0
- package/dist/connectors/apply-element.js +385 -0
- package/dist/connectors/apply.d.ts +136 -0
- package/dist/connectors/apply.js +352 -0
- package/dist/connectors/cards.d.ts +96 -0
- package/dist/connectors/cards.js +306 -0
- package/dist/connectors/catalog.d.ts +79 -0
- package/dist/connectors/catalog.js +209 -0
- package/dist/connectors/index.d.ts +33 -0
- package/dist/connectors/index.js +24 -0
- package/dist/connectors/lines.d.ts +108 -0
- package/dist/connectors/lines.js +568 -0
- package/dist/connectors/select.d.ts +85 -0
- package/dist/connectors/select.js +419 -0
- package/dist/connectors/template-types.d.ts +188 -0
- package/dist/connectors/template-types.js +6 -0
- package/dist/connectors/templates/bpmnkit.d.ts +12 -0
- package/dist/connectors/templates/bpmnkit.js +186 -0
- package/dist/connectors/templates/generated.d.ts +3 -0
- package/dist/connectors/templates/generated.js +95885 -0
- package/dist/connectors/validate.d.ts +66 -0
- package/dist/connectors/validate.js +271 -0
- package/dist/index.d.ts +11 -1
- package/dist/index.js +5 -0
- package/package.json +5 -1
|
@@ -0,0 +1,1568 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A line format for a language model to write a process in.
|
|
3
|
+
*
|
|
4
|
+
* A model's time and cost are its output tokens, and a diagram as compact JSON
|
|
5
|
+
* spends most of them on keys, quotes and ids nobody reads: flow ids, `"type":`,
|
|
6
|
+
* `"from":`, the merge gateway every branch has to be routed into. This format
|
|
7
|
+
* writes a path as `a > b > c`, declares a node inline the first time it is
|
|
8
|
+
* used, and leaves ids, merges and the obvious gaps to {@link parseProcessText}
|
|
9
|
+
* — about a quarter of the tokens of minified compact JSON on the repo's own
|
|
10
|
+
* fixtures (`doc/drop-ai-generate-analysis.md` §2).
|
|
11
|
+
*
|
|
12
|
+
* Every line is a complete fact, so a diagram can be drawn while it is still
|
|
13
|
+
* being written: {@link createProcessTextStream} reads lines as they finish.
|
|
14
|
+
* JSON cannot do this without a brace scanner, and Workers AI's JSON mode cannot
|
|
15
|
+
* stream at all.
|
|
16
|
+
*
|
|
17
|
+
* The parser never throws on its input. Anything it cannot use is reported as a
|
|
18
|
+
* {@link ProcessTextProblem} and left out, and what it adds to complete the
|
|
19
|
+
* diagram is listed in `fixes` — so the result always expands.
|
|
20
|
+
*
|
|
21
|
+
* @packageDocumentation
|
|
22
|
+
*/
|
|
23
|
+
import { parseExpression } from "@bpmnkit/feel";
|
|
24
|
+
import { slugify, uniqueId } from "../plan/slug.js";
|
|
25
|
+
import { expand } from "./compact.js";
|
|
26
|
+
import { CONNECTOR_LINE, parseConnectorLine } from "./connector-line.js";
|
|
27
|
+
/**
|
|
28
|
+
* How to write the format — the part of a system prompt that teaches it.
|
|
29
|
+
*
|
|
30
|
+
* It lives beside the parser so the two cannot drift: a test parses its example
|
|
31
|
+
* and expects no problems.
|
|
32
|
+
*/
|
|
33
|
+
export const PROCESS_TEXT_GUIDE = `Format — one path per line, nothing else:
|
|
34
|
+
# Process name (first line)
|
|
35
|
+
a > b > c sequence flow
|
|
36
|
+
id[kind Name] declare a node the first time it appears; afterwards write only id
|
|
37
|
+
gw >(Label: condition) x conditional branch, condition in FEEL (amount > 1000, status = "ok")
|
|
38
|
+
gw >(Label: default) y branch taken when no condition holds
|
|
39
|
+
Kinds: start end task user service rule (DMN decision) send receive script manual call xor and or eventgw catch (wait for message or timer) throw boundary
|
|
40
|
+
Events take a trigger: start:message end:error catch:timer boundary:error (timer message signal error escalation terminate conditional compensate cancel)
|
|
41
|
+
Attributes after |: on=<task id> (required on boundary), nonint (non-interrupting), job=<job type>, each=<list variable> (once per item), after=<ISO duration> (timer)
|
|
42
|
+
Rules:
|
|
43
|
+
- One start event. Every node is on a path from it to an end event: never a node nothing leads to.
|
|
44
|
+
- An xor has two or more branches: exactly one is (Label: default), each other has a FEEL condition.
|
|
45
|
+
- A boundary starts its own line, on a task you declared, and leads to a task that handles it; never draw an arrow into a boundary.
|
|
46
|
+
- Branches that meet again are joined automatically; join parallel branches with an and node.
|
|
47
|
+
- Steps done at the same time start from an and node and meet at another. A deadline is a boundary:timer with after= on the task that may run late. A step done for every item of a list takes each=<list>; use it only when the description says every or each.
|
|
48
|
+
|
|
49
|
+
Example:
|
|
50
|
+
# Expense approval
|
|
51
|
+
start[start Expense submitted] > check[xor Amount over 1000?]
|
|
52
|
+
check >(Yes: amount > 1000) review[user Review expense] > pay[service Pay expense] > done[end Expense paid]
|
|
53
|
+
check >(No: default) auto[service Approve automatically] > pay
|
|
54
|
+
failed[boundary:error Payment failed | on=pay] > notify[send Notify submitter] > notice[end Payment failed]`;
|
|
55
|
+
export const KINDS = {
|
|
56
|
+
start: "startEvent",
|
|
57
|
+
end: "endEvent",
|
|
58
|
+
task: "task",
|
|
59
|
+
user: "userTask",
|
|
60
|
+
service: "serviceTask",
|
|
61
|
+
rule: "businessRuleTask",
|
|
62
|
+
send: "sendTask",
|
|
63
|
+
receive: "receiveTask",
|
|
64
|
+
script: "scriptTask",
|
|
65
|
+
manual: "manualTask",
|
|
66
|
+
call: "callActivity",
|
|
67
|
+
xor: "exclusiveGateway",
|
|
68
|
+
and: "parallelGateway",
|
|
69
|
+
or: "inclusiveGateway",
|
|
70
|
+
eventgw: "eventBasedGateway",
|
|
71
|
+
catch: "intermediateCatchEvent",
|
|
72
|
+
throw: "intermediateThrowEvent",
|
|
73
|
+
boundary: "boundaryEvent",
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Words models write for a kind instead of the one the guide teaches, taken
|
|
77
|
+
* from the Drop benchmark's recorded answers. Accepted without a problem: the
|
|
78
|
+
* meaning is plain, and teaching them would only lengthen the prompt.
|
|
79
|
+
*/
|
|
80
|
+
export const ALIASES = {
|
|
81
|
+
event: "intermediateCatchEvent",
|
|
82
|
+
parallel: "parallelGateway",
|
|
83
|
+
exclusive: "exclusiveGateway",
|
|
84
|
+
gateway: "exclusiveGateway",
|
|
85
|
+
inclusive: "inclusiveGateway",
|
|
86
|
+
decision: "businessRuleTask",
|
|
87
|
+
dmn: "businessRuleTask",
|
|
88
|
+
human: "userTask",
|
|
89
|
+
};
|
|
90
|
+
export const TRIGGERS = new Set([
|
|
91
|
+
"timer",
|
|
92
|
+
"message",
|
|
93
|
+
"signal",
|
|
94
|
+
"error",
|
|
95
|
+
"escalation",
|
|
96
|
+
"terminate",
|
|
97
|
+
"conditional",
|
|
98
|
+
"compensate",
|
|
99
|
+
"link",
|
|
100
|
+
"cancel",
|
|
101
|
+
]);
|
|
102
|
+
export const EVENTS = new Set([
|
|
103
|
+
"startEvent",
|
|
104
|
+
"endEvent",
|
|
105
|
+
"intermediateCatchEvent",
|
|
106
|
+
"intermediateThrowEvent",
|
|
107
|
+
"boundaryEvent",
|
|
108
|
+
]);
|
|
109
|
+
const GATEWAYS = new Set([
|
|
110
|
+
"exclusiveGateway",
|
|
111
|
+
"parallelGateway",
|
|
112
|
+
"inclusiveGateway",
|
|
113
|
+
"eventBasedGateway",
|
|
114
|
+
]);
|
|
115
|
+
/** Gateways whose outgoing flows carry conditions, and so a default. */
|
|
116
|
+
const CONDITIONAL = new Set(["exclusiveGateway", "inclusiveGateway"]);
|
|
117
|
+
/** Task types that need a `zeebe:taskDefinition` to deploy; the id stands in when none is given. */
|
|
118
|
+
const JOB_TASKS = new Set(["serviceTask", "sendTask"]);
|
|
119
|
+
const ID = /^[A-Za-z_][\w.-]*/;
|
|
120
|
+
/** A line that ends in an arrow, with or without a branch label: the path goes on below. */
|
|
121
|
+
const DANGLING = /\s*-{0,2}>\s*(\([^()]*\))?\s*$/;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the index of the `close` that matches the `open` at `start`, or -1.
|
|
124
|
+
* Quoted text is skipped, so a FEEL string holding a bracket does not end it.
|
|
125
|
+
*/
|
|
126
|
+
function matching(text, start, open, close) {
|
|
127
|
+
let depth = 0;
|
|
128
|
+
let quote = false;
|
|
129
|
+
for (let i = start; i < text.length; i++) {
|
|
130
|
+
const ch = text[i];
|
|
131
|
+
if (ch === '"')
|
|
132
|
+
quote = !quote;
|
|
133
|
+
if (quote)
|
|
134
|
+
continue;
|
|
135
|
+
if (ch === open)
|
|
136
|
+
depth++;
|
|
137
|
+
else if (ch === close && --depth === 0)
|
|
138
|
+
return i;
|
|
139
|
+
}
|
|
140
|
+
return -1;
|
|
141
|
+
}
|
|
142
|
+
/** The type a bare id suggests, for a node written without a kind or never declared. */
|
|
143
|
+
function typeFromId(id) {
|
|
144
|
+
// `pick > and`, `and > dispatch`: the kind written as if it were an id.
|
|
145
|
+
const kind = KINDS[id.toLowerCase()] ?? ALIASES[id.toLowerCase()];
|
|
146
|
+
if (kind !== undefined && GATEWAYS.has(kind))
|
|
147
|
+
return kind;
|
|
148
|
+
if (/^start/i.test(id))
|
|
149
|
+
return "startEvent";
|
|
150
|
+
if (/^(end|done|finish)/i.test(id))
|
|
151
|
+
return "endEvent";
|
|
152
|
+
return "task";
|
|
153
|
+
}
|
|
154
|
+
/** `send_email` / `sendEmail` → "Send email": a readable name for a node the model never named. */
|
|
155
|
+
function nameFromId(id) {
|
|
156
|
+
const words = id
|
|
157
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
158
|
+
.replace(/[_.-]+/g, " ")
|
|
159
|
+
.trim()
|
|
160
|
+
.toLowerCase();
|
|
161
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Splits one complete path line (`a[kind Name] >(Label: cond) b > c`) into its
|
|
165
|
+
* node references and arrow labels, or says where it stops making sense.
|
|
166
|
+
*
|
|
167
|
+
* Shared by {@link parseProcessText} and the change-script parser, so the two
|
|
168
|
+
* read paths the same way.
|
|
169
|
+
*/
|
|
170
|
+
export function tokenizePath(text) {
|
|
171
|
+
const refs = [];
|
|
172
|
+
const labels = [];
|
|
173
|
+
const joined = [];
|
|
174
|
+
const unclosed = [];
|
|
175
|
+
const bracketless = [];
|
|
176
|
+
let note;
|
|
177
|
+
let i = 0;
|
|
178
|
+
for (;;) {
|
|
179
|
+
let id = ID.exec(text.slice(i))?.[0];
|
|
180
|
+
// `1[service List runs] > 2[xor …]`: numbers for ids, which BPMN ids cannot start with
|
|
181
|
+
const number = id === undefined ? /^\d[\w-]*(?=\s*(?:\[|-{0,2}>))/.exec(text.slice(i))?.[0] : undefined;
|
|
182
|
+
if (number !== undefined) {
|
|
183
|
+
id = `n${number}`;
|
|
184
|
+
if (!joined.some((j) => j.id === id))
|
|
185
|
+
joined.push({ written: number, id });
|
|
186
|
+
i += number.length;
|
|
187
|
+
}
|
|
188
|
+
else if (id === undefined) {
|
|
189
|
+
return { error: `expected a node id at "${text.slice(i, i + 20)}"` };
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
i += id.length;
|
|
193
|
+
}
|
|
194
|
+
// `call back[service …]`: words before a bracket are one id
|
|
195
|
+
const words = /^((?: +[A-Za-z_][\w.-]*)+)(?= *\[)/.exec(text.slice(i))?.[1];
|
|
196
|
+
if (words !== undefined) {
|
|
197
|
+
const written = `${id}${words}`;
|
|
198
|
+
id = written.split(/ +/).join("_");
|
|
199
|
+
joined.push({ written, id });
|
|
200
|
+
i += words.length;
|
|
201
|
+
}
|
|
202
|
+
let spec;
|
|
203
|
+
// `… > xor Notify sales? >(Yes: …)`: a kind and a name without brackets, after an
|
|
204
|
+
// arrow. At the start of a line it is more likely prose ("end of the answer").
|
|
205
|
+
const bare = refs.length > 0 && KINDS[id] !== undefined
|
|
206
|
+
? /^ +([^[\]()>|]+?)(?=\s*(?:-{0,2}>|$))/.exec(text.slice(i))
|
|
207
|
+
: null;
|
|
208
|
+
if (bare?.[1]) {
|
|
209
|
+
spec = `${id} ${bare[1].trim()}`;
|
|
210
|
+
bracketless.push(id);
|
|
211
|
+
i += bare[0].length;
|
|
212
|
+
}
|
|
213
|
+
// `done-end [end Done]`: a space before the bracket still declares.
|
|
214
|
+
const gap = /^ +\[/.exec(text.slice(i));
|
|
215
|
+
if (gap)
|
|
216
|
+
i += gap[0].length - 1;
|
|
217
|
+
if (text[i] === "[") {
|
|
218
|
+
let end = matching(text, i, "[", "]");
|
|
219
|
+
let after = end + 1;
|
|
220
|
+
if (end < 0) {
|
|
221
|
+
// `start[start HR) > …` or `end[Page created` at the end of the line: the
|
|
222
|
+
// bracket closes where its name plainly ends, before the next arrow
|
|
223
|
+
const open = /^[^[\]]*?(?:\)(?=\s*-{0,2}>)|(?=\s+-{0,2}>)|$)/.exec(text.slice(i + 1))?.[0];
|
|
224
|
+
if (open === undefined || open.trim() === "")
|
|
225
|
+
return { error: `"${id}[" is not closed` };
|
|
226
|
+
const closer = open.endsWith(")") ? 1 : 0;
|
|
227
|
+
end = i + 1 + open.length - closer;
|
|
228
|
+
after = i + 1 + open.length;
|
|
229
|
+
unclosed.push(id);
|
|
230
|
+
}
|
|
231
|
+
spec = text.slice(i + 1, end).trim();
|
|
232
|
+
i = after;
|
|
233
|
+
}
|
|
234
|
+
refs.push({ id, spec });
|
|
235
|
+
while (text[i] === " ")
|
|
236
|
+
i++;
|
|
237
|
+
if (i >= text.length)
|
|
238
|
+
break;
|
|
239
|
+
// `gr[xor Reproducible?] (ADDED)`: a note after the last node, most
|
|
240
|
+
// often marking what a change added.
|
|
241
|
+
const trailing = /^\([^()]*\)\s*$/.exec(text.slice(i))?.[0];
|
|
242
|
+
if (trailing !== undefined) {
|
|
243
|
+
note = trailing.trim();
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
// `->` and `-->` are what a model reaches for from Mermaid; take them too.
|
|
247
|
+
const arrow = /^-{0,2}>/.exec(text.slice(i))?.[0];
|
|
248
|
+
if (arrow === undefined)
|
|
249
|
+
return { error: `expected ">" at "${text.slice(i, i + 20)}"` };
|
|
250
|
+
i += arrow.length;
|
|
251
|
+
let label;
|
|
252
|
+
// `credit > (No: default) decision`: a space before the label
|
|
253
|
+
const spaced = /^ +\(/.exec(text.slice(i));
|
|
254
|
+
if (spaced)
|
|
255
|
+
i += spaced[0].length - 1;
|
|
256
|
+
if (text[i] === "(") {
|
|
257
|
+
const end = matching(text, i, "(", ")");
|
|
258
|
+
if (end < 0)
|
|
259
|
+
return { error: "edge label is not closed" };
|
|
260
|
+
label = text.slice(i + 1, end);
|
|
261
|
+
i = end + 1;
|
|
262
|
+
}
|
|
263
|
+
labels.push(label);
|
|
264
|
+
while (text[i] === " ")
|
|
265
|
+
i++;
|
|
266
|
+
// `gw >(No: default) > next`: a second arrow after the label.
|
|
267
|
+
if (label !== undefined && text[i] === ">") {
|
|
268
|
+
i++;
|
|
269
|
+
while (text[i] === " ")
|
|
270
|
+
i++;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const tokens = { refs, labels };
|
|
274
|
+
if (note !== undefined)
|
|
275
|
+
tokens.note = note;
|
|
276
|
+
if (joined.length > 0)
|
|
277
|
+
tokens.joined = joined;
|
|
278
|
+
if (unclosed.length > 0)
|
|
279
|
+
tokens.unclosed = unclosed;
|
|
280
|
+
if (bracketless.length > 0)
|
|
281
|
+
tokens.bracketless = bracketless;
|
|
282
|
+
return tokens;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Keeps a condition only if it is FEEL.
|
|
286
|
+
*
|
|
287
|
+
* Models write the branch they mean in prose as often as in FEEL
|
|
288
|
+
* (`No: is not approved`). As an expression that fails at deploy time; as the
|
|
289
|
+
* branch's label it still says what was meant, and the missing condition is
|
|
290
|
+
* one the lint names and a reader can fill in.
|
|
291
|
+
*/
|
|
292
|
+
export function conditionOrLabel(edge) {
|
|
293
|
+
if (edge.condition === undefined)
|
|
294
|
+
return { edge };
|
|
295
|
+
const prose = edge.condition.slice(1).trim();
|
|
296
|
+
if (parseExpression(prose).errors.length === 0)
|
|
297
|
+
return { edge };
|
|
298
|
+
const { condition: _dropped, ...rest } = edge;
|
|
299
|
+
return {
|
|
300
|
+
edge: { ...rest, name: edge.name ? `${edge.name}: ${prose}` : prose },
|
|
301
|
+
problem: `condition "${prose}" is not FEEL; kept as the branch label`,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
class Reader {
|
|
305
|
+
nodes = new Map();
|
|
306
|
+
/**
|
|
307
|
+
* The node each written id means now. Models reuse an id for a second node
|
|
308
|
+
* (`done[end Approved]`, later `done[end Rejected]`); the second becomes
|
|
309
|
+
* `done_2`, and a bare `done` after it means the latest one.
|
|
310
|
+
*/
|
|
311
|
+
current = new Map();
|
|
312
|
+
/** Every node declared under each written id, oldest first. */
|
|
313
|
+
declared = new Map();
|
|
314
|
+
taken = new Set();
|
|
315
|
+
edges = [];
|
|
316
|
+
problems = [];
|
|
317
|
+
/** What reading a declaration repaired, listed with the parser's other fixes. */
|
|
318
|
+
fixes = [];
|
|
319
|
+
/** `with` lines, with the written id resolved to the node it meant when read. */
|
|
320
|
+
connectors = [];
|
|
321
|
+
/** Catch and boundary events written without a trigger, and made message events. */
|
|
322
|
+
guessedMessage = new Set();
|
|
323
|
+
title;
|
|
324
|
+
/** A line that ended in an arrow, waiting for the line that continues it. */
|
|
325
|
+
carry;
|
|
326
|
+
/** The last node of the last path read, which a line starting with an arrow continues. */
|
|
327
|
+
tail;
|
|
328
|
+
/** Reads one line. Returns whether it added anything. */
|
|
329
|
+
line(raw, n) {
|
|
330
|
+
let text = raw.trim();
|
|
331
|
+
if (this.carry !== undefined) {
|
|
332
|
+
if (text === "")
|
|
333
|
+
return false;
|
|
334
|
+
text = `${this.carry.text} ${text}`;
|
|
335
|
+
this.carry = undefined;
|
|
336
|
+
}
|
|
337
|
+
if (text === "" || text.startsWith("```") || text.startsWith("//"))
|
|
338
|
+
return false;
|
|
339
|
+
if (text.startsWith("#")) {
|
|
340
|
+
if (this.title === undefined)
|
|
341
|
+
this.title = text.replace(/^#+/, "").trim() || undefined;
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
if (CONNECTOR_LINE.test(text)) {
|
|
345
|
+
const connector = parseConnectorLine(text, n, this.problems);
|
|
346
|
+
if (connector) {
|
|
347
|
+
this.connectors.push({
|
|
348
|
+
...connector,
|
|
349
|
+
elementId: this.current.get(connector.id) ?? connector.id,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
// `> stop[end Done]`: the path above wrapped before its arrow
|
|
355
|
+
if (/^-{0,2}>/.test(text) && this.tail !== undefined) {
|
|
356
|
+
this.problems.push({
|
|
357
|
+
line: n,
|
|
358
|
+
message: `the line starts with ">"; read it as continuing "${this.tail}"`,
|
|
359
|
+
});
|
|
360
|
+
text = `${this.tail} ${text}`;
|
|
361
|
+
}
|
|
362
|
+
// `engineer[user Fix issue] >` then the next step on the next line: models
|
|
363
|
+
// wrap a long path. The line is read once it is complete.
|
|
364
|
+
if (DANGLING.test(text)) {
|
|
365
|
+
this.carry = { text, line: n };
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
return this.read(text, n);
|
|
369
|
+
}
|
|
370
|
+
/** Reads a line still waiting for its continuation, without its last arrow. */
|
|
371
|
+
finish() {
|
|
372
|
+
const carry = this.carry;
|
|
373
|
+
if (carry === undefined)
|
|
374
|
+
return false;
|
|
375
|
+
this.carry = undefined;
|
|
376
|
+
this.problems.push({
|
|
377
|
+
line: carry.line,
|
|
378
|
+
message: 'the line ends with ">" and nothing follows; read without it',
|
|
379
|
+
});
|
|
380
|
+
return this.read(carry.text.replace(DANGLING, ""), carry.line);
|
|
381
|
+
}
|
|
382
|
+
read(text, n) {
|
|
383
|
+
// Parse the whole line before keeping any of it, so a line that fails half
|
|
384
|
+
// way leaves nothing behind.
|
|
385
|
+
const path = tokenizePath(text);
|
|
386
|
+
if ("error" in path)
|
|
387
|
+
return this.fail(n, path.error);
|
|
388
|
+
if (path.note !== undefined) {
|
|
389
|
+
this.problems.push({ line: n, message: `ignored the note "${path.note}"` });
|
|
390
|
+
}
|
|
391
|
+
for (const id of path.bracketless ?? []) {
|
|
392
|
+
this.problems.push({
|
|
393
|
+
line: n,
|
|
394
|
+
message: `"${id} …" has no brackets; read it as declaring "${id}"`,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
for (const id of path.unclosed ?? []) {
|
|
398
|
+
this.problems.push({
|
|
399
|
+
line: n,
|
|
400
|
+
message: `"${id}[" is not closed; closed it where its name ends`,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
for (const { written, id } of path.joined ?? []) {
|
|
404
|
+
this.problems.push({ line: n, message: `"${written}" is not an id; read as "${id}"` });
|
|
405
|
+
}
|
|
406
|
+
const { refs, labels } = path;
|
|
407
|
+
// Declarations and references resolve left to right, so a chain that
|
|
408
|
+
// reuses an id (`task[A] > task[B]`) links the nodes in the order written.
|
|
409
|
+
const ids = refs.map((ref, k) => ref.spec !== undefined
|
|
410
|
+
? this.declare(ref.id, ref.spec, n, k > 0)
|
|
411
|
+
: (this.current.get(ref.id) ?? ref.id));
|
|
412
|
+
for (let k = 0; k < labels.length; k++) {
|
|
413
|
+
const from = ids[k];
|
|
414
|
+
const to = ids[k + 1];
|
|
415
|
+
if (from !== undefined && to !== undefined) {
|
|
416
|
+
this.edges.push({ from, to, line: n, ...this.feelOrLabel(edgeLabel(labels[k]), n) });
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
this.tail = ids.at(-1);
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
/** {@link conditionOrLabel}, with its problem recorded against line `n`. */
|
|
423
|
+
feelOrLabel(edge, n) {
|
|
424
|
+
const { edge: kept, problem } = conditionOrLabel(edge);
|
|
425
|
+
if (problem !== undefined)
|
|
426
|
+
this.problems.push({ line: n, message: problem });
|
|
427
|
+
return kept;
|
|
428
|
+
}
|
|
429
|
+
fail(n, message) {
|
|
430
|
+
this.problems.push({ line: n, message });
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Declares a node and returns the id it was stored under.
|
|
435
|
+
*
|
|
436
|
+
* @param afterArrow - Whether it follows a `>` on its line. A reused id there
|
|
437
|
+
* is the next step of a path (`check > done[end Rejected]`); at the start of a
|
|
438
|
+
* line it is the model revising a node it has already written.
|
|
439
|
+
*/
|
|
440
|
+
declare(written, spec, n, afterArrow) {
|
|
441
|
+
const before = this.declared.get(written) ?? [];
|
|
442
|
+
// Restating a node is harmless; the same id for something else is a new node.
|
|
443
|
+
const same = before.find((id) => this.nodes.get(id)?.spec.toLowerCase() === spec.toLowerCase());
|
|
444
|
+
if (same !== undefined) {
|
|
445
|
+
this.current.set(written, same);
|
|
446
|
+
return same;
|
|
447
|
+
}
|
|
448
|
+
const earlierId = before.at(-1);
|
|
449
|
+
const earlier = earlierId === undefined ? undefined : this.nodes.get(earlierId);
|
|
450
|
+
if (earlierId !== undefined && earlier) {
|
|
451
|
+
// Only a real kind after an arrow, or a boundary, makes a second node.
|
|
452
|
+
// `and[kind and]` or `ord[id=ord kind=start]` annotate the node already
|
|
453
|
+
// there, and `validate[xor …] > …` restarts from it.
|
|
454
|
+
const word = spec.split(/[\s|]/, 1)[0]?.toLowerCase().split(":")[0] ?? "";
|
|
455
|
+
const kind = KINDS[word] ?? ALIASES[word];
|
|
456
|
+
// A boundary is always new: it cannot be a revision of the task it sits on.
|
|
457
|
+
// `send[…] > send[post Slack message to #support]`: a name of several words
|
|
458
|
+
// after an arrow is a second node, whatever its kind word
|
|
459
|
+
const named = afterArrow && !spec.includes("=") && spec.trim().split(/\s+/).length >= 3;
|
|
460
|
+
if ((kind === undefined && !named) || (!afterArrow && kind !== "boundaryEvent")) {
|
|
461
|
+
this.problems.push({
|
|
462
|
+
line: n,
|
|
463
|
+
message: `"${written}" is already declared on line ${earlier.line}; ignored "${spec}"`,
|
|
464
|
+
});
|
|
465
|
+
return earlierId;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
// Read before the id is remapped: in `pay[boundary:error … | on=pay]` the
|
|
469
|
+
// host is the earlier `pay`.
|
|
470
|
+
const meant = (ref) => this.current.get(ref) ?? ref;
|
|
471
|
+
const hostOf = new Map();
|
|
472
|
+
for (const match of spec.matchAll(/\bon=([\w.-]+)/g)) {
|
|
473
|
+
if (match[1])
|
|
474
|
+
hostOf.set(match[1], meant(match[1]));
|
|
475
|
+
}
|
|
476
|
+
const id = uniqueId(written, this.taken);
|
|
477
|
+
this.current.set(written, id);
|
|
478
|
+
this.declared.set(written, [...before, id]);
|
|
479
|
+
if (earlier) {
|
|
480
|
+
this.problems.push({
|
|
481
|
+
line: n,
|
|
482
|
+
message: `"${written}" on line ${earlier.line} is a different node; this one is "${id}"`,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
const bar = spec.indexOf("|");
|
|
486
|
+
// `boundary:timer on=poll`: an attribute written before the bar is one still
|
|
487
|
+
const loose = /\s+((?:on|job|each|after)=\S+|nonint)(?=\s|$)/g;
|
|
488
|
+
const beforeBar = bar < 0 ? spec : spec.slice(0, bar);
|
|
489
|
+
const head = beforeBar.replace(loose, "").trim();
|
|
490
|
+
const attrs = [...beforeBar.matchAll(loose)].map((m) => m[1]).join(" ") +
|
|
491
|
+
(bar < 0 ? "" : ` ${spec.slice(bar + 1)}`);
|
|
492
|
+
const space = head.search(/\s/);
|
|
493
|
+
const kindWord = space < 0 ? head : head.slice(0, space);
|
|
494
|
+
const name = space < 0 ? undefined : head.slice(space + 1).trim() || undefined;
|
|
495
|
+
const [kind = "", trigger] = kindWord.toLowerCase().split(":");
|
|
496
|
+
let type = KINDS[kind] ?? ALIASES[kind];
|
|
497
|
+
let label = name;
|
|
498
|
+
if (type === undefined) {
|
|
499
|
+
// Most often the kind was left out (`start[Order placed]`): the whole
|
|
500
|
+
// head is the name, and the id is the best remaining hint at the type.
|
|
501
|
+
type = typeFromId(id);
|
|
502
|
+
label = head || undefined;
|
|
503
|
+
this.problems.push({ line: n, message: `unknown kind "${kind}" for "${id}"; used ${type}` });
|
|
504
|
+
}
|
|
505
|
+
const element = { id, type };
|
|
506
|
+
if (trigger !== undefined) {
|
|
507
|
+
if (!EVENTS.has(type)) {
|
|
508
|
+
this.problems.push({
|
|
509
|
+
line: n,
|
|
510
|
+
message: `"${kind}" takes no trigger; ignored ":${trigger}"`,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
else if (!TRIGGERS.has(trigger)) {
|
|
514
|
+
// `start:order received` — a name written where the trigger goes. When the
|
|
515
|
+
// name already says it (`start:kyc Start KYC`), the word is only noise.
|
|
516
|
+
const word = kindWord.slice(kindWord.indexOf(":") + 1);
|
|
517
|
+
const said = label?.toLowerCase().split(/\W+/).includes(trigger) ?? false;
|
|
518
|
+
if (!said)
|
|
519
|
+
label = [word, label].filter(Boolean).join(" ");
|
|
520
|
+
this.problems.push({
|
|
521
|
+
line: n,
|
|
522
|
+
message: `unknown trigger "${trigger}" for "${id}"; ${said ? "ignored, the name already says it" : "read as part of the name"}`,
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
else if (trigger === "link") {
|
|
526
|
+
// A link event needs a link name and a partner, which this format cannot
|
|
527
|
+
// write; in a path a model means a milestone by it.
|
|
528
|
+
if (type === "intermediateCatchEvent")
|
|
529
|
+
element.type = "intermediateThrowEvent";
|
|
530
|
+
this.problems.push({
|
|
531
|
+
line: n,
|
|
532
|
+
message: `"${id}" cannot be a link event here; made it a plain event`,
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
element.eventType = trigger;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (label)
|
|
540
|
+
element.name = label;
|
|
541
|
+
// A catch or boundary event waits for something, and cannot deploy without
|
|
542
|
+
// saying what. Unnamed, it is most often a message: "Payment confirmed".
|
|
543
|
+
// `queue[event catch Send to SQS]` is named for a call it makes, not for what it waits
|
|
544
|
+
// for: it is a service task, as every call to an outside system is
|
|
545
|
+
const call = element.type === "intermediateCatchEvent" && element.eventType === undefined
|
|
546
|
+
? /^(?:(?:catch|throw|event)\s+)?((?:send|post|publish|call|notify|invoke|push|upload|create|update|delete|run|trigger)\b.*)$/i.exec(label ?? "")?.[1]
|
|
547
|
+
: undefined;
|
|
548
|
+
if (call !== undefined) {
|
|
549
|
+
element.type = "serviceTask";
|
|
550
|
+
element.name = call;
|
|
551
|
+
this.problems.push({
|
|
552
|
+
line: n,
|
|
553
|
+
message: `"${id}" is named for a call it makes, "${call}"; made it a service task`,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
else if ((element.type === "intermediateCatchEvent" || element.type === "boundaryEvent") &&
|
|
557
|
+
element.eventType === undefined) {
|
|
558
|
+
element.eventType = "message";
|
|
559
|
+
this.guessedMessage.add(id);
|
|
560
|
+
this.problems.push({
|
|
561
|
+
line: n,
|
|
562
|
+
message: `"${id}" waits for nothing in particular; made it a message event`,
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
const node = { element, line: n, spec };
|
|
566
|
+
// `| on=pay | nonint`: a second bar is only another separator.
|
|
567
|
+
for (const attr of attrs.split(/[\s,|]+/).filter(Boolean)) {
|
|
568
|
+
const [key, value] = attr.split("=", 2);
|
|
569
|
+
if (key === "on" && value)
|
|
570
|
+
node.on = hostOf.get(value) ?? value;
|
|
571
|
+
else if (key === "job" && value)
|
|
572
|
+
element.jobType = value;
|
|
573
|
+
else if (key === "nonint" && value === undefined)
|
|
574
|
+
element.interrupting = false;
|
|
575
|
+
else if (key === "each" && value && GATEWAYS.has(element.type) && element.name) {
|
|
576
|
+
// `and[and Process every recipient | each=recipients]`: a step, run per item
|
|
577
|
+
this.problems.push({
|
|
578
|
+
line: n,
|
|
579
|
+
message: `"${id}" is a gateway with each=, named like a step; made it a task run per item`,
|
|
580
|
+
});
|
|
581
|
+
element.type = "task";
|
|
582
|
+
element.multiInstance = {
|
|
583
|
+
collection: value.startsWith("=") ? value : `=${value}`,
|
|
584
|
+
element: itemOf(value.replace(/^=/, "")),
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
else if (key === "each" && value && ACTIVITIES.has(element.type)) {
|
|
588
|
+
element.multiInstance = {
|
|
589
|
+
collection: value.startsWith("=") ? value : `=${value}`,
|
|
590
|
+
element: itemOf(value.replace(/^=/, "")),
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
else if (key === "after" && value && EVENTS.has(element.type) && duration(value)) {
|
|
594
|
+
// `delay[boundary:error Timeout | after=P1D]`: only a timer waits for a time
|
|
595
|
+
if (element.eventType !== "timer" && element.type !== "endEvent") {
|
|
596
|
+
this.problems.push({
|
|
597
|
+
line: n,
|
|
598
|
+
message: `"${id}" waits ${value}, which only a timer does; made it a timer event`,
|
|
599
|
+
});
|
|
600
|
+
element.eventType = "timer";
|
|
601
|
+
}
|
|
602
|
+
element.timerDuration = duration(value);
|
|
603
|
+
}
|
|
604
|
+
else
|
|
605
|
+
this.problems.push({ line: n, message: `unknown attribute "${attr}" on "${id}"; ignored` });
|
|
606
|
+
}
|
|
607
|
+
// `late[catch:timer | on=poll after=PT5M]`: an event on a task is a boundary event
|
|
608
|
+
const host = node.on === undefined ? undefined : this.nodes.get(node.on)?.element;
|
|
609
|
+
if (element.type === "intermediateCatchEvent" && host && ACTIVITIES.has(host.type)) {
|
|
610
|
+
element.type = "boundaryEvent";
|
|
611
|
+
this.problems.push({
|
|
612
|
+
line: n,
|
|
613
|
+
message: `"${id}" is a catch event on "${node.on}"; made it a boundary event`,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
// "Send email to each stakeholder": a step its name says is done per item runs once
|
|
617
|
+
// per item of the list of them, whose variable only the reader can name for sure
|
|
618
|
+
const per = /\b(?:each|every)\s+([a-z]{3,})\b/i.exec(label ?? "")?.[1]?.toLowerCase();
|
|
619
|
+
if (per &&
|
|
620
|
+
!TIME_WORDS.has(per) &&
|
|
621
|
+
ACTIVITIES.has(element.type) &&
|
|
622
|
+
element.multiInstance === undefined) {
|
|
623
|
+
const list = per.endsWith("y") && !/[aeiou]y$/.test(per) ? `${per.slice(0, -1)}ies` : `${per}s`;
|
|
624
|
+
element.multiInstance = { collection: `=${list}`, element: per };
|
|
625
|
+
this.fixes.push(`"${id}" runs once per ${per}, over the list "${list}"`);
|
|
626
|
+
}
|
|
627
|
+
// `late[boundary:timer 5 minutes | on=poll]`: a timer's name says how long it waits
|
|
628
|
+
if (element.eventType === "timer" && element.timerDuration === undefined && label) {
|
|
629
|
+
const said = duration(label);
|
|
630
|
+
if (said)
|
|
631
|
+
element.timerDuration = said;
|
|
632
|
+
}
|
|
633
|
+
this.nodes.set(id, node);
|
|
634
|
+
return id;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
/** "Every day" is a schedule, not a list. */
|
|
638
|
+
const TIME_WORDS = new Set([
|
|
639
|
+
"second",
|
|
640
|
+
"minute",
|
|
641
|
+
"hour",
|
|
642
|
+
"day",
|
|
643
|
+
"week",
|
|
644
|
+
"month",
|
|
645
|
+
"quarter",
|
|
646
|
+
"year",
|
|
647
|
+
"morning",
|
|
648
|
+
"evening",
|
|
649
|
+
"night",
|
|
650
|
+
"time",
|
|
651
|
+
"other",
|
|
652
|
+
]);
|
|
653
|
+
/** Element types `each=` makes multi-instance. */
|
|
654
|
+
const ACTIVITIES = new Set([
|
|
655
|
+
"task",
|
|
656
|
+
"serviceTask",
|
|
657
|
+
"sendTask",
|
|
658
|
+
"receiveTask",
|
|
659
|
+
"userTask",
|
|
660
|
+
"manualTask",
|
|
661
|
+
"scriptTask",
|
|
662
|
+
"businessRuleTask",
|
|
663
|
+
"callActivity",
|
|
664
|
+
"subProcess",
|
|
665
|
+
]);
|
|
666
|
+
/** The variable one item of a list is in: `recipients` → `recipient`, else `item`. */
|
|
667
|
+
function itemOf(collection) {
|
|
668
|
+
if (!/^[A-Za-z_]\w*$/.test(collection))
|
|
669
|
+
return "item";
|
|
670
|
+
if (collection.endsWith("ies") && collection.length > 4)
|
|
671
|
+
return `${collection.slice(0, -3)}y`;
|
|
672
|
+
if (collection.endsWith("s") && !collection.endsWith("ss") && collection.length > 3)
|
|
673
|
+
return collection.slice(0, -1);
|
|
674
|
+
return "item";
|
|
675
|
+
}
|
|
676
|
+
const UNITS = { s: "S", m: "M", h: "H", d: "D", w: "W" };
|
|
677
|
+
/**
|
|
678
|
+
* An ISO 8601 duration from what models write: `PT5M` as it is, `5m`, `5 min`,
|
|
679
|
+
* `5 minutes`, `2 hours` or `1 day`, also inside a name ("Wait 5 minutes").
|
|
680
|
+
*/
|
|
681
|
+
export function duration(text) {
|
|
682
|
+
// `p1d` is P1D: models write ISO durations in lower case too
|
|
683
|
+
const iso = /\bP(?:\d+[YMWD])*(?:T(?:\d+[HMS])+)?\b/i.exec(text)?.[0]?.toUpperCase();
|
|
684
|
+
if (iso && iso !== "P" && iso !== "PT")
|
|
685
|
+
return iso;
|
|
686
|
+
const m = /(\d+)\s*(s|sec|secs|seconds?|m|min|mins|minutes?|h|hrs?|hours?|d|days?|w|weeks?)\b/i.exec(text);
|
|
687
|
+
if (!m?.[1] || !m[2])
|
|
688
|
+
return undefined;
|
|
689
|
+
const unit = UNITS[m[2][0]?.toLowerCase() ?? ""];
|
|
690
|
+
if (unit === undefined)
|
|
691
|
+
return undefined;
|
|
692
|
+
return unit === "D" || unit === "W" ? `P${m[1]}${unit}` : `PT${m[1]}${unit}`;
|
|
693
|
+
}
|
|
694
|
+
export function edgeLabel(label) {
|
|
695
|
+
if (label === undefined)
|
|
696
|
+
return {};
|
|
697
|
+
const colon = label.indexOf(":");
|
|
698
|
+
const name = (colon < 0 ? label : label.slice(0, colon)).trim();
|
|
699
|
+
const rest = colon < 0 ? "" : label.slice(colon + 1).trim();
|
|
700
|
+
const out = {};
|
|
701
|
+
if (name.toLowerCase() === "default" && rest === "")
|
|
702
|
+
return { isDefault: true };
|
|
703
|
+
if (name)
|
|
704
|
+
out.name = name;
|
|
705
|
+
if (rest.toLowerCase() === "default")
|
|
706
|
+
out.isDefault = true;
|
|
707
|
+
else if (rest)
|
|
708
|
+
out.condition = rest.startsWith("=") ? rest : `= ${rest}`;
|
|
709
|
+
return out;
|
|
710
|
+
}
|
|
711
|
+
/** Branch labels that read as "otherwise" — the branch a gateway should default to. */
|
|
712
|
+
const OTHERWISE = /^(no|not|else|otherwise|default|false|reject(ed)?|fail(ed|ure)?|invalid|denied|declined)\b/i;
|
|
713
|
+
/** `"A", "B" and "C"` */
|
|
714
|
+
function listOf(items) {
|
|
715
|
+
return items.length < 2
|
|
716
|
+
? (items[0] ?? "")
|
|
717
|
+
: `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
|
|
718
|
+
}
|
|
719
|
+
/** `Status approved?` → `statusApproved`: a FEEL variable name for the question a gateway asks. */
|
|
720
|
+
function variableFrom(text) {
|
|
721
|
+
const words = text
|
|
722
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
723
|
+
.split(/[^A-Za-z0-9]+/)
|
|
724
|
+
.filter(Boolean)
|
|
725
|
+
.map((word, k) => {
|
|
726
|
+
const lower = word.toLowerCase();
|
|
727
|
+
return k === 0 ? lower : lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
728
|
+
});
|
|
729
|
+
const name = words.join("");
|
|
730
|
+
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Turns what the reader holds into a diagram `expand` accepts.
|
|
734
|
+
*
|
|
735
|
+
* `final` is false while the text is still arriving: an edge to a node not yet
|
|
736
|
+
* declared is only waiting, so it is left out without a problem, and nothing is
|
|
737
|
+
* added at the ends of paths that are still being written. Once final, the
|
|
738
|
+
* result also keeps the modelling rules `lintDiagram` checks — see
|
|
739
|
+
* {@link parseProcessText}.
|
|
740
|
+
*/
|
|
741
|
+
function assemble(reader, final) {
|
|
742
|
+
const problems = [...reader.problems];
|
|
743
|
+
const fixes = [...reader.fixes];
|
|
744
|
+
// Asked once the names are final, so a question uses the names the diagram shows.
|
|
745
|
+
const asks = [];
|
|
746
|
+
const nodes = new Map();
|
|
747
|
+
for (const [id, node] of reader.nodes) {
|
|
748
|
+
nodes.set(id, { ...node, element: { ...node.element } });
|
|
749
|
+
}
|
|
750
|
+
const taken = new Set(nodes.keys());
|
|
751
|
+
const typeOf = (id) => nodes.get(id)?.element.type;
|
|
752
|
+
const nameOf = (id) => nodes.get(id)?.element.name ?? nameFromId(id);
|
|
753
|
+
const add = (id, type, line, name) => {
|
|
754
|
+
const element = name === undefined ? { id, type } : { id, type, name };
|
|
755
|
+
nodes.set(id, { element, line, spec: "" });
|
|
756
|
+
};
|
|
757
|
+
// An id used in a flow but never declared (`review > pay`) would take every
|
|
758
|
+
// flow on it down with it. Once the text is complete, it becomes a task named
|
|
759
|
+
// from its id instead — while streaming it may yet be declared. An id only an
|
|
760
|
+
// `on=` names is not added: nothing would lead to it.
|
|
761
|
+
if (final) {
|
|
762
|
+
for (const edge of reader.edges) {
|
|
763
|
+
for (const id of [edge.from, edge.to]) {
|
|
764
|
+
if (nodes.has(id))
|
|
765
|
+
continue;
|
|
766
|
+
const type = typeFromId(id);
|
|
767
|
+
add(id, type, edge.line, GATEWAYS.has(type) ? undefined : nameFromId(id));
|
|
768
|
+
taken.add(id);
|
|
769
|
+
problems.push({ line: edge.line, message: `"${id}" is never declared; added as a ${type}` });
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
// Boundary events: a host that is an activity, in this process.
|
|
774
|
+
for (const [id, node] of nodes) {
|
|
775
|
+
if (node.element.type !== "boundaryEvent")
|
|
776
|
+
continue;
|
|
777
|
+
// `send > wait[boundary:timer …]` without on=: the one task drawn into it is its host
|
|
778
|
+
const drawn = node.on === undefined ? reader.edges.filter((edge) => edge.to === id) : [];
|
|
779
|
+
const from = drawn.length === 1 ? nodes.get(drawn[0]?.from ?? "") : undefined;
|
|
780
|
+
if (from && drawn[0] && !EVENTS.has(from.element.type) && !GATEWAYS.has(from.element.type)) {
|
|
781
|
+
node.on = from.element.id;
|
|
782
|
+
reader.edges.splice(reader.edges.indexOf(drawn[0]), 1);
|
|
783
|
+
fixes.push(`put boundary "${id}" on "${from.element.id}", the task drawn into it`);
|
|
784
|
+
}
|
|
785
|
+
const host = node.on === undefined ? undefined : nodes.get(node.on);
|
|
786
|
+
const hostType = host?.element.type;
|
|
787
|
+
if (hostType !== undefined && !EVENTS.has(hostType) && !GATEWAYS.has(hostType)) {
|
|
788
|
+
node.element.attachedTo = node.on;
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
if (!final && node.on !== undefined && host === undefined) {
|
|
792
|
+
nodes.delete(id); // host still to come
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
problems.push({
|
|
796
|
+
line: node.line,
|
|
797
|
+
message: node.on === undefined
|
|
798
|
+
? `boundary "${id}" has no on=<task id>; left out`
|
|
799
|
+
: host === undefined
|
|
800
|
+
? `boundary "${id}" is on "${node.on}", which is never declared; left out`
|
|
801
|
+
: `boundary "${id}" is on "${node.on}", which is not a task; left out`,
|
|
802
|
+
});
|
|
803
|
+
nodes.delete(id);
|
|
804
|
+
}
|
|
805
|
+
// A branch drawn into a boundary event (`check >(No) failed[boundary:error … |
|
|
806
|
+
// on=pay] > notify`) means the path that boundary leads to. Nothing can flow
|
|
807
|
+
// into a boundary, so the branch goes to its handler instead of being lost.
|
|
808
|
+
const written = reader.edges.flatMap((edge) => {
|
|
809
|
+
const target = reader.nodes.get(edge.to);
|
|
810
|
+
if (target?.element.type !== "boundaryEvent")
|
|
811
|
+
return [edge];
|
|
812
|
+
// `poll > late[… | on=poll]`: the arrow from its own task only says where it sits
|
|
813
|
+
if (target.on === edge.from)
|
|
814
|
+
return [];
|
|
815
|
+
const next = reader.edges.filter((out) => out.from === edge.to);
|
|
816
|
+
if (next.length === 0)
|
|
817
|
+
return [edge];
|
|
818
|
+
if (final) {
|
|
819
|
+
fixes.push(`led ${edge.from} > ${edge.to} to what the boundary leads to, since nothing flows into a boundary event`);
|
|
820
|
+
}
|
|
821
|
+
return next.map((out) => ({ ...edge, to: out.to }));
|
|
822
|
+
});
|
|
823
|
+
// Edges: both ends declared, pointing a way BPMN allows, once.
|
|
824
|
+
const seen = new Set();
|
|
825
|
+
let edges = [];
|
|
826
|
+
for (const edge of written) {
|
|
827
|
+
const from = typeOf(edge.from);
|
|
828
|
+
const to = typeOf(edge.to);
|
|
829
|
+
if (from === undefined || to === undefined) {
|
|
830
|
+
if (final) {
|
|
831
|
+
const missing = from === undefined ? edge.from : edge.to;
|
|
832
|
+
const why = reader.nodes.has(missing) ? "was left out" : "is never declared";
|
|
833
|
+
problems.push({
|
|
834
|
+
line: edge.line,
|
|
835
|
+
message: `"${missing}" ${why}; flow ${edge.from} > ${edge.to} left out`,
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
const wrong = from === "endEvent"
|
|
841
|
+
? "an end event has no outgoing flow"
|
|
842
|
+
: to === "startEvent"
|
|
843
|
+
? "a start event has no incoming flow"
|
|
844
|
+
: to === "boundaryEvent"
|
|
845
|
+
? "a boundary event has no incoming flow"
|
|
846
|
+
: edge.from === edge.to
|
|
847
|
+
? "a flow cannot lead back to where it starts"
|
|
848
|
+
: undefined;
|
|
849
|
+
if (wrong) {
|
|
850
|
+
problems.push({
|
|
851
|
+
line: edge.line,
|
|
852
|
+
message: `${wrong}; flow ${edge.from} > ${edge.to} left out`,
|
|
853
|
+
});
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
const key = `${edge.from}>${edge.to}`;
|
|
857
|
+
if (seen.has(key))
|
|
858
|
+
continue;
|
|
859
|
+
seen.add(key);
|
|
860
|
+
edges.push({ ...edge });
|
|
861
|
+
}
|
|
862
|
+
// Defaults: only on a conditional gateway, at most one each.
|
|
863
|
+
const defaulted = new Set();
|
|
864
|
+
for (const edge of edges) {
|
|
865
|
+
if (!edge.isDefault)
|
|
866
|
+
continue;
|
|
867
|
+
const from = typeOf(edge.from);
|
|
868
|
+
if (from === undefined || !CONDITIONAL.has(from) || defaulted.has(edge.from)) {
|
|
869
|
+
problems.push({
|
|
870
|
+
line: edge.line,
|
|
871
|
+
message: defaulted.has(edge.from)
|
|
872
|
+
? `"${edge.from}" already has a default branch; this one is not the default`
|
|
873
|
+
: `only an xor or or gateway has a default branch; ${edge.from} > ${edge.to} is not`,
|
|
874
|
+
});
|
|
875
|
+
edge.isDefault = undefined;
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
defaulted.add(edge.from);
|
|
879
|
+
}
|
|
880
|
+
const outOf = (id) => edges.filter((edge) => edge.from === id);
|
|
881
|
+
const into = (id) => edges.filter((edge) => edge.to === id);
|
|
882
|
+
if (final) {
|
|
883
|
+
// One blank start: a process that starts twice with no trigger to tell the
|
|
884
|
+
// two apart is two processes. The later one goes, and what it led to is
|
|
885
|
+
// placed as any path nothing leads to is, below.
|
|
886
|
+
const blank = [...nodes.values()]
|
|
887
|
+
.filter((node) => node.element.type === "startEvent" && node.element.eventType === undefined)
|
|
888
|
+
.sort((a, b) => a.line - b.line);
|
|
889
|
+
for (const extra of blank.slice(1)) {
|
|
890
|
+
const id = extra.element.id;
|
|
891
|
+
nodes.delete(id);
|
|
892
|
+
edges = edges.filter((edge) => edge.from !== id);
|
|
893
|
+
problems.push({
|
|
894
|
+
line: extra.line,
|
|
895
|
+
message: `"${id}" is a second blank start event; left out`,
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
// Start: one where none was written, and a start the model left unconnected
|
|
899
|
+
// leads to the first path that has no way in.
|
|
900
|
+
const roots = [...nodes.values()].filter((node) => node.element.type !== "startEvent" &&
|
|
901
|
+
node.element.type !== "boundaryEvent" &&
|
|
902
|
+
node.element.type !== "endEvent" &&
|
|
903
|
+
into(node.element.id).length === 0);
|
|
904
|
+
const first = roots[0]?.element.id;
|
|
905
|
+
const starts = [...nodes.values()].filter((node) => node.element.type === "startEvent");
|
|
906
|
+
if (starts.length === 0) {
|
|
907
|
+
const start = uniqueId("start", taken);
|
|
908
|
+
add(start, "startEvent", 0, "Process started");
|
|
909
|
+
if (first)
|
|
910
|
+
edges.unshift({ from: start, to: first, line: 0 });
|
|
911
|
+
fixes.push(`added start event "${start}"${first ? ` before "${first}"` : ""}`);
|
|
912
|
+
}
|
|
913
|
+
else {
|
|
914
|
+
const idle = starts.find((node) => outOf(node.element.id).length === 0);
|
|
915
|
+
if (idle && first) {
|
|
916
|
+
edges.push({ from: idle.element.id, to: first, line: idle.line });
|
|
917
|
+
fixes.push(`connected start event "${idle.element.id}" to "${first}"`);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
// Reachability: every node lies on a path from a start event.
|
|
921
|
+
const reach = () => {
|
|
922
|
+
const reached = new Set();
|
|
923
|
+
const queue = [...nodes.values()]
|
|
924
|
+
.filter((node) => node.element.type === "startEvent")
|
|
925
|
+
.map((node) => node.element.id);
|
|
926
|
+
for (let id = queue.shift(); id !== undefined; id = queue.shift()) {
|
|
927
|
+
if (reached.has(id))
|
|
928
|
+
continue;
|
|
929
|
+
reached.add(id);
|
|
930
|
+
for (const edge of outOf(id))
|
|
931
|
+
queue.push(edge.to);
|
|
932
|
+
for (const node of nodes.values()) {
|
|
933
|
+
if (node.element.attachedTo === id)
|
|
934
|
+
queue.push(node.element.id);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
return reached;
|
|
938
|
+
};
|
|
939
|
+
let reached = reach();
|
|
940
|
+
// A task or gateway nothing leads to, written after a path that stops short,
|
|
941
|
+
// is most often that path's next step with the arrow left out
|
|
942
|
+
// (`… > label` then `gw[xor Done?] > dispatch`): it continues the latest
|
|
943
|
+
// such path written before it. Events are not guessed at — a loose timer
|
|
944
|
+
// or message could belong anywhere.
|
|
945
|
+
for (const node of [...nodes.values()].sort((a, b) => a.line - b.line)) {
|
|
946
|
+
const { id, type } = node.element;
|
|
947
|
+
if (reached.has(id) || EVENTS.has(type) || into(id).length > 0)
|
|
948
|
+
continue;
|
|
949
|
+
const open = [...nodes.values()].filter((other) => reached.has(other.element.id) &&
|
|
950
|
+
other.element.type !== "endEvent" &&
|
|
951
|
+
other.line <= node.line &&
|
|
952
|
+
outOf(other.element.id).length === 0);
|
|
953
|
+
const from = open.reduce((latest, other) => (latest === undefined || other.line >= latest.line ? other : latest), undefined);
|
|
954
|
+
if (from === undefined)
|
|
955
|
+
continue;
|
|
956
|
+
edges.push({ from: from.element.id, to: id, line: node.line });
|
|
957
|
+
fixes.push(`connected "${from.element.id}" to "${id}", which nothing led to`);
|
|
958
|
+
reached = reach();
|
|
959
|
+
}
|
|
960
|
+
// What is still unreached is a fragment the model never connected, and
|
|
961
|
+
// drawing it loose is worse than leaving it out.
|
|
962
|
+
for (const [id, node] of nodes) {
|
|
963
|
+
if (reached.has(id))
|
|
964
|
+
continue;
|
|
965
|
+
problems.push({
|
|
966
|
+
line: node.line,
|
|
967
|
+
message: `"${id}" is not connected to a start event; left out`,
|
|
968
|
+
});
|
|
969
|
+
nodes.delete(id);
|
|
970
|
+
const { type, name = nameFromId(id) } = node.element;
|
|
971
|
+
if (EVENTS.has(type) || GATEWAYS.has(type))
|
|
972
|
+
continue;
|
|
973
|
+
asks.push({
|
|
974
|
+
line: node.line,
|
|
975
|
+
ask: () => ({
|
|
976
|
+
elementId: id,
|
|
977
|
+
text: `"${name}" was left out: nothing leads to it. Where does it belong?`,
|
|
978
|
+
options: [],
|
|
979
|
+
draft: `Put "${name}" after `,
|
|
980
|
+
}),
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
edges = edges.filter((edge) => reached.has(edge.from) && reached.has(edge.to));
|
|
984
|
+
// Ends: every path that stops elsewhere, including a boundary with nowhere to go.
|
|
985
|
+
const addEnds = () => {
|
|
986
|
+
for (const node of [...nodes.values()]) {
|
|
987
|
+
const { id, type } = node.element;
|
|
988
|
+
if (type === "endEvent" || outOf(id).length > 0)
|
|
989
|
+
continue;
|
|
990
|
+
const end = uniqueId(`${id}_end`, taken);
|
|
991
|
+
const name = EVENTS.has(type) ? node.element.name : undefined;
|
|
992
|
+
add(end, "endEvent", node.line, name ?? "Process completed");
|
|
993
|
+
edges.push({ from: id, to: end, line: node.line });
|
|
994
|
+
fixes.push(`added end event "${end}" after "${id}"`);
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
addEnds();
|
|
998
|
+
// Exits: a loop with no way out never ends. Its last decision gets a branch
|
|
999
|
+
// to an end event; the condition rules below give it a condition. A loop
|
|
1000
|
+
// with no decision in it (`and > it[task IT] > and`, parallel branches
|
|
1001
|
+
// drawn back into their own split) is not a loop anyone meant: its flows
|
|
1002
|
+
// back go, and each path then ends.
|
|
1003
|
+
for (;;) {
|
|
1004
|
+
const ends = new Set();
|
|
1005
|
+
const queue = [...nodes.values()]
|
|
1006
|
+
.filter((node) => node.element.type === "endEvent")
|
|
1007
|
+
.map((node) => node.element.id);
|
|
1008
|
+
for (let id = queue.shift(); id !== undefined; id = queue.shift()) {
|
|
1009
|
+
if (ends.has(id))
|
|
1010
|
+
continue;
|
|
1011
|
+
ends.add(id);
|
|
1012
|
+
for (const edge of into(id))
|
|
1013
|
+
queue.push(edge.from);
|
|
1014
|
+
}
|
|
1015
|
+
const stuck = [...nodes.values()].filter((node) => !ends.has(node.element.id) && node.element.type !== "boundaryEvent");
|
|
1016
|
+
const decision = stuck
|
|
1017
|
+
.filter((node) => CONDITIONAL.has(node.element.type))
|
|
1018
|
+
.sort((a, b) => b.line - a.line)[0];
|
|
1019
|
+
if (decision === undefined) {
|
|
1020
|
+
if (stuck.length === 0)
|
|
1021
|
+
break;
|
|
1022
|
+
const inStuck = new Set(stuck.map((node) => node.element.id));
|
|
1023
|
+
const backs = new Set();
|
|
1024
|
+
const done = new Set();
|
|
1025
|
+
const onPath = new Set();
|
|
1026
|
+
const visit = (id) => {
|
|
1027
|
+
onPath.add(id);
|
|
1028
|
+
for (const edge of outOf(id)) {
|
|
1029
|
+
if (onPath.has(edge.to)) {
|
|
1030
|
+
if (inStuck.has(edge.to))
|
|
1031
|
+
backs.add(edge);
|
|
1032
|
+
}
|
|
1033
|
+
else if (!done.has(edge.to))
|
|
1034
|
+
visit(edge.to);
|
|
1035
|
+
}
|
|
1036
|
+
onPath.delete(id);
|
|
1037
|
+
done.add(id);
|
|
1038
|
+
};
|
|
1039
|
+
for (const node of nodes.values()) {
|
|
1040
|
+
if (node.element.type === "startEvent")
|
|
1041
|
+
visit(node.element.id);
|
|
1042
|
+
}
|
|
1043
|
+
for (const edge of backs) {
|
|
1044
|
+
problems.push({
|
|
1045
|
+
line: edge.line,
|
|
1046
|
+
message: `flow ${edge.from} > ${edge.to} loops with no way out; left out`,
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
edges = edges.filter((edge) => !backs.has(edge));
|
|
1050
|
+
addEnds();
|
|
1051
|
+
if (backs.size > 0)
|
|
1052
|
+
continue;
|
|
1053
|
+
for (const node of stuck) {
|
|
1054
|
+
problems.push({
|
|
1055
|
+
line: node.line,
|
|
1056
|
+
message: `"${node.element.id}" is in a loop with no way out`,
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
break;
|
|
1060
|
+
}
|
|
1061
|
+
const id = decision.element.id;
|
|
1062
|
+
const end = uniqueId(`${id}_end`, taken);
|
|
1063
|
+
add(end, "endEvent", decision.line, "Process completed");
|
|
1064
|
+
edges.push({ from: id, to: end, line: decision.line, name: "Otherwise" });
|
|
1065
|
+
fixes.push(`added an exit from the loop at "${id}" to end event "${end}"`);
|
|
1066
|
+
asks.push({
|
|
1067
|
+
line: decision.line,
|
|
1068
|
+
ask: () => {
|
|
1069
|
+
const name = nameOf(id);
|
|
1070
|
+
return {
|
|
1071
|
+
elementId: id,
|
|
1072
|
+
text: `The loop at "${name}" had no way out, so one was added. When does it end?`,
|
|
1073
|
+
options: [],
|
|
1074
|
+
draft: `Leave the loop at "${name}" when `,
|
|
1075
|
+
};
|
|
1076
|
+
},
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
// `fork[and] > pick > pack > label > joined[and]`: steps between an and split and an
|
|
1080
|
+
// and join with nothing beside them are the parallel branches, drawn in a row
|
|
1081
|
+
for (const [id, node] of nodes) {
|
|
1082
|
+
if (node.element.type !== "parallelGateway")
|
|
1083
|
+
continue;
|
|
1084
|
+
if (into(id).length !== 1 || outOf(id).length !== 1)
|
|
1085
|
+
continue;
|
|
1086
|
+
const chain = [];
|
|
1087
|
+
let at = outOf(id)[0]?.to;
|
|
1088
|
+
while (at !== undefined) {
|
|
1089
|
+
const next = nodes.get(at)?.element;
|
|
1090
|
+
if (!next || GATEWAYS.has(next.type) || EVENTS.has(next.type))
|
|
1091
|
+
break;
|
|
1092
|
+
if (into(at).length !== 1 || outOf(at).length !== 1)
|
|
1093
|
+
break;
|
|
1094
|
+
chain.push(at);
|
|
1095
|
+
at = outOf(at)[0]?.to;
|
|
1096
|
+
}
|
|
1097
|
+
const join = at === undefined ? undefined : nodes.get(at);
|
|
1098
|
+
if (chain.length < 2 ||
|
|
1099
|
+
!join ||
|
|
1100
|
+
at === undefined ||
|
|
1101
|
+
join.element.type !== "parallelGateway" ||
|
|
1102
|
+
into(at).length !== 1) {
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
const end = at;
|
|
1106
|
+
edges = edges.filter((edge) => edge.from !== id && !chain.includes(edge.from));
|
|
1107
|
+
for (const step of chain) {
|
|
1108
|
+
edges.push({ from: id, to: step, line: node.line });
|
|
1109
|
+
edges.push({ from: step, to: end, line: node.line });
|
|
1110
|
+
}
|
|
1111
|
+
fixes.push(`ran ${chain.map((c) => `"${c}"`).join(", ")} in parallel between "${id}" and "${end}", as their and nodes say`);
|
|
1112
|
+
}
|
|
1113
|
+
// Pass-through gateways: one way in and one way out decides nothing. Most
|
|
1114
|
+
// often a question the model asked and then answered only one way.
|
|
1115
|
+
const dedupe = () => {
|
|
1116
|
+
const once = new Set();
|
|
1117
|
+
edges = edges.filter((edge) => {
|
|
1118
|
+
const key = `${edge.from}>${edge.to}`;
|
|
1119
|
+
if (once.has(key))
|
|
1120
|
+
return false;
|
|
1121
|
+
once.add(key);
|
|
1122
|
+
return true;
|
|
1123
|
+
});
|
|
1124
|
+
};
|
|
1125
|
+
for (let removed = true; removed;) {
|
|
1126
|
+
removed = false;
|
|
1127
|
+
// Removing one gateway can leave two flows between the same nodes: one is enough
|
|
1128
|
+
dedupe();
|
|
1129
|
+
for (const [id, node] of nodes) {
|
|
1130
|
+
if (!GATEWAYS.has(node.element.type))
|
|
1131
|
+
continue;
|
|
1132
|
+
const [inEdge, ...moreIn] = into(id);
|
|
1133
|
+
const [outEdge, ...moreOut] = outOf(id);
|
|
1134
|
+
if (!inEdge || !outEdge || moreIn.length > 0 || moreOut.length > 0)
|
|
1135
|
+
continue;
|
|
1136
|
+
if (inEdge.from === outEdge.to)
|
|
1137
|
+
continue;
|
|
1138
|
+
// `pack[and Pack into box]`: a step written with a gateway's kind is that step
|
|
1139
|
+
if (node.element.name &&
|
|
1140
|
+
(node.element.type === "parallelGateway" || node.element.type === "inclusiveGateway")) {
|
|
1141
|
+
node.element.type = "task";
|
|
1142
|
+
problems.push({
|
|
1143
|
+
line: node.line,
|
|
1144
|
+
message: `"${id}" is a gateway with one way in and out, named like a step; made it a task`,
|
|
1145
|
+
});
|
|
1146
|
+
continue;
|
|
1147
|
+
}
|
|
1148
|
+
const target = typeOf(outEdge.to);
|
|
1149
|
+
if (node.element.type === "eventBasedGateway" &&
|
|
1150
|
+
target !== "intermediateCatchEvent" &&
|
|
1151
|
+
target !== "receiveTask") {
|
|
1152
|
+
// Waiting for one event is a catch event, not a race between events.
|
|
1153
|
+
node.element.type = "intermediateCatchEvent";
|
|
1154
|
+
node.element.eventType = "message";
|
|
1155
|
+
problems.push({
|
|
1156
|
+
line: node.line,
|
|
1157
|
+
message: `"${id}" waits for only one event; made it a message catch event`,
|
|
1158
|
+
});
|
|
1159
|
+
continue;
|
|
1160
|
+
}
|
|
1161
|
+
inEdge.to = outEdge.to;
|
|
1162
|
+
edges = edges.filter((edge) => edge !== outEdge);
|
|
1163
|
+
nodes.delete(id);
|
|
1164
|
+
problems.push({ line: node.line, message: `"${id}" has only one branch; gateway removed` });
|
|
1165
|
+
if (CONDITIONAL.has(node.element.type)) {
|
|
1166
|
+
// Most often a question the model asked and answered only one way.
|
|
1167
|
+
const name = node.element.name ?? `${nameFromId(id)}?`;
|
|
1168
|
+
asks.push({
|
|
1169
|
+
line: node.line,
|
|
1170
|
+
ask: () => ({
|
|
1171
|
+
elementId: id,
|
|
1172
|
+
text: `"${name}" had only one way to go, so it was removed. What happens otherwise?`,
|
|
1173
|
+
options: [],
|
|
1174
|
+
draft: `At "${name}", otherwise `,
|
|
1175
|
+
}),
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
removed = true;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
const once = new Set();
|
|
1182
|
+
edges = edges.filter((edge) => {
|
|
1183
|
+
const key = `${edge.from}>${edge.to}`;
|
|
1184
|
+
if (once.has(key))
|
|
1185
|
+
return false;
|
|
1186
|
+
once.add(key);
|
|
1187
|
+
return true;
|
|
1188
|
+
});
|
|
1189
|
+
// Implicit splits: a task or event with several ways out forks the token
|
|
1190
|
+
// without saying so. Labelled branches are a decision. Unlabelled ones that
|
|
1191
|
+
// all wait, at least one on a catch event (`poll > timeout[catch:timer 5m]`,
|
|
1192
|
+
// `poll > get[receive Report]`), are a race: whichever happens first. Other
|
|
1193
|
+
// unlabelled ones run in parallel, which is what the flows mean in BPMN.
|
|
1194
|
+
for (const node of [...nodes.values()]) {
|
|
1195
|
+
const { id, type } = node.element;
|
|
1196
|
+
const out = outOf(id);
|
|
1197
|
+
if (GATEWAYS.has(type) || out.length < 2)
|
|
1198
|
+
continue;
|
|
1199
|
+
// A DMN decision's outcomes are alternatives, labelled or not
|
|
1200
|
+
const decides = type === "businessRuleTask" ||
|
|
1201
|
+
out.some((edge) => edge.condition !== undefined || edge.name !== undefined);
|
|
1202
|
+
const targets = out.map((edge) => nodes.get(edge.to)?.element);
|
|
1203
|
+
const races = !decides &&
|
|
1204
|
+
targets.every((t) => t?.type === "intermediateCatchEvent" || t?.type === "receiveTask") &&
|
|
1205
|
+
targets.some((t) => t?.type === "intermediateCatchEvent");
|
|
1206
|
+
const split = uniqueId(`${id}_split`, taken);
|
|
1207
|
+
const gatewayType = decides
|
|
1208
|
+
? "exclusiveGateway"
|
|
1209
|
+
: races
|
|
1210
|
+
? "eventBasedGateway"
|
|
1211
|
+
: "parallelGateway";
|
|
1212
|
+
const name = decides ? `${node.element.name ?? nameFromId(id)} outcome?` : undefined;
|
|
1213
|
+
add(split, gatewayType, node.line, name);
|
|
1214
|
+
for (const edge of out)
|
|
1215
|
+
edge.from = split;
|
|
1216
|
+
edges.push({ from: id, to: split, line: node.line });
|
|
1217
|
+
if (races) {
|
|
1218
|
+
// Camunda 8 waits on catch events only after an event-based gateway.
|
|
1219
|
+
for (const target of targets) {
|
|
1220
|
+
if (!target)
|
|
1221
|
+
continue;
|
|
1222
|
+
if (target.type === "receiveTask")
|
|
1223
|
+
target.type = "intermediateCatchEvent";
|
|
1224
|
+
target.eventType ??= "message";
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
const kind = decides ? "xor" : races ? "event" : "and";
|
|
1228
|
+
fixes.push(`split the flows out of "${id}" with ${kind} gateway "${split}"`);
|
|
1229
|
+
if (kind === "and") {
|
|
1230
|
+
// Read now: a join below may take a branch's place as its target.
|
|
1231
|
+
const targetIds = out.map((edge) => edge.to);
|
|
1232
|
+
asks.push({
|
|
1233
|
+
line: node.line,
|
|
1234
|
+
ask: () => {
|
|
1235
|
+
const name = nameOf(id);
|
|
1236
|
+
const branches = listOf(targetIds.map((to) => `"${nameOf(to)}"`));
|
|
1237
|
+
return {
|
|
1238
|
+
elementId: split,
|
|
1239
|
+
text: `After "${name}", ${branches} run at the same time. Is that right?`,
|
|
1240
|
+
options: [
|
|
1241
|
+
{
|
|
1242
|
+
label: "Only one of them",
|
|
1243
|
+
change: `After "${name}", only one of ${branches} happens: decide which with an xor gateway.`,
|
|
1244
|
+
},
|
|
1245
|
+
{
|
|
1246
|
+
label: "One after the other",
|
|
1247
|
+
change: `After "${name}", do ${branches} one after the other, not at the same time.`,
|
|
1248
|
+
},
|
|
1249
|
+
],
|
|
1250
|
+
draft: `After "${name}", `,
|
|
1251
|
+
};
|
|
1252
|
+
},
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
// Joins: branches that meet at anything but a join gateway meet at a join
|
|
1258
|
+
// gateway first. Camunda style keeps one incoming flow per task and one role
|
|
1259
|
+
// per gateway, and it is the piece of structure a model most often leaves out.
|
|
1260
|
+
// The join matches the split the branches came from: an xor join after a
|
|
1261
|
+
// parallel split would run what follows once per branch.
|
|
1262
|
+
const splitOf = (edge) => {
|
|
1263
|
+
const visited = new Set();
|
|
1264
|
+
for (let id = edge.from; !visited.has(id);) {
|
|
1265
|
+
visited.add(id);
|
|
1266
|
+
if (GATEWAYS.has(typeOf(id) ?? "task") && outOf(id).length > 1)
|
|
1267
|
+
return id;
|
|
1268
|
+
const [only, ...more] = into(id);
|
|
1269
|
+
if (!only || more.length > 0)
|
|
1270
|
+
return undefined;
|
|
1271
|
+
id = only.from;
|
|
1272
|
+
}
|
|
1273
|
+
return undefined;
|
|
1274
|
+
};
|
|
1275
|
+
for (const [target, node] of [...nodes]) {
|
|
1276
|
+
const incoming = into(target);
|
|
1277
|
+
const type = node.element.type;
|
|
1278
|
+
if (incoming.length < 2 || (GATEWAYS.has(type) && outOf(target).length < 2))
|
|
1279
|
+
continue;
|
|
1280
|
+
const splits = new Set(incoming.map(splitOf));
|
|
1281
|
+
const [split] = splits;
|
|
1282
|
+
const splitType = splits.size === 1 && split !== undefined ? typeOf(split) : undefined;
|
|
1283
|
+
const joinType = splitType === "parallelGateway" || splitType === "inclusiveGateway"
|
|
1284
|
+
? splitType
|
|
1285
|
+
: "exclusiveGateway";
|
|
1286
|
+
const join = uniqueId(`${target}_join`, taken);
|
|
1287
|
+
add(join, joinType, node.line);
|
|
1288
|
+
for (const edge of incoming)
|
|
1289
|
+
edge.to = join;
|
|
1290
|
+
edges.push({ from: join, to: target, line: node.line });
|
|
1291
|
+
const kind = joinType === "parallelGateway" ? "and" : joinType === "inclusiveGateway" ? "or" : "xor";
|
|
1292
|
+
fixes.push(`joined ${incoming.length} flows into "${target}" with ${kind} gateway "${join}"`);
|
|
1293
|
+
}
|
|
1294
|
+
if (final) {
|
|
1295
|
+
// Conditions: every branch of a decision has a FEEL condition, or is its one
|
|
1296
|
+
// default; every other flow carries neither, nor a label that implies one.
|
|
1297
|
+
for (const [id, node] of nodes) {
|
|
1298
|
+
const out = outOf(id);
|
|
1299
|
+
if (!CONDITIONAL.has(node.element.type) || out.length < 2) {
|
|
1300
|
+
for (const edge of out) {
|
|
1301
|
+
if (edge.condition !== undefined) {
|
|
1302
|
+
fixes.push(`dropped the condition on ${id} > ${edge.to}, which is not a decision`);
|
|
1303
|
+
}
|
|
1304
|
+
edge.condition = undefined;
|
|
1305
|
+
edge.isDefault = undefined;
|
|
1306
|
+
edge.name = undefined;
|
|
1307
|
+
}
|
|
1308
|
+
continue;
|
|
1309
|
+
}
|
|
1310
|
+
if (!out.some((edge) => edge.isDefault)) {
|
|
1311
|
+
const plain = out.filter((edge) => edge.condition === undefined);
|
|
1312
|
+
const pool = plain.length > 0 ? plain : out;
|
|
1313
|
+
const otherwise = pool.find((edge) => OTHERWISE.test(edge.name ?? ""));
|
|
1314
|
+
const chosen = otherwise ?? pool[pool.length - 1];
|
|
1315
|
+
if (chosen && otherwise === undefined) {
|
|
1316
|
+
// Read now: the loop below gives every other branch a label.
|
|
1317
|
+
const labels = new Map(out.map((edge) => [edge, edge.name?.split(":")[0]?.trim()]));
|
|
1318
|
+
asks.push({
|
|
1319
|
+
line: node.line,
|
|
1320
|
+
ask: () => {
|
|
1321
|
+
const name = nameOf(id);
|
|
1322
|
+
const label = (edge) => labels.get(edge) || nameOf(edge.to);
|
|
1323
|
+
return {
|
|
1324
|
+
elementId: id,
|
|
1325
|
+
text: `When no condition at "${name}" holds, it takes "${label(chosen)}". Is that the right fallback?`,
|
|
1326
|
+
options: out
|
|
1327
|
+
.filter((edge) => edge !== chosen)
|
|
1328
|
+
.map((edge) => ({
|
|
1329
|
+
label: label(edge),
|
|
1330
|
+
change: `At "${name}", make "${label(edge)}" the default branch.`,
|
|
1331
|
+
})),
|
|
1332
|
+
draft: `At "${name}", when nothing else holds, `,
|
|
1333
|
+
};
|
|
1334
|
+
},
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
if (chosen) {
|
|
1338
|
+
chosen.isDefault = true;
|
|
1339
|
+
if (chosen.condition !== undefined) {
|
|
1340
|
+
chosen.condition = undefined;
|
|
1341
|
+
fixes.push(`made ${id} > ${chosen.to} the default branch of "${id}" instead of its condition`);
|
|
1342
|
+
}
|
|
1343
|
+
else {
|
|
1344
|
+
fixes.push(`made ${id} > ${chosen.to} the default branch of "${id}"`);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
const variable = variableFrom(node.element.name ?? id);
|
|
1349
|
+
let madeUp = false;
|
|
1350
|
+
for (const edge of out) {
|
|
1351
|
+
if (edge.isDefault) {
|
|
1352
|
+
edge.condition = undefined;
|
|
1353
|
+
continue;
|
|
1354
|
+
}
|
|
1355
|
+
if (edge.condition === undefined) {
|
|
1356
|
+
// A branch the model described in prose, or not at all: a condition on
|
|
1357
|
+
// a variable named for the question keeps the gateway deployable and
|
|
1358
|
+
// says plainly which variable decides it.
|
|
1359
|
+
const label = (edge.name ?? nodes.get(edge.to)?.element.name ?? edge.to)
|
|
1360
|
+
.split(":")[0]
|
|
1361
|
+
?.trim()
|
|
1362
|
+
.replace(/"/g, "");
|
|
1363
|
+
edge.condition = /^(yes|true)\b/i.test(label ?? "")
|
|
1364
|
+
? `= ${variable} = true`
|
|
1365
|
+
: /^(no|false)\b/i.test(label ?? "")
|
|
1366
|
+
? `= ${variable} = false`
|
|
1367
|
+
: `= ${variable} = "${label}"`;
|
|
1368
|
+
fixes.push(`gave ${id} > ${edge.to} the condition "${edge.condition}"`);
|
|
1369
|
+
madeUp = true;
|
|
1370
|
+
}
|
|
1371
|
+
edge.name ??= edge.condition.replace(/^=\s*/, "");
|
|
1372
|
+
}
|
|
1373
|
+
if (madeUp) {
|
|
1374
|
+
asks.push({
|
|
1375
|
+
line: node.line,
|
|
1376
|
+
ask: () => {
|
|
1377
|
+
const name = nameOf(id);
|
|
1378
|
+
return {
|
|
1379
|
+
elementId: id,
|
|
1380
|
+
text: `"${name}" decides on "${variable}", a variable the diagram made up. What data decides it?`,
|
|
1381
|
+
options: [],
|
|
1382
|
+
draft: `At "${name}", decide on `,
|
|
1383
|
+
};
|
|
1384
|
+
},
|
|
1385
|
+
});
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
// Names: every event, task and decision says what it is.
|
|
1389
|
+
let named = 0;
|
|
1390
|
+
for (const [id, node] of nodes) {
|
|
1391
|
+
const { element } = node;
|
|
1392
|
+
if (element.name?.trim())
|
|
1393
|
+
continue;
|
|
1394
|
+
if (element.type === "parallelGateway" || element.type === "eventBasedGateway")
|
|
1395
|
+
continue;
|
|
1396
|
+
if (GATEWAYS.has(element.type) && outOf(id).length < 2)
|
|
1397
|
+
continue;
|
|
1398
|
+
element.name = GATEWAYS.has(element.type) ? `${nameFromId(id)}?` : nameFromId(id);
|
|
1399
|
+
named++;
|
|
1400
|
+
}
|
|
1401
|
+
if (named > 0)
|
|
1402
|
+
fixes.push(`named ${named} unnamed element(s) from their ids`);
|
|
1403
|
+
for (const id of reader.guessedMessage) {
|
|
1404
|
+
const node = nodes.get(id);
|
|
1405
|
+
if (node?.element.eventType !== "message")
|
|
1406
|
+
continue;
|
|
1407
|
+
asks.push({
|
|
1408
|
+
line: node.line,
|
|
1409
|
+
ask: () => {
|
|
1410
|
+
const name = nameOf(id);
|
|
1411
|
+
return {
|
|
1412
|
+
elementId: id,
|
|
1413
|
+
text: `"${name}" was written without a trigger, so it waits for a message. Is it something else?`,
|
|
1414
|
+
options: [
|
|
1415
|
+
{ label: "A timer", change: `"${name}" waits for a timer, not a message.` },
|
|
1416
|
+
{ label: "A signal", change: `"${name}" waits for a signal, not a message.` },
|
|
1417
|
+
{
|
|
1418
|
+
label: "Nothing",
|
|
1419
|
+
change: `"${name}" waits for nothing: make it a plain event, or the end.`,
|
|
1420
|
+
},
|
|
1421
|
+
],
|
|
1422
|
+
draft: `"${name}" waits for `,
|
|
1423
|
+
};
|
|
1424
|
+
},
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
for (const node of nodes.values()) {
|
|
1429
|
+
if (JOB_TASKS.has(node.element.type) && node.element.jobType === undefined) {
|
|
1430
|
+
node.element.jobType = node.element.id;
|
|
1431
|
+
}
|
|
1432
|
+
// A DMN task needs a decision to deploy; the id stands in, as it does for a job type.
|
|
1433
|
+
if (node.element.type === "businessRuleTask" &&
|
|
1434
|
+
node.element.jobType === undefined &&
|
|
1435
|
+
node.element.decisionId === undefined) {
|
|
1436
|
+
node.element.decisionId = node.element.id;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
const processBase = reader.title ? slugify(reader.title) : "Process_1";
|
|
1440
|
+
const processId = uniqueId(/^[A-Za-z_]/.test(processBase) ? processBase : `Process_${processBase}`, taken);
|
|
1441
|
+
let flowNumber = 0;
|
|
1442
|
+
const flows = edges.map(({ line: _line, ...edge }) => {
|
|
1443
|
+
let id;
|
|
1444
|
+
do
|
|
1445
|
+
id = `Flow_${++flowNumber}`;
|
|
1446
|
+
while (taken.has(id));
|
|
1447
|
+
taken.add(id);
|
|
1448
|
+
const flow = { id, ...edge };
|
|
1449
|
+
for (const key of ["name", "condition", "isDefault"]) {
|
|
1450
|
+
if (flow[key] === undefined)
|
|
1451
|
+
delete flow[key];
|
|
1452
|
+
}
|
|
1453
|
+
return flow;
|
|
1454
|
+
});
|
|
1455
|
+
// A `with` line configures a node of the diagram; one whose node was left out, or
|
|
1456
|
+
// never written, has nothing to configure.
|
|
1457
|
+
const connectors = [];
|
|
1458
|
+
for (const ref of reader.connectors) {
|
|
1459
|
+
if (nodes.has(ref.elementId))
|
|
1460
|
+
connectors.push(ref);
|
|
1461
|
+
else if (final) {
|
|
1462
|
+
problems.push({
|
|
1463
|
+
line: ref.line,
|
|
1464
|
+
message: `"with ${ref.id}:" names no node of the diagram; ignored`,
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
return {
|
|
1469
|
+
diagram: {
|
|
1470
|
+
id: "Definitions_1",
|
|
1471
|
+
processes: [
|
|
1472
|
+
{
|
|
1473
|
+
id: processId,
|
|
1474
|
+
...(reader.title ? { name: reader.title } : {}),
|
|
1475
|
+
elements: [...nodes.values()].map((node) => node.element),
|
|
1476
|
+
flows,
|
|
1477
|
+
},
|
|
1478
|
+
],
|
|
1479
|
+
},
|
|
1480
|
+
problems: problems.sort((a, b) => a.line - b.line),
|
|
1481
|
+
fixes,
|
|
1482
|
+
questions: asks.sort((a, b) => a.line - b.line).map(({ ask }) => ask()),
|
|
1483
|
+
connectors,
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1487
|
+
* Reads a process written in the line format ({@link PROCESS_TEXT_GUIDE}).
|
|
1488
|
+
*
|
|
1489
|
+
* Never throws. The returned diagram always expands, and keeps the structural
|
|
1490
|
+
* rules `lintDiagram` checks; what could not be used is in `problems`, what was
|
|
1491
|
+
* added or changed to complete the diagram is in `fixes`:
|
|
1492
|
+
*
|
|
1493
|
+
* - flow ids are generated, and a repeated edge is written once;
|
|
1494
|
+
* - a missing start event is added before the first node without an incoming
|
|
1495
|
+
* flow, and a start left unconnected leads there;
|
|
1496
|
+
* - every node lies on a path from a start event: a task or gateway nothing
|
|
1497
|
+
* leads to continues the latest path written before it that stops short of
|
|
1498
|
+
* an end event, and a fragment that still cannot be reached is left out,
|
|
1499
|
+
* never drawn loose;
|
|
1500
|
+
* - an end event is added after every path that stops elsewhere;
|
|
1501
|
+
* - a gateway with one way in and one way out is removed;
|
|
1502
|
+
* - a task or event with several ways out gets a split gateway: xor when the
|
|
1503
|
+
* branches are labelled, and otherwise parallel;
|
|
1504
|
+
* - branches meeting at a task, an event or a splitting gateway are joined
|
|
1505
|
+
* first, by a gateway of the type they were split with;
|
|
1506
|
+
* - every decision has a default branch, and every other branch a FEEL
|
|
1507
|
+
* condition — a prose one becomes a condition on a variable named for the
|
|
1508
|
+
* gateway's question; flows out of anything else carry no condition or label;
|
|
1509
|
+
* - an unnamed event, task or decision is named from its id;
|
|
1510
|
+
* - a service or send task without `job=` uses its id as its job type, and a
|
|
1511
|
+
* rule task its id as its decision id.
|
|
1512
|
+
*
|
|
1513
|
+
* @example
|
|
1514
|
+
* ```typescript
|
|
1515
|
+
* const { diagram, problems } = parseProcessText(modelOutput)
|
|
1516
|
+
* const xml = Bpmn.export(expand(diagram))
|
|
1517
|
+
* ```
|
|
1518
|
+
*/
|
|
1519
|
+
export function parseProcessText(text) {
|
|
1520
|
+
const reader = new Reader();
|
|
1521
|
+
text.split(/\r?\n/).forEach((line, index) => reader.line(line, index + 1));
|
|
1522
|
+
reader.finish();
|
|
1523
|
+
return assemble(reader, true);
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* Reads the line format while it is being written, for a live preview.
|
|
1527
|
+
*
|
|
1528
|
+
* Only complete lines are read, so every frame is built from whole facts. A
|
|
1529
|
+
* frame leaves out edges whose far end has not been declared yet and adds no
|
|
1530
|
+
* start or end events; {@link ProcessTextStream.end} applies the full
|
|
1531
|
+
* {@link parseProcessText} rules.
|
|
1532
|
+
*
|
|
1533
|
+
* @example
|
|
1534
|
+
* ```typescript
|
|
1535
|
+
* const stream = createProcessTextStream()
|
|
1536
|
+
* for await (const chunk of tokens) {
|
|
1537
|
+
* const defs = stream.push(chunk)
|
|
1538
|
+
* if (defs) render(defs)
|
|
1539
|
+
* }
|
|
1540
|
+
* const { diagram, problems } = stream.end()
|
|
1541
|
+
* ```
|
|
1542
|
+
*/
|
|
1543
|
+
export function createProcessTextStream() {
|
|
1544
|
+
const reader = new Reader();
|
|
1545
|
+
let pending = "";
|
|
1546
|
+
let lineNumber = 0;
|
|
1547
|
+
return {
|
|
1548
|
+
push(chunk) {
|
|
1549
|
+
pending += chunk;
|
|
1550
|
+
let changed = false;
|
|
1551
|
+
for (let nl = pending.indexOf("\n"); nl >= 0; nl = pending.indexOf("\n")) {
|
|
1552
|
+
changed = reader.line(pending.slice(0, nl), ++lineNumber) || changed;
|
|
1553
|
+
pending = pending.slice(nl + 1);
|
|
1554
|
+
}
|
|
1555
|
+
if (!changed || reader.nodes.size === 0)
|
|
1556
|
+
return null;
|
|
1557
|
+
return expand(assemble(reader, false).diagram);
|
|
1558
|
+
},
|
|
1559
|
+
end() {
|
|
1560
|
+
if (pending !== "")
|
|
1561
|
+
reader.line(pending, ++lineNumber);
|
|
1562
|
+
reader.finish();
|
|
1563
|
+
pending = "";
|
|
1564
|
+
return assemble(reader, true);
|
|
1565
|
+
},
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
//# sourceMappingURL=process-text.js.map
|