produce_consume_PDEVS.mvc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. include "primitives.alh"
  2. AtomicDEVSBlock Generator {
  3. name = "Generator"
  4. initialState = """
  5. return {'name': 'generating'}
  6. """
  7. intTransition = """
  8. if self.state['name'] == 'generating':
  9. new_state = {}
  10. new_state['name'] = 'generating'
  11. return new_state
  12. """
  13. timeAdvance = """
  14. if self.state['name'] == 'generating':
  15. return 1
  16. """
  17. outputFnc = """
  18. if self.state['name'] == 'generating':
  19. return {self.my_ports['g_out']: [{'type': 'Job', 'repr': {'duration': 0.3}}]}
  20. """
  21. extTransition = """
  22. inputs = {k.getPortName(): v for k, v in my_inputs.iteritems()}
  23. return AtomicDEVS.extTransition(self, my_inputs)
  24. """
  25. confTransition = """
  26. inputs = {k.getPortName(): v for k, v in my_inputs.iteritems()}
  27. return AtomicDEVS.confTransition(self, my_inputs)
  28. """
  29. }
  30. AtomicDEVSBlock Processor {
  31. name = "Processor"
  32. initialState = """
  33. return {'name': 'idle'}
  34. """
  35. intTransition = """
  36. if self.state['name'] == 'processing':
  37. new_state = {}
  38. new_state['name'] = 'idle'
  39. return new_state
  40. """
  41. timeAdvance = """
  42. if self.state['name'] == 'processing':
  43. return self.state['job']['duration']
  44. if self.state['name'] == 'idle':
  45. return INFINITY
  46. """
  47. outputFnc = """
  48. if self.state['name'] == 'processing':
  49. return {self.my_ports['p_out']: [{'type': 'Job', 'repr': self.state['job']}]}
  50. if self.state['name'] == 'idle':
  51. return {}
  52. """
  53. extTransition = """
  54. inputs = {k.getPortName(): v for k, v in my_inputs.iteritems()}
  55. if self.state['name'] == 'idle':
  56. new_state = {}
  57. new_state['name'] = 'processing'
  58. new_state['job'] = inputs['p_in'][0]['repr']
  59. return new_state
  60. else:
  61. return AtomicDEVS.extTransition(self, my_inputs)
  62. """
  63. confTransition = """
  64. inputs = {k.getPortName(): v for k, v in my_inputs.iteritems()}
  65. return AtomicDEVS.confTransition(self, my_inputs)
  66. """
  67. }
  68. AtomicDEVSBlock Collector {
  69. name = "Collector"
  70. initialState = """
  71. return {'name': 'waiting', 'nr_of_jobs': 0}
  72. """
  73. intTransition = """
  74. return AtomicDEVS.intTransition(self)
  75. """
  76. timeAdvance = """
  77. if self.state['name'] == 'waiting':
  78. return INFINITY
  79. """
  80. outputFnc = """
  81. if self.state['name'] == 'waiting':
  82. return {}
  83. """
  84. extTransition = """
  85. inputs = {k.getPortName(): v for k, v in my_inputs.iteritems()}
  86. if self.state['name'] == 'waiting':
  87. new_state = {}
  88. new_state['name'] = 'waiting'
  89. new_state['nr_of_jobs'] = self.state['nr_of_jobs'] + 1
  90. return new_state
  91. else:
  92. return AtomicDEVS.extTransition(self, my_inputs)
  93. """
  94. confTransition = """
  95. inputs = {k.getPortName(): v for k, v in my_inputs.iteritems()}
  96. return AtomicDEVS.confTransition(self, my_inputs)
  97. """
  98. }
  99. OutputPort g_out {name = "g_out"}
  100. InputPort p_in {name = "p_in"}
  101. OutputPort p_out {name = "p_out"}
  102. InputPort c_in {name = "c_in"}
  103. DEVSBlockToPort(Generator, g_out) {}
  104. DEVSBlockToPort(Processor, p_in) {}
  105. DEVSBlockToPort(Processor, p_out) {}
  106. DEVSBlockToPort(Collector, c_in) {}
  107. CoupledDEVSBlock CoupledProcessor {
  108. name = "CoupledProcessor"
  109. }
  110. InputPort cp_in {name = "cp_in"}
  111. OutputPort cp_out {name = "cp_out"}
  112. DEVSBlockToPort (CoupledProcessor, cp_in) {}
  113. DEVSBlockToPort (CoupledProcessor, cp_out) {}
  114. DEVSInstance p1 {
  115. name = "p1"
  116. type = "Processor"
  117. }
  118. DEVSInstance p2 {
  119. name = "p2"
  120. type = "Processor"
  121. }
  122. SubModel (CoupledProcessor, p1) {}
  123. SubModel (CoupledProcessor, p2) {}
  124. InputPort p1_in {name = "p_in"}
  125. OutputPort p1_out {name = "p_out"}
  126. InputPort p2_in {name = "p_in"}
  127. OutputPort p2_out {name = "p_out"}
  128. DEVSInstanceToPort (p1, p1_in) {}
  129. DEVSInstanceToPort (p1, p1_out) {}
  130. DEVSInstanceToPort (p2, p2_in) {}
  131. DEVSInstanceToPort (p2, p2_out) {}
  132. Channel (cp_in, p1_in) {}
  133. Channel (p1_out, p2_in) {}
  134. Channel (p2_out, cp_out) {}
  135. CoupledDEVSBlock Root {
  136. name = "Root"
  137. }
  138. DEVSInstance generator {
  139. name = "generator"
  140. type = "Generator"
  141. }
  142. DEVSInstance coupledprocessor {
  143. name = "coupledprocessor"
  144. type = "CoupledProcessor"
  145. }
  146. DEVSInstance processor {
  147. name = "processor"
  148. type = "Processor"
  149. }
  150. DEVSInstance collector {
  151. name = "collector"
  152. type = "Collector"
  153. }
  154. SubModel (Root, generator) {}
  155. SubModel (Root, coupledprocessor) {}
  156. SubModel (Root, processor) {}
  157. SubModel (Root, collector) {}
  158. OutputPort generator_out {name = "g_out"}
  159. InputPort coupledprocessor_in {name = "cp_in"}
  160. OutputPort coupledprocessor_out {name = "cp_out"}
  161. InputPort processor_in {name = "p_in"}
  162. OutputPort processor_out {name = "p_out"}
  163. InputPort collector_in {name = "c_in"}
  164. DEVSInstanceToPort (generator, generator_out) {}
  165. DEVSInstanceToPort (coupledprocessor, coupledprocessor_in) {}
  166. DEVSInstanceToPort (coupledprocessor, coupledprocessor_out) {}
  167. DEVSInstanceToPort (processor, processor_in) {}
  168. DEVSInstanceToPort (processor, processor_out) {}
  169. DEVSInstanceToPort (collector, collector_in) {}
  170. Channel (generator_out, coupledprocessor_in) {}
  171. Channel (coupledprocessor_out, processor_in) {}
  172. Channel (processor_out, collector_in) {}