tracer.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class Tracers(object):
  16. """
  17. Interface for all tracers
  18. """
  19. def __init__(self):
  20. """
  21. Constructor
  22. """
  23. self.tracers = []
  24. self.tracers_init = []
  25. self.uid = 0
  26. def registerTracer(self, tracer, server, recover):
  27. """
  28. Register a tracer, so that it will also receive all transitions.
  29. :param tracer: tuple of the form (file, classname, [args])
  30. :param server: the server object to be able to make remote calls
  31. :param recover: whether or not this is a recovered registration (used during checkpointing)
  32. """
  33. loc = {}
  34. try:
  35. exec("from pypdevs.tracers.%s import %s" % (tracer[0], tracer[1]), {}, loc)
  36. except:
  37. exec("from %s import %s" % (tracer[0], tracer[1]), {}, loc)
  38. self.tracers.append(loc[tracer[1]](self.uid, server, *tracer[2]))
  39. # self.tracers.append(eval("%s(%i, server, *%s)" % (tracer[1],
  40. # self.uid,
  41. # tracer[2])))
  42. self.tracers_init.append(tracer)
  43. self.uid += 1
  44. self.tracers[-1].startTracer(recover)
  45. def hasTracers(self):
  46. """
  47. Checks whether or not there are any registered tracers
  48. :returns: bool
  49. """
  50. return len(self.tracers) > 0
  51. def getByID(self, uid):
  52. """
  53. Gets a tracer by its UID
  54. :param uid: the UID of the tracer to return
  55. :returns: tracer
  56. """
  57. return self.tracers[uid]
  58. def stopTracers(self):
  59. """
  60. Stop all registered tracers
  61. """
  62. for tracer in self.tracers:
  63. tracer.stopTracer()
  64. def tracesUser(self, time, aDEVS, variable, value):
  65. """
  66. Perform all tracing actions for a user imposed modification. This is NOT supported by default DEVS, so we don't require tracers to handle this either.
  67. :param time: the time at which the modification happend; this will be the termination time of the previous simulation run and **not** the time at which the timeAdvance was recomputed!
  68. :param aDEVS: the atomic DEVS model that was altered
  69. :param variable: the variable that was altered (as a string)
  70. :param value: the new value of the variable
  71. """
  72. for tracer in self.tracers:
  73. try:
  74. tracer.traceUser(time, aDEVS, variable, value)
  75. except AttributeError:
  76. # Some tracers choose to ignore this event
  77. pass
  78. def tracesInit(self, aDEVS, t):
  79. """
  80. Perform all tracing actions for an initialisation
  81. :param aDEVS: the model that was initialised
  82. :param t: the time at which the initialisation should be logged
  83. """
  84. if aDEVS.full_name is None:
  85. return
  86. for tracer in self.tracers:
  87. tracer.traceInit(aDEVS, t)
  88. def tracesInternal(self, aDEVS):
  89. """
  90. Perform all tracing actions for an internal transition
  91. :param aDEVS: the model that transitioned
  92. """
  93. if aDEVS.full_name is None:
  94. return
  95. for tracer in self.tracers:
  96. tracer.traceInternal(aDEVS)
  97. def tracesExternal(self, aDEVS):
  98. """
  99. Perform all tracing actions for an external transition
  100. :param aDEVS: the model that transitioned
  101. """
  102. if aDEVS.full_name is None:
  103. return
  104. for tracer in self.tracers:
  105. tracer.traceExternal(aDEVS)
  106. def tracesConfluent(self, aDEVS):
  107. """
  108. Perform all tracing actions for a confluent transition
  109. :param aDEVS: the model that transitioned
  110. """
  111. if aDEVS.full_name is None:
  112. return
  113. for tracer in self.tracers:
  114. tracer.traceConfluent(aDEVS)