@zerodep-dev/flow-ts-model 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/definition/define.d.ts +32 -0
- package/dist/definition/define.d.ts.map +1 -0
- package/dist/definition/define.js +3 -0
- package/dist/definition/define.js.map +1 -0
- package/dist/definition/definition.d.ts +408 -0
- package/dist/definition/definition.d.ts.map +1 -0
- package/dist/definition/definition.js +32 -0
- package/dist/definition/definition.js.map +1 -0
- package/dist/definition/diff.d.ts +43 -0
- package/dist/definition/diff.d.ts.map +1 -0
- package/dist/definition/diff.js +70 -0
- package/dist/definition/diff.js.map +1 -0
- package/dist/definition/edit.d.ts +56 -0
- package/dist/definition/edit.d.ts.map +1 -0
- package/dist/definition/edit.js +290 -0
- package/dist/definition/edit.js.map +1 -0
- package/dist/definition/gateway-pairs.d.ts +24 -0
- package/dist/definition/gateway-pairs.d.ts.map +1 -0
- package/dist/definition/gateway-pairs.js +133 -0
- package/dist/definition/gateway-pairs.js.map +1 -0
- package/dist/definition/graph.d.ts +24 -0
- package/dist/definition/graph.d.ts.map +1 -0
- package/dist/definition/graph.js +32 -0
- package/dist/definition/graph.js.map +1 -0
- package/dist/definition/schema.d.ts +21 -0
- package/dist/definition/schema.d.ts.map +1 -0
- package/dist/definition/schema.js +33 -0
- package/dist/definition/schema.js.map +1 -0
- package/dist/definition/validate.d.ts +33 -0
- package/dist/definition/validate.d.ts.map +1 -0
- package/dist/definition/validate.js +577 -0
- package/dist/definition/validate.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +62 -0
- package/dist/errors.js.map +1 -0
- package/dist/form/access.d.ts +68 -0
- package/dist/form/access.d.ts.map +1 -0
- package/dist/form/access.js +125 -0
- package/dist/form/access.js.map +1 -0
- package/dist/form/field-values.d.ts +42 -0
- package/dist/form/field-values.d.ts.map +1 -0
- package/dist/form/field-values.js +165 -0
- package/dist/form/field-values.js.map +1 -0
- package/dist/form/form-source.d.ts +8 -0
- package/dist/form/form-source.d.ts.map +1 -0
- package/dist/form/form-source.js +40 -0
- package/dist/form/form-source.js.map +1 -0
- package/dist/form/manifest.d.ts +58 -0
- package/dist/form/manifest.d.ts.map +1 -0
- package/dist/form/manifest.js +22 -0
- package/dist/form/manifest.js.map +1 -0
- package/dist/form/validate-manifest.d.ts +14 -0
- package/dist/form/validate-manifest.d.ts.map +1 -0
- package/dist/form/validate-manifest.js +209 -0
- package/dist/form/validate-manifest.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/project.d.ts +85 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +14 -0
- package/dist/project.js.map +1 -0
- package/dist/routing/condition.d.ts +37 -0
- package/dist/routing/condition.d.ts.map +1 -0
- package/dist/routing/condition.js +136 -0
- package/dist/routing/condition.js.map +1 -0
- package/dist/routing/routing.d.ts +34 -0
- package/dist/routing/routing.d.ts.map +1 -0
- package/dist/routing/routing.js +56 -0
- package/dist/routing/routing.js.map +1 -0
- package/dist/routing/values.d.ts +11 -0
- package/dist/routing/values.d.ts.map +1 -0
- package/dist/routing/values.js +110 -0
- package/dist/routing/values.js.map +1 -0
- package/package.json +50 -3
- package/src/definition/define.ts +38 -0
- package/src/definition/definition.ts +455 -0
- package/src/definition/diff.ts +118 -0
- package/src/definition/edit.ts +369 -0
- package/src/definition/gateway-pairs.ts +159 -0
- package/src/definition/graph.ts +50 -0
- package/src/definition/schema.ts +49 -0
- package/src/definition/validate.ts +684 -0
- package/src/errors.ts +107 -0
- package/src/form/access.ts +204 -0
- package/src/form/field-values.ts +221 -0
- package/src/form/form-source.ts +50 -0
- package/src/form/manifest.ts +88 -0
- package/src/form/validate-manifest.ts +245 -0
- package/src/index.ts +29 -0
- package/src/project.ts +88 -0
- package/src/routing/condition.ts +162 -0
- package/src/routing/routing.ts +73 -0
- package/src/routing/values.ts +132 -0
- package/README.md +0 -3
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 设计器的编辑操作(ROADMAP 14.8a:像低代码一样简单):从模板起步,常见结构一步加上,问题用业务的话说清楚。
|
|
3
|
+
* 都是纯函数:拿一份定义、返回新的一份,不改原来的;设计器与测试共用。
|
|
4
|
+
*
|
|
5
|
+
* let draft = templates.leave();
|
|
6
|
+
* draft = addApprovalStep(draft, 'manager', { name: '总监审批', assignees: [{ role: '3' }] });
|
|
7
|
+
* draft = addAmountBranch(draft, 'manager', { field: 'amount', over: 10000, step: { name: '总经理审批', assignees: [{ user: '1004' }] } });
|
|
8
|
+
* draft = addCcStep(draft, 'manager', { name: '抄送财务', to: [{ role: '2' }] });
|
|
9
|
+
* draft = addCallStep(draft, 'manager', { name: '供应商核实', process: 'vendor-check' });
|
|
10
|
+
* draft = removeStep(draft, 'hr');
|
|
11
|
+
* problemMessage(problem); // → '“财务复核”没有处理人'
|
|
12
|
+
*/
|
|
13
|
+
import type {
|
|
14
|
+
AssigneeSource,
|
|
15
|
+
CallNode,
|
|
16
|
+
CcNode,
|
|
17
|
+
MultiInstance,
|
|
18
|
+
ProcessDefinition,
|
|
19
|
+
ProcessEdge,
|
|
20
|
+
TaskNode,
|
|
21
|
+
} from './definition.ts';
|
|
22
|
+
import type { Problem } from './validate.ts';
|
|
23
|
+
|
|
24
|
+
/** 加一个审批环节时给的 */
|
|
25
|
+
export interface StepInput {
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly assignees: readonly AssigneeSource[];
|
|
28
|
+
/** 会签:几个人一起审 */
|
|
29
|
+
readonly multi?: MultiInstance;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 新的、流程里没用过的标识 */
|
|
33
|
+
export function uniqueId(definition: ProcessDefinition, base: string): string {
|
|
34
|
+
const used = new Set([
|
|
35
|
+
...definition.nodes.map((n) => n.id),
|
|
36
|
+
...definition.edges.map((e) => e.id),
|
|
37
|
+
]);
|
|
38
|
+
if (!used.has(base)) return base;
|
|
39
|
+
for (let n = 2; ; n++) if (!used.has(`${base}${n}`)) return `${base}${n}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 审批环节:同意往下走,不同意(要写意见)到“不通过”的结束 */
|
|
43
|
+
function approvalTask(id: string, input: StepInput): TaskNode {
|
|
44
|
+
return {
|
|
45
|
+
id,
|
|
46
|
+
type: 'task',
|
|
47
|
+
name: input.name,
|
|
48
|
+
assignees: input.assignees,
|
|
49
|
+
...(input.multi ? { multi: input.multi } : {}),
|
|
50
|
+
outcomes: [
|
|
51
|
+
{ key: 'approve', label: '同意' },
|
|
52
|
+
{ key: 'reject', label: '不同意', kind: 'reject', comment: 'required' },
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 不通过的结束节点:已有就用它,没有就加一个 */
|
|
58
|
+
function ensureRejectedEnd(definition: ProcessDefinition): {
|
|
59
|
+
definition: ProcessDefinition;
|
|
60
|
+
id: string;
|
|
61
|
+
} {
|
|
62
|
+
const existing = definition.nodes.find((n) => n.type === 'end' && n.result === 'rejected');
|
|
63
|
+
if (existing) return { definition, id: existing.id };
|
|
64
|
+
const id = uniqueId(definition, 'rejected');
|
|
65
|
+
return {
|
|
66
|
+
definition: {
|
|
67
|
+
...definition,
|
|
68
|
+
nodes: [...definition.nodes, { id, type: 'end', name: '不通过', result: 'rejected' }],
|
|
69
|
+
},
|
|
70
|
+
id,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 节点往下走的连线(退回、提级不算);人工节点只算“通过”的 */
|
|
75
|
+
function forwardOut(definition: ProcessDefinition, node: string): ProcessEdge[] {
|
|
76
|
+
const task = definition.nodes.find((n) => n.id === node);
|
|
77
|
+
const approve =
|
|
78
|
+
task?.type === 'task'
|
|
79
|
+
? task.outcomes.find((o) => (o.kind ?? 'approve') === 'approve')?.key
|
|
80
|
+
: undefined;
|
|
81
|
+
return definition.edges.filter(
|
|
82
|
+
(e) =>
|
|
83
|
+
e.from === node &&
|
|
84
|
+
e.kind !== 'return' &&
|
|
85
|
+
e.kind !== 'escalate' &&
|
|
86
|
+
(approve === undefined || e.outcome === undefined || e.outcome === approve),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 在 after 之后加一个审批环节:after 原来往下走的连线改连到新环节,新环节同意后接着原来的路 */
|
|
91
|
+
export function addApprovalStep(
|
|
92
|
+
definition: ProcessDefinition,
|
|
93
|
+
after: string,
|
|
94
|
+
input: StepInput,
|
|
95
|
+
): ProcessDefinition {
|
|
96
|
+
const id = uniqueId(definition, 'step');
|
|
97
|
+
const outgoing = forwardOut(definition, after);
|
|
98
|
+
const { definition: withEnd, id: rejected } = ensureRejectedEnd(definition);
|
|
99
|
+
const moved = new Set(outgoing.map((e) => e.id));
|
|
100
|
+
const edges = withEnd.edges.map((e) => (moved.has(e.id) ? { ...e, to: id } : e));
|
|
101
|
+
const continued = outgoing.map((e, index) => ({
|
|
102
|
+
id: uniqueId({ ...withEnd, edges }, `${id}-next${index === 0 ? '' : index + 1}`),
|
|
103
|
+
from: id,
|
|
104
|
+
to: e.to,
|
|
105
|
+
outcome: 'approve',
|
|
106
|
+
}));
|
|
107
|
+
return {
|
|
108
|
+
...withEnd,
|
|
109
|
+
nodes: [...withEnd.nodes, approvalTask(id, input)],
|
|
110
|
+
edges: [
|
|
111
|
+
...edges,
|
|
112
|
+
...continued,
|
|
113
|
+
{
|
|
114
|
+
id: uniqueId({ ...withEnd, edges }, `${id}-reject`),
|
|
115
|
+
from: id,
|
|
116
|
+
to: rejected,
|
|
117
|
+
outcome: 'reject',
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 在 after 之后插一个直通的环节(调用流程、抄送):after 原来往下走的连线改连到它,它再接着原来的路 */
|
|
124
|
+
function insertPassThrough(
|
|
125
|
+
definition: ProcessDefinition,
|
|
126
|
+
after: string,
|
|
127
|
+
node: CallNode | CcNode,
|
|
128
|
+
): ProcessDefinition {
|
|
129
|
+
const outgoing = forwardOut(definition, after);
|
|
130
|
+
const moved = new Set(outgoing.map((e) => e.id));
|
|
131
|
+
const edges = definition.edges.map((e) => (moved.has(e.id) ? { ...e, to: node.id } : e));
|
|
132
|
+
const targets = [...new Set(outgoing.map((e) => e.to))];
|
|
133
|
+
const continued = targets.map((to, index) => ({
|
|
134
|
+
id: uniqueId({ ...definition, edges }, `${node.id}-next${index === 0 ? '' : index + 1}`),
|
|
135
|
+
from: node.id,
|
|
136
|
+
to,
|
|
137
|
+
}));
|
|
138
|
+
return { ...definition, nodes: [...definition.nodes, node], edges: [...edges, ...continued] };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 在 after 之后调用另一个流程(同步:等子流程结束,按它的结果接着原来的路) */
|
|
142
|
+
export function addCallStep(
|
|
143
|
+
definition: ProcessDefinition,
|
|
144
|
+
after: string,
|
|
145
|
+
input: { readonly name: string; readonly process: string },
|
|
146
|
+
): ProcessDefinition {
|
|
147
|
+
const id = uniqueId(definition, 'call');
|
|
148
|
+
return insertPassThrough(definition, after, { id, type: 'call', mode: 'sync', ...input });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 在 after 之后抄送:抄给 to 里的人(不用处理),流程接着原来的路 */
|
|
152
|
+
export function addCcStep(
|
|
153
|
+
definition: ProcessDefinition,
|
|
154
|
+
after: string,
|
|
155
|
+
input: {
|
|
156
|
+
readonly name: string;
|
|
157
|
+
readonly to: readonly AssigneeSource[];
|
|
158
|
+
readonly message?: string;
|
|
159
|
+
},
|
|
160
|
+
): ProcessDefinition {
|
|
161
|
+
const id = uniqueId(definition, 'cc');
|
|
162
|
+
return insertPassThrough(definition, after, { id, type: 'cc', ...input });
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** 在 after 之后按金额(或别的数字)分支:超过 over 的多走一个审批环节,其余照原来的路 */
|
|
166
|
+
export function addAmountBranch(
|
|
167
|
+
definition: ProcessDefinition,
|
|
168
|
+
after: string,
|
|
169
|
+
input: { readonly field: string; readonly over: number; readonly step: StepInput },
|
|
170
|
+
): ProcessDefinition {
|
|
171
|
+
const gateway = uniqueId(definition, 'branch');
|
|
172
|
+
const outgoing = forwardOut(definition, after);
|
|
173
|
+
if (outgoing.length !== 1) {
|
|
174
|
+
throw new Error(`“${after}”往下走的路不止一条,先选其中一条路上的环节再加分支`);
|
|
175
|
+
}
|
|
176
|
+
const next = outgoing[0]!;
|
|
177
|
+
let draft: ProcessDefinition = {
|
|
178
|
+
...definition,
|
|
179
|
+
nodes: [
|
|
180
|
+
...definition.nodes,
|
|
181
|
+
{ id: gateway, type: 'exclusive', name: `${input.field} 超过 ${input.over}?` },
|
|
182
|
+
],
|
|
183
|
+
edges: [
|
|
184
|
+
...definition.edges.map((e) => (e.id === next.id ? { ...e, to: gateway } : e)),
|
|
185
|
+
{
|
|
186
|
+
id: uniqueId(definition, `${gateway}-small`),
|
|
187
|
+
from: gateway,
|
|
188
|
+
to: next.to,
|
|
189
|
+
default: true,
|
|
190
|
+
label: '其余',
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
};
|
|
194
|
+
// 超过的那条:先连到原来的下一步,再在它前面插审批环节
|
|
195
|
+
const large = uniqueId(draft, `${gateway}-large`);
|
|
196
|
+
draft = {
|
|
197
|
+
...draft,
|
|
198
|
+
edges: [
|
|
199
|
+
...draft.edges,
|
|
200
|
+
{
|
|
201
|
+
id: large,
|
|
202
|
+
from: gateway,
|
|
203
|
+
to: next.to,
|
|
204
|
+
when: { field: input.field, op: 'gt', value: input.over },
|
|
205
|
+
label: `超过 ${input.over}`,
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
};
|
|
209
|
+
const step = uniqueId(draft, 'step');
|
|
210
|
+
const { definition: withEnd, id: rejected } = ensureRejectedEnd(draft);
|
|
211
|
+
return {
|
|
212
|
+
...withEnd,
|
|
213
|
+
nodes: [...withEnd.nodes, approvalTask(step, input.step)],
|
|
214
|
+
edges: [
|
|
215
|
+
...withEnd.edges.map((e) => (e.id === large ? { ...e, to: step } : e)),
|
|
216
|
+
{ id: uniqueId(withEnd, `${step}-next`), from: step, to: next.to, outcome: 'approve' },
|
|
217
|
+
{ id: uniqueId(withEnd, `${step}-reject`), from: step, to: rejected, outcome: 'reject' },
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** 删掉一个环节:连到它的路改连到它往下走的那一步,它自己的连线一起删掉 */
|
|
223
|
+
export function removeStep(definition: ProcessDefinition, id: string): ProcessDefinition {
|
|
224
|
+
const node = definition.nodes.find((n) => n.id === id);
|
|
225
|
+
if (!node || node.type === 'start' || node.type === 'end') {
|
|
226
|
+
throw new Error('开始与结束不能删');
|
|
227
|
+
}
|
|
228
|
+
const next = forwardOut(definition, id)[0]?.to;
|
|
229
|
+
const edges = definition.edges
|
|
230
|
+
.filter((e) => e.from !== id && !(e.to === id && next === undefined))
|
|
231
|
+
.map((e) => (e.to === id ? { ...e, to: next! } : e));
|
|
232
|
+
return { ...definition, nodes: definition.nodes.filter((n) => n.id !== id), edges };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 问题的业务说法;没有专门说法的给出代号 */
|
|
236
|
+
export function problemMessage(problem: Problem, definition?: ProcessDefinition): string {
|
|
237
|
+
const name = (id: string | undefined) =>
|
|
238
|
+
id ? `“${definition?.nodes.find((n) => n.id === id)?.name ?? id}”` : '这一步';
|
|
239
|
+
const node = name(problem.node);
|
|
240
|
+
const params = problem.params ?? {};
|
|
241
|
+
switch (problem.code) {
|
|
242
|
+
case 'start-missing':
|
|
243
|
+
return '流程还没有开始节点';
|
|
244
|
+
case 'end-missing':
|
|
245
|
+
return '流程还没有结束节点:走到最后要有个结果';
|
|
246
|
+
case 'assignee-missing':
|
|
247
|
+
return `${node}没有处理人:选发起人、角色或指定的人`;
|
|
248
|
+
case 'task-no-outcome':
|
|
249
|
+
return `${node}没有处理按钮:至少要有“同意”`;
|
|
250
|
+
case 'outcome-unrouted':
|
|
251
|
+
return `${node}的“${String(params['outcome'] ?? '')}”按钮按下后不知道去哪:给它连一条线`;
|
|
252
|
+
case 'node-unreachable':
|
|
253
|
+
return `${node}走不到:从开始没有路通到它`;
|
|
254
|
+
case 'node-dead-end':
|
|
255
|
+
case 'node-no-outgoing':
|
|
256
|
+
return `${node}走到这里就停了:给它连到下一步或结束`;
|
|
257
|
+
case 'gateway-unpaired':
|
|
258
|
+
return `${node}分出去的几条路没有汇到一起`;
|
|
259
|
+
case 'condition-incomplete':
|
|
260
|
+
return `${node}后面的条件没写完整`;
|
|
261
|
+
case 'edge-unknown-node':
|
|
262
|
+
return '有一条线连到了不存在的环节';
|
|
263
|
+
case 'outcome-duplicate':
|
|
264
|
+
return `${node}有两个一样的按钮`;
|
|
265
|
+
case 'multi-no-fallback':
|
|
266
|
+
return `${node}按比例会签时要写“都办完还没达到比例按哪个结果”`;
|
|
267
|
+
case 'logic.missing': {
|
|
268
|
+
// 引擎发布时查的:定义里写了 logic.名字,logic.ts 的那一类里没有
|
|
269
|
+
const kinds: Record<string, string> = {
|
|
270
|
+
resolvers: '处理人解析器',
|
|
271
|
+
guards: '守卫',
|
|
272
|
+
actions: '动作',
|
|
273
|
+
functions: '计算值',
|
|
274
|
+
};
|
|
275
|
+
const kind = String(params['kind'] ?? '');
|
|
276
|
+
return `logic.ts 里没有${kinds[kind] ?? kind}“${String(params['name'] ?? '')}”:在 defineLogic 的 ${kind} 里加上,或改掉定义里的引用`;
|
|
277
|
+
}
|
|
278
|
+
default:
|
|
279
|
+
return `${node}:${problem.code}`;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** 模板:一键生成再改 */
|
|
284
|
+
export const templates = {
|
|
285
|
+
/** 请假:经理审批,超过 3 天再给人事 */
|
|
286
|
+
leave: (): ProcessDefinition =>
|
|
287
|
+
addAmountBranch(
|
|
288
|
+
{
|
|
289
|
+
key: 'leave',
|
|
290
|
+
name: '请假',
|
|
291
|
+
title: '请假:{reason}',
|
|
292
|
+
variables: [
|
|
293
|
+
{ name: 'days', type: 'number', label: '天数' },
|
|
294
|
+
{ name: 'reason', type: 'text', label: '事由' },
|
|
295
|
+
],
|
|
296
|
+
nodes: [
|
|
297
|
+
{ id: 'start', type: 'start', name: '提交' },
|
|
298
|
+
approvalTask('manager', { name: '经理审批', assignees: [{ role: '1', name: '经理' }] }),
|
|
299
|
+
{ id: 'approved', type: 'end', name: '通过', result: 'approved' },
|
|
300
|
+
{ id: 'rejected', type: 'end', name: '不通过', result: 'rejected' },
|
|
301
|
+
],
|
|
302
|
+
edges: [
|
|
303
|
+
{ id: 'submit', from: 'start', to: 'manager' },
|
|
304
|
+
{ id: 'manager-next', from: 'manager', to: 'approved', outcome: 'approve' },
|
|
305
|
+
{ id: 'manager-reject', from: 'manager', to: 'rejected', outcome: 'reject' },
|
|
306
|
+
],
|
|
307
|
+
},
|
|
308
|
+
'manager',
|
|
309
|
+
{
|
|
310
|
+
field: 'days',
|
|
311
|
+
over: 3,
|
|
312
|
+
step: { name: '人事备案', assignees: [{ role: '2', name: '人事' }] },
|
|
313
|
+
},
|
|
314
|
+
),
|
|
315
|
+
/** 采购:部门经理审批,金额超过一万再给总经理 */
|
|
316
|
+
purchase: (): ProcessDefinition =>
|
|
317
|
+
addAmountBranch(
|
|
318
|
+
{
|
|
319
|
+
key: 'purchase',
|
|
320
|
+
name: '采购申请',
|
|
321
|
+
title: '采购:{title}',
|
|
322
|
+
variables: [
|
|
323
|
+
{ name: 'title', type: 'text', label: '采购什么' },
|
|
324
|
+
{ name: 'amount', type: 'number', label: '金额' },
|
|
325
|
+
],
|
|
326
|
+
nodes: [
|
|
327
|
+
{ id: 'start', type: 'start', name: '提交' },
|
|
328
|
+
approvalTask('manager', {
|
|
329
|
+
name: '部门经理审批',
|
|
330
|
+
assignees: [{ role: '1', name: '经理' }],
|
|
331
|
+
}),
|
|
332
|
+
{ id: 'approved', type: 'end', name: '通过', result: 'approved' },
|
|
333
|
+
{ id: 'rejected', type: 'end', name: '不通过', result: 'rejected' },
|
|
334
|
+
],
|
|
335
|
+
edges: [
|
|
336
|
+
{ id: 'submit', from: 'start', to: 'manager' },
|
|
337
|
+
{ id: 'manager-next', from: 'manager', to: 'approved', outcome: 'approve' },
|
|
338
|
+
{ id: 'manager-reject', from: 'manager', to: 'rejected', outcome: 'reject' },
|
|
339
|
+
],
|
|
340
|
+
},
|
|
341
|
+
'manager',
|
|
342
|
+
{
|
|
343
|
+
field: 'amount',
|
|
344
|
+
over: 10000,
|
|
345
|
+
step: { name: '总经理审批', assignees: [{ role: '4', name: '总经理' }] },
|
|
346
|
+
},
|
|
347
|
+
),
|
|
348
|
+
/** 报销:财务复核(财务收件箱里认领) */
|
|
349
|
+
expense: (): ProcessDefinition => ({
|
|
350
|
+
key: 'expense',
|
|
351
|
+
name: '报销',
|
|
352
|
+
title: '报销:{title}',
|
|
353
|
+
variables: [
|
|
354
|
+
{ name: 'title', type: 'text', label: '事由' },
|
|
355
|
+
{ name: 'amount', type: 'number', label: '金额' },
|
|
356
|
+
],
|
|
357
|
+
nodes: [
|
|
358
|
+
{ id: 'start', type: 'start', name: '提交' },
|
|
359
|
+
approvalTask('finance', { name: '财务复核', assignees: [{ role: '2', name: '财务' }] }),
|
|
360
|
+
{ id: 'approved', type: 'end', name: '通过', result: 'approved' },
|
|
361
|
+
{ id: 'rejected', type: 'end', name: '不通过', result: 'rejected' },
|
|
362
|
+
],
|
|
363
|
+
edges: [
|
|
364
|
+
{ id: 'submit', from: 'start', to: 'finance' },
|
|
365
|
+
{ id: 'finance-next', from: 'finance', to: 'approved', outcome: 'approve' },
|
|
366
|
+
{ id: 'finance-reject', from: 'finance', to: 'rejected', outcome: 'reject' },
|
|
367
|
+
],
|
|
368
|
+
}),
|
|
369
|
+
} as const;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 并行、包容网关的分叉与汇合配对(0005 的 4.4,结构化):每个分叉有一个汇合,中间的分支不跳出去。
|
|
3
|
+
*
|
|
4
|
+
* 按前进图(去掉退回、提级连线)算:汇合是分叉的直接后支配节点,分叉是汇合的直接支配节点,两者同类型。
|
|
5
|
+
* 校验用它报 `gateway-unpaired`,引擎用它在分叉时记下"汇合要等几条分支"、在退回跳出区域时找出要取消的分支。
|
|
6
|
+
* 与 Java 的 `GatewayPairs` 同一套算法。
|
|
7
|
+
*/
|
|
8
|
+
import type { ProcessDefinition } from './definition.ts';
|
|
9
|
+
import { processGraph } from './graph.ts';
|
|
10
|
+
|
|
11
|
+
/** 一份定义的配对 */
|
|
12
|
+
export interface GatewayPairs {
|
|
13
|
+
/** 分叉配对的汇合;不是分叉、或没配上对时为 undefined */
|
|
14
|
+
joinOf(fork: string): string | undefined;
|
|
15
|
+
/** 汇合配对的分叉 */
|
|
16
|
+
forkOf(join: string): string | undefined;
|
|
17
|
+
/** 没配上对的并行、包容网关(按定义里的先后) */
|
|
18
|
+
readonly unpaired: readonly string[];
|
|
19
|
+
/** 分叉与汇合之间的节点(不含两头) */
|
|
20
|
+
region(fork: string): ReadonlySet<string>;
|
|
21
|
+
/** 节点在哪些区域里,由里到外(嵌套的并行) */
|
|
22
|
+
forksAround(node: string): string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 结束节点都连到这个虚拟的出口,后支配树才有唯一的根 */
|
|
26
|
+
const EXIT = '\u0000exit';
|
|
27
|
+
|
|
28
|
+
const cache = new WeakMap<ProcessDefinition, GatewayPairs>();
|
|
29
|
+
|
|
30
|
+
/** 算出一份定义的配对(同一个定义对象只算一次) */
|
|
31
|
+
export function gatewayPairs(definition: ProcessDefinition): GatewayPairs {
|
|
32
|
+
const cached = cache.get(definition);
|
|
33
|
+
if (cached) return cached;
|
|
34
|
+
const graph = processGraph(definition);
|
|
35
|
+
const succ = new Map<string, string[]>();
|
|
36
|
+
const pred = new Map<string, string[]>();
|
|
37
|
+
const link = (map: Map<string, string[]>, from: string, to: string) =>
|
|
38
|
+
(map.get(from) ?? map.set(from, []).get(from)!).push(to);
|
|
39
|
+
for (const e of definition.edges) {
|
|
40
|
+
if (e.kind === 'return' || e.kind === 'escalate' || !graph.node(e.from) || !graph.node(e.to))
|
|
41
|
+
continue;
|
|
42
|
+
link(succ, e.from, e.to);
|
|
43
|
+
link(pred, e.to, e.from);
|
|
44
|
+
}
|
|
45
|
+
for (const node of definition.nodes) {
|
|
46
|
+
if (node.type !== 'end') continue;
|
|
47
|
+
link(succ, node.id, EXIT);
|
|
48
|
+
link(pred, EXIT, node.id);
|
|
49
|
+
}
|
|
50
|
+
const successors = (id: string) => succ.get(id) ?? [];
|
|
51
|
+
const predecessors = (id: string) => pred.get(id) ?? [];
|
|
52
|
+
|
|
53
|
+
const joins = new Map<string, string>();
|
|
54
|
+
const forks = new Map<string, string>();
|
|
55
|
+
const unpaired: string[] = [];
|
|
56
|
+
const regions = new Map<string, ReadonlySet<string>>();
|
|
57
|
+
if (graph.start) {
|
|
58
|
+
const idom = immediateDominators(graph.start.id, successors, predecessors);
|
|
59
|
+
const ipdom = immediateDominators(EXIT, predecessors, successors);
|
|
60
|
+
const type = (id: string | undefined) => (id === undefined ? undefined : graph.node(id)?.type);
|
|
61
|
+
const paired = (fork: string, join: string | undefined) =>
|
|
62
|
+
join !== undefined &&
|
|
63
|
+
join !== EXIT &&
|
|
64
|
+
type(join) === type(fork) &&
|
|
65
|
+
predecessors(join).length > 1 &&
|
|
66
|
+
idom.get(join) === fork;
|
|
67
|
+
for (const node of definition.nodes) {
|
|
68
|
+
if (node.type !== 'parallel' && node.type !== 'inclusive') continue;
|
|
69
|
+
if (!idom.has(node.id) || !ipdom.has(node.id)) continue;
|
|
70
|
+
if (successors(node.id).length > 1) {
|
|
71
|
+
const join = ipdom.get(node.id);
|
|
72
|
+
if (paired(node.id, join)) {
|
|
73
|
+
joins.set(node.id, join!);
|
|
74
|
+
forks.set(join!, node.id);
|
|
75
|
+
regions.set(node.id, between(node.id, join!, successors));
|
|
76
|
+
} else {
|
|
77
|
+
unpaired.push(node.id);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (predecessors(node.id).length > 1) {
|
|
81
|
+
const fork = idom.get(node.id);
|
|
82
|
+
if (fork === undefined || !paired(fork, node.id) || ipdom.get(fork) !== node.id)
|
|
83
|
+
unpaired.push(node.id);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const pairs: GatewayPairs = {
|
|
88
|
+
joinOf: (fork) => joins.get(fork),
|
|
89
|
+
forkOf: (join) => forks.get(join),
|
|
90
|
+
unpaired: [...new Set(unpaired)],
|
|
91
|
+
region: (fork) => regions.get(fork) ?? new Set(),
|
|
92
|
+
forksAround: (node) =>
|
|
93
|
+
[...regions.entries()]
|
|
94
|
+
.filter(([, region]) => region.has(node))
|
|
95
|
+
.sort(([, a], [, b]) => a.size - b.size)
|
|
96
|
+
.map(([fork]) => fork),
|
|
97
|
+
};
|
|
98
|
+
cache.set(definition, pairs);
|
|
99
|
+
return pairs;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 从分叉往前走、不过汇合能到的节点 */
|
|
103
|
+
function between(
|
|
104
|
+
fork: string,
|
|
105
|
+
join: string,
|
|
106
|
+
successors: (id: string) => readonly string[],
|
|
107
|
+
): ReadonlySet<string> {
|
|
108
|
+
const seen = new Set<string>();
|
|
109
|
+
const queue = [...successors(fork)];
|
|
110
|
+
while (queue.length) {
|
|
111
|
+
const id = queue.shift()!;
|
|
112
|
+
if (id === join || id === EXIT || seen.has(id)) continue;
|
|
113
|
+
seen.add(id);
|
|
114
|
+
queue.push(...successors(id));
|
|
115
|
+
}
|
|
116
|
+
return seen;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 直接支配节点(Cooper–Harvey–Kennedy 的迭代算法);root 映射到自己,走不到的节点不在结果里 */
|
|
120
|
+
function immediateDominators(
|
|
121
|
+
root: string,
|
|
122
|
+
successors: (id: string) => readonly string[],
|
|
123
|
+
predecessors: (id: string) => readonly string[],
|
|
124
|
+
): Map<string, string> {
|
|
125
|
+
const order: string[] = [];
|
|
126
|
+
const visited = new Set<string>();
|
|
127
|
+
const visit = (id: string) => {
|
|
128
|
+
visited.add(id);
|
|
129
|
+
for (const next of successors(id)) if (!visited.has(next)) visit(next);
|
|
130
|
+
order.push(id);
|
|
131
|
+
};
|
|
132
|
+
visit(root);
|
|
133
|
+
const rpo = order.reverse();
|
|
134
|
+
const index = new Map(rpo.map((id, i) => [id, i]));
|
|
135
|
+
const idom = new Map<string, string>([[root, root]]);
|
|
136
|
+
const intersect = (a: string, b: string): string => {
|
|
137
|
+
let x = a;
|
|
138
|
+
let y = b;
|
|
139
|
+
while (x !== y) {
|
|
140
|
+
while (index.get(x)! > index.get(y)!) x = idom.get(x)!;
|
|
141
|
+
while (index.get(y)! > index.get(x)!) y = idom.get(y)!;
|
|
142
|
+
}
|
|
143
|
+
return x;
|
|
144
|
+
};
|
|
145
|
+
for (let changed = true; changed;) {
|
|
146
|
+
changed = false;
|
|
147
|
+
for (const id of rpo) {
|
|
148
|
+
if (id === root) continue;
|
|
149
|
+
const preds = predecessors(id).filter((p) => idom.has(p));
|
|
150
|
+
if (preds.length === 0) continue;
|
|
151
|
+
const next = preds.slice(1).reduce(intersect, preds[0]!);
|
|
152
|
+
if (idom.get(id) !== next) {
|
|
153
|
+
idom.set(id, next);
|
|
154
|
+
changed = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return idom;
|
|
159
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 流程的结构索引:按标识找节点、泳道,按节点找出边、入边。同一个定义对象只建一次(定义发布后不可变)。
|
|
3
|
+
*
|
|
4
|
+
* const graph = processGraph(definition);
|
|
5
|
+
* graph.outgoing('check'); // 从"资料审核"出去的连线,按定义的先后
|
|
6
|
+
*/
|
|
7
|
+
import type { Lane, ProcessDefinition, ProcessEdge, ProcessNode, StartNode } from './definition.ts';
|
|
8
|
+
|
|
9
|
+
/** 流程的结构索引 */
|
|
10
|
+
export interface ProcessGraph {
|
|
11
|
+
readonly definition: ProcessDefinition;
|
|
12
|
+
/** 按标识找节点;标识重复时取第一个 */
|
|
13
|
+
node(id: string): ProcessNode | undefined;
|
|
14
|
+
/** 按标识找泳道 */
|
|
15
|
+
lane(id: string | undefined): Lane | undefined;
|
|
16
|
+
/** 从这个节点出去的连线,按定义的先后 */
|
|
17
|
+
outgoing(id: string): readonly ProcessEdge[];
|
|
18
|
+
/** 进这个节点的连线 */
|
|
19
|
+
incoming(id: string): readonly ProcessEdge[];
|
|
20
|
+
/** 开始节点;没有时为 undefined */
|
|
21
|
+
readonly start: StartNode | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const graphs = new WeakMap<ProcessDefinition, ProcessGraph>();
|
|
25
|
+
|
|
26
|
+
/** 流程的结构索引(同一个定义对象只建一次) */
|
|
27
|
+
export function processGraph(definition: ProcessDefinition): ProcessGraph {
|
|
28
|
+
const cached = graphs.get(definition);
|
|
29
|
+
if (cached) return cached;
|
|
30
|
+
const nodes = new Map<string, ProcessNode>();
|
|
31
|
+
for (const node of definition.nodes) if (!nodes.has(node.id)) nodes.set(node.id, node);
|
|
32
|
+
const lanes = new Map<string, Lane>();
|
|
33
|
+
for (const lane of definition.lanes ?? []) if (!lanes.has(lane.id)) lanes.set(lane.id, lane);
|
|
34
|
+
const outgoing = new Map<string, ProcessEdge[]>();
|
|
35
|
+
const incoming = new Map<string, ProcessEdge[]>();
|
|
36
|
+
for (const edge of definition.edges) {
|
|
37
|
+
(outgoing.get(edge.from) ?? outgoing.set(edge.from, []).get(edge.from)!).push(edge);
|
|
38
|
+
(incoming.get(edge.to) ?? incoming.set(edge.to, []).get(edge.to)!).push(edge);
|
|
39
|
+
}
|
|
40
|
+
const graph: ProcessGraph = {
|
|
41
|
+
definition,
|
|
42
|
+
node: (id) => nodes.get(id),
|
|
43
|
+
lane: (id) => (id == null ? undefined : lanes.get(id)),
|
|
44
|
+
outgoing: (id) => outgoing.get(id) ?? [],
|
|
45
|
+
incoming: (id) => incoming.get(id) ?? [],
|
|
46
|
+
start: definition.nodes.find((node): node is StartNode => node.type === 'start'),
|
|
47
|
+
};
|
|
48
|
+
graphs.set(definition, graph);
|
|
49
|
+
return graph;
|
|
50
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 流程定义 JSON 的格式版本(定义里的 `schema`,0005 第 1 节),同 Java 的 `DefinitionSchema`:已发布的版本存的是发布时的原样,
|
|
3
|
+
* 读之前按 STEPS 一步步升到 DEFINITION_SCHEMA。不写 `schema` 的是第 1 版。
|
|
4
|
+
*
|
|
5
|
+
* 改定义格式(改名、换结构)时:DEFINITION_SCHEMA 加一,STEPS 末尾加一步"上一版 → 这一版",Java 那边同样加一步,
|
|
6
|
+
* 规格用例(spec/definitions)加一份旧格式的定义。只加字段、不改原有字段的意思时不用加版本。
|
|
7
|
+
*
|
|
8
|
+
* const definition = parseDefinition(json); // 旧格式升到当前格式;比当前还新时抛错
|
|
9
|
+
*/
|
|
10
|
+
import type { ProcessDefinition } from './definition.ts';
|
|
11
|
+
|
|
12
|
+
/** 这一版的模型包写的格式 */
|
|
13
|
+
export const DEFINITION_SCHEMA = 1;
|
|
14
|
+
|
|
15
|
+
type Tree = Record<string, unknown>;
|
|
16
|
+
|
|
17
|
+
/** 第 i 个是第 i+1 版 → 第 i+2 版;现在只有第 1 版,没有要升的 */
|
|
18
|
+
const STEPS: readonly ((tree: Tree) => Tree)[] = [];
|
|
19
|
+
|
|
20
|
+
/** 定义写的是第几版:没写时是 1;写的不是正整数时抛错 */
|
|
21
|
+
export function definitionSchemaOf(tree: Readonly<Tree>): number {
|
|
22
|
+
const schema = tree['schema'];
|
|
23
|
+
if (schema === undefined) return 1;
|
|
24
|
+
if (typeof schema !== 'number' || !Number.isInteger(schema) || schema < 1)
|
|
25
|
+
throw new Error('$.schema: must be a positive integer');
|
|
26
|
+
return schema;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 升到当前的格式:返回新的一份(`schema` 是 DEFINITION_SCHEMA),原来的不改;不是对象、比当前还新时抛错 */
|
|
30
|
+
export function upgradeDefinition(raw: unknown): ProcessDefinition {
|
|
31
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw))
|
|
32
|
+
throw new Error('$: a process definition must be an object');
|
|
33
|
+
const version = definitionSchemaOf(raw as Tree);
|
|
34
|
+
if (version > DEFINITION_SCHEMA) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`$.schema: format ${version} is newer than this version understands (${DEFINITION_SCHEMA}); upgrade flow`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
if (version === DEFINITION_SCHEMA) return raw as ProcessDefinition;
|
|
40
|
+
const upgraded = STEPS.slice(version - 1).reduce((tree, step) => step(tree), {
|
|
41
|
+
...(raw as Tree),
|
|
42
|
+
});
|
|
43
|
+
return { ...upgraded, schema: DEFINITION_SCHEMA } as unknown as ProcessDefinition;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 读一段定义 JSON:JSON.parse 之后升到当前格式 */
|
|
47
|
+
export function parseDefinition(json: string): ProcessDefinition {
|
|
48
|
+
return upgradeDefinition(JSON.parse(json));
|
|
49
|
+
}
|