Simulator.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @file Simulator.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef PDEVS_SIMULATOR
  26. #define PDEVS_SIMULATOR 1
  27. #include <common/Coordinator.hpp>
  28. #include <common/Simulator.hpp>
  29. #include <common/Trace.hpp>
  30. #include <cassert>
  31. namespace paradevs { namespace pdevs {
  32. template < class Time, class Dynamics >
  33. class Simulator : public common::Simulator < Time >
  34. {
  35. public :
  36. Simulator(const std::string& name) :
  37. common::Simulator < Time >(name), _dynamics(name)
  38. { }
  39. ~Simulator()
  40. { }
  41. /*************************************************
  42. * when i-message(t)
  43. * tl = t - e
  44. * tn = tl + ta(s)
  45. *************************************************/
  46. typename Time::type start(typename Time::type t)
  47. {
  48. #ifdef WITH_TRACE
  49. common::Trace < Time >::trace()
  50. << common::TraceElement < Time >(
  51. Simulator < Time, Dynamics >::get_name(), t,
  52. common::I_MESSAGE)
  53. << ": BEFORE => "
  54. << "tl = " << Simulator < Time, Dynamics >::_tl
  55. << " ; tn = " << Simulator < Time, Dynamics >::_tn;
  56. common::Trace < Time >::trace().flush();
  57. #endif
  58. Simulator < Time, Dynamics >::_tl = t;
  59. Simulator < Time, Dynamics >::_tn =
  60. Simulator < Time, Dynamics >::_tl + _dynamics.start(t);
  61. #ifdef WITH_TRACE
  62. common::Trace < Time >::trace()
  63. << common::TraceElement < Time >(
  64. Simulator < Time, Dynamics >::get_name(), t,
  65. common::I_MESSAGE)
  66. << ": AFTER => "
  67. << "tl = " << Simulator < Time, Dynamics >::_tl
  68. << " ; tn = " << Simulator < Time, Dynamics >::_tn;
  69. common::Trace < Time >::trace().flush();
  70. #endif
  71. return Simulator < Time, Dynamics >::_tn;
  72. }
  73. void observation(std::ostream &file) const
  74. {
  75. _dynamics.observation(file);
  76. }
  77. /*************************************************
  78. * when *-message(t)
  79. * if (t = tn) then
  80. * y = lambda(s)
  81. * send y-message(y,t) to parent
  82. *************************************************/
  83. void output(typename Time::type t)
  84. {
  85. #ifdef WITH_TRACE
  86. common::Trace < Time >::trace()
  87. << common::TraceElement < Time >(
  88. Simulator < Time, Dynamics >::get_name(), t,
  89. common::OUTPUT)
  90. << ": BEFORE";
  91. common::Trace < Time >::trace().flush();
  92. #endif
  93. if(t == Simulator < Time, Dynamics >::_tn) {
  94. common::Bag < Time > bag = _dynamics.lambda(t);
  95. if (not bag.empty()) {
  96. for (auto & event : bag) {
  97. event.set_model(this);
  98. }
  99. dynamic_cast < common::Coordinator < Time >* >(
  100. Simulator < Time, Dynamics >::get_parent())
  101. ->dispatch_events(bag, t);
  102. }
  103. }
  104. #ifdef WITH_TRACE
  105. common::Trace < Time >::trace()
  106. << common::TraceElement < Time >(
  107. Simulator < Time, Dynamics >::get_name(), t,
  108. common::OUTPUT)
  109. << ": AFTER";
  110. common::Trace < Time >::trace().flush();
  111. #endif
  112. }
  113. void post_event(typename Time::type t,
  114. const common::ExternalEvent < Time >& event)
  115. {
  116. #ifndef WITH_TRACE
  117. (void)t;
  118. #endif
  119. #ifdef WITH_TRACE
  120. common::Trace < Time >::trace()
  121. << common::TraceElement < Time >(
  122. Simulator < Time, Dynamics >::get_name(), t,
  123. common::POST_EVENT)
  124. << ": BEFORE => " << event.to_string();
  125. common::Trace < Time >::trace().flush();
  126. #endif
  127. Simulator < Time, Dynamics >::add_event(event);
  128. #ifdef WITH_TRACE
  129. common::Trace < Time >::trace()
  130. << common::TraceElement < Time >(
  131. Simulator < Time, Dynamics >::get_name(), t,
  132. common::POST_EVENT)
  133. << ": AFTER => " << event.to_string();
  134. common::Trace < Time >::trace().flush();
  135. #endif
  136. }
  137. /*************************************************
  138. * when x-message(t)
  139. * if (x is empty and t = tn) then
  140. * s = delta_int(s)
  141. * else if (x isn't empty and t = tn)
  142. * s = delta_conf(s,x)
  143. * else if (x isn't empty and t < tn)
  144. * e = t - tl
  145. * s = delta_ext(s,e,x)
  146. * tn = t + ta(s)
  147. * tl = t
  148. *************************************************/
  149. typename Time::type transition(typename Time::type t)
  150. {
  151. #ifdef WITH_TRACE
  152. common::Trace < Time >::trace()
  153. << common::TraceElement < Time >(
  154. Simulator < Time, Dynamics >::get_name(), t,
  155. common::S_MESSAGE)
  156. << ": BEFORE => "
  157. << "tl = " << Simulator < Time, Dynamics >::_tl
  158. << " ; tn = " << Simulator < Time, Dynamics >::_tn;
  159. common::Trace < Time >::trace().flush();
  160. #endif
  161. // assert(Simulator < Time, Dynamics >::_tl <= t and t <= Simulator < Time, Dynamics >::_tn);
  162. if(t == Simulator < Time, Dynamics >::_tn) {
  163. if (Simulator < Time, Dynamics >::event_number() == 0) {
  164. _dynamics.dint(t);
  165. } else {
  166. _dynamics.dconf(t, t - Simulator < Time, Dynamics >::_tl,
  167. Simulator < Time, Dynamics >::get_bag());
  168. }
  169. } else {
  170. _dynamics.dext(t, t - Simulator < Time, Dynamics >::_tl,
  171. Simulator < Time, Dynamics >::get_bag());
  172. }
  173. Simulator < Time, Dynamics >::_tn = t + _dynamics.ta(t);
  174. Simulator < Time, Dynamics >::_tl = t;
  175. Simulator < Time, Dynamics >::clear_bag();
  176. #ifdef WITH_TRACE
  177. common::Trace < Time >::trace()
  178. << common::TraceElement < Time >(
  179. Simulator < Time, Dynamics >::get_name(), t,
  180. common::S_MESSAGE)
  181. << ": AFTER => "
  182. << "tl = " << Simulator < Time, Dynamics >::_tl
  183. << " ; tn = " << Simulator < Time, Dynamics >::_tn;
  184. common::Trace < Time >::trace().flush();
  185. #endif
  186. return Simulator < Time, Dynamics >::_tn;
  187. }
  188. private :
  189. Dynamics _dynamics;
  190. };
  191. } } // namespace paradevs pdevs
  192. #endif