-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.ts
More file actions
249 lines (214 loc) · 8.58 KB
/
example1.ts
File metadata and controls
249 lines (214 loc) · 8.58 KB
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
import { CancellationToken } from "@zxteam/contract";
import {
Activity, BreakpointActivity, BusinessActivity, ContextActivity,
ConsoleLogActivity, DelayActivity, LoopActivity, NativeActivity, RandomIntActivity,
RandomUintActivity, SequenceActivity, WorkflowVirtualMachine, IfActivity, IfElement, WorkflowApplication
} from "../src";
import { WorkflowInvoker } from "../src";
import { createInterface } from "readline";
import { InvalidOperationError } from "@zxteam/errors";
// let testStartBreakpoint: BreakpointActivity<any>;
// let testFinishBreakpoint: BreakpointActivity<any>;
let workflowInvoker: WorkflowInvoker;
// class SetupBreakpointActivity extends BreakpointActivity {
// private readonly _breakpointResumedSymbol: symbol;
// public constructor(opts: BreakpointActivity.Opts, child: Activity) {
// super(opts);
// this._breakpointResumedSymbol = Symbol.for("BreakpointActivity.Awaiter:" + this.name);
// }
// public async execute(cancellationToken: CancellationToken, ctx: WorkflowVirtualMachine.NativeExecutionContext): Promise<void> {
// const { runtimeSymbols } = ctx;
// this.resolveAwaiters(runtimeSymbols);
// if (this.isResumeAllowed(ctx)) {
// if (runtimeSymbols.has(this._breakpointResumedSymbol)) {
// // remove itself
// ctx.stackPop();
// } else {
// // push child
// ctx.stackPush(cancellationToken);
// }
// }
// }
// }
class MyBreakpointActivity extends BreakpointActivity {
public async execute(cancellationToken: CancellationToken, ctx: WorkflowVirtualMachine.NativeExecutionContext): Promise<void> {
console.log("MyBreakpointActivity#execute");
return super.execute(cancellationToken, ctx);
}
}
async function main(): Promise<void> {
const dummyCancellationToken: CancellationToken = {
isCancellationRequested: false,
addCancelListener(cb: Function): void { /* noop */ },
removeCancelListener(cb: Function): void { /* noop */ },
throwIfCancellationRequested(): void { /* noop */ }
};
// interface Context { readonly appName: string; }
// interface PersonContext { name: string; age: number; }
@Activity.Id("46d44efd-7341-4b7e-a581-41b605ac5f6c")
class PersonRenderActivity extends BusinessActivity {
protected onExecute(cancellationToken: CancellationToken, ctx: WorkflowVirtualMachine.ExecutionContext) {
console.log(`Application '${ctx.variables.getString("appName")}' The ${ctx.variables.getString("name")} is ${ctx.variables.getInteger("age")} years old.`);
}
}
@Activity.Id("5b839031-04ff-4e52-849a-194ddd28b094")
class IncrementAgeAndBreakLoop extends BusinessActivity {
protected onExecute(cancellationToken: CancellationToken, ctx: WorkflowVirtualMachine.ExecutionContext): void | Promise<void> {
const currentAge = ctx.variables.getInteger("age");
if (currentAge > 45) {
LoopActivity.of(ctx).break();
} else {
ctx.variables.set("age", currentAge + 1);
}
}
}
class CrashTestActivity extends BusinessActivity {
protected onExecute(cancellationToken: CancellationToken, ctx: WorkflowVirtualMachine.ExecutionContext): void {
throw "Crash";
}
}
class IsRandomPositive extends BusinessActivity {
protected onExecute(cancellationToken: CancellationToken, wvm: WorkflowVirtualMachine.ExecutionContext): void {
const ifElement: IfElement = IfActivity.of(wvm);
if (wvm.variables.getInteger("random") > 0) {
ifElement.markTrue();
} else {
ifElement.markFalse();
}
}
}
// const workflow = new WhileActivity({
// condition: new CodeActivity(async (cancellationToken, $$) => {
// if ($$.age === undefined) { $$.age = 42; }
// console.log("Checking condition in CodeActivity");
// $$.age++;
// if ($$.age > 45) {
// $$[WhileActivity.Done]();
// }
// return;
// }),
// child: new SequenceActivity({
// children: [
// new ConsoleLogActivity(($$) => { text: `one: ${$$.age}` }),
// new DelayActivity({ durationMilliseconds: 100 }),
// new ConsoleLogActivity(($$) => { text: `two: ${$$.age}` }),
// new DelayActivity({ durationMilliseconds: 200 }),
// new ConsoleLogActivity(($$) => { text: `three: ${$$.age}` }),
// new DelayActivity({ durationMilliseconds: 300 }),
// new PersonRenderActivity()
// ]
// })
// });
// const workflow = new ContextActivity({ appName: "example1", random: 0 },
// new SequenceActivity(
// new RandomUintActivity({ targetVariable: "random" }),
// new ContextActivity({ name: "Maks", age: 40 },
// new IfActivity(
// new IsRandomPositive(),
// new LoopActivity(
// new SequenceActivity(
// new ConsoleLogActivity({ text: "one" }),
// new DelayActivity({ durationMilliseconds: 100 }),
// new ConsoleLogActivity({ text: "two" }),
// new DelayActivity({ durationMilliseconds: 200 }),
// new ConsoleLogActivity({ text: "three" }),
// new DelayActivity({ durationMilliseconds: 300 }),
// new PersonRenderActivity(),
// new IncrementAgeAndBreakLoop(),
// new BreakpointActivity({ name: "LOOP_BREAKPOINT", description: "Waiting user's approval to continue" })
// )
// ),
// new ConsoleLogActivity({ text: "Random value is NEGATIVE" })
// )
// ),
// new BreakpointActivity({ name: "TEST_BREAKPOINT", description: "Waiting user's approval to continue" })
// )
// );
const workflow = new ContextActivity({ appName: "example1", random: 0 },
new BreakpointActivity({ name: "SETUP_BREAKPOINT", description: "Setup data" },
new SequenceActivity(
//new CrashTestActivity(),
new RandomIntActivity({ targetVariable: "random" }),
//new ContextActivity({ name: "Maks", age: 40 },
new IfActivity({
conditionActivity:
new IsRandomPositive(),
trueActivity: new LoopActivity(
new SequenceActivity(
new ConsoleLogActivity({ text: "Random value is POSITIVE" }),
new ConsoleLogActivity({ text: "one" }),
new DelayActivity({ durationMilliseconds: 100 }),
new ConsoleLogActivity({ text: "two" }),
new DelayActivity({ durationMilliseconds: 200 }),
new ConsoleLogActivity({ text: "three" }),
new DelayActivity({ durationMilliseconds: 300 }),
new PersonRenderActivity(),
new IncrementAgeAndBreakLoop()
)
),
falseActivity: new ConsoleLogActivity({ text: "Random value is NEGATIVE" })
})
//)
,
new MyBreakpointActivity({ name: "TEST_BREAKPOINT", description: "Waiting user's approval to continue" }),
new ConsoleLogActivity({ text: "Workflow is finished" })
)
)
);
// const workflow = new ContextActivity({ age: 18 },
// new ConsoleLogActivity({ text: "two" })
// );
// const workflow = new ContextActivity({ age: 18 },
// new AgeLogActivity()
// );
// const workflow = new ConsoleLogActivity({ text: "one" });
workflowInvoker = WorkflowInvoker.create("example1", workflow);
workflowInvoker.waitForBreakpoint(dummyCancellationToken, "SETUP_BREAKPOINT").then(() => {
const variables = workflowInvoker.currentExecutionContext.variables;
variables.define("name", "Maks", WorkflowVirtualMachine.Scope.INHERIT);
variables.define("age", 40, WorkflowVirtualMachine.Scope.INHERIT);
workflowInvoker.resumeBreakpoint("SETUP_BREAKPOINT");
});
workflowInvoker.waitForBreakpoint(dummyCancellationToken, "TEST_BREAKPOINT")
.then((brk) => {
console.log("UUUha! TEST_BREAKPOINT is reached. Will resume it in 5 seconds. Description: " + brk.description);
setTimeout(function () {
workflowInvoker.resumeBreakpoint("TEST_BREAKPOINT");
console.log("Resumed TEST_BREAKPOINT");
}, 5000);
})
.catch(reason => {
console.log("Wau1! TEST_BREAKPOINT is crashed");
});
workflowInvoker.waitForBreakpoint(dummyCancellationToken, "TEST_BREAKPOINT")
.catch(reason => {
console.log("Wau2! TEST_BREAKPOINT is crashed");
});
workflowInvoker.waitForBreakpoint(dummyCancellationToken, "TEST_BREAKPOINT")
.catch(reason => {
console.log("Wau3! TEST_BREAKPOINT is crashed");
});
await workflowInvoker.invoke(dummyCancellationToken);
//await WorkflowInvoker.run(dummyCancellationToken, workflow);
}
main()
.then(() => { process.exit(0); })
.catch((e: Error) => console.error(e && e.stack || e));
// const rl = createInterface({ input: process.stdin, output: process.stdout, prompt: "Admin> " });
// rl.prompt();
// rl.on("line", (line) => {
// switch (line.trim()) {
// case "hup":
// console.log(`Resume breakpoint TEST_BREAKPOINT`);
// workflowInvoker.resumeBreakpoint("TEST_BREAKPOINT");
// break;
// case "loop":
// console.log(`Resume breakpoint LOOP_BREAKPOINT`);
// workflowInvoker.resumeBreakpoint("LOOP_BREAKPOINT");
// break;
// default:
// rl.prompt();
// break;
// }
// rl.prompt();
// });