Package SimPy :: Module SimulationGUIDebug

[hide private]
[frames] | no frames]

# Source Code for Module SimPy.SimulationGUIDebug

      1  from SimPy.SimulationStep import *
      2  from Tkinter import *
      3  import SimPy.SimulationStep, GUIDebug
      4
      5  # global variables
      6  _breakpoints = []
      7  _until = 0
      8  _callback = None
      9  _lastCommandIssued = ""
     10  _simStarted = False
     11  _registeredClasses = []
     12  _runMode = None
     13
     14  # run modes
     15  STEP = 1
     16  NO_STEP = 2
     17
     18  # register new object for windowing
     19 -def register(obj,hook=lambda :"",name=None):
     20
     21      global _registeredClasses
     22
     23      # if process subclass is given register it
     24      if type(obj) == TypeType and issubclass(obj, Process):
     25          _registeredClasses += [(obj,name,hook)]
     26
     27      # if instance of process is given register it
     28      elif issubclass(type(obj), Process):
     29          _guiCtrl.addNewProcess(obj,name,hook)
     30
     31      # if instance of Resource is given register it
     32      elif issubclass(type(obj), Resource):
     33          _guiCtrl.addNewResource(obj,name,hook)
     34
     35      # else create a generic window with hook
     36      else:
     37          _guiCtrl.addNewWindow(obj,name,hook)
     38
     39  # override activate to catch registered class instances
     40 -def activate(obj,process,at="undefined",delay="undefined",prior=False):
     41
     42      global _registeredClasses
     43
     44      SimPy.SimulationStep.activate(obj,process,at,delay,prior)
     45
     46      # if obj is instance of the class register it
     47      for c,n,h in _registeredClasses:
     48          if isinstance(obj, c):
     49              _guiCtrl.addNewProcess(obj,n,h)
     50
     51  # add to breakpoints
     52 -def newBreakpoint(newBpt):
     53
     54      global _breakpoints
     55      _breakpoints.append(newBpt)
     56      _breakpoints.sort()
     57
     58  # set the current run mode of simulation
     59 -def setRunMode(runMode):
     60
     61      global _runMode
     62      _runMode = runMode
     63
     64  # initialize the simulation and the GUI
     65 -def initialize():
     66
     67      SimPy.SimulationStep.initialize()
     68
     69      # create gui controller
     70      global _guiCtrl
     71      _guiCtrl = GUIDebug.GUIController()
     72
     73      # initialize run mode if not already set
     74      global _runMode
     75      if not _runMode:
     76          _runMode = STEP
     77
     78  # simulation function
     79 -def simulate(callback=lambda :None, until=0):
     80
     81      global _runMode
     82
     83      # print usage
     84      if( _runMode == STEP ):
     85          print "Breakpoint Usage:"
     86          print "  [c]   Continue simulation"
     87          print "  [s]   Step to next event"
     88          print "  [b #] Add new breakpoint"
     89          print
     90          print "  [q]   Quit debugger"
     91          print
     92
     93      # set global variables
     94      global _until
     95      _until = until
     96
     97      global _callback
     98      _callback = callback
     99
    100      # initialize to step command
    101      global _lastCommandIssued
    102      _lastCommandIssued = "s"
    103
    104      # only prompt user if we are in STEP mode
    105      if( _runMode == STEP): promptUser()
    106
    107      # quit if user entered 'q'
    108      if( _lastCommandIssued == 'q'):
    109          return
    110
    111      # begin simulation
    112      global _simStarted
    113      _simStarted = True
    114      startStepping()
    115      SimPy.SimulationStep.simulate(callback=callbackFunction,until=_until)
    116
    117  # check for breakpoints
    118 -def callbackFunction():
    119
    120      global _breakpoints,_runMode,_guiCtrl
    121
    122      # NO_STEP mode means we update windows and take no breaks
    123      # this is used for compatibility with REAL debuggers
    124      if( _runMode == NO_STEP ):
    125          _guiCtrl.updateAllWindows()
    126          return
    127
    128      if( 0 == len(_breakpoints) ):
    129          return
    130
    131      # this is a breakpoint
    132      if( now() >= _breakpoints[0] ):
    133
    134          # update gui
    135          _guiCtrl.updateAllWindows()
    136
    137          # remove past times from breakpoints list
    138          while( 0 != len(_breakpoints) and now() >= _breakpoints[0] ):
    139              _breakpoints.pop(0)
    140
    141          # call user's callback function
    142          global _callback
    143          _callback()
    144
    145          promptUser()
    146
    147  # prompt user for next command
    148 -def promptUser():
    149
    150      global _simStarted
    151
    152      # set prompt text
    153      prompt = '(SimDB) > '
    154
    155      # pause for breakpoint
    156      while( 1 ):
    157          input = raw_input( prompt )
    158
    159          # take a look at the last command issued
    160          global _lastCommandIssued
    161
    162          if 0 == len(input):
    163              input = _lastCommandIssued
    164
    165          _lastCommandIssued = input
    166
    167          # continue
    168          if( "c" == input ):
    169              break
    170
    171          # step
    172          elif( "s" == input ):
    173              global _breakpoints
    174              _breakpoints.insert(0,0)
    175              break
    176
    177          # add breakpoint
    178          elif( 0 == input.find("b")):
    179              try:
    180                  for i in eval( input[1:] + "," ):
    181                      newBreakpoint( int(i) )
    182              except SyntaxError:
    183                  print "missing breakpoint values"
    184
    185          # quit
    186          elif( "q" == input ):
    187              SimPy.SimulationStep.stopSimulation()
    188              return
    189
    190          else:
    191              print "  unknown command"
    192