Commit dbf87446 authored by Bas Lijnse's avatar Bas Lijnse

Added a simple workflow for getting coffee. This will be used in a tutorial of basic iTasks use.

Also added some basic tasks and combinators to hide unnecessary complexity in the example.

git-svn-id: https://svn.cs.ru.nl/repos/iTask-system/trunk@950 63da3aa8-80fd-4f01-9db8-e6ea747a3da2
parent 6a91e585
......@@ -20,6 +20,7 @@ import Coffeemachine
import Newsgroups
import ChangeHandling
import textEditor
import CoffeeTime
//Crisis response examples
import AmbulanceDispatch
......@@ -50,6 +51,7 @@ where
// , bugReportExample
, coffeemachineExample
, textEditor
, coffeeTimeExample
, newsgroupsExample
, exceptionHandlingExample
, changeHandlingExample
......
definition module CoffeeTime
import iTasks
coffeeTimeExample :: [Workflow]
\ No newline at end of file
implementation module CoffeeTime
import iTasks
coffeeTimeExample :: [Workflow]
coffeeTimeExample = [workflow "Examples/Miscellaneous/Coffee time" coffeeTime]
/*
* Main workflow:
* - list all users
* - collect orders from all users
* - determine who goes to get coffee
* - let someone get coffee
*/
coffeeTime :: Task Void
coffeeTime
= getUserNames
>>= \users ->
collectOrders users
>>= \orders ->
determineWhoGoes (orderlist users orders)
>>= \victim ->
goGetCoffee victim (orderlist users orders)
where
orderlist users orders = [(user, fromJust order) \\ user <- users & order <- orders | isJust order]
/*
* Collect the drinks orders from all users
*/
collectOrders :: [UserName] -> Task [Maybe String]
collectOrders users = allTasks [u @: ("Coffee time!",getOrder) \\ u <- users]
/*
* Ask someone if he/she wants something to drink
*/
getOrder :: Task (Maybe String)
getOrder
= requestConfirmation "It is coffee time, do you want something?"
>>= \yes -> if yes
(enterChoice "What do you want" ["Coffee","Tea","Chocolate"] >>= \choice -> return (Just choice))
(return Nothing)
/*
* Determine who has to go get coffee
* A random choice is made between the people who want something
*/
determineWhoGoes :: [(UserName,String)] -> Task UserName
determineWhoGoes orders = randomChoice [user \\ (user,_) <- orders]
/*
* Give someone directions to go get coffee for everyone
*/
goGetCoffee :: UserName [(UserName,String)] -> Task Void
goGetCoffee user orders = user @: ("Get coffee", showInstructionAbout "Coffee orders" "You have been chosen to get the following drinks" orders)
......@@ -38,6 +38,12 @@ getContextManager :: Task UserName
* @return The default value
*/
getDefaultValue :: Task a | iTask a
/**
* Gets a random integer
*
* @return The random number
*/
getRandomInt :: Task Int
/**
* Create a new process.
......
implementation module SystemTasks
import StdList
from TSt import :: Task, :: TSt(..), :: Store, :: HTTPRequest, :: Config
from TSt import :: ChangeLifeTime, :: StaticInfo(..), :: Workflow
......@@ -10,7 +11,8 @@ from TSt import qualified createTaskInstance
import Types
from TaskTree import :: TaskTree, :: TaskInfo, ::TaskProperties(..), :: TaskSystemProperties(..), :: TaskWorkerProperties, :: TaskManagerProperties(..), :: TaskPriority(..), ::TaskParallelType(..)
from Time import :: Timestamp
from Time import :: Timestamp, :: Clock(..), clock
from Random import genRandInt
from UserDB import qualified getUser
......@@ -53,7 +55,12 @@ where
= (TaskFinished d,tst)
getRandomInt :: Task Int
getRandomInt = mkInstantTask "getRandomInt" getRandomInt`
where
getRandomInt` tst
# (Clock seed, tst) = accWorldTSt clock tst
= (TaskFinished (hd (genRandInt seed)), tst)
spawnProcess :: !UserName !Bool !(Task a) -> Task (ProcessRef a) | iTask a
spawnProcess username activate task = mkInstantTask "spawnProcess" spawnProcess`
......
......@@ -23,6 +23,10 @@ getUsers :: Task [User]
* Finds all users (user id + display name) who have the given role
*/
getUsersWithRole :: !Role -> Task [User]
/*
* Lists all usernames
*/
getUserNames :: Task [UserName]
/**
* Looks up the corresponding display names for a list of user ids
*/
......
......@@ -25,7 +25,14 @@ getUsers = mkInstantTask "getUsers" (mkTaskFunction UserDB@getUsers)
getUsersWithRole :: !Role -> Task [User]
getUsersWithRole role = mkInstantTask "getUsersWithRole" (mkTaskFunction (UserDB@getUsersWithRole role))
getUserNames :: Task [UserName]
getUserNames = mkInstantTask "getUserNames" getUserNames`
where
getUserNames` tst
# (users,tst) = UserDB@getUsers tst
= (TaskFinished (map toUserName users), tst)
getDisplayNames :: ![UserName] -> Task [DisplayName]
getDisplayNames usernames = mkInstantTask "getDisplayNames" (mkTaskFunction (UserDB@getDisplayNames usernames))
......
......@@ -43,6 +43,8 @@ transformResult :: !(a -> b) !(Task a) -> Task b | iTask a & iTask b
//Synonym for (return Void)
stop :: Task Void
//Random choice
randomChoice :: ![a] -> Task a | iTask a
//Task delegation
class (@:) infix 3 u :: u !(LabeledTask a) -> Task a | iTask a
......
......@@ -3,7 +3,7 @@ implementation module CommonCombinators
* This module contains a collection of handy iTasks combinators defined in terms of the basic iTask combinators
* with Thanks to Erik Zuurbier for suggesting some of the advanced combinators
*/
import StdBool, StdList,StdOrdList, StdTuple, StdGeneric, StdMisc
import StdBool, StdList,StdOrdList, StdTuple, StdGeneric, StdMisc, StdInt, StdClass
from StdFunc import id, const
from TSt import :: Task(..), :: TaskDescription(..), :: TSt{..}, :: TaskInfo{..}, :: StaticInfo{..}, :: Workflow, :: ChangeLifeTime,:: HTTPRequest, :: Config
......@@ -15,7 +15,7 @@ from TaskTree import :: TaskTree, :: TaskParallelInfo{..}, :: TaskParallelType{.
from CommonDomain import :: Note
import TaskTree
import SystemTasks, InteractionTasks, UserDBTasks, CoreCombinators, TuningCombinators, LiftingCombinators
import SystemTasks, InteractionTasks, UserDBTasks, CoreCombinators, TuningCombinators, LiftingCombinators, ExceptionCombinators
import Util, Either
import GenVisualize, GenUpdate
......@@ -187,6 +187,10 @@ transformResult fun task = "transformResult" @>> (task >>= \a -> return (fun a))
stop :: Task Void
stop = "stop" @>> return Void
randomChoice :: ![a] -> Task a | iTask a
randomChoice [] = throw "Cannot make a choice from an empty list"
randomChoice list = getRandomInt >>= \i -> return (list !! (i rem (length list)))
sortByIndex :: ![(Int,a)] -> [a]
sortByIndex [] = []
sortByIndex [(i,v):ps] = sortByIndex [(is,vs) \\ (is,vs) <- ps | is < i] ++ [v] ++ sortByIndex [(is,vs) \\ (is,vs) <- ps | is > i]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment