asynchronousComboGenerator.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import select
  16. import sys
  17. import threading
  18. import pypdevs.accurate_time as time
  19. from pypdevs.util import DEVSException
  20. class AsynchronousComboGenerator(object):
  21. """
  22. The asynchronous combo generator: it generates events from file input
  23. The name no longer represents what it actually is, as previously it also generated input from stdin (denoting the 'combo').
  24. It does NOT use the default *interrupt()* calls for the threading backend, as this would require the generator to run
  25. on a different thread. The generator should be called at every iteration and its *getNextTime()* value should be taken into
  26. account by every *wait()* call.
  27. """
  28. def __init__(self, filename, backend):
  29. """
  30. Constructor.
  31. :param filename: the name of the input file to use for file input. None for no file input.
  32. :param backend: subsystem to use for threading
  33. .. note:: *filename* parameter should not be a file handle
  34. """
  35. self.backend = backend
  36. if filename is not None:
  37. self.infile = open(filename, 'r')
  38. else:
  39. self.infile = None
  40. self.next_scheduled = float('inf')
  41. self.file_event = None
  42. # Call this here already for time 0, to schedule the first event
  43. self.checkInterrupt(0)
  44. def checkInterrupt(self, current_time):
  45. """
  46. Checks for whether an interrupt should happen at this time; if so, it also reschedules the next one.
  47. This method must be called before the internal interrupt is fetched, as otherwise it will not be taken into account.
  48. :param current_time: the current simulation time to check for interrupts
  49. """
  50. if self.infile is not None:
  51. # First check for if the scheduled message happened
  52. if (self.next_scheduled - current_time) <= 0:
  53. if self.backend.setInterrupt(self.file_event):
  54. self.next_scheduled = float('inf')
  55. self.file_event = None
  56. # Now check for the next one
  57. if self.next_scheduled == float('inf'):
  58. # We don't have a scheduled event, so fetch one
  59. line = self.infile.readline()
  60. if line == "":
  61. self.infile.close()
  62. self.infile = None
  63. else:
  64. event = line.split(" ", 1)
  65. if len(event) != 2:
  66. raise DEVSException(
  67. "Inproperly formatted input in file: %s" % event)
  68. self.next_scheduled = float(event[0])
  69. self.file_event = event[1].rstrip()
  70. def getNextTime(self):
  71. """
  72. Return the time of the next event from this generator
  73. """
  74. return self.next_scheduled