tracerVerbose.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. from pypdevs.tracers.tracerBase import BaseTracer
  16. from pypdevs.util import runTraceAtController
  17. import sys
  18. class TracerVerbose(BaseTracer):
  19. """
  20. A tracer for simple verbose output
  21. """
  22. def __init__(self, uid, server, filename):
  23. """
  24. Constructor
  25. :param uid: the UID of this tracer
  26. :param server: the server to make remote calls on
  27. :param filename: file to save the trace to, can be None for output to stdout
  28. """
  29. super(TracerVerbose, self).__init__(uid, server)
  30. if server.getName() == 0:
  31. self.filename = filename
  32. else:
  33. self.filename = None
  34. self.prevtime = (-1, -1)
  35. def startTracer(self, recover):
  36. """
  37. Starts up the tracer
  38. :param recover: whether or not this is a recovery call (so whether or not the file should be appended to)
  39. """
  40. if self.filename is None:
  41. self.verb_file = sys.stdout
  42. elif recover:
  43. self.verb_file = open(self.filename, 'a+')
  44. else:
  45. self.verb_file = open(self.filename, 'w')
  46. def stopTracer(self):
  47. """
  48. Stop the tracer
  49. """
  50. self.verb_file.flush()
  51. def trace(self, time, text):
  52. """
  53. Actual tracing function
  54. :param time: time at which this trace happened
  55. :param text: the text that was traced
  56. """
  57. string = ""
  58. if time > self.prevtime:
  59. string = ("\n__ Current Time: %10.6f " + "_"*42 + " \n\n") % (time[0])
  60. self.prevtime = time
  61. string += "%s\n" % text
  62. try:
  63. self.verb_file.write(string)
  64. except TypeError:
  65. self.verb_file.write(string.encode())
  66. def traceInternal(self, aDEVS):
  67. """
  68. Tracing done for the internal transition function
  69. :param aDEVS: the model that transitioned
  70. """
  71. text = "\n"
  72. text += "\tINTERNAL TRANSITION in model <%s>\n" % aDEVS.getModelFullName()
  73. text += "\t\tNew State: %s\n" % str(aDEVS.state)
  74. text += "\t\tOutput Port Configuration:\n"
  75. for I in range(len(aDEVS.OPorts)):
  76. text += "\t\t\tport <" + str(aDEVS.OPorts[I].getPortName()) + ">:\n"
  77. for msg in aDEVS.my_output.get(aDEVS.OPorts[I], []):
  78. text += "\t\t\t\t" + str(msg) + "\n"
  79. # Don't show the age
  80. text += "\t\tNext scheduled internal transition at time %.6f\n" \
  81. % (aDEVS.time_next[0])
  82. runTraceAtController(self.server,
  83. self.uid,
  84. aDEVS,
  85. [aDEVS.time_last, '"' + text + '"'])
  86. def traceConfluent(self, aDEVS):
  87. """
  88. Tracing done for the confluent transition function
  89. :param aDEVS: the model that transitioned
  90. """
  91. text = "\n"
  92. text += "\tCONFLUENT TRANSITION in model <%s>\n" % aDEVS.getModelFullName()
  93. text += "\t\tInput Port Configuration:\n"
  94. for I in range(len(aDEVS.IPorts)):
  95. text += "\t\t\tport <" + str(aDEVS.IPorts[I].getPortName()) + ">: \n"
  96. for msg in aDEVS.my_input.get(aDEVS.IPorts[I], []):
  97. text += "\t\t\t\t" + str(msg) + "\n"
  98. text += "\t\tNew State: %s\n" % str(aDEVS.state)
  99. text += "\t\tOutput Port Configuration:\n"
  100. for I in range(len(aDEVS.OPorts)):
  101. text += "\t\t\tport <" + str(aDEVS.OPorts[I].getPortName()) + ">:\n"
  102. for msg in aDEVS.my_output.get(aDEVS.OPorts[I], []):
  103. text += "\t\t\t\t" + str(msg) + "\n"
  104. # Don't show the age
  105. text += "\t\tNext scheduled internal transition at time %.6f\n" \
  106. % (aDEVS.time_next[0])
  107. runTraceAtController(self.server,
  108. self.uid,
  109. aDEVS,
  110. [aDEVS.time_last, '"' + text + '"'])
  111. def traceExternal(self, aDEVS):
  112. """
  113. Tracing done for the external transition function
  114. :param aDEVS: the model that transitioned
  115. """
  116. text = "\n"
  117. text += "\tEXTERNAL TRANSITION in model <%s>\n" % aDEVS.getModelFullName()
  118. text += "\t\tInput Port Configuration:\n"
  119. for I in range(len(aDEVS.IPorts)):
  120. text += "\t\t\tport <" + str(aDEVS.IPorts[I].getPortName()) + ">:\n"
  121. for msg in aDEVS.my_input.get(aDEVS.IPorts[I], []):
  122. text += "\t\t\t\t" + str(msg) + "\n"
  123. text += "\t\tNew State: %s\n" % str(aDEVS.state)
  124. # Don't show the age
  125. text += "\t\tNext scheduled internal transition at time %.6f\n" \
  126. % (aDEVS.time_next[0])
  127. runTraceAtController(self.server,
  128. self.uid,
  129. aDEVS,
  130. [aDEVS.time_last, '"' + text + '"'])
  131. def traceInit(self, aDEVS, t):
  132. """
  133. Tracing done for the initialisation
  134. :param aDEVS: the model that was initialised
  135. :param t: time at which it should be traced
  136. """
  137. text = "\n"
  138. text += "\tINITIAL CONDITIONS in model <%s>\n" % aDEVS.getModelFullName()
  139. text += "\t\tInitial State: %s\n" % str(aDEVS.state)
  140. # Don't show the age
  141. text += "\t\tNext scheduled internal transition at time %.6f\n" \
  142. % (aDEVS.time_next[0])
  143. runTraceAtController(self.server,
  144. self.uid,
  145. aDEVS,
  146. [t, '"' + text + '"'])
  147. def traceUser(self, time, aDEVS, variable, value):
  148. text = "\n"
  149. text += "\tUSER CHANGE in model <%s>\n" % aDEVS.getModelFullName()
  150. text += "\t\tAltered attribute <%s> to value <%s>\n" % (variable, value)
  151. # Is only called at the controller, outside of the GVT loop, so commit directly
  152. self.trace(time, text)