examples/example-2.rkt
#lang racket
;;; Example 2 - Resources

(require (planet williams/simulation/simulation))

;;; n-attendants : exact-positive-integer? = 2
;;; attendant : (or/c resource? false/c) = #f
(define n-attendants 2)
(define attendant #f)

;;; (generator n) -> void?
;;;   n : exact-nonnegative-integer?
(define (generator n)
  (for ((i (in-range n)))
    (wait (random-exponential 4.0))
    (schedule #:now (customer i))))

;;; process (customer i) -> void?
;;;   i : exact-nonnegative-integer?
(define-process (customer i)
  (printf "~a: customer ~a enters~n" (current-simulation-time) i)
  (request attendant #:units 1)
  (printf "~a: customer ~a gets an attendant~n" (current-simulation-time) i)
  (work (random-flat 2.0 10.0))
  (relinquish attendant)
  (printf "~a: customer ~a leaves~n" (current-simulation-time) i))

;;; (run-simulation n) -> void?
;;;   n : exact-nonnegative-integer?
(define (run-simulation n)
  (with-new-simulation-environment
   (set! attendant (make-resource n-attendants))
   (schedule #:at 0.0 (generator n))
   (start-simulation)))

;;; Run the simulation for 10 customers.
(run-simulation 10)