Simulator.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @file kernel/sss/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-2015 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 SSS_SIMULATOR
  26. #define SSS_SIMULATOR 1
  27. #include <paradevs/common/Coordinator.hpp>
  28. #include <paradevs/common/Parameters.hpp>
  29. #include <paradevs/common/Simulator.hpp>
  30. #include <paradevs/common/utils/Trace.hpp>
  31. #include <paradevs/kernel/sss/Model.hpp>
  32. #include <cassert>
  33. namespace paradevs { namespace sss {
  34. template < class Time, class Dynamics,
  35. class SchedulerHandle =
  36. paradevs::common::scheduler::NoSchedulerHandle,
  37. class Parameters = common::NoParameters >
  38. class Simulator : public common::Simulator < Time, SchedulerHandle >,
  39. public sss::Model < Time, SchedulerHandle >
  40. {
  41. typedef Simulator < Time, Dynamics, SchedulerHandle, Parameters > type;
  42. public:
  43. Simulator(const std::string& name, typename Time::type time_step,
  44. const Parameters& parameters) :
  45. common::Model < Time, SchedulerHandle >(name),
  46. common::Simulator < Time, SchedulerHandle >(name),
  47. sss::Model < Time, SchedulerHandle >(name),
  48. _dynamics(name, parameters),
  49. _time_step(time_step)
  50. { }
  51. ~Simulator()
  52. { }
  53. virtual bool is_atomic() const
  54. { return common::Simulator < Time, SchedulerHandle >::is_atomic(); }
  55. virtual std::string to_string(int level) const
  56. { return common::Simulator < Time, SchedulerHandle >::to_string(level); }
  57. typename Time::type start(typename Time::type t)
  58. {
  59. #ifdef WITH_TRACE
  60. common::Trace < Time >::trace()
  61. << common::TraceElement < Time >(type::get_name(), t,
  62. common::I_MESSAGE)
  63. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  64. << type::_tn;
  65. common::Trace < Time >::trace().flush();
  66. #endif
  67. _dynamics.start(t);
  68. type::_tl = t;
  69. type::_tn = t;
  70. #ifdef WITH_TRACE
  71. common::Trace < Time >::trace()
  72. << common::TraceElement < Time >(type::get_name(), t,
  73. common::I_MESSAGE)
  74. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  75. << type::_tn;
  76. common::Trace < Time >::trace().flush();
  77. #endif
  78. return type::_tn;
  79. }
  80. void observation(std::ostream &file) const
  81. {
  82. _dynamics.observation(file);
  83. }
  84. void output(typename Time::type t)
  85. {
  86. #ifdef WITH_TRACE
  87. common::Trace < Time >::trace()
  88. << common::TraceElement < Time >(type::get_name(), t,
  89. common::OUTPUT) << ": BEFORE";
  90. common::Trace < Time >::trace().flush();
  91. #endif
  92. if (t == type::_tn) {
  93. common::Bag < Time, SchedulerHandle > bag = _dynamics.lambda(t);
  94. if (not bag.empty()) {
  95. for (auto & event : bag) {
  96. event.set_model(this);
  97. }
  98. dynamic_cast < common::Coordinator < Time, SchedulerHandle >* >(
  99. type::get_parent())->dispatch_events(bag, t);
  100. }
  101. }
  102. #ifdef WITH_TRACE
  103. common::Trace < Time >::trace()
  104. << common::TraceElement < Time >(type::get_name(), t,
  105. common::OUTPUT) << ": AFTER";
  106. common::Trace < Time >::trace().flush();
  107. #endif
  108. }
  109. void post_event(typename Time::type t,
  110. const common::ExternalEvent < Time,
  111. SchedulerHandle >& event)
  112. {
  113. #ifndef WITH_TRACE
  114. (void)t;
  115. #endif
  116. #ifdef WITH_TRACE
  117. common::Trace < Time >::trace()
  118. << common::TraceElement < Time >(type::get_name(), t,
  119. common::POST_EVENT)
  120. << ": BEFORE => " << event.to_string();
  121. common::Trace < Time >::trace().flush();
  122. #endif
  123. type::add_event(event);
  124. #ifdef WITH_TRACE
  125. common::Trace < Time >::trace()
  126. << common::TraceElement < Time >(type::get_name(), t,
  127. common::POST_EVENT)
  128. << ": AFTER => " << event.to_string();
  129. common::Trace < Time >::trace().flush();
  130. #endif
  131. }
  132. typename Time::type transition(typename Time::type t)
  133. {
  134. #ifdef WITH_TRACE
  135. common::Trace < Time >::trace()
  136. << common::TraceElement < Time >(type::get_name(), t,
  137. common::S_MESSAGE)
  138. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  139. << type::_tn;
  140. common::Trace < Time >::trace().flush();
  141. #endif
  142. assert(t == type::_tn);
  143. if (type::is_marked()) {
  144. if (type::is_send()) {
  145. type::_tl = t;
  146. type::_tn = t + _time_step;
  147. }
  148. } else {
  149. _dynamics.transition(type::get_bag(), t);
  150. type::clear_bag();
  151. }
  152. #ifdef WITH_TRACE
  153. common::Trace < Time >::trace()
  154. << common::TraceElement < Time >(type::get_name(), t,
  155. common::S_MESSAGE)
  156. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  157. << type::_tn;
  158. common::Trace < Time >::trace().flush();
  159. #endif
  160. return type::_tn;
  161. }
  162. virtual void update_buffer(typename Time::type time)
  163. { _dynamics.update_buffer(time); }
  164. private :
  165. Dynamics _dynamics;
  166. typename Time::type _time_step;
  167. };
  168. } } // namespace paradevs sss
  169. #endif