Coordinator.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @file kernel/dtss/Coordinator.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 DTSS_COORDINATOR
  26. #define DTSS_COORDINATOR 1
  27. #include <paradevs/common/Coordinator.hpp>
  28. #include <paradevs/common/Parameters.hpp>
  29. #include <paradevs/common/utils/Trace.hpp>
  30. #include <cassert>
  31. #include <iostream>
  32. namespace paradevs { namespace dtss {
  33. template < class Time >
  34. class Parameters
  35. {
  36. public:
  37. Parameters(typename Time::type time_step) : _time_step(time_step)
  38. { }
  39. typename Time::type _time_step;
  40. };
  41. template < class Time, class Policy, class GraphManager,
  42. class SchedulerHandle, class Parameters = Parameters < Time >,
  43. class GraphParameters = common::NoParameters >
  44. class Coordinator : public common::Coordinator < Time, SchedulerHandle >
  45. {
  46. typedef Coordinator < Time, Policy, GraphManager, SchedulerHandle,
  47. Parameters, GraphParameters > type;
  48. public:
  49. typedef Parameters parameters_type;
  50. typedef GraphParameters graph_parameters_type;
  51. Coordinator(const std::string& name,
  52. const Parameters& parameters,
  53. const GraphParameters& graph_paramaters) :
  54. common::Model < Time, SchedulerHandle >(name),
  55. common::Coordinator < Time, SchedulerHandle >(name),
  56. _graph_manager(this, graph_paramaters),
  57. _time_step(parameters._time_step)
  58. { }
  59. virtual ~Coordinator()
  60. { }
  61. typename Time::type start(typename Time::type t)
  62. {
  63. #ifdef WITH_TRACE
  64. common::Trace < Time >::trace()
  65. << common::TraceElement < Time >(type::get_name(), t,
  66. common::I_MESSAGE)
  67. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  68. << type::_tn;
  69. common::Trace < Time >::trace().flush();
  70. #endif
  71. assert(_graph_manager.children().size() > 0);
  72. for (auto & child : _graph_manager.children()) {
  73. child->start(t);
  74. }
  75. type::_tl = t;
  76. type::_tn = t;
  77. #ifdef WITH_TRACE
  78. common::Trace < Time >::trace()
  79. << common::TraceElement < Time >(type::get_name(), t,
  80. common::I_MESSAGE)
  81. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  82. << type::_tn;
  83. common::Trace < Time >::trace().flush();
  84. #endif
  85. return type::_tn;
  86. }
  87. typename Time::type dispatch_events(common::Bag < Time,
  88. SchedulerHandle > bag,
  89. typename Time::type t)
  90. {
  91. #ifdef WITH_TRACE
  92. common::Trace < Time >::trace()
  93. << common::TraceElement < Time >(type::get_name(), t,
  94. common::Y_MESSAGE)
  95. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  96. << type::_tn << " ; bag = " << bag.to_string();
  97. common::Trace < Time >::trace().flush();
  98. #endif
  99. _graph_manager.dispatch_events(bag, t);
  100. #ifdef WITH_TRACE
  101. common::Trace < Time >::trace()
  102. << common::TraceElement < Time >(type::get_name(), t,
  103. common::Y_MESSAGE)
  104. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  105. << type::_tn;
  106. common::Trace < Time >::trace().flush();
  107. #endif
  108. return type::_tn;
  109. }
  110. void observation(std::ostream& file) const
  111. {
  112. for (auto & child : _graph_manager.children()) {
  113. child->observation(file);
  114. }
  115. }
  116. void output(typename Time::type t)
  117. {
  118. #ifdef WITH_TRACE
  119. common::Trace < Time >::trace()
  120. << common::TraceElement < Time >(type::get_name(), t,
  121. common::OUTPUT)
  122. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  123. << type::_tn;
  124. common::Trace < Time >::trace().flush();
  125. #endif
  126. if (t == type::_tn) {
  127. for (auto & model : _graph_manager.children()) {
  128. model->output(t);
  129. }
  130. }
  131. #ifdef WITH_TRACE
  132. common::Trace < Time >::trace()
  133. << common::TraceElement < Time >(type::get_name(), t,
  134. common::OUTPUT)
  135. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  136. common::Trace < Time >::trace().flush();
  137. #endif
  138. }
  139. void post_event(typename Time::type t,
  140. const common::ExternalEvent < Time,
  141. SchedulerHandle >& event)
  142. {
  143. #ifdef WITH_TRACE
  144. common::Trace < Time >::trace()
  145. << common::TraceElement < Time >(type::get_name(), t,
  146. common::POST_EVENT)
  147. << ": BEFORE => " << event.to_string();
  148. common::Trace < Time >::trace().flush();
  149. #endif
  150. if (t == type::_tn) {
  151. _graph_manager.post_event(t, event);
  152. } else {
  153. _policy(t, event, type::_tl, type::_tn);
  154. }
  155. #ifdef WITH_TRACE
  156. common::Trace < Time >::trace()
  157. << common::TraceElement < Time >(type::get_name(), t,
  158. common::POST_EVENT)
  159. << ": AFTER => " << event.to_string();
  160. common::Trace < Time >::trace().flush();
  161. #endif
  162. }
  163. typename Time::type transition(typename Time::type t)
  164. {
  165. #ifdef WITH_TRACE
  166. common::Trace < Time >::trace()
  167. << common::TraceElement < Time >(type::get_name(), t,
  168. common::S_MESSAGE)
  169. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  170. << type::_tn;
  171. common::Trace < Time >::trace().flush();
  172. #endif
  173. if (t == type::_tn) {
  174. for (auto & event : _policy.bag()) {
  175. post_event(t, event);
  176. }
  177. for (auto & model : _graph_manager.children()) {
  178. model->transition(t);
  179. }
  180. type::_tl = t;
  181. type::_tn = t + _time_step;
  182. }
  183. type::clear_bag();
  184. #ifdef WITH_TRACE
  185. common::Trace < Time >::trace()
  186. << common::TraceElement < Time >(type::get_name(), t,
  187. common::S_MESSAGE)
  188. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  189. common::Trace < Time >::trace().flush();
  190. #endif
  191. return type::_tn;
  192. }
  193. private:
  194. GraphManager _graph_manager;
  195. typename Time::type _time_step;
  196. Policy _policy;
  197. };
  198. } } // namespace paradevs dtss
  199. #endif