Simulator.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file pdevs/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/Parameters.hpp>
  29. #include <common/Simulator.hpp>
  30. #include <common/utils/Trace.hpp>
  31. #include <cassert>
  32. namespace paradevs { namespace pdevs {
  33. template < class Time, class Dynamics, class SchedulerHandle,
  34. class Parameters = common::NoParameters >
  35. class Simulator : public common::Simulator < Time, SchedulerHandle >
  36. {
  37. typedef Simulator < Time, Dynamics, SchedulerHandle, Parameters > type;
  38. public :
  39. Simulator(const std::string& name, const Parameters& parameters) :
  40. common::Simulator < Time, SchedulerHandle >(name),
  41. _dynamics(name, parameters)
  42. { }
  43. ~Simulator()
  44. { }
  45. /*************************************************
  46. * when i-message(t)
  47. * tl = t - e
  48. * tn = tl + ta(s)
  49. *************************************************/
  50. typename Time::type start(typename Time::type t)
  51. {
  52. #ifdef WITH_TRACE
  53. common::Trace < Time >::trace()
  54. << common::TraceElement < Time >(type::get_name(), t,
  55. common::I_MESSAGE)
  56. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  57. << type::_tn;
  58. common::Trace < Time >::trace().flush();
  59. #endif
  60. type::_tl = t;
  61. type::_tn =
  62. type::_tl + _dynamics.start(t);
  63. #ifdef WITH_TRACE
  64. common::Trace < Time >::trace()
  65. << common::TraceElement < Time >(type::get_name(), t,
  66. common::I_MESSAGE)
  67. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  68. << type::_tn;
  69. common::Trace < Time >::trace().flush();
  70. #endif
  71. return type::_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 >(type::get_name(), t,
  88. common::OUTPUT) << ": BEFORE";
  89. common::Trace < Time >::trace().flush();
  90. #endif
  91. if(t == type::_tn) {
  92. common::Bag < Time, SchedulerHandle > bag = _dynamics.lambda(t);
  93. if (not bag.empty()) {
  94. for (auto & event : bag) {
  95. event.set_model(this);
  96. }
  97. dynamic_cast < common::Coordinator < Time, SchedulerHandle >* >(
  98. type::get_parent())->dispatch_events(bag, t);
  99. }
  100. }
  101. #ifdef WITH_TRACE
  102. common::Trace < Time >::trace()
  103. << common::TraceElement < Time >(type::get_name(), t,
  104. common::OUTPUT) << ": AFTER";
  105. common::Trace < Time >::trace().flush();
  106. #endif
  107. }
  108. void post_event(typename Time::type t,
  109. const common::ExternalEvent < Time,
  110. SchedulerHandle >& event)
  111. {
  112. #ifndef WITH_TRACE
  113. (void)t;
  114. #endif
  115. #ifdef WITH_TRACE
  116. common::Trace < Time >::trace()
  117. << common::TraceElement < Time >(type::get_name(), t,
  118. common::POST_EVENT)
  119. << ": BEFORE => " << event.to_string();
  120. common::Trace < Time >::trace().flush();
  121. #endif
  122. type::add_event(event);
  123. #ifdef WITH_TRACE
  124. common::Trace < Time >::trace()
  125. << common::TraceElement < Time >(type::get_name(), t,
  126. common::POST_EVENT)
  127. << ": AFTER => " << event.to_string();
  128. common::Trace < Time >::trace().flush();
  129. #endif
  130. }
  131. /*************************************************
  132. * when x-message(t)
  133. * if (x is empty and t = tn) then
  134. * s = delta_int(s)
  135. * else if (x isn't empty and t = tn)
  136. * s = delta_conf(s,x)
  137. * else if (x isn't empty and t < tn)
  138. * e = t - tl
  139. * s = delta_ext(s,e,x)
  140. * tn = t + ta(s)
  141. * tl = t
  142. *************************************************/
  143. typename Time::type transition(typename Time::type t)
  144. {
  145. #ifdef WITH_TRACE
  146. common::Trace < Time >::trace()
  147. << common::TraceElement < Time >(type::get_name(), t,
  148. common::S_MESSAGE)
  149. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  150. << type::_tn;
  151. common::Trace < Time >::trace().flush();
  152. #endif
  153. assert(type::_tl <= t and t <= type::_tn);
  154. if(t == type::_tn) {
  155. if (type::event_number() == 0) {
  156. _dynamics.dint(t);
  157. } else {
  158. _dynamics.dconf(t, t - type::_tl, type::get_bag());
  159. }
  160. } else {
  161. _dynamics.dext(t, t - type::_tl, type::get_bag());
  162. }
  163. type::_tn = t + _dynamics.ta(t);
  164. type::_tl = t;
  165. type::clear_bag();
  166. #ifdef WITH_TRACE
  167. common::Trace < Time >::trace()
  168. << common::TraceElement < Time >(type::get_name(), t,
  169. common::S_MESSAGE)
  170. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  171. common::Trace < Time >::trace().flush();
  172. #endif
  173. return type::_tn;
  174. }
  175. private :
  176. Dynamics _dynamics;
  177. };
  178. } } // namespace paradevs pdevs
  179. #endif