@bpmnkit/core 1.0.0 → 1.2.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 +30 -3
- package/dist/bpmn/agentic.js +3 -1
- package/dist/bpmn/auto-layout.js +184 -15
- package/dist/bpmn/bpmn-builder.js +69 -7
- package/dist/bpmn/bpmn-model.d.ts +78 -0
- package/dist/bpmn/bpmn-parser.js +195 -37
- package/dist/bpmn/bpmn-serializer.js +121 -72
- package/dist/bpmn/bpmnlint.d.ts +153 -0
- package/dist/bpmn/bpmnlint.js +364 -0
- package/dist/bpmn/camunda-compat-data.d.ts +165 -0
- package/dist/bpmn/camunda-compat-data.js +423 -0
- package/dist/bpmn/camunda-compat-feel.d.ts +68 -0
- package/dist/bpmn/camunda-compat-feel.js +338 -0
- package/dist/bpmn/camunda-compat.d.ts +120 -0
- package/dist/bpmn/camunda-compat.js +1677 -0
- package/dist/bpmn/camunda7/juel.d.ts +41 -0
- package/dist/bpmn/camunda7/juel.js +333 -0
- package/dist/bpmn/camunda7/migrate.d.ts +72 -0
- package/dist/bpmn/camunda7/migrate.js +1152 -0
- package/dist/bpmn/compact.d.ts +4 -0
- package/dist/bpmn/compact.js +80 -0
- package/dist/bpmn/full-operations.js +5 -2
- package/dist/bpmn/label-style.d.ts +51 -0
- package/dist/bpmn/label-style.js +116 -0
- package/dist/bpmn/lint.d.ts +23 -3
- package/dist/bpmn/lint.js +47 -10
- package/dist/bpmn/optimize/bpmnlint-rules.d.ts +31 -0
- package/dist/bpmn/optimize/bpmnlint-rules.js +916 -0
- package/dist/bpmn/optimize/deploy.d.ts +8 -2
- package/dist/bpmn/optimize/deploy.js +16 -3
- package/dist/bpmn/optimize/index.js +11 -2
- package/dist/bpmn/optimize/patterns.js +3 -0
- package/dist/bpmn/optimize/types.d.ts +14 -0
- package/dist/bpmn/optimize/variable-flow.js +4 -1
- package/dist/bpmn/process-delta.d.ts +88 -0
- package/dist/bpmn/process-delta.js +236 -0
- package/dist/bpmn/process-text-writer.d.ts +53 -0
- package/dist/bpmn/process-text-writer.js +193 -0
- package/dist/bpmn/process-text.d.ts +175 -0
- package/dist/bpmn/process-text.js +1255 -0
- package/dist/bpmn/process-types.d.ts +112 -0
- package/dist/bpmn/process-types.js +490 -0
- package/dist/bpmn/svg.js +51 -22
- package/dist/doc/docx.d.ts +20 -0
- package/dist/doc/docx.js +253 -0
- package/dist/doc/html.d.ts +21 -0
- package/dist/doc/html.js +303 -0
- package/dist/doc/index.d.ts +7 -0
- package/dist/doc/index.js +5 -0
- package/dist/doc/markdown.d.ts +11 -0
- package/dist/doc/markdown.js +160 -0
- package/dist/doc/model.d.ts +129 -0
- package/dist/doc/model.js +617 -0
- package/dist/doc/zip.d.ts +5 -0
- package/dist/doc/zip.js +75 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +11 -0
- package/dist/layout/annotations.d.ts +4 -2
- package/dist/layout/annotations.js +31 -2
- package/dist/layout/collaboration/ordering.d.ts +8 -0
- package/dist/layout/collaboration/ordering.js +57 -18
- package/dist/layout/orthogonal.d.ts +32 -0
- package/dist/layout/orthogonal.js +367 -0
- package/dist/layout/repair.d.ts +31 -0
- package/dist/layout/repair.js +220 -0
- package/dist/layout/semantic/route.js +45 -3
- package/dist/node/bpmnlint.d.ts +109 -0
- package/dist/node/bpmnlint.js +198 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.js +1 -0
- package/dist/xml/xml-patch.d.ts +21 -0
- package/dist/xml/xml-patch.js +27 -1
- package/package.json +2 -2
|
@@ -0,0 +1,1255 @@
|
|
|
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
|
+
/**
|
|
27
|
+
* How to write the format — the part of a system prompt that teaches it.
|
|
28
|
+
*
|
|
29
|
+
* It lives beside the parser so the two cannot drift: a test parses its example
|
|
30
|
+
* and expects no problems.
|
|
31
|
+
*/
|
|
32
|
+
export const PROCESS_TEXT_GUIDE = `Format — one path per line, nothing else:
|
|
33
|
+
# Process name (first line)
|
|
34
|
+
a > b > c sequence flow
|
|
35
|
+
id[kind Name] declare a node the first time it appears; afterwards write only id
|
|
36
|
+
gw >(Label: condition) x conditional branch, condition in FEEL (amount > 1000, status = "ok")
|
|
37
|
+
gw >(Label: default) y branch taken when no condition holds
|
|
38
|
+
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
|
|
39
|
+
Events take a trigger: start:message end:error catch:timer boundary:error (timer message signal error escalation terminate conditional compensate cancel)
|
|
40
|
+
Attributes after |: on=<task id> (required on boundary), nonint (non-interrupting), job=<job type>
|
|
41
|
+
Rules:
|
|
42
|
+
- One start event. Every node is on a path from it to an end event: never a node nothing leads to.
|
|
43
|
+
- An xor has two or more branches: exactly one is (Label: default), each other has a FEEL condition.
|
|
44
|
+
- 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.
|
|
45
|
+
- Branches that meet again are joined automatically; join parallel branches with an and node.
|
|
46
|
+
|
|
47
|
+
Example:
|
|
48
|
+
# Expense approval
|
|
49
|
+
start[start Expense submitted] > check[xor Amount over 1000?]
|
|
50
|
+
check >(Yes: amount > 1000) review[user Review expense] > pay[service Pay expense] > done[end Expense paid]
|
|
51
|
+
check >(No: default) auto[service Approve automatically] > pay
|
|
52
|
+
failed[boundary:error Payment failed | on=pay] > notify[send Notify submitter] > notice[end Payment failed]`;
|
|
53
|
+
export const KINDS = {
|
|
54
|
+
start: "startEvent",
|
|
55
|
+
end: "endEvent",
|
|
56
|
+
task: "task",
|
|
57
|
+
user: "userTask",
|
|
58
|
+
service: "serviceTask",
|
|
59
|
+
rule: "businessRuleTask",
|
|
60
|
+
send: "sendTask",
|
|
61
|
+
receive: "receiveTask",
|
|
62
|
+
script: "scriptTask",
|
|
63
|
+
manual: "manualTask",
|
|
64
|
+
call: "callActivity",
|
|
65
|
+
xor: "exclusiveGateway",
|
|
66
|
+
and: "parallelGateway",
|
|
67
|
+
or: "inclusiveGateway",
|
|
68
|
+
eventgw: "eventBasedGateway",
|
|
69
|
+
catch: "intermediateCatchEvent",
|
|
70
|
+
throw: "intermediateThrowEvent",
|
|
71
|
+
boundary: "boundaryEvent",
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Words models write for a kind instead of the one the guide teaches, taken
|
|
75
|
+
* from the Drop benchmark's recorded answers. Accepted without a problem: the
|
|
76
|
+
* meaning is plain, and teaching them would only lengthen the prompt.
|
|
77
|
+
*/
|
|
78
|
+
export const ALIASES = {
|
|
79
|
+
event: "intermediateCatchEvent",
|
|
80
|
+
parallel: "parallelGateway",
|
|
81
|
+
exclusive: "exclusiveGateway",
|
|
82
|
+
gateway: "exclusiveGateway",
|
|
83
|
+
inclusive: "inclusiveGateway",
|
|
84
|
+
decision: "businessRuleTask",
|
|
85
|
+
dmn: "businessRuleTask",
|
|
86
|
+
human: "userTask",
|
|
87
|
+
};
|
|
88
|
+
export const TRIGGERS = new Set([
|
|
89
|
+
"timer",
|
|
90
|
+
"message",
|
|
91
|
+
"signal",
|
|
92
|
+
"error",
|
|
93
|
+
"escalation",
|
|
94
|
+
"terminate",
|
|
95
|
+
"conditional",
|
|
96
|
+
"compensate",
|
|
97
|
+
"link",
|
|
98
|
+
"cancel",
|
|
99
|
+
]);
|
|
100
|
+
export const EVENTS = new Set([
|
|
101
|
+
"startEvent",
|
|
102
|
+
"endEvent",
|
|
103
|
+
"intermediateCatchEvent",
|
|
104
|
+
"intermediateThrowEvent",
|
|
105
|
+
"boundaryEvent",
|
|
106
|
+
]);
|
|
107
|
+
const GATEWAYS = new Set([
|
|
108
|
+
"exclusiveGateway",
|
|
109
|
+
"parallelGateway",
|
|
110
|
+
"inclusiveGateway",
|
|
111
|
+
"eventBasedGateway",
|
|
112
|
+
]);
|
|
113
|
+
/** Gateways whose outgoing flows carry conditions, and so a default. */
|
|
114
|
+
const CONDITIONAL = new Set(["exclusiveGateway", "inclusiveGateway"]);
|
|
115
|
+
/** Task types that need a `zeebe:taskDefinition` to deploy; the id stands in when none is given. */
|
|
116
|
+
const JOB_TASKS = new Set(["serviceTask", "sendTask"]);
|
|
117
|
+
const ID = /^[A-Za-z_][\w.-]*/;
|
|
118
|
+
/** A line that ends in an arrow, with or without a branch label: the path goes on below. */
|
|
119
|
+
const DANGLING = /\s*-{0,2}>\s*(\([^()]*\))?\s*$/;
|
|
120
|
+
/**
|
|
121
|
+
* Returns the index of the `close` that matches the `open` at `start`, or -1.
|
|
122
|
+
* Quoted text is skipped, so a FEEL string holding a bracket does not end it.
|
|
123
|
+
*/
|
|
124
|
+
function matching(text, start, open, close) {
|
|
125
|
+
let depth = 0;
|
|
126
|
+
let quote = false;
|
|
127
|
+
for (let i = start; i < text.length; i++) {
|
|
128
|
+
const ch = text[i];
|
|
129
|
+
if (ch === '"')
|
|
130
|
+
quote = !quote;
|
|
131
|
+
if (quote)
|
|
132
|
+
continue;
|
|
133
|
+
if (ch === open)
|
|
134
|
+
depth++;
|
|
135
|
+
else if (ch === close && --depth === 0)
|
|
136
|
+
return i;
|
|
137
|
+
}
|
|
138
|
+
return -1;
|
|
139
|
+
}
|
|
140
|
+
/** The type a bare id suggests, for a node written without a kind or never declared. */
|
|
141
|
+
function typeFromId(id) {
|
|
142
|
+
// `pick > and`, `and > dispatch`: the kind written as if it were an id.
|
|
143
|
+
const kind = KINDS[id.toLowerCase()] ?? ALIASES[id.toLowerCase()];
|
|
144
|
+
if (kind !== undefined && GATEWAYS.has(kind))
|
|
145
|
+
return kind;
|
|
146
|
+
if (/^start/i.test(id))
|
|
147
|
+
return "startEvent";
|
|
148
|
+
if (/^(end|done|finish)/i.test(id))
|
|
149
|
+
return "endEvent";
|
|
150
|
+
return "task";
|
|
151
|
+
}
|
|
152
|
+
/** `send_email` / `sendEmail` → "Send email": a readable name for a node the model never named. */
|
|
153
|
+
function nameFromId(id) {
|
|
154
|
+
const words = id
|
|
155
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
156
|
+
.replace(/[_.-]+/g, " ")
|
|
157
|
+
.trim()
|
|
158
|
+
.toLowerCase();
|
|
159
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Splits one complete path line (`a[kind Name] >(Label: cond) b > c`) into its
|
|
163
|
+
* node references and arrow labels, or says where it stops making sense.
|
|
164
|
+
*
|
|
165
|
+
* Shared by {@link parseProcessText} and the change-script parser, so the two
|
|
166
|
+
* read paths the same way.
|
|
167
|
+
*/
|
|
168
|
+
export function tokenizePath(text) {
|
|
169
|
+
const refs = [];
|
|
170
|
+
const labels = [];
|
|
171
|
+
let note;
|
|
172
|
+
let i = 0;
|
|
173
|
+
for (;;) {
|
|
174
|
+
const id = ID.exec(text.slice(i))?.[0];
|
|
175
|
+
if (id === undefined)
|
|
176
|
+
return { error: `expected a node id at "${text.slice(i, i + 20)}"` };
|
|
177
|
+
i += id.length;
|
|
178
|
+
let spec;
|
|
179
|
+
// `done-end [end Done]`: a space before the bracket still declares.
|
|
180
|
+
const gap = /^ +\[/.exec(text.slice(i));
|
|
181
|
+
if (gap)
|
|
182
|
+
i += gap[0].length - 1;
|
|
183
|
+
if (text[i] === "[") {
|
|
184
|
+
const end = matching(text, i, "[", "]");
|
|
185
|
+
if (end < 0)
|
|
186
|
+
return { error: `"${id}[" is not closed` };
|
|
187
|
+
spec = text.slice(i + 1, end).trim();
|
|
188
|
+
i = end + 1;
|
|
189
|
+
}
|
|
190
|
+
refs.push({ id, spec });
|
|
191
|
+
while (text[i] === " ")
|
|
192
|
+
i++;
|
|
193
|
+
if (i >= text.length)
|
|
194
|
+
break;
|
|
195
|
+
// `gr[xor Reproducible?] (ADDED)`: a note after the last node, most
|
|
196
|
+
// often marking what a change added.
|
|
197
|
+
const trailing = /^\([^()]*\)\s*$/.exec(text.slice(i))?.[0];
|
|
198
|
+
if (trailing !== undefined) {
|
|
199
|
+
note = trailing.trim();
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
// `->` and `-->` are what a model reaches for from Mermaid; take them too.
|
|
203
|
+
const arrow = /^-{0,2}>/.exec(text.slice(i))?.[0];
|
|
204
|
+
if (arrow === undefined)
|
|
205
|
+
return { error: `expected ">" at "${text.slice(i, i + 20)}"` };
|
|
206
|
+
i += arrow.length;
|
|
207
|
+
let label;
|
|
208
|
+
if (text[i] === "(") {
|
|
209
|
+
const end = matching(text, i, "(", ")");
|
|
210
|
+
if (end < 0)
|
|
211
|
+
return { error: "edge label is not closed" };
|
|
212
|
+
label = text.slice(i + 1, end);
|
|
213
|
+
i = end + 1;
|
|
214
|
+
}
|
|
215
|
+
labels.push(label);
|
|
216
|
+
while (text[i] === " ")
|
|
217
|
+
i++;
|
|
218
|
+
// `gw >(No: default) > next`: a second arrow after the label.
|
|
219
|
+
if (label !== undefined && text[i] === ">") {
|
|
220
|
+
i++;
|
|
221
|
+
while (text[i] === " ")
|
|
222
|
+
i++;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return note === undefined ? { refs, labels } : { refs, labels, note };
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Keeps a condition only if it is FEEL.
|
|
229
|
+
*
|
|
230
|
+
* Models write the branch they mean in prose as often as in FEEL
|
|
231
|
+
* (`No: is not approved`). As an expression that fails at deploy time; as the
|
|
232
|
+
* branch's label it still says what was meant, and the missing condition is
|
|
233
|
+
* one the lint names and a reader can fill in.
|
|
234
|
+
*/
|
|
235
|
+
export function conditionOrLabel(edge) {
|
|
236
|
+
if (edge.condition === undefined)
|
|
237
|
+
return { edge };
|
|
238
|
+
const prose = edge.condition.slice(1).trim();
|
|
239
|
+
if (parseExpression(prose).errors.length === 0)
|
|
240
|
+
return { edge };
|
|
241
|
+
const { condition: _dropped, ...rest } = edge;
|
|
242
|
+
return {
|
|
243
|
+
edge: { ...rest, name: edge.name ? `${edge.name}: ${prose}` : prose },
|
|
244
|
+
problem: `condition "${prose}" is not FEEL; kept as the branch label`,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
class Reader {
|
|
248
|
+
nodes = new Map();
|
|
249
|
+
/**
|
|
250
|
+
* The node each written id means now. Models reuse an id for a second node
|
|
251
|
+
* (`done[end Approved]`, later `done[end Rejected]`); the second becomes
|
|
252
|
+
* `done_2`, and a bare `done` after it means the latest one.
|
|
253
|
+
*/
|
|
254
|
+
current = new Map();
|
|
255
|
+
/** Every node declared under each written id, oldest first. */
|
|
256
|
+
declared = new Map();
|
|
257
|
+
taken = new Set();
|
|
258
|
+
edges = [];
|
|
259
|
+
problems = [];
|
|
260
|
+
/** Catch and boundary events written without a trigger, and made message events. */
|
|
261
|
+
guessedMessage = new Set();
|
|
262
|
+
title;
|
|
263
|
+
/** A line that ended in an arrow, waiting for the line that continues it. */
|
|
264
|
+
carry;
|
|
265
|
+
/** Reads one line. Returns whether it added anything. */
|
|
266
|
+
line(raw, n) {
|
|
267
|
+
let text = raw.trim();
|
|
268
|
+
if (this.carry !== undefined) {
|
|
269
|
+
if (text === "")
|
|
270
|
+
return false;
|
|
271
|
+
text = `${this.carry.text} ${text}`;
|
|
272
|
+
this.carry = undefined;
|
|
273
|
+
}
|
|
274
|
+
if (text === "" || text.startsWith("```") || text.startsWith("//"))
|
|
275
|
+
return false;
|
|
276
|
+
if (text.startsWith("#")) {
|
|
277
|
+
if (this.title === undefined)
|
|
278
|
+
this.title = text.replace(/^#+/, "").trim() || undefined;
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
// `engineer[user Fix issue] >` then the next step on the next line: models
|
|
282
|
+
// wrap a long path. The line is read once it is complete.
|
|
283
|
+
if (DANGLING.test(text)) {
|
|
284
|
+
this.carry = { text, line: n };
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
return this.read(text, n);
|
|
288
|
+
}
|
|
289
|
+
/** Reads a line still waiting for its continuation, without its last arrow. */
|
|
290
|
+
finish() {
|
|
291
|
+
const carry = this.carry;
|
|
292
|
+
if (carry === undefined)
|
|
293
|
+
return false;
|
|
294
|
+
this.carry = undefined;
|
|
295
|
+
this.problems.push({
|
|
296
|
+
line: carry.line,
|
|
297
|
+
message: 'the line ends with ">" and nothing follows; read without it',
|
|
298
|
+
});
|
|
299
|
+
return this.read(carry.text.replace(DANGLING, ""), carry.line);
|
|
300
|
+
}
|
|
301
|
+
read(text, n) {
|
|
302
|
+
// Parse the whole line before keeping any of it, so a line that fails half
|
|
303
|
+
// way leaves nothing behind.
|
|
304
|
+
const path = tokenizePath(text);
|
|
305
|
+
if ("error" in path)
|
|
306
|
+
return this.fail(n, path.error);
|
|
307
|
+
if (path.note !== undefined) {
|
|
308
|
+
this.problems.push({ line: n, message: `ignored the note "${path.note}"` });
|
|
309
|
+
}
|
|
310
|
+
const { refs, labels } = path;
|
|
311
|
+
// Declarations and references resolve left to right, so a chain that
|
|
312
|
+
// reuses an id (`task[A] > task[B]`) links the nodes in the order written.
|
|
313
|
+
const ids = refs.map((ref, k) => ref.spec !== undefined
|
|
314
|
+
? this.declare(ref.id, ref.spec, n, k > 0)
|
|
315
|
+
: (this.current.get(ref.id) ?? ref.id));
|
|
316
|
+
for (let k = 0; k < labels.length; k++) {
|
|
317
|
+
const from = ids[k];
|
|
318
|
+
const to = ids[k + 1];
|
|
319
|
+
if (from !== undefined && to !== undefined) {
|
|
320
|
+
this.edges.push({ from, to, line: n, ...this.feelOrLabel(edgeLabel(labels[k]), n) });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
/** {@link conditionOrLabel}, with its problem recorded against line `n`. */
|
|
326
|
+
feelOrLabel(edge, n) {
|
|
327
|
+
const { edge: kept, problem } = conditionOrLabel(edge);
|
|
328
|
+
if (problem !== undefined)
|
|
329
|
+
this.problems.push({ line: n, message: problem });
|
|
330
|
+
return kept;
|
|
331
|
+
}
|
|
332
|
+
fail(n, message) {
|
|
333
|
+
this.problems.push({ line: n, message });
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Declares a node and returns the id it was stored under.
|
|
338
|
+
*
|
|
339
|
+
* @param afterArrow - Whether it follows a `>` on its line. A reused id there
|
|
340
|
+
* is the next step of a path (`check > done[end Rejected]`); at the start of a
|
|
341
|
+
* line it is the model revising a node it has already written.
|
|
342
|
+
*/
|
|
343
|
+
declare(written, spec, n, afterArrow) {
|
|
344
|
+
const before = this.declared.get(written) ?? [];
|
|
345
|
+
// Restating a node is harmless; the same id for something else is a new node.
|
|
346
|
+
const same = before.find((id) => this.nodes.get(id)?.spec.toLowerCase() === spec.toLowerCase());
|
|
347
|
+
if (same !== undefined) {
|
|
348
|
+
this.current.set(written, same);
|
|
349
|
+
return same;
|
|
350
|
+
}
|
|
351
|
+
const earlierId = before.at(-1);
|
|
352
|
+
const earlier = earlierId === undefined ? undefined : this.nodes.get(earlierId);
|
|
353
|
+
if (earlierId !== undefined && earlier) {
|
|
354
|
+
// Only a real kind after an arrow, or a boundary, makes a second node.
|
|
355
|
+
// `and[kind and]` or `ord[id=ord kind=start]` annotate the node already
|
|
356
|
+
// there, and `validate[xor …] > …` restarts from it.
|
|
357
|
+
const word = spec.split(/[\s|]/, 1)[0]?.toLowerCase().split(":")[0] ?? "";
|
|
358
|
+
const kind = KINDS[word] ?? ALIASES[word];
|
|
359
|
+
// A boundary is always new: it cannot be a revision of the task it sits on.
|
|
360
|
+
if (kind === undefined || (!afterArrow && kind !== "boundaryEvent")) {
|
|
361
|
+
this.problems.push({
|
|
362
|
+
line: n,
|
|
363
|
+
message: `"${written}" is already declared on line ${earlier.line}; ignored "${spec}"`,
|
|
364
|
+
});
|
|
365
|
+
return earlierId;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
// Read before the id is remapped: in `pay[boundary:error … | on=pay]` the
|
|
369
|
+
// host is the earlier `pay`.
|
|
370
|
+
const meant = (ref) => this.current.get(ref) ?? ref;
|
|
371
|
+
const hostOf = new Map();
|
|
372
|
+
for (const match of spec.matchAll(/\bon=([\w.-]+)/g)) {
|
|
373
|
+
if (match[1])
|
|
374
|
+
hostOf.set(match[1], meant(match[1]));
|
|
375
|
+
}
|
|
376
|
+
const id = uniqueId(written, this.taken);
|
|
377
|
+
this.current.set(written, id);
|
|
378
|
+
this.declared.set(written, [...before, id]);
|
|
379
|
+
if (earlier) {
|
|
380
|
+
this.problems.push({
|
|
381
|
+
line: n,
|
|
382
|
+
message: `"${written}" on line ${earlier.line} is a different node; this one is "${id}"`,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
const bar = spec.indexOf("|");
|
|
386
|
+
const head = (bar < 0 ? spec : spec.slice(0, bar)).trim();
|
|
387
|
+
const attrs = bar < 0 ? "" : spec.slice(bar + 1);
|
|
388
|
+
const space = head.search(/\s/);
|
|
389
|
+
const kindWord = space < 0 ? head : head.slice(0, space);
|
|
390
|
+
const name = space < 0 ? undefined : head.slice(space + 1).trim() || undefined;
|
|
391
|
+
const [kind = "", trigger] = kindWord.toLowerCase().split(":");
|
|
392
|
+
let type = KINDS[kind] ?? ALIASES[kind];
|
|
393
|
+
let label = name;
|
|
394
|
+
if (type === undefined) {
|
|
395
|
+
// Most often the kind was left out (`start[Order placed]`): the whole
|
|
396
|
+
// head is the name, and the id is the best remaining hint at the type.
|
|
397
|
+
type = typeFromId(id);
|
|
398
|
+
label = head || undefined;
|
|
399
|
+
this.problems.push({ line: n, message: `unknown kind "${kind}" for "${id}"; used ${type}` });
|
|
400
|
+
}
|
|
401
|
+
const element = { id, type };
|
|
402
|
+
if (trigger !== undefined) {
|
|
403
|
+
if (!EVENTS.has(type)) {
|
|
404
|
+
this.problems.push({
|
|
405
|
+
line: n,
|
|
406
|
+
message: `"${kind}" takes no trigger; ignored ":${trigger}"`,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
else if (!TRIGGERS.has(trigger)) {
|
|
410
|
+
// `start:order received` — a name written where the trigger goes. When the
|
|
411
|
+
// name already says it (`start:kyc Start KYC`), the word is only noise.
|
|
412
|
+
const word = kindWord.slice(kindWord.indexOf(":") + 1);
|
|
413
|
+
const said = label?.toLowerCase().split(/\W+/).includes(trigger) ?? false;
|
|
414
|
+
if (!said)
|
|
415
|
+
label = [word, label].filter(Boolean).join(" ");
|
|
416
|
+
this.problems.push({
|
|
417
|
+
line: n,
|
|
418
|
+
message: `unknown trigger "${trigger}" for "${id}"; ${said ? "ignored, the name already says it" : "read as part of the name"}`,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
else if (trigger === "link") {
|
|
422
|
+
// A link event needs a link name and a partner, which this format cannot
|
|
423
|
+
// write; in a path a model means a milestone by it.
|
|
424
|
+
if (type === "intermediateCatchEvent")
|
|
425
|
+
element.type = "intermediateThrowEvent";
|
|
426
|
+
this.problems.push({
|
|
427
|
+
line: n,
|
|
428
|
+
message: `"${id}" cannot be a link event here; made it a plain event`,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
element.eventType = trigger;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (label)
|
|
436
|
+
element.name = label;
|
|
437
|
+
// A catch or boundary event waits for something, and cannot deploy without
|
|
438
|
+
// saying what. Unnamed, it is most often a message: "Payment confirmed".
|
|
439
|
+
if ((element.type === "intermediateCatchEvent" || element.type === "boundaryEvent") &&
|
|
440
|
+
element.eventType === undefined) {
|
|
441
|
+
element.eventType = "message";
|
|
442
|
+
this.guessedMessage.add(id);
|
|
443
|
+
this.problems.push({
|
|
444
|
+
line: n,
|
|
445
|
+
message: `"${id}" waits for nothing in particular; made it a message event`,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
const node = { element, line: n, spec };
|
|
449
|
+
// `| on=pay | nonint`: a second bar is only another separator.
|
|
450
|
+
for (const attr of attrs.split(/[\s,|]+/).filter(Boolean)) {
|
|
451
|
+
const [key, value] = attr.split("=", 2);
|
|
452
|
+
if (key === "on" && value)
|
|
453
|
+
node.on = hostOf.get(value) ?? value;
|
|
454
|
+
else if (key === "job" && value)
|
|
455
|
+
element.jobType = value;
|
|
456
|
+
else if (key === "nonint" && value === undefined)
|
|
457
|
+
element.interrupting = false;
|
|
458
|
+
else
|
|
459
|
+
this.problems.push({ line: n, message: `unknown attribute "${attr}" on "${id}"; ignored` });
|
|
460
|
+
}
|
|
461
|
+
this.nodes.set(id, node);
|
|
462
|
+
return id;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
export function edgeLabel(label) {
|
|
466
|
+
if (label === undefined)
|
|
467
|
+
return {};
|
|
468
|
+
const colon = label.indexOf(":");
|
|
469
|
+
const name = (colon < 0 ? label : label.slice(0, colon)).trim();
|
|
470
|
+
const rest = colon < 0 ? "" : label.slice(colon + 1).trim();
|
|
471
|
+
const out = {};
|
|
472
|
+
if (name.toLowerCase() === "default" && rest === "")
|
|
473
|
+
return { isDefault: true };
|
|
474
|
+
if (name)
|
|
475
|
+
out.name = name;
|
|
476
|
+
if (rest.toLowerCase() === "default")
|
|
477
|
+
out.isDefault = true;
|
|
478
|
+
else if (rest)
|
|
479
|
+
out.condition = rest.startsWith("=") ? rest : `= ${rest}`;
|
|
480
|
+
return out;
|
|
481
|
+
}
|
|
482
|
+
/** Branch labels that read as "otherwise" — the branch a gateway should default to. */
|
|
483
|
+
const OTHERWISE = /^(no|not|else|otherwise|default|false|reject(ed)?|fail(ed|ure)?|invalid|denied|declined)\b/i;
|
|
484
|
+
/** `"A", "B" and "C"` */
|
|
485
|
+
function listOf(items) {
|
|
486
|
+
return items.length < 2
|
|
487
|
+
? (items[0] ?? "")
|
|
488
|
+
: `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
|
|
489
|
+
}
|
|
490
|
+
/** `Status approved?` → `statusApproved`: a FEEL variable name for the question a gateway asks. */
|
|
491
|
+
function variableFrom(text) {
|
|
492
|
+
const words = text
|
|
493
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
494
|
+
.split(/[^A-Za-z0-9]+/)
|
|
495
|
+
.filter(Boolean)
|
|
496
|
+
.map((word, k) => {
|
|
497
|
+
const lower = word.toLowerCase();
|
|
498
|
+
return k === 0 ? lower : lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
499
|
+
});
|
|
500
|
+
const name = words.join("");
|
|
501
|
+
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Turns what the reader holds into a diagram `expand` accepts.
|
|
505
|
+
*
|
|
506
|
+
* `final` is false while the text is still arriving: an edge to a node not yet
|
|
507
|
+
* declared is only waiting, so it is left out without a problem, and nothing is
|
|
508
|
+
* added at the ends of paths that are still being written. Once final, the
|
|
509
|
+
* result also keeps the modelling rules `lintDiagram` checks — see
|
|
510
|
+
* {@link parseProcessText}.
|
|
511
|
+
*/
|
|
512
|
+
function assemble(reader, final) {
|
|
513
|
+
const problems = [...reader.problems];
|
|
514
|
+
const fixes = [];
|
|
515
|
+
// Asked once the names are final, so a question uses the names the diagram shows.
|
|
516
|
+
const asks = [];
|
|
517
|
+
const nodes = new Map();
|
|
518
|
+
for (const [id, node] of reader.nodes) {
|
|
519
|
+
nodes.set(id, { ...node, element: { ...node.element } });
|
|
520
|
+
}
|
|
521
|
+
const taken = new Set(nodes.keys());
|
|
522
|
+
const typeOf = (id) => nodes.get(id)?.element.type;
|
|
523
|
+
const nameOf = (id) => nodes.get(id)?.element.name ?? nameFromId(id);
|
|
524
|
+
const add = (id, type, line, name) => {
|
|
525
|
+
const element = name === undefined ? { id, type } : { id, type, name };
|
|
526
|
+
nodes.set(id, { element, line, spec: "" });
|
|
527
|
+
};
|
|
528
|
+
// An id used in a flow but never declared (`review > pay`) would take every
|
|
529
|
+
// flow on it down with it. Once the text is complete, it becomes a task named
|
|
530
|
+
// from its id instead — while streaming it may yet be declared. An id only an
|
|
531
|
+
// `on=` names is not added: nothing would lead to it.
|
|
532
|
+
if (final) {
|
|
533
|
+
for (const edge of reader.edges) {
|
|
534
|
+
for (const id of [edge.from, edge.to]) {
|
|
535
|
+
if (nodes.has(id))
|
|
536
|
+
continue;
|
|
537
|
+
const type = typeFromId(id);
|
|
538
|
+
add(id, type, edge.line, GATEWAYS.has(type) ? undefined : nameFromId(id));
|
|
539
|
+
taken.add(id);
|
|
540
|
+
problems.push({ line: edge.line, message: `"${id}" is never declared; added as a ${type}` });
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
// Boundary events: a host that is an activity, in this process.
|
|
545
|
+
for (const [id, node] of nodes) {
|
|
546
|
+
if (node.element.type !== "boundaryEvent")
|
|
547
|
+
continue;
|
|
548
|
+
const host = node.on === undefined ? undefined : nodes.get(node.on);
|
|
549
|
+
const hostType = host?.element.type;
|
|
550
|
+
if (hostType !== undefined && !EVENTS.has(hostType) && !GATEWAYS.has(hostType)) {
|
|
551
|
+
node.element.attachedTo = node.on;
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if (!final && node.on !== undefined && host === undefined) {
|
|
555
|
+
nodes.delete(id); // host still to come
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
problems.push({
|
|
559
|
+
line: node.line,
|
|
560
|
+
message: node.on === undefined
|
|
561
|
+
? `boundary "${id}" has no on=<task id>; left out`
|
|
562
|
+
: host === undefined
|
|
563
|
+
? `boundary "${id}" is on "${node.on}", which is never declared; left out`
|
|
564
|
+
: `boundary "${id}" is on "${node.on}", which is not a task; left out`,
|
|
565
|
+
});
|
|
566
|
+
nodes.delete(id);
|
|
567
|
+
}
|
|
568
|
+
// A branch drawn into a boundary event (`check >(No) failed[boundary:error … |
|
|
569
|
+
// on=pay] > notify`) means the path that boundary leads to. Nothing can flow
|
|
570
|
+
// into a boundary, so the branch goes to its handler instead of being lost.
|
|
571
|
+
const written = reader.edges.flatMap((edge) => {
|
|
572
|
+
if (reader.nodes.get(edge.to)?.element.type !== "boundaryEvent")
|
|
573
|
+
return [edge];
|
|
574
|
+
const next = reader.edges.filter((out) => out.from === edge.to);
|
|
575
|
+
if (next.length === 0)
|
|
576
|
+
return [edge];
|
|
577
|
+
if (final) {
|
|
578
|
+
fixes.push(`led ${edge.from} > ${edge.to} to what the boundary leads to, since nothing flows into a boundary event`);
|
|
579
|
+
}
|
|
580
|
+
return next.map((out) => ({ ...edge, to: out.to }));
|
|
581
|
+
});
|
|
582
|
+
// Edges: both ends declared, pointing a way BPMN allows, once.
|
|
583
|
+
const seen = new Set();
|
|
584
|
+
let edges = [];
|
|
585
|
+
for (const edge of written) {
|
|
586
|
+
const from = typeOf(edge.from);
|
|
587
|
+
const to = typeOf(edge.to);
|
|
588
|
+
if (from === undefined || to === undefined) {
|
|
589
|
+
if (final) {
|
|
590
|
+
const missing = from === undefined ? edge.from : edge.to;
|
|
591
|
+
const why = reader.nodes.has(missing) ? "was left out" : "is never declared";
|
|
592
|
+
problems.push({
|
|
593
|
+
line: edge.line,
|
|
594
|
+
message: `"${missing}" ${why}; flow ${edge.from} > ${edge.to} left out`,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
const wrong = from === "endEvent"
|
|
600
|
+
? "an end event has no outgoing flow"
|
|
601
|
+
: to === "startEvent"
|
|
602
|
+
? "a start event has no incoming flow"
|
|
603
|
+
: to === "boundaryEvent"
|
|
604
|
+
? "a boundary event has no incoming flow"
|
|
605
|
+
: edge.from === edge.to
|
|
606
|
+
? "a flow cannot lead back to where it starts"
|
|
607
|
+
: undefined;
|
|
608
|
+
if (wrong) {
|
|
609
|
+
problems.push({
|
|
610
|
+
line: edge.line,
|
|
611
|
+
message: `${wrong}; flow ${edge.from} > ${edge.to} left out`,
|
|
612
|
+
});
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
const key = `${edge.from}>${edge.to}`;
|
|
616
|
+
if (seen.has(key))
|
|
617
|
+
continue;
|
|
618
|
+
seen.add(key);
|
|
619
|
+
edges.push({ ...edge });
|
|
620
|
+
}
|
|
621
|
+
// Defaults: only on a conditional gateway, at most one each.
|
|
622
|
+
const defaulted = new Set();
|
|
623
|
+
for (const edge of edges) {
|
|
624
|
+
if (!edge.isDefault)
|
|
625
|
+
continue;
|
|
626
|
+
const from = typeOf(edge.from);
|
|
627
|
+
if (from === undefined || !CONDITIONAL.has(from) || defaulted.has(edge.from)) {
|
|
628
|
+
problems.push({
|
|
629
|
+
line: edge.line,
|
|
630
|
+
message: defaulted.has(edge.from)
|
|
631
|
+
? `"${edge.from}" already has a default branch; this one is not the default`
|
|
632
|
+
: `only an xor or or gateway has a default branch; ${edge.from} > ${edge.to} is not`,
|
|
633
|
+
});
|
|
634
|
+
edge.isDefault = undefined;
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
defaulted.add(edge.from);
|
|
638
|
+
}
|
|
639
|
+
const outOf = (id) => edges.filter((edge) => edge.from === id);
|
|
640
|
+
const into = (id) => edges.filter((edge) => edge.to === id);
|
|
641
|
+
if (final) {
|
|
642
|
+
// One blank start: a process that starts twice with no trigger to tell the
|
|
643
|
+
// two apart is two processes. The later one goes, and what it led to is
|
|
644
|
+
// placed as any path nothing leads to is, below.
|
|
645
|
+
const blank = [...nodes.values()]
|
|
646
|
+
.filter((node) => node.element.type === "startEvent" && node.element.eventType === undefined)
|
|
647
|
+
.sort((a, b) => a.line - b.line);
|
|
648
|
+
for (const extra of blank.slice(1)) {
|
|
649
|
+
const id = extra.element.id;
|
|
650
|
+
nodes.delete(id);
|
|
651
|
+
edges = edges.filter((edge) => edge.from !== id);
|
|
652
|
+
problems.push({
|
|
653
|
+
line: extra.line,
|
|
654
|
+
message: `"${id}" is a second blank start event; left out`,
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
// Start: one where none was written, and a start the model left unconnected
|
|
658
|
+
// leads to the first path that has no way in.
|
|
659
|
+
const roots = [...nodes.values()].filter((node) => node.element.type !== "startEvent" &&
|
|
660
|
+
node.element.type !== "boundaryEvent" &&
|
|
661
|
+
node.element.type !== "endEvent" &&
|
|
662
|
+
into(node.element.id).length === 0);
|
|
663
|
+
const first = roots[0]?.element.id;
|
|
664
|
+
const starts = [...nodes.values()].filter((node) => node.element.type === "startEvent");
|
|
665
|
+
if (starts.length === 0) {
|
|
666
|
+
const start = uniqueId("start", taken);
|
|
667
|
+
add(start, "startEvent", 0, "Process started");
|
|
668
|
+
if (first)
|
|
669
|
+
edges.unshift({ from: start, to: first, line: 0 });
|
|
670
|
+
fixes.push(`added start event "${start}"${first ? ` before "${first}"` : ""}`);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
const idle = starts.find((node) => outOf(node.element.id).length === 0);
|
|
674
|
+
if (idle && first) {
|
|
675
|
+
edges.push({ from: idle.element.id, to: first, line: idle.line });
|
|
676
|
+
fixes.push(`connected start event "${idle.element.id}" to "${first}"`);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
// Reachability: every node lies on a path from a start event.
|
|
680
|
+
const reach = () => {
|
|
681
|
+
const reached = new Set();
|
|
682
|
+
const queue = [...nodes.values()]
|
|
683
|
+
.filter((node) => node.element.type === "startEvent")
|
|
684
|
+
.map((node) => node.element.id);
|
|
685
|
+
for (let id = queue.shift(); id !== undefined; id = queue.shift()) {
|
|
686
|
+
if (reached.has(id))
|
|
687
|
+
continue;
|
|
688
|
+
reached.add(id);
|
|
689
|
+
for (const edge of outOf(id))
|
|
690
|
+
queue.push(edge.to);
|
|
691
|
+
for (const node of nodes.values()) {
|
|
692
|
+
if (node.element.attachedTo === id)
|
|
693
|
+
queue.push(node.element.id);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return reached;
|
|
697
|
+
};
|
|
698
|
+
let reached = reach();
|
|
699
|
+
// A task or gateway nothing leads to, written after a path that stops short,
|
|
700
|
+
// is most often that path's next step with the arrow left out
|
|
701
|
+
// (`… > label` then `gw[xor Done?] > dispatch`): it continues the latest
|
|
702
|
+
// such path written before it. Events are not guessed at — a loose timer
|
|
703
|
+
// or message could belong anywhere.
|
|
704
|
+
for (const node of [...nodes.values()].sort((a, b) => a.line - b.line)) {
|
|
705
|
+
const { id, type } = node.element;
|
|
706
|
+
if (reached.has(id) || EVENTS.has(type) || into(id).length > 0)
|
|
707
|
+
continue;
|
|
708
|
+
const open = [...nodes.values()].filter((other) => reached.has(other.element.id) &&
|
|
709
|
+
other.element.type !== "endEvent" &&
|
|
710
|
+
other.line <= node.line &&
|
|
711
|
+
outOf(other.element.id).length === 0);
|
|
712
|
+
const from = open.reduce((latest, other) => (latest === undefined || other.line >= latest.line ? other : latest), undefined);
|
|
713
|
+
if (from === undefined)
|
|
714
|
+
continue;
|
|
715
|
+
edges.push({ from: from.element.id, to: id, line: node.line });
|
|
716
|
+
fixes.push(`connected "${from.element.id}" to "${id}", which nothing led to`);
|
|
717
|
+
reached = reach();
|
|
718
|
+
}
|
|
719
|
+
// What is still unreached is a fragment the model never connected, and
|
|
720
|
+
// drawing it loose is worse than leaving it out.
|
|
721
|
+
for (const [id, node] of nodes) {
|
|
722
|
+
if (reached.has(id))
|
|
723
|
+
continue;
|
|
724
|
+
problems.push({
|
|
725
|
+
line: node.line,
|
|
726
|
+
message: `"${id}" is not connected to a start event; left out`,
|
|
727
|
+
});
|
|
728
|
+
nodes.delete(id);
|
|
729
|
+
const { type, name = nameFromId(id) } = node.element;
|
|
730
|
+
if (EVENTS.has(type) || GATEWAYS.has(type))
|
|
731
|
+
continue;
|
|
732
|
+
asks.push({
|
|
733
|
+
line: node.line,
|
|
734
|
+
ask: () => ({
|
|
735
|
+
elementId: id,
|
|
736
|
+
text: `"${name}" was left out: nothing leads to it. Where does it belong?`,
|
|
737
|
+
options: [],
|
|
738
|
+
draft: `Put "${name}" after `,
|
|
739
|
+
}),
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
edges = edges.filter((edge) => reached.has(edge.from) && reached.has(edge.to));
|
|
743
|
+
// Ends: every path that stops elsewhere, including a boundary with nowhere to go.
|
|
744
|
+
const addEnds = () => {
|
|
745
|
+
for (const node of [...nodes.values()]) {
|
|
746
|
+
const { id, type } = node.element;
|
|
747
|
+
if (type === "endEvent" || outOf(id).length > 0)
|
|
748
|
+
continue;
|
|
749
|
+
const end = uniqueId(`${id}_end`, taken);
|
|
750
|
+
const name = EVENTS.has(type) ? node.element.name : undefined;
|
|
751
|
+
add(end, "endEvent", node.line, name ?? "Process completed");
|
|
752
|
+
edges.push({ from: id, to: end, line: node.line });
|
|
753
|
+
fixes.push(`added end event "${end}" after "${id}"`);
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
addEnds();
|
|
757
|
+
// Exits: a loop with no way out never ends. Its last decision gets a branch
|
|
758
|
+
// to an end event; the condition rules below give it a condition. A loop
|
|
759
|
+
// with no decision in it (`and > it[task IT] > and`, parallel branches
|
|
760
|
+
// drawn back into their own split) is not a loop anyone meant: its flows
|
|
761
|
+
// back go, and each path then ends.
|
|
762
|
+
for (;;) {
|
|
763
|
+
const ends = new Set();
|
|
764
|
+
const queue = [...nodes.values()]
|
|
765
|
+
.filter((node) => node.element.type === "endEvent")
|
|
766
|
+
.map((node) => node.element.id);
|
|
767
|
+
for (let id = queue.shift(); id !== undefined; id = queue.shift()) {
|
|
768
|
+
if (ends.has(id))
|
|
769
|
+
continue;
|
|
770
|
+
ends.add(id);
|
|
771
|
+
for (const edge of into(id))
|
|
772
|
+
queue.push(edge.from);
|
|
773
|
+
}
|
|
774
|
+
const stuck = [...nodes.values()].filter((node) => !ends.has(node.element.id) && node.element.type !== "boundaryEvent");
|
|
775
|
+
const decision = stuck
|
|
776
|
+
.filter((node) => CONDITIONAL.has(node.element.type))
|
|
777
|
+
.sort((a, b) => b.line - a.line)[0];
|
|
778
|
+
if (decision === undefined) {
|
|
779
|
+
if (stuck.length === 0)
|
|
780
|
+
break;
|
|
781
|
+
const inStuck = new Set(stuck.map((node) => node.element.id));
|
|
782
|
+
const backs = new Set();
|
|
783
|
+
const done = new Set();
|
|
784
|
+
const onPath = new Set();
|
|
785
|
+
const visit = (id) => {
|
|
786
|
+
onPath.add(id);
|
|
787
|
+
for (const edge of outOf(id)) {
|
|
788
|
+
if (onPath.has(edge.to)) {
|
|
789
|
+
if (inStuck.has(edge.to))
|
|
790
|
+
backs.add(edge);
|
|
791
|
+
}
|
|
792
|
+
else if (!done.has(edge.to))
|
|
793
|
+
visit(edge.to);
|
|
794
|
+
}
|
|
795
|
+
onPath.delete(id);
|
|
796
|
+
done.add(id);
|
|
797
|
+
};
|
|
798
|
+
for (const node of nodes.values()) {
|
|
799
|
+
if (node.element.type === "startEvent")
|
|
800
|
+
visit(node.element.id);
|
|
801
|
+
}
|
|
802
|
+
for (const edge of backs) {
|
|
803
|
+
problems.push({
|
|
804
|
+
line: edge.line,
|
|
805
|
+
message: `flow ${edge.from} > ${edge.to} loops with no way out; left out`,
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
edges = edges.filter((edge) => !backs.has(edge));
|
|
809
|
+
addEnds();
|
|
810
|
+
if (backs.size > 0)
|
|
811
|
+
continue;
|
|
812
|
+
for (const node of stuck) {
|
|
813
|
+
problems.push({
|
|
814
|
+
line: node.line,
|
|
815
|
+
message: `"${node.element.id}" is in a loop with no way out`,
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
const id = decision.element.id;
|
|
821
|
+
const end = uniqueId(`${id}_end`, taken);
|
|
822
|
+
add(end, "endEvent", decision.line, "Process completed");
|
|
823
|
+
edges.push({ from: id, to: end, line: decision.line, name: "Otherwise" });
|
|
824
|
+
fixes.push(`added an exit from the loop at "${id}" to end event "${end}"`);
|
|
825
|
+
asks.push({
|
|
826
|
+
line: decision.line,
|
|
827
|
+
ask: () => {
|
|
828
|
+
const name = nameOf(id);
|
|
829
|
+
return {
|
|
830
|
+
elementId: id,
|
|
831
|
+
text: `The loop at "${name}" had no way out, so one was added. When does it end?`,
|
|
832
|
+
options: [],
|
|
833
|
+
draft: `Leave the loop at "${name}" when `,
|
|
834
|
+
};
|
|
835
|
+
},
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
// Pass-through gateways: one way in and one way out decides nothing. Most
|
|
839
|
+
// often a question the model asked and then answered only one way.
|
|
840
|
+
for (let removed = true; removed;) {
|
|
841
|
+
removed = false;
|
|
842
|
+
for (const [id, node] of nodes) {
|
|
843
|
+
if (!GATEWAYS.has(node.element.type))
|
|
844
|
+
continue;
|
|
845
|
+
const [inEdge, ...moreIn] = into(id);
|
|
846
|
+
const [outEdge, ...moreOut] = outOf(id);
|
|
847
|
+
if (!inEdge || !outEdge || moreIn.length > 0 || moreOut.length > 0)
|
|
848
|
+
continue;
|
|
849
|
+
if (inEdge.from === outEdge.to)
|
|
850
|
+
continue;
|
|
851
|
+
const target = typeOf(outEdge.to);
|
|
852
|
+
if (node.element.type === "eventBasedGateway" &&
|
|
853
|
+
target !== "intermediateCatchEvent" &&
|
|
854
|
+
target !== "receiveTask") {
|
|
855
|
+
// Waiting for one event is a catch event, not a race between events.
|
|
856
|
+
node.element.type = "intermediateCatchEvent";
|
|
857
|
+
node.element.eventType = "message";
|
|
858
|
+
problems.push({
|
|
859
|
+
line: node.line,
|
|
860
|
+
message: `"${id}" waits for only one event; made it a message catch event`,
|
|
861
|
+
});
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
inEdge.to = outEdge.to;
|
|
865
|
+
edges = edges.filter((edge) => edge !== outEdge);
|
|
866
|
+
nodes.delete(id);
|
|
867
|
+
problems.push({ line: node.line, message: `"${id}" has only one branch; gateway removed` });
|
|
868
|
+
if (CONDITIONAL.has(node.element.type)) {
|
|
869
|
+
// Most often a question the model asked and answered only one way.
|
|
870
|
+
const name = node.element.name ?? `${nameFromId(id)}?`;
|
|
871
|
+
asks.push({
|
|
872
|
+
line: node.line,
|
|
873
|
+
ask: () => ({
|
|
874
|
+
elementId: id,
|
|
875
|
+
text: `"${name}" had only one way to go, so it was removed. What happens otherwise?`,
|
|
876
|
+
options: [],
|
|
877
|
+
draft: `At "${name}", otherwise `,
|
|
878
|
+
}),
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
removed = true;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
const once = new Set();
|
|
885
|
+
edges = edges.filter((edge) => {
|
|
886
|
+
const key = `${edge.from}>${edge.to}`;
|
|
887
|
+
if (once.has(key))
|
|
888
|
+
return false;
|
|
889
|
+
once.add(key);
|
|
890
|
+
return true;
|
|
891
|
+
});
|
|
892
|
+
// Implicit splits: a task or event with several ways out forks the token
|
|
893
|
+
// without saying so. Labelled branches are a decision. Unlabelled ones that
|
|
894
|
+
// all wait, at least one on a catch event (`poll > timeout[catch:timer 5m]`,
|
|
895
|
+
// `poll > get[receive Report]`), are a race: whichever happens first. Other
|
|
896
|
+
// unlabelled ones run in parallel, which is what the flows mean in BPMN.
|
|
897
|
+
for (const node of [...nodes.values()]) {
|
|
898
|
+
const { id, type } = node.element;
|
|
899
|
+
const out = outOf(id);
|
|
900
|
+
if (GATEWAYS.has(type) || out.length < 2)
|
|
901
|
+
continue;
|
|
902
|
+
const decides = out.some((edge) => edge.condition !== undefined || edge.name !== undefined);
|
|
903
|
+
const targets = out.map((edge) => nodes.get(edge.to)?.element);
|
|
904
|
+
const races = !decides &&
|
|
905
|
+
targets.every((t) => t?.type === "intermediateCatchEvent" || t?.type === "receiveTask") &&
|
|
906
|
+
targets.some((t) => t?.type === "intermediateCatchEvent");
|
|
907
|
+
const split = uniqueId(`${id}_split`, taken);
|
|
908
|
+
const gatewayType = decides
|
|
909
|
+
? "exclusiveGateway"
|
|
910
|
+
: races
|
|
911
|
+
? "eventBasedGateway"
|
|
912
|
+
: "parallelGateway";
|
|
913
|
+
const name = decides ? `${node.element.name ?? nameFromId(id)} outcome?` : undefined;
|
|
914
|
+
add(split, gatewayType, node.line, name);
|
|
915
|
+
for (const edge of out)
|
|
916
|
+
edge.from = split;
|
|
917
|
+
edges.push({ from: id, to: split, line: node.line });
|
|
918
|
+
if (races) {
|
|
919
|
+
// Camunda 8 waits on catch events only after an event-based gateway.
|
|
920
|
+
for (const target of targets) {
|
|
921
|
+
if (!target)
|
|
922
|
+
continue;
|
|
923
|
+
if (target.type === "receiveTask")
|
|
924
|
+
target.type = "intermediateCatchEvent";
|
|
925
|
+
target.eventType ??= "message";
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
const kind = decides ? "xor" : races ? "event" : "and";
|
|
929
|
+
fixes.push(`split the flows out of "${id}" with ${kind} gateway "${split}"`);
|
|
930
|
+
if (kind === "and") {
|
|
931
|
+
// Read now: a join below may take a branch's place as its target.
|
|
932
|
+
const targetIds = out.map((edge) => edge.to);
|
|
933
|
+
asks.push({
|
|
934
|
+
line: node.line,
|
|
935
|
+
ask: () => {
|
|
936
|
+
const name = nameOf(id);
|
|
937
|
+
const branches = listOf(targetIds.map((to) => `"${nameOf(to)}"`));
|
|
938
|
+
return {
|
|
939
|
+
elementId: split,
|
|
940
|
+
text: `After "${name}", ${branches} run at the same time. Is that right?`,
|
|
941
|
+
options: [
|
|
942
|
+
{
|
|
943
|
+
label: "Only one of them",
|
|
944
|
+
change: `After "${name}", only one of ${branches} happens: decide which with an xor gateway.`,
|
|
945
|
+
},
|
|
946
|
+
{
|
|
947
|
+
label: "One after the other",
|
|
948
|
+
change: `After "${name}", do ${branches} one after the other, not at the same time.`,
|
|
949
|
+
},
|
|
950
|
+
],
|
|
951
|
+
draft: `After "${name}", `,
|
|
952
|
+
};
|
|
953
|
+
},
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
// Joins: branches that meet at anything but a join gateway meet at a join
|
|
959
|
+
// gateway first. Camunda style keeps one incoming flow per task and one role
|
|
960
|
+
// per gateway, and it is the piece of structure a model most often leaves out.
|
|
961
|
+
// The join matches the split the branches came from: an xor join after a
|
|
962
|
+
// parallel split would run what follows once per branch.
|
|
963
|
+
const splitOf = (edge) => {
|
|
964
|
+
const visited = new Set();
|
|
965
|
+
for (let id = edge.from; !visited.has(id);) {
|
|
966
|
+
visited.add(id);
|
|
967
|
+
if (GATEWAYS.has(typeOf(id) ?? "task") && outOf(id).length > 1)
|
|
968
|
+
return id;
|
|
969
|
+
const [only, ...more] = into(id);
|
|
970
|
+
if (!only || more.length > 0)
|
|
971
|
+
return undefined;
|
|
972
|
+
id = only.from;
|
|
973
|
+
}
|
|
974
|
+
return undefined;
|
|
975
|
+
};
|
|
976
|
+
for (const [target, node] of [...nodes]) {
|
|
977
|
+
const incoming = into(target);
|
|
978
|
+
const type = node.element.type;
|
|
979
|
+
if (incoming.length < 2 || (GATEWAYS.has(type) && outOf(target).length < 2))
|
|
980
|
+
continue;
|
|
981
|
+
const splits = new Set(incoming.map(splitOf));
|
|
982
|
+
const [split] = splits;
|
|
983
|
+
const splitType = splits.size === 1 && split !== undefined ? typeOf(split) : undefined;
|
|
984
|
+
const joinType = splitType === "parallelGateway" || splitType === "inclusiveGateway"
|
|
985
|
+
? splitType
|
|
986
|
+
: "exclusiveGateway";
|
|
987
|
+
const join = uniqueId(`${target}_join`, taken);
|
|
988
|
+
add(join, joinType, node.line);
|
|
989
|
+
for (const edge of incoming)
|
|
990
|
+
edge.to = join;
|
|
991
|
+
edges.push({ from: join, to: target, line: node.line });
|
|
992
|
+
const kind = joinType === "parallelGateway" ? "and" : joinType === "inclusiveGateway" ? "or" : "xor";
|
|
993
|
+
fixes.push(`joined ${incoming.length} flows into "${target}" with ${kind} gateway "${join}"`);
|
|
994
|
+
}
|
|
995
|
+
if (final) {
|
|
996
|
+
// Conditions: every branch of a decision has a FEEL condition, or is its one
|
|
997
|
+
// default; every other flow carries neither, nor a label that implies one.
|
|
998
|
+
for (const [id, node] of nodes) {
|
|
999
|
+
const out = outOf(id);
|
|
1000
|
+
if (!CONDITIONAL.has(node.element.type) || out.length < 2) {
|
|
1001
|
+
for (const edge of out) {
|
|
1002
|
+
if (edge.condition !== undefined) {
|
|
1003
|
+
fixes.push(`dropped the condition on ${id} > ${edge.to}, which is not a decision`);
|
|
1004
|
+
}
|
|
1005
|
+
edge.condition = undefined;
|
|
1006
|
+
edge.isDefault = undefined;
|
|
1007
|
+
edge.name = undefined;
|
|
1008
|
+
}
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
if (!out.some((edge) => edge.isDefault)) {
|
|
1012
|
+
const plain = out.filter((edge) => edge.condition === undefined);
|
|
1013
|
+
const pool = plain.length > 0 ? plain : out;
|
|
1014
|
+
const otherwise = pool.find((edge) => OTHERWISE.test(edge.name ?? ""));
|
|
1015
|
+
const chosen = otherwise ?? pool[pool.length - 1];
|
|
1016
|
+
if (chosen && otherwise === undefined) {
|
|
1017
|
+
// Read now: the loop below gives every other branch a label.
|
|
1018
|
+
const labels = new Map(out.map((edge) => [edge, edge.name?.split(":")[0]?.trim()]));
|
|
1019
|
+
asks.push({
|
|
1020
|
+
line: node.line,
|
|
1021
|
+
ask: () => {
|
|
1022
|
+
const name = nameOf(id);
|
|
1023
|
+
const label = (edge) => labels.get(edge) || nameOf(edge.to);
|
|
1024
|
+
return {
|
|
1025
|
+
elementId: id,
|
|
1026
|
+
text: `When no condition at "${name}" holds, it takes "${label(chosen)}". Is that the right fallback?`,
|
|
1027
|
+
options: out
|
|
1028
|
+
.filter((edge) => edge !== chosen)
|
|
1029
|
+
.map((edge) => ({
|
|
1030
|
+
label: label(edge),
|
|
1031
|
+
change: `At "${name}", make "${label(edge)}" the default branch.`,
|
|
1032
|
+
})),
|
|
1033
|
+
draft: `At "${name}", when nothing else holds, `,
|
|
1034
|
+
};
|
|
1035
|
+
},
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
if (chosen) {
|
|
1039
|
+
chosen.isDefault = true;
|
|
1040
|
+
if (chosen.condition !== undefined) {
|
|
1041
|
+
chosen.condition = undefined;
|
|
1042
|
+
fixes.push(`made ${id} > ${chosen.to} the default branch of "${id}" instead of its condition`);
|
|
1043
|
+
}
|
|
1044
|
+
else {
|
|
1045
|
+
fixes.push(`made ${id} > ${chosen.to} the default branch of "${id}"`);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
const variable = variableFrom(node.element.name ?? id);
|
|
1050
|
+
let madeUp = false;
|
|
1051
|
+
for (const edge of out) {
|
|
1052
|
+
if (edge.isDefault) {
|
|
1053
|
+
edge.condition = undefined;
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
if (edge.condition === undefined) {
|
|
1057
|
+
// A branch the model described in prose, or not at all: a condition on
|
|
1058
|
+
// a variable named for the question keeps the gateway deployable and
|
|
1059
|
+
// says plainly which variable decides it.
|
|
1060
|
+
const label = (edge.name ?? nodes.get(edge.to)?.element.name ?? edge.to)
|
|
1061
|
+
.split(":")[0]
|
|
1062
|
+
?.trim()
|
|
1063
|
+
.replace(/"/g, "");
|
|
1064
|
+
edge.condition = /^(yes|true)\b/i.test(label ?? "")
|
|
1065
|
+
? `= ${variable} = true`
|
|
1066
|
+
: /^(no|false)\b/i.test(label ?? "")
|
|
1067
|
+
? `= ${variable} = false`
|
|
1068
|
+
: `= ${variable} = "${label}"`;
|
|
1069
|
+
fixes.push(`gave ${id} > ${edge.to} the condition "${edge.condition}"`);
|
|
1070
|
+
madeUp = true;
|
|
1071
|
+
}
|
|
1072
|
+
edge.name ??= edge.condition.replace(/^=\s*/, "");
|
|
1073
|
+
}
|
|
1074
|
+
if (madeUp) {
|
|
1075
|
+
asks.push({
|
|
1076
|
+
line: node.line,
|
|
1077
|
+
ask: () => {
|
|
1078
|
+
const name = nameOf(id);
|
|
1079
|
+
return {
|
|
1080
|
+
elementId: id,
|
|
1081
|
+
text: `"${name}" decides on "${variable}", a variable the diagram made up. What data decides it?`,
|
|
1082
|
+
options: [],
|
|
1083
|
+
draft: `At "${name}", decide on `,
|
|
1084
|
+
};
|
|
1085
|
+
},
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
// Names: every event, task and decision says what it is.
|
|
1090
|
+
let named = 0;
|
|
1091
|
+
for (const [id, node] of nodes) {
|
|
1092
|
+
const { element } = node;
|
|
1093
|
+
if (element.name?.trim())
|
|
1094
|
+
continue;
|
|
1095
|
+
if (element.type === "parallelGateway" || element.type === "eventBasedGateway")
|
|
1096
|
+
continue;
|
|
1097
|
+
if (GATEWAYS.has(element.type) && outOf(id).length < 2)
|
|
1098
|
+
continue;
|
|
1099
|
+
element.name = GATEWAYS.has(element.type) ? `${nameFromId(id)}?` : nameFromId(id);
|
|
1100
|
+
named++;
|
|
1101
|
+
}
|
|
1102
|
+
if (named > 0)
|
|
1103
|
+
fixes.push(`named ${named} unnamed element(s) from their ids`);
|
|
1104
|
+
for (const id of reader.guessedMessage) {
|
|
1105
|
+
const node = nodes.get(id);
|
|
1106
|
+
if (node?.element.eventType !== "message")
|
|
1107
|
+
continue;
|
|
1108
|
+
asks.push({
|
|
1109
|
+
line: node.line,
|
|
1110
|
+
ask: () => {
|
|
1111
|
+
const name = nameOf(id);
|
|
1112
|
+
return {
|
|
1113
|
+
elementId: id,
|
|
1114
|
+
text: `"${name}" was written without a trigger, so it waits for a message. Is it something else?`,
|
|
1115
|
+
options: [
|
|
1116
|
+
{ label: "A timer", change: `"${name}" waits for a timer, not a message.` },
|
|
1117
|
+
{ label: "A signal", change: `"${name}" waits for a signal, not a message.` },
|
|
1118
|
+
{
|
|
1119
|
+
label: "Nothing",
|
|
1120
|
+
change: `"${name}" waits for nothing: make it a plain event, or the end.`,
|
|
1121
|
+
},
|
|
1122
|
+
],
|
|
1123
|
+
draft: `"${name}" waits for `,
|
|
1124
|
+
};
|
|
1125
|
+
},
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
for (const node of nodes.values()) {
|
|
1130
|
+
if (JOB_TASKS.has(node.element.type) && node.element.jobType === undefined) {
|
|
1131
|
+
node.element.jobType = node.element.id;
|
|
1132
|
+
}
|
|
1133
|
+
// A DMN task needs a decision to deploy; the id stands in, as it does for a job type.
|
|
1134
|
+
if (node.element.type === "businessRuleTask" &&
|
|
1135
|
+
node.element.jobType === undefined &&
|
|
1136
|
+
node.element.decisionId === undefined) {
|
|
1137
|
+
node.element.decisionId = node.element.id;
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
const processBase = reader.title ? slugify(reader.title) : "Process_1";
|
|
1141
|
+
const processId = uniqueId(/^[A-Za-z_]/.test(processBase) ? processBase : `Process_${processBase}`, taken);
|
|
1142
|
+
let flowNumber = 0;
|
|
1143
|
+
const flows = edges.map(({ line: _line, ...edge }) => {
|
|
1144
|
+
let id;
|
|
1145
|
+
do
|
|
1146
|
+
id = `Flow_${++flowNumber}`;
|
|
1147
|
+
while (taken.has(id));
|
|
1148
|
+
taken.add(id);
|
|
1149
|
+
const flow = { id, ...edge };
|
|
1150
|
+
for (const key of ["name", "condition", "isDefault"]) {
|
|
1151
|
+
if (flow[key] === undefined)
|
|
1152
|
+
delete flow[key];
|
|
1153
|
+
}
|
|
1154
|
+
return flow;
|
|
1155
|
+
});
|
|
1156
|
+
return {
|
|
1157
|
+
diagram: {
|
|
1158
|
+
id: "Definitions_1",
|
|
1159
|
+
processes: [
|
|
1160
|
+
{
|
|
1161
|
+
id: processId,
|
|
1162
|
+
...(reader.title ? { name: reader.title } : {}),
|
|
1163
|
+
elements: [...nodes.values()].map((node) => node.element),
|
|
1164
|
+
flows,
|
|
1165
|
+
},
|
|
1166
|
+
],
|
|
1167
|
+
},
|
|
1168
|
+
problems: problems.sort((a, b) => a.line - b.line),
|
|
1169
|
+
fixes,
|
|
1170
|
+
questions: asks.sort((a, b) => a.line - b.line).map(({ ask }) => ask()),
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Reads a process written in the line format ({@link PROCESS_TEXT_GUIDE}).
|
|
1175
|
+
*
|
|
1176
|
+
* Never throws. The returned diagram always expands, and keeps the structural
|
|
1177
|
+
* rules `lintDiagram` checks; what could not be used is in `problems`, what was
|
|
1178
|
+
* added or changed to complete the diagram is in `fixes`:
|
|
1179
|
+
*
|
|
1180
|
+
* - flow ids are generated, and a repeated edge is written once;
|
|
1181
|
+
* - a missing start event is added before the first node without an incoming
|
|
1182
|
+
* flow, and a start left unconnected leads there;
|
|
1183
|
+
* - every node lies on a path from a start event: a task or gateway nothing
|
|
1184
|
+
* leads to continues the latest path written before it that stops short of
|
|
1185
|
+
* an end event, and a fragment that still cannot be reached is left out,
|
|
1186
|
+
* never drawn loose;
|
|
1187
|
+
* - an end event is added after every path that stops elsewhere;
|
|
1188
|
+
* - a gateway with one way in and one way out is removed;
|
|
1189
|
+
* - a task or event with several ways out gets a split gateway: xor when the
|
|
1190
|
+
* branches are labelled, and otherwise parallel;
|
|
1191
|
+
* - branches meeting at a task, an event or a splitting gateway are joined
|
|
1192
|
+
* first, by a gateway of the type they were split with;
|
|
1193
|
+
* - every decision has a default branch, and every other branch a FEEL
|
|
1194
|
+
* condition — a prose one becomes a condition on a variable named for the
|
|
1195
|
+
* gateway's question; flows out of anything else carry no condition or label;
|
|
1196
|
+
* - an unnamed event, task or decision is named from its id;
|
|
1197
|
+
* - a service or send task without `job=` uses its id as its job type, and a
|
|
1198
|
+
* rule task its id as its decision id.
|
|
1199
|
+
*
|
|
1200
|
+
* @example
|
|
1201
|
+
* ```typescript
|
|
1202
|
+
* const { diagram, problems } = parseProcessText(modelOutput)
|
|
1203
|
+
* const xml = Bpmn.export(expand(diagram))
|
|
1204
|
+
* ```
|
|
1205
|
+
*/
|
|
1206
|
+
export function parseProcessText(text) {
|
|
1207
|
+
const reader = new Reader();
|
|
1208
|
+
text.split(/\r?\n/).forEach((line, index) => reader.line(line, index + 1));
|
|
1209
|
+
reader.finish();
|
|
1210
|
+
return assemble(reader, true);
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* Reads the line format while it is being written, for a live preview.
|
|
1214
|
+
*
|
|
1215
|
+
* Only complete lines are read, so every frame is built from whole facts. A
|
|
1216
|
+
* frame leaves out edges whose far end has not been declared yet and adds no
|
|
1217
|
+
* start or end events; {@link ProcessTextStream.end} applies the full
|
|
1218
|
+
* {@link parseProcessText} rules.
|
|
1219
|
+
*
|
|
1220
|
+
* @example
|
|
1221
|
+
* ```typescript
|
|
1222
|
+
* const stream = createProcessTextStream()
|
|
1223
|
+
* for await (const chunk of tokens) {
|
|
1224
|
+
* const defs = stream.push(chunk)
|
|
1225
|
+
* if (defs) render(defs)
|
|
1226
|
+
* }
|
|
1227
|
+
* const { diagram, problems } = stream.end()
|
|
1228
|
+
* ```
|
|
1229
|
+
*/
|
|
1230
|
+
export function createProcessTextStream() {
|
|
1231
|
+
const reader = new Reader();
|
|
1232
|
+
let pending = "";
|
|
1233
|
+
let lineNumber = 0;
|
|
1234
|
+
return {
|
|
1235
|
+
push(chunk) {
|
|
1236
|
+
pending += chunk;
|
|
1237
|
+
let changed = false;
|
|
1238
|
+
for (let nl = pending.indexOf("\n"); nl >= 0; nl = pending.indexOf("\n")) {
|
|
1239
|
+
changed = reader.line(pending.slice(0, nl), ++lineNumber) || changed;
|
|
1240
|
+
pending = pending.slice(nl + 1);
|
|
1241
|
+
}
|
|
1242
|
+
if (!changed || reader.nodes.size === 0)
|
|
1243
|
+
return null;
|
|
1244
|
+
return expand(assemble(reader, false).diagram);
|
|
1245
|
+
},
|
|
1246
|
+
end() {
|
|
1247
|
+
if (pending !== "")
|
|
1248
|
+
reader.line(pending, ++lineNumber);
|
|
1249
|
+
reader.finish();
|
|
1250
|
+
pending = "";
|
|
1251
|
+
return assemble(reader, true);
|
|
1252
|
+
},
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
//# sourceMappingURL=process-text.js.map
|