_Simulating the Denver Airport Automated Baggage System_
by John Swartz

Listing One
; <<<>>> Simulation of the Denver Airport Automated Baggage System
; <<<>>> Programmer:  John Swartz     
; <<<>>> The design is a based on the design sketched in
; <<<>>> chapters 21, 22, 23 and 24 of Goldberg/Robson's 
; <<<>>> Smalltalk-80: The Language and Its Implementation.

(defun express (time chain) 
          (cond
               ((null chain) (print "exit function express"))
               (t (progn 
                    (setq cc (list time 'T (car  chain)))
;                   (print cc)
                    (denver :enqueue cc)
                    (express (1+ time) (cdr chain))
                    ))))
(defun service (item)     ;  <<<===   process events off the
event queue  !!!
          (case (cadr item)
               ( 'B    (subst (list (+ 600 (car item))  (denver:query) 
                    (car (telecarts :get 'bags))) item  item))
               ('"gateOne" (setq vv (car (last item)))
                    (gateOne :accept  vv )
                         ())
               ('"gateTwo" (setq vv (car (last item)))
                    (gateTwo :accept  vv )
                         ())
               ('"gateThree" (setq vv (car (last item)))
                    (gateThree :accept  vv )
                         ())
               ('"gateFour" (setq vv (car (last item)))
                    (gateFour :accept  vv )
                         ())
               ('"gateFive" (setq vv (car (last item)))
                    (gateFive :accept  vv )
                         ())
               ( 'A 
               (setq pp (list  '"Airliner arrival at system time " (car item)))
                    (print ())
                    (print pp)
                         (setq rr (denver :report))
                         (print (cadar rr))
                         (if (> 300 (cadar rr)) 
                    (print "Simulation terminating because of deadlock"))
                         (mapcar print rr)
                         (if (> 300 (cadar rr)) (exit))
                    (dotimes (x 400 t) ;  <<<===  send 500 carts to airplane
                         (setq zz
                         (list (+ 1200 (car item) x)
                              'T
                              (car (telecarts  :get 'bags))
                              ))
                         (denver :enqueue zz))
               (or  
                     (and (gateone :test) 
                          (gateone :set)
                           (print "active gate is gateOne")
                         (express (+ 600 (car item)) (gateOne:dispatch))
                         (gateOne :initialize)
;                        (mapcar print (denver :report))
                          (denver :assign '"gateOne")
                         (subst (list (+ 1800 (car item)) 'F
                         "gateOne") item item))
                    (and (gatetwo :test) 
                           (print "active gate is gateTwo")
                         (express (+ 600 (car item)) (gateTwo:dispatch))
                         (gateTwo :initialize)
;                        (mapcar print (denver :report))
                          (gatetwo :set)
                          (denver :assign '"gateTwo")
                         (subst (list (+ 1800 (car item)) 'F
                         "gateTwo") item item))
                    (and (gatethree :test) 
                          (gatethree :set)
                           (print "active gate is gateThree")
                         (express (+ 600 (car item)) (gateThree:dispatch))
                         (gateThree :initialize)
                          (denver :assign '"gateThree")
                         (subst (list (+ 1800 (car item)) 'F
                         "gateThree") item item))
                    (and (gatefour :test) 
                          (gatefour :set)
                           (print "active gate is gateFour")
                         (express (+ 600 (car item)) (gateFour:dispatch))
                         (gateFour :initialize)
                          (denver :assign '"gateFour")
                         (subst (list (+ 1800 (car item)) 'F
                         "gateFour") item item))
                    (and (gatefive :test) 
                          (gatefive :set)
                           (print "active gate is gateFive")
                          (denver :assign '"gatefive")
                         (subst (list (+ 1800 (car item)) 'F
                         "gateFive") item item))))
               (  'F  (setq pp (list '"Airliner departure from gate  "
                    (car (last item))  '"  at simulation time  " (car item)))
                    (print ())
                    (print pp)
                    (mapcar print (denver :report))
                    (case (caddr item)
                         ('"gateOne" (gateOne :reset))
                         ('"gateTwo" (gateTwo :reset))
                         ('"gateThree" (gateThree :reset))
                         ('"gateFour" (gateFour :reset))
                         ('"gateFive" (gateFive :reset))
                         )
                    ())
               (  'T  (telecarts :put 'bags (car (last item)))
                    ())
                    ))
(defun actuate (xactions)

;    locate next event on the eventQueue .... and move it to front of same.
     (cons      (assoc (apply 'min (mapcar car xactions)) xactions)
          (remove   (assoc (apply 'min (mapcar car xactions)) xactions)
               xactions)))
(expand 500)
(print (mem))
;(load "trace.lsp")
;(trace 'express)
;    Create classes
(setq simulation (class :new '(resources currentTime eventQueue activeGate)))
(simulation :answer :isnew '()
     '((setq resources nil) 
       (setq currentTime nil)
       (setq eventQueue nil)
       (setq activeGate "gateOne")  self))
(simulation :answer :enqueue '(event)
          '((setq eventQueue (cons event eventQueue) )  ))
(simulation :answer :abridge '()
          '((setq eventqueue
          (remove (assoc (apply 'min (mapcar car eventqueue)) eventqueue)
               eventqueue))
               ))
;         *******
;         this is the primary method of the simulation
;         ********
(simulation :answer :dequeue '(&aux tick)
          '((setq tick (car (actuate eventqueue)))
               ))
;            (self :abridge )))
(simulation :answer :revise '()
          '((setq eventqueue (cdr (actuate eventqueue))) self))
(simulation :answer :assign '(gate)
          '((setq activegate gate) self))
(simulation :answer :display '()
          '((mapcar print eventqueue) self))
(simulation :answer :query '(&aux active)
          '((setq active activegate)))
(simulation :answer :report '(&aux line)
          '((setq line 
          (list 
          (list '"telecarts at conveyorHead" (length (telecarts:get 'bags)))
               (list '"telecarts at  gateOne  " (gateone:display))
                (list '"telecarts at gateTwo  " (gatetwo:display))
               (list '"telecarts at gateThree  " ( gatethree:display))
               (list '"telecarts at gateFour   " (gatefour:display))
               (list '"telecarts at gateFive   " (gatefive:display))
               ))))
(load "resource.lsp")
;(print "\n simulation")
;(print (simulation :show))

(setq exponential (class  :new '(mu type)))
(exponential :answer :isnew '() 
     '((setq mu nil) 
          (setq type nil) self))
(exponential  :answer  :parameter  '(value)
          '((setq mu value) self ))
(exponential :answer :next '( &aux  time)
          '((setq time (* mu (- ( log (random 1.0)))))))
(print "\n exponential")
(print (exponential :show))
(print "resource")
; vvv  instantiate the simulation  v v v
(setq denver (simulation :new))
(setq terminalSide (exponential :new))
(terminalSide :parameter  '10)
(setq ss 0)
(do ((x (terminalside :next) (+ x (terminalside :next)))) ((> x 6000 )) 
          (if (zerop ss) (setq ss x))
           (setq a (list x 'B))
          (denver :enqueue a))
(setq flightSide (exponential :new))
(flightSide :parameter  '800 )
(do ((x (flightside :next) (+ x (flightside :next))))((> x
15000)) (setq a (list x 'A))
          (denver :enqueue a))
;    !!! process trasactions off the event queue  
(setq ww (list ' "Begin Denver International Airport Automated
Baggage System simulation at time   " ss))
(print ww)
(mapcar print (denver :report))
(dotimes (x 4000 t)
(setq ee (denver :dequeue))
(setq xx (service ee))
( if xx (denver :enqueue xx))
(denver :abridge)
)
(setq ww (list '"Automated Baggage System simulation terminated at time   "
          (car ee)))
(print ww)
(mapcar print (denver :report))

(print (mem))
(exit)
;    ***********
;    this is the *resource* module
;    ***********
;    Create classes
(setq resource (class :new   '() '(pending buffer 
amountAvailable)))
(resource :answer :isnew '()
     '((setq pending 0)
       (setq buffer ())
       (setq amountAvailable  0) self))
(resource :answer :initialize '(tag size)  
      '((setq a ())
        (dotimes (x size a)(setq a (cons x a)))
          (setq buffer
     (cons (cons tag  a) buffer))
          a))
;    ******
;create resource instance
;    *******
(setq telecarts (resource :new))
(telecarts :initialize 'bags '3000)
(print "shrink method  follows:")
(resource :answer :shrink '(tag)
     '((setq zz (cddr  (assoc tag  buffer)))
       (setq buffer ()) 
       (setq  buffer (cons (cons tag zz) buffer)) 
          zz))
(print "this is the :get function")
(resource :answer :get '(tag &aux entry)
     '((cond ((setq entry (assoc tag buffer))
          (cdr entry))
          (t ()))
          (self :shrink tag)))
(resource :answer :put '(tag entity)
     '((setq zz (assoc tag buffer))
       (setq buffer ())
       (setq zz (cons entity (cdr zz)))
       (setq buffer (cons (cons tag zz) buffer ))
               zz))
(setq arrivalGates (class :new  '(semaphore teleQueue)))
(arrivalGates :answer :isnew '()
     '((setq semaphore ())
       (setq teleQueue ()) self))
(arrivalGates :answer :accept '(tcart)
     '((setq telequeue (cons tcart telequeue)) self))
(arrivalGates :answer :set '()
          '((setq semaphore t)  self ))
;            (setq telequeue ()) 
(arrivalGates :answer :reset '()
          '((setq semaphore ()) self))
(arrivalGates :answer :test '(&aux flag)
          '((setq flag (not  semaphore))     
               ))
(arrivalGates :answer :display '(&aux val)
          '((setq val (length telequeue)) ))
(arrivalGates :answer :dispatch '(&aux train)
          '((setq train telequeue) ))
(arrivalGates :answer :initialize '()
            '((setq telequeue ()) self))
(setq gateOne (arrivalGates :new))
(setq gateTwo (arrivalGates :new))
(setq gateThree (arrivalGates :new))
(setq gateFour (arrivalGates :new))
(setq gateFive (arrivalGates :new))
(print "gateOne")
(print (gateOne :show))

Figure 2:
Class simulation
:enqueue
:dequeue
:abridge
:revise
:assign
:display
:query
:report

Class exponential
:parameter
:next

Class resource
:initialize
:get
:put
:shrink

Class arrivalGates
:accept
:set
:test
:display
:dispatch


Figure 5:

XLISP version 1.6, Copyright (c) 1985, by David Betz
; loading "4ddj.lsp"
Nodes:       468000
Free nodes:  464136
Segments:    468
Allocate:    1000
Total:       4928506
Collections: 2
Sat Jul 13 20:36:44 1996
NIL
; loading "resource.lsp"
"resource"
("Begin Denver International Airport Automated Baggage System simulation at time   " 308.732)
("telecarts at conveyorHead" 1999)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
NIL
("Airliner arrival at system time  " 650.452)
("telecarts at conveyorHead" 1975)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
"active gate is gateOne"
NIL
("Airliner departure from gate  " "gateOne" "  at simulation time  " 2450.45)
("telecarts at conveyorHead" 1795)
("telecarts at  gateOne  " 136)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
NIL
("Airliner arrival at system time  " 3366.34)
("telecarts at conveyorHead" 1720)
("telecarts at  gateOne  " 229)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
"active gate is gateOne"
NIL
("Airliner arrival at system time  " 3577.37)
("telecarts at conveyorHead" 1394)
("telecarts at  gateOne  " 16)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
"active gate is gateTwo"
NIL
("Airliner arrival at system time  " 3816.76)
("telecarts at conveyorHead" 1064)
("telecarts at  gateOne  " 33)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
"active gate is gateThree"
NIL
("Airliner arrival at system time  " 4570.28)
("telecarts at conveyorHead" 927)
("telecarts at  gateOne  " 72)
("telecarts at gateTwo  " 29)
("telecarts at gateThree  " 13)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
"active gate is gateFour"
NIL
("Airliner arrival at system time  " 4746.94)
("telecarts at conveyorHead" 782)
("telecarts at  gateOne  " 72)
("telecarts at gateTwo  " 29)
("telecarts at gateThree  " 25)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
"active gate is gateFive"
NIL
("Airliner departure from gate  " "gateOne" "  at simulation time  " 5166.34)
("telecarts at conveyorHead" 1025)
("telecarts at  gateOne  " 72)
("telecarts at gateTwo  " 29)
("telecarts at gateThree  " 68)
("telecarts at gateFour   " 0)
("telecarts at gateFive   " 0)
NIL
("Airliner arrival at system time  " 5316.35)
("telecarts at conveyorHead" 1174)
("telecarts at  gateOne  " 72)
("telecarts at gateTwo  " 29)
("telecarts at gateThree  " 69)
("telecarts at gateFour   " 18)
("telecarts at gateFive   " 0)
"active gate is gateOne"
NIL
("Airliner departure from gate  " "gateTwo" "  at simulation time  " 5377.37)
("telecarts at conveyorHead" 873)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 29)
("telecarts at gateThree  " 69)
("telecarts at gateFour   " 21)
("telecarts at gateFive   " 0)
NIL
("Airliner arrival at system time  " 5422.31)
("telecarts at conveyorHead" 872)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 29)
("telecarts at gateThree  " 69)
("telecarts at gateFour   " 21)
("telecarts at gateFive   " 0)
"active gate is gateTwo"
NIL
("Airliner arrival at system time  " 5425.1)
571
("telecarts at conveyorHead" 571)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 69)
("telecarts at gateFour   " 21)
("telecarts at gateFive   " 0)
NIL
("Airliner departure from gate  " "gateThree" "  at simulation time  " 5616.76)
("telecarts at conveyorHead" 270)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 69)
("telecarts at gateFour   " 21)
("telecarts at gateFive   " 0)
NIL
("Airliner arrival at system time  " 6152.14)
("telecarts at conveyorHead" 876)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 69)
("telecarts at gateFour   " 21)
("telecarts at gateFive   " 0)
"active gate is gateThree"
("Automated Baggage System simulation terminated at time   " 6211.94)
("telecarts at conveyorHead" 635)
("telecarts at  gateOne  " 0)
("telecarts at gateTwo  " 0)
("telecarts at gateThree  " 0)
("telecarts at gateFour   " 21)
("telecarts at gateFive   " 0)
Nodes:       468000
Free nodes:  262579
Segments:    468
Allocate:    1000
Total:       4932371
Collections: 116
Sat Jul 13 22:18:48 1996
NIL

*****************  end figure ***************

