examples/example-4.rkt
#lang racket
;;; Example 4 - Classes

(require (planet williams/simulation/simulation-with-graphics))

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

(define-process-class generator%
  (init-field (n 1000))
  (for ((i (in-range n)))
    (wait (random-exponential 4.0))
    (make-object customer% i)))

(define-process-class customer%
    (init-field i)
    (begin
      (send attendant request)
      (work (random-flat 2.0 10.0))
      (send attendant relinquish)))

;;; (run-simulation n) -> void?
;;;   n : exact-nonnegative-integer>
(define (run-simulation n)
  (with-new-simulation-environment
   (set! attendant (make-object resource% n-attendants))
   (make-object generator% n)
   (accumulate (variable-statistics (send attendant queue-variable-n)))
   (accumulate (variable-history (send attendant queue-variable-n)))
   (start-simulation)
   (printf "--- Example 4 - Classes ---~n")
   (printf "Maximum queue length = ~a~n"
           (variable-maximum (send attendant queue-variable-n)))
   (printf "Average queue length = ~a~n"
           (variable-mean (send attendant queue-variable-n)))
   (printf "Variance             = ~a~n"
           (variable-variance (send attendant queue-variable-n)))
   (printf "Utilization          = ~a~n"
           (variable-mean (send attendant satisfied-variable-n)))
   (printf "Variance             = ~a~n"
           (variable-variance (send attendant satisfied-variable-n)))
   (printf "~a~n" (history-plot (variable-history 
                                 (send attendant queue-variable-n))))))

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