This file is indexed.

/usr/share/doc/python-simpy-doc/html/_sources/Manuals/SimPyOO_API.txt is in python-simpy-doc 2.3.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
=============================================================
SimPy's Object Oriented API
=============================================================

:Authors: - Klaus Muller <Muller@users.sourceforge.net>
:SimPy release: |release|
:Python version: 2.6 and later
:Date: |today|

.. contents:: Contents
   :depth: 2

.. highlight:: python
   :linenothreshold: 12

Introduction
=============

This document describes the object oriented (OO) programming
interface introduced with SimPy 2.0. This
is an add-on to the existing API, an alternative API. There is full backward compatibility:

Motivation
-------------

Many simulation languages support a procedural modelling style. Using them,
problems are decomposed into procedures (functions, subroutines) and either
represented by general components, such as queues, or represented in code
with data structures.

There are fundamental problems with using the procedural style of
modelling and simulation. Procedures do not correspond to real world
components. Instead, they correspond to methods and algorithms.
Mapping from the real (problem) world to the model and back is difficult
and not obvious, particularly for users expert in the problem domain, but
not in computer science. Perhaps the greatest limitation of the procedural
style is the lack of model extensibility. The only way in this style
to change simulation models is through functional extension. One can
add structural functionality but not alter any of its basic processes.

Right from its beginning, SimPy, on the other hand, has supported an
**object oriented approach** to simulation modelling.
In SimPy, models can be implemented as collections of autonomous,
cooperating objects.
These objects are self-sufficient and independent. The actions on these
objects are tied to the objects and their attributes. The object-oriented
capabilities of Python strongly support this encapsulation.

Why does this matter for simulation models? It helps with the mapping from
real-world objects and their activities to modelled objects and activities,
and back. This not only reduces the complexity of the models, it also
makes for easier validation of models and interpretation of simulation
results in real world terms.

The new API allows different, often more concise, cleaner program patterns.
It strongly supports the development of libraries
of model components for specific real world domains. It also supports
the re-use and extension of models when model specifications change.
In particular larger SimPy programs written with the advanced OO API should be
easier to maintain and extend. Users are advised to familiarize themselves
with this programming paradigm by reading the models in the
`SimPyModels` folder. Most of them are provided in two implementations, i.e. in the
existing and in the OO API. Similarly, the programs in the Bank tutorials
are provided with both APIs.

The advanced OO API has been developed very elegantly by Stefan Scherfke and
Ontje L |uumlaut| nsdorf, starting from SimPy 1.9. Thanks, guys, for this great job!

.. |uumlaut| unicode:: U+00FC
   :trim:

Readers of this document should be familiar with the basics of SimPy and
have read at least "Basic SimPy - Manual For First Time Users". They should
also know how subclassing is done in Python.

Basic SimPy OO API Design
==========================

A class ``Simulation`` has been added to module ``SimPy.Simulation``.
``SimulationTrace``, ``SimulationStep`` and  ``SimulationRT`` are subclasses of
``Simulation``. Multiple instances of these classes can co-exist in a SimPy program.

Backward compatibility
-----------------------------

Since SimPy 2.0, the package offers both the existing procedural API and an 
object-oriented API
where simulation capabilities are provided by instantiating ``Simulation``.
``SimulationTrace``, ``SimulationStep`` or  ``SimulationRT`` are subclasses of
``Simulation``.

Each ``SimulationXX`` instance has its own event list and therefore its own simulation time.
A ``SimulationXX`` instance can effectively be considered as a simulated, isolated parallel
world. Any *Process*, *Resource*, *Store*, *Level*, *Monitor*, *Tally* or *SimEvent*
instance belongs to one and only one world (i.e., ``Simulationxx`` instance).

The following program shows what this means for API and program structure::

    from SimPy.Simulation import *
    """Object Oriented SimPy API"""

    ## Model components -------------------------------

    class Car(Process):
        def run(self,res):
            yield request,self,res
            yield hold,self,10
            yield release,self,res
            print "Time: %s"%self.sim.now()

    ## Model and Experiment ---------------------------

    s=Simulation()
    s.initialize()
    r = Resource(capacity=5,sim=s)
    auto = Car(sim=s)
    s.activate(auto,auto.run(res=r))
    s.simulate(until=100)

Using the existing API, the following program is semantically the same and also works
under the OO version::

    from SimPy.Simulation import *
    """Traditional SimPy API"""

    ## Model components -------------------------------

    class Car(Process):
        def run(self,res):
            yield request,self,res
            yield hold,self,10
            yield release,self,res
            print "Time: %s"%now()

    ## Model and Experiment ---------------------------

    initialize()
    r = Resource(capacity=5)
    auto = Car()
    activate(auto,auto.run(res=r))
    simulate(until=100)

This full (backwards) compatibility is achieved by the automatic generation
of a *SimulationXX* instance "behind the scenes".

Models as SimulationXX subclasses
-----------------------------------

The advanced OO API can be used to generate model classes which are SimulationXX subclasses.
This ties a model and a SimulationXX instance together beautifully. See the following
example::

    ## CarModel.py
    from SimPy.Simulation import *
    """Advanced Object Oriented SimPy API"""

    ## Model components -------------------------------

    class Car(Process):
        def park(self):
            yield request,self,sim.self.parking
            yield hold,self,10
            yield release,self,sim.self.parking
            print "%s done at %s"%(self.name, self.sim.now())

    ## Model ------------------------------------------

    class Model(Simulation):
        def __init__(self,name,nrCars,spaces):
            Simulation.__init__(self)
            self.name = name
            self.nrCars = nrCars
            self.spaces = spaces
        def runModel(self):
            ## Initialize Simulation instance
            self.initialize()
            self.parking = Resource(name="Parking lot",unitName="spaces",
                                    capacity=self.spaces,sim=self)
            for i in range(self.nrCars):
                auto = Car(name="Car%s"%i, sim=self)
                self.activate(auto, auto.park())
            self.simulate(until=100)

    if __name__=="__main__":

        ## Experiment ----------------------------------

        myModel = Model(name="Experiment 1", nrCars=10, spaces=5)
        myModel.runModel()
        print myModel.now()

class ``Model`` here is a subclass of ``Simulation``. Every model execution, i.e. call to
``runModel``, reinitializes the simulation (creates an empty event list and sets
the time to 0) (see line 24). ``runModel`` can thus be called repeatedly for multiple runs of
the same experiment setup::

    if __name__=="__main__":

        ## Experiments ---------------------------------

        myModel = Model(name="Experiment 1",nrCars=10,spaces=5)
        for repetition in range(100):

        ## One Experiment -------------------------------

            myModel.runModel()
            print myModel.now()

Model extension by subclassing
---------------------------------

With the advanced OO API, it is now very easy and clean to extend a model by subclassing. This
effectively allows the creation of model libraries.

For example, the model in the previous example can be extended to one in which also vans
compete for parking spaces. This is done by importing the ``CarModel`` module
and subclassing ``Model`` as follows::

    ## CarModelExtension.py

    ## Model components -------------------------------

    from CarModel import *

    class Van(Process):
        def park(self):
            yield request,self,sim.self.parking
            yield hold,self,5
            yield release,self,sim.self.parking
            print "%s done at %s"%(self.name,self.sim.now())

    ## Model ------------------------------------------

    class ModelExtension(Model):
        def __init__(self,name,nrCars,capacity,spaces,nrTrucks):
            Model.__init__(self,name=name,nrCars=nrCars,spaces=spaces)
            self.nrTrucks = nrTrucks

        def runModel(self):
            self.initialize()
            r = Resource(capacity=self.resCapacity,sim=self)
            for i in range(self.nrCars):
                auto = Car(name="Car%s"%i,sim=self)
                self.activate(auto,auto.park())
            for i in range(self.nrTrucks):
                truck = Van(name="Van%s"%i,sim=self)
                self.activate(truck,truck.park())
            self.simulate(until=100)

    ## Experiment ----------------------------------

    myModel1 = ModelExtension(name="Experiment 2",nrCars=10,spaces=5,nrTrucks=3)
    myModel1.runModel()

Let's walk through this:

*Line 5*:
    This import makes available all the objects of SimPy.Simulation and the ones defined by
    the ``CarModel`` module (class ``Car`` and class ``Model``).

*Lines 7-12*:
    Addition of a ``Van`` class with a ``park`` PEM.

*Line 16*:
    Definition of a subclass ``ModelExtension`` which extends class ``Model``.

*Lines 17-18*:
    Initialization of the model class (``Model``) from which ``ModelExtension``
    is derived. When subclassing a class in Python, this is always necessary:
    Python does **not** automatically initialize the super-class.

*Lines 21-30*:
    Defines a ``runModel`` method for ``ModelExtension`` which also generates
    and activates ``Van`` objects.

API changes
============

Module ``SimPy.Simulation``
-----------------------------------

The only change to the API of module ``SimPy.Simulation``
is the addition of class ``Simulation``::

 Module SimPy.Simulation:
    ################ Unchanged ################
    ## yield-verb constants --------------------
    get
    hold
    passivate
    put
    queueevent
    release
    request
    waitevent
    waituntil
    ## version constant ------------------------
    version
    ## classes ---------------------------------
    FatalSimerror
    Simerror
    ################ Added ################
    Simulation

Thus, after the import::

   from SimPy.Simulation import *

class ``Simulation`` is available to a program.

Actually,::

   from SimPy.Simulation import Simulation

is sufficient and even clearer.

class ``Simulation``
~~~~~~~~~~~~~~~~~~~~~

The simulation capabilities of a model are provided by instantiating class
``Simulation`` like this::

  from SimPy.Simulation import *

  aSimulation = Simulation()
  ## model code follows

Better OO programming style is actually to define a model class which inherits
from ``Simulation``::

    from SimPy.Simulation import *

    class MyModel(Simulation):
        def run(self):
            self.initialize()
            ## model code follows

    myMo = MyModel()
    myMo.run()

The ``self.initialize()`` is not really necessary, as the ``Simulation`` instance is
initialized at generation time. If method ``run`` for a model (here ``myMo`` ) is
executed more than once, e.g. for running a simulation repeatedly, ``self.initialize()``
resets the model to an empty event list and simulation time 0.

Methods of class Simulation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class ``Simulation`` has these methods::

  class Simulation:
    ## Methods ----------------------------------
    __init__(self)
    initialize(self)
    now(self)
    stopSimulation(self)
    allEventNotices(self)
    allEventTimes(self)
    activate(self, obj, process, at='undefined', delay='undefined', prior=False)
    reactivate(self, obj, at='undefined', delay='undefined', prior=False)
    startCollection(self, when=0.0, monitors=None, tallies=None)
    simulate(self, until=0)

The semantics and parameters (except for ``self``) of the methods are identical to those of the non-OO
``SimPy.Simulation`` functions of the same name. For example, to get the current
simulation time of a Simulation object ``so``, the call is::

  tcurrent = so.now()

..
    New classes
    ------------

    class ``Simulation``
    ~~~~~~~~~~~~~~~~~~~~~

    The simulation capabilities are provided by instantiating class ``Simulation``. The three
    other SimPy run modes (``SimulationTrace``, ``SimulationRT`` and ``SimulationStep``) are
    subclasses of ``Simulation``.

    Methods of class ``Simulation``
    ++++++++++++++++++++++++++++++++

    The semantics and parameters of the methods are identical to those of the non-OO
    ``SimPy.Simulation`` functions of the same name.

    - *initialize*

    - *activate*

    - *reactivate*

    - *simulate*

    - *now*

    - *stopSimulation*

    - *startCollection*

    - *allEventNotices*

    - *allEventTimes*

    Example calls (snippet)::

       from SimPy.Simulation import *
       s = Simulation()
       s.initialize()
       s.simulate(until=100)

Module ``SimPy.SimulationTrace``
----------------------------------------------

The only change to the API of module ``SimPy.SimulationTrace``
is the addition of class ``SimulationTrace``::

  Module SimPy.SimulationTrace:
    ################ Unchanged ################
    ## yield-verb constants --------------------
    get
    hold
    passivate
    put
    queueevent
    release
    request
    waitevent
    waituntil
    ## version constant ------------------------
    version
    ## classes ---------------------------------
    FatalSimerror
    Simerror
    Trace
    ################ Added ################
    SimulationTrace


class ``SimulationTrace``
~~~~~~~~~~~~~~~~~~~~~~~~~

The simulation capabilities of a model with tracing are provided by instantiating class
``SimulationTrace`` like this::

  from SimPy.SimulationTrace import *

  aSimulation = SimulationTrace()
  ## model code follows

Again, better OO programming style is actually to define a model class which inherits
from Simulation::

    from SimPy.SimulationTrace import *

    class MyModel(SimulationTrace):
        def run(self):
            self.initialize()
            # model code follows

    myMo = MyModel()
    myMo.run()

class ``SimulationTrace`` is a subclass of ``Simulation`` and thus
provides the same methods, albeit with tracing added.

The semantics and parameters of the methods are identical to those of the non-OO
``SimPy.SimulationTrace`` functions of the same name.

Methods and attributes of class SimulationTrace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  class SimulationTrace:
     ## Methods ----------------------------------
     __init__(self)
     initialize(self)
     now(self)
     stopSimulation(self)
     allEventNotices(self)
     allEventTimes(self)
     activate(self, obj, process, at='undefined', delay='undefined', prior=False)
     reactivate(self, obj, at='undefined', delay='undefined', prior=False)
     startCollection(self, when=0.0, monitors=None, tallies=None)
     simulate(self, until=0)
     ## trace attribute ---------------------------
     trace

Attribute ``trace``
++++++++++++++++++++

An initialization of class ``SimulationTrace`` generates an instance of
class ``Trace``. This becomes an attribute ``trace`` of the ``SimulationTrace``
instance.

``Trace`` methods
+++++++++++++++++++++

The semantics and parameters of the ``Trace`` methods are identical to those of
the non-OO ``SimPy.SimulationTrace`` ``trace`` instance of the same name.

- trace.start(self)

Example::

    s.trace.start()

- trace.stop(self)

- trace.treset(self)

- trace.tchange(self, \*\*kmvar)

- trace.ttext(self,par)

Example calls (snippet)::

   from SimPy.SimulationTrace import *
   s = SimulationTrace()
   s.initialize()
   s.trace.ttext("Here we go")

Again, note that you have to qualify the ``trace`` instance (see e.g. the last line
of the snippet) with the ``SimulationTrace`` instance, here ``s``.

Module ``SimPy.SimulationRT``
----------------------------------------------

class ``SimulationRT``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The simulation capabilities plus real time  synchronization are provided by instantiating
class ``SimulationRT``.

Methods of class ``SimulationRT``
+++++++++++++++++++++++++++++++++++++++

The ``SimulationRT`` subclass adds two methods to those inherited
from ``Simulation``.

The semantics and parameters of the methods are identical to those of the non-OO
``SimPy.SimulationRT`` functions of the same name.

- rtnow

- rtset

Example calls (snippet)::

   from SimPy.SimulationRT import *
   class Car(Process):
      def __init__(self):
         Process.__init__(self, sim=self.sim)
      def run(self):
         print self.sim.rtnow()
         yield hold,self,10


class ``SimulationStep``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The simulation capabilities plus event stepping are provided by instantiating
class ``SimulationStep``.

Methods of class ``SimulationStep``
+++++++++++++++++++++++++++++++++++++++

The ``SimulationStep`` subclass adds three methods to those inherited
from ``Simulation``.

The semantics and parameters of the methods are identical to those of the non-OO
``SimPy.SimulationStep`` functions of the same name.

- startStepping

- stopStepping

- simulateStep

Example call (snippet)::

   from SimPy.SimulationStep import *
   s = SimulationStep()
   s.initialize()
   s.simulateStep(until=100, callback=myCallBack)


Classes with a ``SimulationXX`` attribute
------------------------------------------

All SimPy entity (*Process*, *Resource*, *Store*, *Level*, *SimEvent*)
and monitoring (*Monitor*, *Tally*) classes have time-related functions.
In the OO-API of SimPy, they therefore have a ``.sim`` attribute which is a
reference to the *SimulationXX* instance to which they belong. This association
is made by providing that reference as a parameter to the constructor of the class.

.. Important::
   **All class instances instances must refer to the same SimulationXX instance,
   i.e., their .sim attributes must have the same value. That value must be the
   reference to the SimulationXX instance.** Any deviation from this will
   lead to strange mis-functioning of a SimPy script.

The constructor calls (signatures) for the classes in question thus change as follows:

class ``Process``
~~~~~~~~~~~~~~~~~~

::

  Process.__init__(self, name = 'a_process', sim = None)

Example 1 (snippet)::

  class Car(Process):
      def drive(self):
         yield hold,self,10
         print "Arrived at", self.sim.now()

  aSim = Simulation()
  aSim.initialize()
  c=Car(name="Mine", sim=aSim)

Example 2, with an ``__init__`` method (snippet):

  class Car(Process):
     def __init__(self,name):
         Process.__init__(self,name=name, sim=self.sim)

  aSim = Simulation()
  aSim.initialize()
  c=Car(name="Mine", whichSim=aSim)

class ``Resource``
~~~~~~~~~~~~~~~~~~~~~

::

    Resource.__init__(self, capacity = 1, name = 'a_resource', unitName = 'units',
                 qType = FIFO, preemptable = 0, monitored = False,
                 monitorType = Monitor,sim=None)

Example (snippet)::

  aSim = Simulation()
  aSim.initialize()
  res=Resource(name="Server",sim=aSim)

classes ``Store`` and ``Level``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

    Store.__init__(self, name = None, capacity = 'unbounded', unitName = 'units',
                putQType = FIFO, getQType = FIFO,
                monitored = False, monitorType = Monitor, initialBuffered = None,
                sim = None)

::
    Level.__init__(self, name = None, capacity = 'unbounded', unitName = 'units',
                putQType = FIFO, getQType = FIFO,
                monitored = False, monitorType = Monitor, initialBuffered = None,
                sim = None)

Example (snippet)::

  aSim = Simulation()
  aSim.initialize()
  buffer = Store(name="Parts",sim=aSim)

class ``SimEvent``
~~~~~~~~~~~~~~~~~~~~~~

::

  SimEvent.__init__(self, name = 'a_SimEvent', sim = None)

Example (snippet)::

  aSim = Simulation()
  aSim.initialize()
  evt = SimEvent("Boing!", sim=aSim)

classes ``Monitor`` and ``Tally``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

    Monitor.__init__(self, name = 'a_Monitor', ylab = 'y', tlab = 't', sim = None)

::
    Tally.__init__(self, name = 'a_Tally', ylab = 'y', tlab = 't', sim = None)

Example (snippet)::

  aSim = Simulation()
  aSim.initialize()
  myMoni = Monitor(name="Counting cars", sim=aSim)