Package SimPy :: Module stepping

[hide private]
[frames] | no frames]

# Source Code for Module SimPy.stepping

     1  # coding=utf-8

     2  """

     3  This is a small utility for interactively stepping through a simulation.

     4

     5  Usage:

     6      import stepping

     7      (simulation model)

     8      stepping.stepping(Globals) # instead of 'simulate(until = endtime)

     9  """
    10  # $Revision: 464 $ $Date: 2010-04-05 05:59:53 +0200 (Mo, 05 Apr 2010) $ kgm

    11  # SimPy version: 2.1

    12 -def stepping(glob):
    13      asim = glob.sim
    14      help = {'s':"next event",'r':"run to end",'e':"end run",

    15             '<time>':"skip to event at <time>",'l':"show eventlist",

    16             'p<name>':"skip to event for <name>",'h':"help"}
    17      evlist = asim._timestamps
    18      while True:
    19          if not evlist:
    20              print "No more events at t=%s"%asim.now()
    21              break
    22          tEvt = evlist[0][0]
    23          who = evlist[0][2]
    24          while evlist[0][3]: #skip cancelled event notices
    25              step()
    26          print "\nTime now: %s, next event at: t=%s for process: %s "\

    27                %(asim.now(),tEvt,who.name)
    28

    29          while True:
    30              cmd = raw_input("Command ('h' for help): ")
    31              if cmd == "h":
    32                  for i in help:
    33                      print i, ":", help[i]
    34              else:
    35                  break
    36          try:
    37              nexttime = float(cmd)
    38              while asim.peek() < nexttime:
    39                  asim.step()
    40          except:
    41              if cmd == 's':
    42                  asim.step()
    43              elif cmd == 'r':
    44                  while evlist:
    45                      asim.step()
    46                  print "Run ended at t=%s"%asim.now()
    47                  break
    48              elif cmd == 'e':
    49                  asim.stopSimulation()
    50                  break
    51              elif cmd == 'l':
    52                  print "Events scheduled: \n%s"%asim.allEventNotices()
    53              elif cmd[0] == 'p':
    54                  while evlist and evlist[0][2].name <> cmd[1:]:
    55                      asim.step()
    56              else:
    57                  print "%s not a valid command" % cmd
    58

    59  if __name__ == "__main__":
    60      from SimPy.SimulationTrace import *
    61

    62 -    class Car(Process):
    63 -        def drive(self, speed, howlong):
    64              going = speed
    65              yield hold,self,howlong
    66              going = 0
    67

    68      initialize()
    69      b = Car(name = "Beemer")
    70      activate(b, b.drive(speed = 180, howlong = 45))
    71      c = Car(name = "Caddy")
    72      activate(c, c.drive(speed = 120, howlong = 120), at = 25)
    73      stepping(Globals)
    74