Skip to content

Commit 9479190

Browse files
authored
Merge pull request #75 from antmendoza/add-booklending--example
Add booklending example
2 parents d97bc70 + 4dd42f2 commit 9479190

13 files changed

+420
-260
lines changed

spec/examples/applicantrequest.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {WorkflowBuilder} from "../../src/model/workflow.builder";
1818
import * as fs from "fs";
1919
import {FunctionDefBuilder} from "../../src/model/function-def.builder";
2020
import {DatabasedSwitchBuilder} from "../../src/model/databased-switch.builder";
21-
import {TransitiondataconditionBuilder} from "../../src/model/transitiondatacondition.builder";
21+
import {TransitionDataConditionBuilder} from "../../src/model/transition-data-condition.builder";
2222
import {OperationStateBuilder} from "../../src/model/operation-state.builder";
2323
import {SubFlowStateBuilder} from "../../src/model/sub-flow-state.builder";
2424
import {ActionBuilder} from "../../src/model/action.builder";
@@ -45,11 +45,11 @@ describe("applicationrequest workflow example", () => {
4545
new DatabasedSwitchBuilder()
4646
.withName("CheckApplication")
4747
.withDataConditions(
48-
[new TransitiondataconditionBuilder()
48+
[new TransitionDataConditionBuilder()
4949
.withCondition("${ .applicants | .age >= 18 }")
5050
.withTransition("StartApplication")
5151
.build(),
52-
new TransitiondataconditionBuilder()
52+
new TransitionDataConditionBuilder()
5353
.withCondition("${ .applicants | .age < 18 }")
5454
.withTransition("RejectApplication")
5555
.build()])

spec/examples/booklending.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,100 @@
3030
}
3131
],
3232
"transition": "Book Status Decision"
33+
},
34+
{
35+
"name": "Book Status Decision",
36+
"type": "switch",
37+
"dataConditions": [
38+
{
39+
"name": "Book is on loan",
40+
"condition": "${ .book.status == \"onloan\" }",
41+
"transition": "Report Status To Lender"
42+
},
43+
{
44+
"name": "Check is available",
45+
"condition": "${ .book.status == \"available\" }",
46+
"transition": "Check Out Book"
47+
}
48+
]
49+
},
50+
{
51+
"name": "Report Status To Lender",
52+
"type": "operation",
53+
"actions": [
54+
{
55+
"functionRef": {
56+
"refName": "Send status to lender",
57+
"arguments": {
58+
"bookid": "${ .book.id }",
59+
"message": "Book ${ .book.title } is already on loan"
60+
}
61+
}
62+
}
63+
],
64+
"transition": "Wait for Lender response"
65+
},
66+
{
67+
"name": "Wait for Lender response",
68+
"type": "switch",
69+
"eventConditions": [
70+
{
71+
"name": "Hold Book",
72+
"eventRef": "Hold Book Event",
73+
"transition": "Request Hold"
74+
},
75+
{
76+
"name": "Decline Book Hold",
77+
"eventRef": "Decline Hold Event",
78+
"transition": "Cancel Request"
79+
}
80+
]
81+
},
82+
{
83+
"name": "Request Hold",
84+
"type": "operation",
85+
"actions": [
86+
{
87+
"functionRef": {
88+
"refName": "Request hold for lender",
89+
"arguments": {
90+
"bookid": "${ .book.id }",
91+
"lender": "${ .lender }"
92+
}
93+
}
94+
}
95+
],
96+
"transition": "Wait two weeks"
97+
},
98+
{
99+
"name": "Wait two weeks",
100+
"type": "delay",
101+
"timeDelay": "PT2W",
102+
"transition": "Get Book Status"
103+
},
104+
{
105+
"name": "Check Out Book",
106+
"type": "operation",
107+
"actions": [
108+
{
109+
"functionRef": {
110+
"refName": "Check out book with id",
111+
"arguments": {
112+
"bookid": "${ .book.id }"
113+
}
114+
}
115+
},
116+
{
117+
"functionRef": {
118+
"refName": "Notify Lender for checkout",
119+
"arguments": {
120+
"bookid": "${ .book.id }",
121+
"lender": "${ .lender }"
122+
}
123+
}
124+
}
125+
],
126+
"end": true
33127
}
34128
],
35129
"functions": "file://books/lending/functions.json",

spec/examples/booklending.spec.ts

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {ActionBuilder, EventsBuilder, OperationStateBuilder, WorkflowBuilder} from '../../src';
17+
import {
18+
ActionBuilder,
19+
DatabasedSwitchBuilder,
20+
EventsBuilder,
21+
OperationStateBuilder,
22+
TransitionDataConditionBuilder,
23+
WorkflowBuilder,
24+
} from '../../src';
1825
import * as fs from 'fs';
1926
import {StartBuilder} from '../../src/model/start.builder';
2027
import {EventStateBuilder} from '../../src/model/event-state.builder';
2128
import {OnEventBuilder} from '../../src/model/on-event.builder';
2229
import {FunctionRefBuilder} from '../../src/model/function-ref.builder';
2330
import {FunctionsBuilder} from '../../src/model/functions.builder';
31+
import {EventBasedSwitchBuilder} from '../../src/model/event-based-switch.builder';
32+
import {TransitionEventConditionBuilder} from '../../src/model/transition-event-condition.builder';
33+
import {DelayStateBuilder} from '../../src/model/delay-state.builder';
2434

2535

2636
describe("booklending workflow example", () => {
@@ -59,6 +69,96 @@ describe("booklending workflow example", () => {
5969
])
6070
.withTransition("Book Status Decision")
6171
.build(),
72+
new DatabasedSwitchBuilder()
73+
.withName("Book Status Decision")
74+
.withDataConditions([
75+
new TransitionDataConditionBuilder()
76+
.withName("Book is on loan")
77+
.withCondition("${ .book.status == \"onloan\" }")
78+
.withTransition("Report Status To Lender")
79+
.build(),
80+
new TransitionDataConditionBuilder()
81+
.withName("Check is available")
82+
.withCondition("${ .book.status == \"available\" }")
83+
.withTransition("Check Out Book")
84+
.build(),
85+
])
86+
.build(),
87+
new OperationStateBuilder()
88+
.withName("Report Status To Lender")
89+
.withActions([
90+
new ActionBuilder()
91+
.withFunctionRef(new FunctionRefBuilder()
92+
.withRefName("Send status to lender")
93+
.withArguments({
94+
"bookid": "${ .book.id }",
95+
"message": "Book ${ .book.title } is already on loan",
96+
})
97+
.build())
98+
.build(),
99+
])
100+
.withTransition("Wait for Lender response")
101+
.build(),
102+
new EventBasedSwitchBuilder()
103+
.withName("Wait for Lender response")
104+
.withEventConditions([
105+
new TransitionEventConditionBuilder()
106+
.withName("Hold Book")
107+
.withEventRef("Hold Book Event")
108+
.withTransition("Request Hold")
109+
.build(),
110+
new TransitionEventConditionBuilder()
111+
.withName("Decline Book Hold")
112+
.withEventRef("Decline Hold Event")
113+
.withTransition("Cancel Request")
114+
.build(),
115+
])
116+
.build(),
117+
new OperationStateBuilder()
118+
.withName("Request Hold")
119+
.withActions([
120+
new ActionBuilder().withFunctionRef(
121+
new FunctionRefBuilder()
122+
.withRefName("Request hold for lender")
123+
.withArguments({
124+
"bookid": "${ .book.id }",
125+
"lender": "${ .lender }",
126+
}).build()).build(),
127+
])
128+
.withTransition("Wait two weeks")
129+
.build(),
130+
new DelayStateBuilder()
131+
.withName("Wait two weeks")
132+
.withTimeDelay("PT2W")
133+
.withTransition("Get Book Status")
134+
.build(),
135+
new OperationStateBuilder()
136+
.withName("Check Out Book")
137+
.withActions([
138+
new ActionBuilder()
139+
.withFunctionRef(
140+
new FunctionRefBuilder()
141+
.withRefName("Check out book with id")
142+
.withArguments({
143+
"bookid": "${ .book.id }"
144+
})
145+
.build())
146+
.build(),
147+
new ActionBuilder()
148+
.withFunctionRef(
149+
new FunctionRefBuilder()
150+
.withRefName("Notify Lender for checkout")
151+
.withArguments({
152+
"bookid": "${ .book.id }",
153+
"lender": "${ .lender }"
154+
})
155+
.build())
156+
.build()
157+
158+
])
159+
.withEnd(true)
160+
.build()
161+
62162
])
63163
.withFunctions(new FunctionsBuilder()
64164
.withURIDefinition("file://books/lending/functions.json")
@@ -68,6 +168,7 @@ describe("booklending workflow example", () => {
68168
.build())
69169
.build();
70170

171+
71172
const expected = JSON.parse(fs.readFileSync("./spec/examples/booklending.json")
72173
.toLocaleString()) as any;
73174
expect(workflow).toEqual(expected);

spec/examples/transition.builder.js

Whitespace-only changes.

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ export {BaseWorkflow} from "./base-workflow";
2020
export {ActionBuilder} from "./model/action.builder";
2121
export {ActionDataFilterBuilder} from "./model/action-data-filter.builder";
2222
export {CronDefBuilder} from "./model/cron-def.builder";
23+
export {DelayStateBuilder} from "./model/delay-state.builder";
2324
export {DatabasedSwitchBuilder} from "./model/databased-switch.builder";
2425
export {DefaultTransitionTypeBuilder} from "./model/default-transition-type.builder";
2526
export {EventBuilder} from "./model/event.builder";
27+
export {EventBasedSwitchBuilder} from "./model/event-based-switch.builder";
2628
export {EventRefBuilder} from "./model/event-ref.builder";
2729
export {EventStateBuilder} from "./model/event-state.builder";
2830
export {EventsBuilder} from "./model/events.builder";
@@ -38,7 +40,8 @@ export {RepeatBuilder} from "./model/repeat.builder";
3840
export {ScheduleBuilder} from "./model/schedule.builder";
3941
export {StartBuilder} from "./model/start.builder";
4042
export {SubFlowStateBuilder} from "./model/sub-flow-state.builder";
41-
export {TransitiondataconditionBuilder} from "./model/transitiondatacondition.builder";
43+
export {TransitionDataConditionBuilder} from "./model/transition-data-condition.builder";
44+
export {TransitionEventConditionBuilder} from "./model/transition-event-condition.builder";
4245
export {WorkflowBuilder} from "./model/workflow.builder";
4346

4447
export * from "./model/workflow.validator";

src/model/databased-switch.builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {Databasedswitch} from "./workflow";
18-
import {DataConditionsType, DefaultTransitionType} from "./types";
17+
import {DataDasedSwitch} from "./workflow";
18+
import {DataConditions, DefaultTransitionType} from "./types";
1919

2020

2121
export class DatabasedSwitchBuilder {
2222

2323

2424
// @ts-ignore
25-
private model: Databasedswitch = {
25+
private model: DataDasedSwitch = {
2626
type: "switch"
2727
}
2828

@@ -32,7 +32,7 @@ export class DatabasedSwitchBuilder {
3232
return this;
3333
}
3434

35-
withDataConditions(value: DataConditionsType): DatabasedSwitchBuilder {
35+
withDataConditions(value: DataConditions): DatabasedSwitchBuilder {
3636
this.model.dataConditions = value;
3737
return this;
3838
}

src/model/delay-state.builder.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 {DelayState, Transition} from './workflow';
18+
19+
export class DelayStateBuilder {
20+
private model: DelayState = {
21+
type: "delay",
22+
};
23+
24+
build(): DelayState {
25+
return this.model;
26+
}
27+
28+
withName(value: string): DelayStateBuilder {
29+
this.model.name = value;
30+
return this;
31+
}
32+
33+
withTimeDelay(value: string): DelayStateBuilder {
34+
this.model.timeDelay = value;
35+
return this;
36+
37+
}
38+
39+
withTransition(value: Transition): DelayStateBuilder {
40+
this.model.transition = value;
41+
return this;
42+
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 {EventBasedSwitch, EventConditions} from '../index';
18+
19+
export class EventBasedSwitchBuilder {
20+
// @ts-ignore
21+
private model: EventBasedSwitch = {
22+
type: "switch",
23+
};
24+
25+
withName(value: string): EventBasedSwitchBuilder {
26+
this.model.name = value;
27+
return this;
28+
}
29+
30+
withEventConditions(value: EventConditions): any {
31+
this.model.eventConditions = value;
32+
return this;
33+
}
34+
35+
build(): EventBasedSwitch {
36+
return this.model;
37+
}
38+
}

0 commit comments

Comments
 (0)