Skip to content

Commit a072cc4

Browse files
committed
add parallel example
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent ff7d0da commit a072cc4

File tree

9 files changed

+272
-57
lines changed

9 files changed

+272
-57
lines changed

spec/examples/checkcarvitals.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {WorkflowBuilder} from "../../src/model/workflow.builder";
17+
import {
18+
EventBuilder,
19+
EventsBuilder,
20+
EventStateBuilder,
21+
OnEventBuilder,
22+
RepeatBuilder,
23+
SubFlowStateBuilder,
24+
WorkflowBuilder,
25+
} from "../../src";
1826
import * as fs from "fs";
19-
import {SubFlowStateBuilder} from "../../src/model/sub-flow-state.builder";
20-
import {EventBuilder, EventsBuilder, EventStateBuilder, OnEventBuilder, RepeatBuilder} from '../../src';
2127

2228

2329
describe("checkcarvitals workflow example", () => {

spec/examples/parallel.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"id": "parallelexec",
3+
"version": "1.0",
4+
"name": "Parallel Execution Workflow",
5+
"description": "Executes two branches in parallel",
6+
"start": "ParallelExec",
7+
"states":[
8+
{
9+
"name": "ParallelExec",
10+
"type": "parallel",
11+
"completionType": "and",
12+
"branches": [
13+
{
14+
"name": "ShortDelayBranch",
15+
"workflowId": "shortdelayworkflowid"
16+
},
17+
{
18+
"name": "LongDelayBranch",
19+
"workflowId": "longdelayworkflowid"
20+
}
21+
],
22+
"end": true
23+
}
24+
]
25+
}

spec/examples/parallel.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import * as fs from "fs";
18+
import {BranchBuilder, ParallelStateBuilder, WorkflowBuilder} from '../../src';
19+
20+
21+
describe("parallel workflow example", () => {
22+
23+
24+
it('should generate Workflow object', function () {
25+
26+
const workflow = new WorkflowBuilder()
27+
.withId("parallelexec")
28+
.withVersion("1.0")
29+
.withName("Parallel Execution Workflow")
30+
.withDescription("Executes two branches in parallel")
31+
.withStart("ParallelExec")
32+
.withStates([
33+
new ParallelStateBuilder()
34+
.withName("ParallelExec")
35+
.withCompletionType("and")
36+
.withBranches([
37+
new BranchBuilder()
38+
.withName("ShortDelayBranch")
39+
.withWorkflowId("shortdelayworkflowid")
40+
.build(),
41+
new BranchBuilder()
42+
.withName("LongDelayBranch")
43+
.withWorkflowId("longdelayworkflowid")
44+
.build(),
45+
])
46+
.withEnd(true)
47+
.build(),
48+
])
49+
.build();
50+
51+
52+
const expected = JSON.parse(fs.readFileSync("./spec/examples/parallel.json")
53+
.toLocaleString()) as any;
54+
expect(workflow).toEqual(expected);
55+
56+
});
57+
58+
59+
});

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export {BaseWorkflow} from "./base-workflow";
1919

2020
export {ActionBuilder} from "./model/action.builder";
2121
export {ActionDataFilterBuilder} from "./model/action-data-filter.builder";
22+
export {BranchBuilder} from "./model/branch.builder";
2223
export {CronDefBuilder} from "./model/cron-def.builder";
2324
export {DelayStateBuilder} from "./model/delay-state.builder";
2425
export {DatabasedSwitchBuilder} from "./model/databased-switch.builder";
@@ -38,6 +39,7 @@ export {MetadataBuilder} from "./model/metadata.builder";
3839
export {OnEventBuilder} from "./model/on-event.builder";
3940
export {OnErrorBuilder} from "./model/on-error.builder";
4041
export {OperationStateBuilder} from "./model/operation-state.builder";
42+
export {ParallelStateBuilder} from './model/parallel-state.builder';
4143
export {ProduceEventDefBuilder} from "./model/produce-event-def.builder";
4244
export {RepeatBuilder} from "./model/repeat.builder";
4345
export {ScheduleBuilder} from "./model/schedule.builder";

src/model/branch.builder.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import {Branch} from './types';
18+
19+
export class BranchBuilder {
20+
// @ts-ignore
21+
private model: Branch = {};
22+
23+
withName(value: string): BranchBuilder {
24+
this.model.name = value;
25+
return this;
26+
27+
}
28+
29+
withWorkflowId(value: string): BranchBuilder {
30+
this.model.workflowId = value;
31+
return this;
32+
33+
}
34+
35+
build(): Branch {
36+
return this.model;
37+
38+
}
39+
}

src/model/operation-state.builder.ts

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,52 @@ import {OnErrors, OperationState, Transition} from "./workflow";
1818
import {ActionModeType, Actions, End, StateDataFilter} from "./types";
1919

2020
export class OperationStateBuilder {
21-
22-
private model: OperationState = {
23-
type: "operation"
24-
}
25-
26-
withName(value: string): OperationStateBuilder {
27-
this.model.name = value;
28-
return this;
29-
30-
}
31-
32-
33-
withActionMode(value: ActionModeType): OperationStateBuilder {
34-
this.model.actionMode = value;
35-
return this;
36-
37-
}
38-
39-
withEnd(value: End): OperationStateBuilder {
40-
this.model.end = value;
41-
return this;
42-
}
43-
44-
withActions(value: Actions): OperationStateBuilder {
45-
this.model.actions = value;
46-
return this;
47-
}
48-
49-
withTransition(value: Transition): OperationStateBuilder {
50-
this.model.transition = value;
51-
return this;
52-
53-
}
21+
22+
private model: OperationState = {
23+
type: "operation",
24+
};
25+
26+
withName(value: string): OperationStateBuilder {
27+
this.model.name = value;
28+
return this;
29+
30+
}
31+
32+
33+
withActionMode(value: ActionModeType): OperationStateBuilder {
34+
this.model.actionMode = value;
35+
return this;
36+
37+
}
38+
39+
withEnd(value: End): OperationStateBuilder {
40+
this.model.end = value;
41+
return this;
42+
}
43+
44+
withActions(value: Actions): OperationStateBuilder {
45+
this.model.actions = value;
46+
return this;
47+
}
48+
49+
withTransition(value: Transition): OperationStateBuilder {
50+
this.model.transition = value;
51+
return this;
52+
53+
}
54+
5455
withStateDataFilter(value: StateDataFilter): OperationStateBuilder {
5556
this.model.stateDataFilter = value;
5657
return this;
5758
}
58-
withOnErrors(value: OnErrors): OperationStateBuilder {
59-
this.model.onErrors = value;
60-
return this;
61-
}
62-
63-
build(): OperationState {
64-
return this.model;
65-
}
66-
59+
60+
withOnErrors(value: OnErrors): OperationStateBuilder {
61+
this.model.onErrors = value;
62+
return this;
63+
}
64+
65+
build(): OperationState {
66+
return this.model;
67+
}
68+
6769
}

src/model/parallel-state.builder.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import {ParallelState} from './workflow';
18+
import {Branches, CompletionType, End} from './types';
19+
20+
export class ParallelStateBuilder {
21+
private model: ParallelState = {
22+
type: "parallel",
23+
};
24+
25+
26+
withName(value: string): ParallelStateBuilder {
27+
this.model.name = value;
28+
return this;
29+
30+
}
31+
32+
withCompletionType(value: CompletionType): ParallelStateBuilder {
33+
this.model.completionType = value;
34+
return this;
35+
36+
}
37+
38+
withBranches(value: Branches): ParallelStateBuilder {
39+
this.model.branches = value;
40+
return this;
41+
}
42+
43+
withEnd(value: End): ParallelStateBuilder {
44+
this.model.end = value;
45+
return this;
46+
47+
}
48+
49+
build(): ParallelState {
50+
return this.model;
51+
}
52+
53+
54+
}

src/model/types.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,37 @@ export interface TransitionDataCondition {
466466
*/
467467
metadata?: Metadata;
468468
}
469+
470+
471+
472+
export type Branch =
473+
{
474+
/**
475+
* Branch name
476+
*/
477+
name: string;
478+
/**
479+
* Actions to be executed in this branch
480+
*/
481+
actions?: Action[];
482+
/**
483+
* Unique Id of a workflow to be executed in this branch
484+
*/
485+
workflowId: string;
486+
} | {
487+
/**
488+
* Branch name
489+
*/
490+
name: string;
491+
/**
492+
* Actions to be executed in this branch
493+
*/
494+
actions: Action[];
495+
/**
496+
* Unique Id of a workflow to be executed in this branch
497+
*/
498+
workflowId?: string;
499+
};
500+
export type Branches = Branch[];
501+
502+
export type CompletionType = "and" | "xor" | "n_of_m";

0 commit comments

Comments
 (0)