Simulator.hpp 6.1 KB

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