OrderProcessing.icl 4.33 KB
Newer Older
1
implementation module OrderProcessing
2

3
import iTasks
4 5

/*
6 7 8 9 10 11 12 13 14 15 16 17 18
 * The scenario contains a number of different events and activities:
 * The customer places an order and fills out her credit card number. 
 * Your web system checks that the credit card is valid and sends a 
 * request to the bank for the money. After a while a confirmation 
 * arrives from the bank telling the system that there are sufficient 
 * funds in the account. The system sends a notification to storage 
 * with the customers address along with information on what the 
 * customer ordered. The storage personnel package the order and send 
 * it off with the delivery service. They also log into the system and 
 * confirm that the order has been sent. The system sends an email with 
 * the confirmation to the customer. 
 */

19 20 21 22
orderProcessingExample :: [Workflow]
orderProcessingExample
= [	{	name		= "Examples/Business/Order processing"
	,	label		= "Order processing"
23
	,	roles		= []
24
	,	mainTask	= orderPlacement customer >>| return Void
25
	}
26
  ]
27 28
orderPlacement :: UserId -> Task Void
orderPlacement user =
29
  customer @:  ( "Order items from shop"
30
               , orderItemsFromShop -&&- (getDefaultValue >>= fillInAndCheckCreditCard)
31
               ) >>= \(basket, cardInfo) ->
32
  customer @:  ( "Order confirmation"
Thomas van Noort's avatar
Thomas van Noort committed
33
               , confirmOrder
34
               ) >>| 
35
  bank     @:  ("Cash request"
Thomas van Noort's avatar
Thomas van Noort committed
36
               , cashRequest (amountFrom basket) cardInfo
37
               ) >>= \granted ->
38
  if granted
39
    (storage  @:  ( "Deliver order"
Thomas van Noort's avatar
Thomas van Noort committed
40
                  , deliverOrder basket (deliveryAddress cardInfo)
41
                  ) >>|
42
     customer @:  ( "Delivery confirmation"
Thomas van Noort's avatar
Thomas van Noort committed
43 44
                  , confirmDelivery basket (deliveryAddress cardInfo)
                  )
45
    )
46
    (customer @:  ( "Delivery failure"
Thomas van Noort's avatar
Thomas van Noort committed
47 48
                  , failedDelivery basket (deliveryAddress cardInfo)
                  )
49
    ) 
50 51
									  
fillInAndCheckCreditCard :: CardInfo -> Task CardInfo
52
fillInAndCheckCreditCard cardInfo =
53
  fillInCreditCard cardInfo >>= \cardInfo ->
Thomas van Noort's avatar
Thomas van Noort committed
54 55
  creditcard @: ( "Validate credit card"
                , validateCreditCard cardInfo
56
                ) >>= \valid ->
57
  if valid
58
    (return cardInfo)
59
    (invalidCreditCard cardInfo >>| 
60 61
     fillInAndCheckCreditCard cardInfo
    )
Bas Lijnse's avatar
Bas Lijnse committed
62
												
63
// Low level definitions
Thomas van Noort's avatar
Thomas van Noort committed
64 65 66 67
customer   :== 10
bank       :== 11
storage    :== 12
creditcard :== 13 // not used at the moment (see validateCreditCard)
68 69

:: CardInfo :== String
70 71 72 73 74
:: Address  :== CardInfo

deliveryAddress :: CardInfo -> String
deliveryAddress cardInfo = "delivery address: " +++ cardInfo

75
:: ItemInfo :== (Int, String, Real)
76 77 78
:: Basket   :== ItemInfo

amountFrom :: ItemInfo -> Real
79
amountFrom (amount, _,price) = toReal amount * price
80 81

items :: [ItemInfo]
82
items = map (\(descr, price) -> (1, descr, price)) descrs
83 84 85 86 87 88 89 90
  where 
    descrs :: [(String, Real)]
    descrs = [ ("The Shawshank Redemption", 4.99)
             , ("O Brother Where Art Thou", 6.99)
             , ("Bambi", 9.99)
             ]    

orderItemsFromShop :: Task ItemInfo
Bas Lijnse's avatar
Bas Lijnse committed
91
orderItemsFromShop
92
	= updateInformation
Bas Lijnse's avatar
Bas Lijnse committed
93 94
		"Please select how many items you would like to order from our shop:"
		(hd items)
95 96

fillInCreditCard :: CardInfo -> Task CardInfo
Bas Lijnse's avatar
Bas Lijnse committed
97
fillInCreditCard cardInfo
98
	= updateInformation
Bas Lijnse's avatar
Bas Lijnse committed
99 100 101
		"Please fill in your credit card number (at least 5 digits):"
		cardInfo
		
102
validateCreditCard :: CardInfo -> Task Bool
103
validateCreditCard cardInfo = return (size cardInfo == 5)
104 105

invalidCreditCard :: CardInfo -> Task Void	
Bas Lijnse's avatar
Bas Lijnse committed
106 107
invalidCreditCard cardInfo
	= showMessage "Your credit card was invalid!"
108 109

confirmOrder :: Task Void
Bas Lijnse's avatar
Bas Lijnse committed
110 111
confirmOrder
	= showMessage "Your order will be processed!"
112 113

cashRequest :: Real CardInfo -> Task Bool
Bas Lijnse's avatar
Bas Lijnse committed
114 115
cashRequest amount cardInfo
	= requestConfirmation ("Can we subtract " <+++ amount <+++ " from card " <+++ cardInfo <+++ "?")
116 117
    
deliverOrder :: Basket Address -> Task Void 
Bas Lijnse's avatar
Bas Lijnse committed
118 119
deliverOrder basket address
	= showMessage ("Please deliver " <+++ basket <+++ " to " <+++ address <+++ ".")
120 121

confirmDelivery :: Basket Address -> Task Void
Bas Lijnse's avatar
Bas Lijnse committed
122 123
confirmDelivery basket address
	= showMessage ("Your order " <+++ basket <+++ " will be delivered to " <+++ address <+++ ".")
124 125

failedDelivery :: Basket Address -> Task Void
Bas Lijnse's avatar
Bas Lijnse committed
126 127
failedDelivery basket address
	= showMessage ("Your order " <+++ basket <+++ " cannot be delivered to " <+++ address <+++ ".")