@@ -25,6 +25,7 @@ Provides Serverless Workflow language examples
25
25
- [ Purchase order deadline (ExecTimeout)] ( #Purchase-order-deadline )
26
26
- [ Accumulate room readings and create timely reports (ExecTimeout and KeepActive)] ( #Accumulate-room-readings )
27
27
- [ Car vitals checks (SubFlow state Repeat)] ( #Car-Vitals-Checks )
28
+ - [ Book Lending Workflow] ( #Book-Lending )
28
29
29
30
### Hello World Example
30
31
@@ -3488,4 +3489,267 @@ functions:
3488
3489
3489
3490
</td>
3490
3491
</tr>
3491
- </table>
3492
+ </table>
3493
+
3494
+ # ## Book Lending
3495
+
3496
+ # ### Description
3497
+
3498
+ In this example we want to create a book lending workflow. The workflow starts when a lender
3499
+ submits a book lending request (via event "Book Lending Request Event").
3500
+ The workflow describes our business logic around lending a book, from checking its current availability,
3501
+ to waiting on the lender's response if the book is currently not available, to checking out the book and notifying
3502
+ the lender.
3503
+
3504
+ This example expects the "Book Lending Request Event" event to have a payload of for example :
3505
+
3506
+ ` ` ` json
3507
+ {
3508
+ "book": {
3509
+ "title": " ... ",
3510
+ "id": " ... "
3511
+ },
3512
+ "lender": {
3513
+ "name": "John Doe",
3514
+ "address": " ... ",
3515
+ "phone": " ... "
3516
+ }
3517
+ }
3518
+ ` ` `
3519
+
3520
+ where the "book" property defines the book to be lent out, and the "lender" property provides info
3521
+ about the person wanting to lend the book.
3522
+
3523
+ For the sake of the example we assume the functions and event definitions are defined in separate JSON files.
3524
+
3525
+ # ### Workflow Diagram
3526
+
3527
+ <p align="center">
3528
+ <img src="../media/examples/example-booklending.png" height="400px" alt="Book Lending Example"/>
3529
+ </p>
3530
+
3531
+ # ### Workflow Definition
3532
+
3533
+ <table>
3534
+ <tr>
3535
+ <th>JSON</th>
3536
+ <th>YAML</th>
3537
+ </tr>
3538
+ <tr>
3539
+ <td valign="top">
3540
+
3541
+ ` ` ` json
3542
+ {
3543
+ "id": "booklending",
3544
+ "name": "Book Lending Workflow",
3545
+ "version": "1.0",
3546
+ "start": "Book Lending Request",
3547
+ "states": [
3548
+ {
3549
+ "name": "Book Lending Request",
3550
+ "type": "event",
3551
+ "onEvents": [
3552
+ {
3553
+ "eventRefs": ["Book Lending Request Event"]
3554
+ }
3555
+ ],
3556
+ "transition": "Get Book Status"
3557
+ },
3558
+ {
3559
+ "name": "Get Book Status",
3560
+ "type": "operation",
3561
+ "actions": [
3562
+ {
3563
+ "functionRef": {
3564
+ "refName": "Get status for book",
3565
+ "arguments": {
3566
+ "bookid": "${ .book.id }"
3567
+ }
3568
+ }
3569
+ }
3570
+ ],
3571
+ "transition": "Book Status Decision"
3572
+ },
3573
+ {
3574
+ "name": "Book Status Decision",
3575
+ "type": "switch",
3576
+ "dataConditions": [
3577
+ {
3578
+ "name": "Book is on loan",
3579
+ "condition": "${ .book.status == \" onloan\" }",
3580
+ "transition": "Report Status To Lender"
3581
+ },
3582
+ {
3583
+ "name": "Check is available",
3584
+ "condition": "${ .book.status == \" available\" }",
3585
+ "transition": "Check Out Book"
3586
+ }
3587
+ ]
3588
+ },
3589
+ {
3590
+ "name": "Report Status To Lender",
3591
+ "type": "operation",
3592
+ "actions": [
3593
+ {
3594
+ "functionRef": {
3595
+ "refName": "Send status to lender",
3596
+ "arguments": {
3597
+ "bookid": "${ .book.id }",
3598
+ "message": "Book ${ .book.title } is already on loan"
3599
+ }
3600
+ }
3601
+ }
3602
+ ],
3603
+ "transition": "Wait for Lender response"
3604
+ },
3605
+ {
3606
+ "name": "Wait for Lender response",
3607
+ "type": "switch",
3608
+ "eventConditions": [
3609
+ {
3610
+ "name": "Hold Book",
3611
+ "eventRef": "Hold Book Event",
3612
+ "transition": "Request Hold"
3613
+ },
3614
+ {
3615
+ "name": "Decline Book Hold",
3616
+ "eventRef": "Decline Hold Event",
3617
+ "transition": "Cancel Request"
3618
+ }
3619
+ ]
3620
+ },
3621
+ {
3622
+ "name": "Request Hold",
3623
+ "type": "operation",
3624
+ "actions": [
3625
+ {
3626
+ "functionRef": {
3627
+ "refName": "Request hold for lender",
3628
+ "arguments": {
3629
+ "bookid": "${ .book.id }",
3630
+ "lender": "${ .lender }"
3631
+ }
3632
+ }
3633
+ }
3634
+ ],
3635
+ "transition": "Wait two weeks"
3636
+ },
3637
+ {
3638
+ "name": "Wait two weeks",
3639
+ "type": "delay",
3640
+ "timeDelay": "PT2W",
3641
+ "transition": "Get Book Status"
3642
+ },
3643
+ {
3644
+ "name": "Check Out Book",
3645
+ "type": "operation",
3646
+ "actions": [
3647
+ {
3648
+ "functionRef": {
3649
+ "refName": "Check out book with id",
3650
+ "arguments": {
3651
+ "bookid": "${ .book.id }"
3652
+ }
3653
+ }
3654
+ },
3655
+ {
3656
+ "functionRef": {
3657
+ "refName": "Notify Lender for checkout",
3658
+ "arguments": {
3659
+ "bookid": "${ .book.id }",
3660
+ "lender": "${ .lender }"
3661
+ }
3662
+ }
3663
+ }
3664
+ ],
3665
+ "end": true
3666
+ }
3667
+ ],
3668
+ "functions": "file://books/lending/functions.json",
3669
+ "events": "file://books/lending/events.json"
3670
+ }
3671
+ ` ` `
3672
+
3673
+ </td>
3674
+ <td valign="top">
3675
+
3676
+ ` ` ` yaml
3677
+ id: booklending
3678
+ name: Book Lending Workflow
3679
+ version: '1.0'
3680
+ start: Book Lending Request
3681
+ states:
3682
+ - name: Book Lending Request
3683
+ type: event
3684
+ onEvents:
3685
+ - eventRefs:
3686
+ - Book Lending Request Event
3687
+ transition: Get Book Status
3688
+ - name: Get Book Status
3689
+ type: operation
3690
+ actions:
3691
+ - functionRef:
3692
+ refName: Get status for book
3693
+ arguments:
3694
+ bookid: "${ .book.id }"
3695
+ transition: Book Status Decision
3696
+ - name: Book Status Decision
3697
+ type: switch
3698
+ dataConditions:
3699
+ - name: Book is on loan
3700
+ condition: ${ .book.status == "onloan" }
3701
+ transition: Report Status To Lender
3702
+ - name: Check is available
3703
+ condition: ${ .book.status == "available" }
3704
+ transition: Check Out Book
3705
+ - name: Report Status To Lender
3706
+ type: operation
3707
+ actions:
3708
+ - functionRef:
3709
+ refName: Send status to lender
3710
+ arguments:
3711
+ bookid: "${ .book.id }"
3712
+ message: Book ${ .book.title } is already on loan
3713
+ transition: Wait for Lender response
3714
+ - name: Wait for Lender response
3715
+ type: switch
3716
+ eventConditions:
3717
+ - name: Hold Book
3718
+ eventRef: Hold Book Event
3719
+ transition: Request Hold
3720
+ - name: Decline Book Hold
3721
+ eventRef: Decline Hold Event
3722
+ transition: Cancel Request
3723
+ - name: Request Hold
3724
+ type: operation
3725
+ actions:
3726
+ - functionRef:
3727
+ refName: Request fold for lender
3728
+ arguments:
3729
+ bookid: "${ .book.id }"
3730
+ lender: "${ .lender }"
3731
+ transition: Wait two weeks
3732
+ - name: Wait two weeks
3733
+ type: delay
3734
+ timeDelay: PT2W
3735
+ transition: Get Book Status
3736
+ - name: Check Out Book
3737
+ type: operation
3738
+ actions:
3739
+ - functionRef:
3740
+ refName: Check out book with id
3741
+ arguments:
3742
+ bookid: "${ .book.id }"
3743
+ - functionRef:
3744
+ refName: Notify Lender for checkout
3745
+ arguments:
3746
+ bookid: "${ .book.id }"
3747
+ lender: "${ .lender }"
3748
+ end: true
3749
+ functions: file://books/lending/functions.json
3750
+ events: file://books/lending/events.json
3751
+ ` ` `
3752
+
3753
+ </td>
3754
+ </tr>
3755
+ </table>
0 commit comments