examples/example-4.ss
; Example 4 - Classes

(require (planet "simulation-with-graphics.ss"
                 ("williams" "simulation.plt")))
(require (planet "random-distributions.ss"
                 ("williams" "science.plt")))

(define n-attendants 2)
(define attendant #f)

(define-process-class generator%
  (init-field (n 1000))
  (do ((i 0 (+ i 1)))
      ((= i n) (void))
    (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)))

(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)))
   (print (history-plot (variable-history 
                         (send attendant queue-variable-n))))))

(run-simulation 1000)