-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepnames.ts
447 lines (387 loc) · 11.1 KB
/
stepnames.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import {
AssignStepAST,
ForStepASTNamed,
NamedWorkflowStep,
NextStepAST,
ParallelStepASTNamed,
StepName,
StepsStepASTNamed,
SwitchStepASTNamed,
TryStepASTNamed,
WorkflowAST,
WorkflowStepASTWithNamedNested,
nestedSteps,
} from './steps.js'
import { Subworkflow, WorkflowApp } from './workflows.js'
interface JumpStackElement {
namedStep: NamedWorkflowStep
nestingLevel: number
isLastInBlock: boolean
}
export class StepNameGenerator {
private counters: Map<string, number>
constructor() {
this.counters = new Map<string, number>()
}
generate(prefix: string): string {
const i = this.counters.get(prefix) ?? 1
this.counters.set(prefix, i + 1)
return `${prefix}${i}`
}
}
export function generateStepNames(ast: WorkflowAST): WorkflowApp {
const stepNameGenerator = new StepNameGenerator()
const subworkflows = ast.subworkflows
.map((subworkflow) => {
return subworkflow.withStepNames((x) => stepNameGenerator.generate(x))
})
.map(fixJumpLabels)
return new WorkflowApp(subworkflows)
}
function fixJumpLabels(subworkflow: Subworkflow): Subworkflow {
const jumpTargetLabels = collectActualJumpTargets(subworkflow)
const stepsWithoutJumpTargetNodes = removeJumpTargetSteps(subworkflow.steps)
const relabeledSteps = relabelNextLabels(
stepsWithoutJumpTargetNodes,
jumpTargetLabels,
)
return new Subworkflow(subworkflow.name, relabeledSteps, subworkflow.params)
}
/**
* Find a mapping from jump target labels to step names.
*
* Iterate over all steps in the workflow. For JumpTargetAST nodes, find the
* next node that is not a JumpTargetAST, save its name as the real jump taget
* name.
*/
function collectActualJumpTargets(
subworkflow: Subworkflow,
): Map<StepName, StepName> {
const replacements = new Map<StepName, StepName>()
// The processing is done iteratively with an explicit stack because
// nextNonJumpTargetNode() needs the stack. Note the order of steps on the
// stack: the first step is the last element of the stack.
const stack: JumpStackElement[] = []
stack.push(...stepsToJumpStackElements(subworkflow.steps, 0))
while (stack.length > 0) {
// Do not yet pop in case nextNonJumpTargetNode needs to search the stack
const { namedStep, nestingLevel } = stack[stack.length - 1]
if (namedStep.step.tag === 'jumptarget') {
const currentLabel = namedStep.step.label
const target = nextNonJumpTargetNode(stack)
const targetName = target ? target.name : 'end'
replacements.set(currentLabel, targetName)
}
// Now nextNonJumpTargetNode has been executed and it's safe to pop the
// current element from the stack.
stack.pop()
const children = nestedSteps(namedStep.step).map((x) =>
stepsToJumpStackElements(x, nestingLevel + 1),
)
children.reverse()
children.forEach((children) => {
stack.push(...children)
})
}
return replacements
}
function stepsToJumpStackElements(
steps: NamedWorkflowStep[],
nestingLevel: number,
): JumpStackElement[] {
const block = steps.map((step, i) => ({
namedStep: step,
nestingLevel,
isLastInBlock: i === steps.length - 1,
}))
block.reverse()
return block
}
function nextNonJumpTargetNode(
stack: readonly JumpStackElement[],
): NamedWorkflowStep | undefined {
if (stack.length <= 0) {
return undefined
}
let nestingLevel = stack[stack.length - 1].nestingLevel
while (nestingLevel >= 0) {
// Consider only the steps in the current code block (= the same nesting
// level, taking steps until isLastInBlock)
let endOfBlockIndex = stack.findLastIndex(
(x) => x.nestingLevel === nestingLevel && x.isLastInBlock,
)
if (endOfBlockIndex < 0) {
// should not be reached
endOfBlockIndex = stack.findLastIndex(
(x) => x.nestingLevel <= nestingLevel,
)
if (endOfBlockIndex < 0) {
endOfBlockIndex = 0
}
}
const firstNonJumpTarget = stack
.slice(endOfBlockIndex)
.findLast(
(x) =>
x.nestingLevel === nestingLevel &&
x.namedStep.step.tag !== 'jumptarget',
)
if (firstNonJumpTarget) {
return firstNonJumpTarget.namedStep
}
nestingLevel--
}
return undefined
}
function removeJumpTargetSteps(
steps: NamedWorkflowStep[],
): NamedWorkflowStep[] {
return steps
.filter((x) => x.step.tag !== 'jumptarget')
.map(({ name, step }) => ({
name,
step: removeJumpTargetRecurse(step),
}))
}
function removeJumpTargetRecurse(
step: WorkflowStepASTWithNamedNested,
): WorkflowStepASTWithNamedNested {
switch (step.tag) {
case 'assign':
case 'call':
case 'next':
case 'raise':
case 'return':
case 'jumptarget':
return step
case 'for':
return removeJumpTargetsFor(step)
case 'parallel':
return removeJumpTargetsParallel(step)
case 'steps':
return new StepsStepASTNamed(removeJumpTargetSteps(step.steps))
case 'switch':
return removeJumpTargetsSwitch(step)
case 'try':
return new TryStepASTNamed(
removeJumpTargetSteps(step.trySteps),
step.exceptSteps !== undefined
? removeJumpTargetSteps(step.exceptSteps)
: undefined,
step.retryPolicy,
step.errorMap,
)
}
}
function removeJumpTargetsFor(step: ForStepASTNamed): ForStepASTNamed {
return new ForStepASTNamed(
removeJumpTargetSteps(step.steps),
step.loopVariableName,
step.listExpression,
step.indexVariableName,
step.rangeStart,
step.rangeEnd,
)
}
function removeJumpTargetsParallel(
step: ParallelStepASTNamed,
): ParallelStepASTNamed {
let transformedSteps: Record<StepName, StepsStepASTNamed> | ForStepASTNamed
if (step.branches) {
transformedSteps = Object.fromEntries(
step.branches.map((x) => {
return [
x.name,
new StepsStepASTNamed(
removeJumpTargetSteps(nestedSteps(x.step).flat()),
),
]
}),
)
} else if (step.forStep) {
transformedSteps = removeJumpTargetsFor(step.forStep)
} else {
// should not be reached
transformedSteps = {}
}
return new ParallelStepASTNamed(
transformedSteps,
step.shared,
step.concurrenceLimit,
step.exceptionPolicy,
)
}
function removeJumpTargetsSwitch(step: SwitchStepASTNamed): SwitchStepASTNamed {
const transformedConditions = step.conditions.map((cond) => {
return {
condition: cond.condition,
steps: removeJumpTargetSteps(cond.steps),
next: cond.next,
}
})
return new SwitchStepASTNamed(transformedConditions, step.next)
}
function relabelNextLabels(
steps: NamedWorkflowStep[],
replacements: Map<StepName, StepName>,
): NamedWorkflowStep[] {
return steps.map((step) => ({
name: step.name,
step: renameJumpTargets(step.step, replacements),
}))
}
/**
* Renames a copy of a step with jump targets renamed according to replaceLabels map.
*/
function renameJumpTargets(
step: WorkflowStepASTWithNamedNested,
replaceLabels: Map<StepName, StepName>,
): WorkflowStepASTWithNamedNested {
switch (step.tag) {
case 'call':
case 'raise':
case 'return':
case 'jumptarget':
return step
case 'assign':
return renameJumpTargetsAssign(step, replaceLabels)
case 'next':
return renameJumpTargetsNext(step, replaceLabels)
case 'for':
return renameJumpTargetsFor(step, replaceLabels)
case 'parallel':
return renameJumpTargetsParallel(step, replaceLabels)
case 'steps':
return renameJumpTargetsSteps(step, replaceLabels)
case 'switch':
return renameJumpTargetsSwitch(step, replaceLabels)
case 'try':
return renameJumpTargetsTry(step, replaceLabels)
}
}
function renameJumpTargetsAssign(
step: AssignStepAST,
replaceLabels: Map<StepName, StepName>,
): AssignStepAST {
if (step.next) {
const newLabel = replaceLabels.get(step.next)
if (newLabel) {
return step.withNext(newLabel)
}
}
return step
}
function renameJumpTargetsFor(
step: ForStepASTNamed,
replaceLabels: Map<StepName, StepName>,
): ForStepASTNamed {
const transformedSteps = step.steps.map(({ name, step: nested }) => ({
name,
step: renameJumpTargets(nested, replaceLabels),
}))
return new ForStepASTNamed(
transformedSteps,
step.loopVariableName,
step.listExpression,
step.indexVariableName,
step.rangeStart,
step.rangeEnd,
)
}
function renameJumpTargetsNext(
step: NextStepAST,
replaceLabels: Map<StepName, StepName>,
): NextStepAST {
const newLabel = replaceLabels.get(step.target)
if (newLabel) {
return new NextStepAST(newLabel)
} else {
return step
}
}
function renameJumpTargetsParallel(
step: ParallelStepASTNamed,
replaceLabels: Map<StepName, StepName>,
): ParallelStepASTNamed {
let transformedSteps: Record<StepName, StepsStepASTNamed> | ForStepASTNamed
if (step.branches) {
transformedSteps = Object.fromEntries(
step.branches.map(({ name, step: nested }) => {
const renamedNested = nestedSteps(nested)
.flat()
.map((x) => ({
name: x.name,
step: renameJumpTargets(x.step, replaceLabels),
}))
return [name, new StepsStepASTNamed(renamedNested)]
}),
)
} else if (step.forStep) {
transformedSteps = renameJumpTargetsFor(step.forStep, replaceLabels)
} else {
// should not be reached
transformedSteps = {}
}
return new ParallelStepASTNamed(
transformedSteps,
step.shared,
step.concurrenceLimit,
step.exceptionPolicy,
)
}
function renameJumpTargetsSteps(
step: StepsStepASTNamed,
replaceLabels: Map<StepName, StepName>,
): StepsStepASTNamed {
const transformedSteps = step.steps.map(({ name, step: nested }) => ({
name,
step: renameJumpTargets(nested, replaceLabels),
}))
return new StepsStepASTNamed(transformedSteps)
}
function renameJumpTargetsSwitch(
step: SwitchStepASTNamed,
replaceLabels: Map<StepName, StepName>,
): SwitchStepASTNamed {
let updatedNext: StepName | undefined = undefined
if (step.next) {
updatedNext = replaceLabels.get(step.next) ?? step.next
}
const updatedConditions = step.conditions.map((cond) => {
let updatedCondNext: StepName | undefined = undefined
if (cond.next) {
updatedCondNext = replaceLabels.get(cond.next) ?? cond.next
}
const updatedCondSteps = cond.steps.map((nested) => ({
name: nested.name,
step: renameJumpTargets(nested.step, replaceLabels),
}))
return {
condition: cond.condition,
steps: updatedCondSteps,
next: updatedCondNext,
}
})
return new SwitchStepASTNamed(updatedConditions, updatedNext)
}
function renameJumpTargetsTry(
step: TryStepASTNamed,
replaceLabels: Map<StepName, StepName>,
): TryStepASTNamed {
const transformedTrySteps = step.trySteps.map(({ name, step: nested }) => ({
name,
step: renameJumpTargets(nested, replaceLabels),
}))
const transformedExceptSteps = step.exceptSteps?.map(
({ name, step: nested }) => ({
name,
step: renameJumpTargets(nested, replaceLabels),
}),
)
return new TryStepASTNamed(
transformedTrySteps,
transformedExceptSteps,
step.retryPolicy,
step.errorMap,
)
}