Coordinator.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @file kernel/pdevs/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 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_COORDINATOR
  26. #define PDEVS_COORDINATOR 1
  27. #include <common/Coordinator.hpp>
  28. #include <common/Parameters.hpp>
  29. #include <common/utils/Trace.hpp>
  30. #include <cassert>
  31. #include <iostream>
  32. namespace paradevs { namespace pdevs {
  33. template < class Time,
  34. class Scheduler,
  35. class SchedulerHandle,
  36. class GraphManager,
  37. class Parameters = common::NoParameters,
  38. class GraphParameters = common::NoParameters >
  39. class Coordinator : public common::Coordinator < Time, SchedulerHandle >
  40. {
  41. typedef Coordinator < Time, Scheduler, SchedulerHandle, GraphManager,
  42. Parameters, GraphParameters > type;
  43. public:
  44. typedef Parameters parameters_type;
  45. typedef GraphParameters graph_parameters_type;
  46. Coordinator(const std::string& name,
  47. const Parameters& /* parameters */,
  48. const GraphParameters& graph_parameters) :
  49. common::Coordinator < Time, SchedulerHandle >(name),
  50. _graph_manager(this, graph_parameters)
  51. { }
  52. virtual ~Coordinator()
  53. { }
  54. typename Time::type start(typename Time::type t)
  55. {
  56. #ifdef WITH_TRACE
  57. common::Trace < Time >::trace()
  58. << common::TraceElement < Time >(type::get_name(), t,
  59. common::I_MESSAGE)
  60. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  61. << type::_tn;
  62. common::Trace < Time >::trace().flush();
  63. #endif
  64. assert(_graph_manager.children().size() > 0);
  65. for (auto & child : _graph_manager.children()) {
  66. _event_table.init(child->start(
  67. Coordinator < Time, Scheduler,
  68. SchedulerHandle, GraphManager, Parameters,
  69. GraphParameters >::_tn), child);
  70. }
  71. type::_tl = t;
  72. type::_tn = _event_table.get_current_time();
  73. #ifdef WITH_TRACE
  74. common::Trace < Time >::trace()
  75. << common::TraceElement < Time >(type::get_name(), t,
  76. common::I_MESSAGE)
  77. << ": AFTER => " << "tl = " << type::_tl
  78. << " ; tn = " << type::_tn;
  79. common::Trace < Time >::trace().flush();
  80. #endif
  81. return type::_tn;
  82. }
  83. /**************************************************
  84. * when *-message(t)
  85. * calculate IMM (models with tn = t in scheduler
  86. * calculate INF from IMM
  87. * for each e in IMM U INF
  88. * calculate influencer
  89. * ...
  90. * send done to parent
  91. **************************************************/
  92. void output(typename Time::type t)
  93. {
  94. #ifdef WITH_TRACE
  95. common::Trace < Time >::trace()
  96. << common::TraceElement < Time >(type::get_name(), t,
  97. common::OUTPUT)
  98. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  99. << type::_tn << " ; scheduler = " << _event_table.to_string();
  100. common::Trace < Time >::trace().flush();
  101. #endif
  102. assert(t == type::_tn);
  103. common::Models < Time, SchedulerHandle > IMM =
  104. _event_table.get_current_models(t);
  105. #ifdef WITH_TRACE
  106. common::Trace < Time >::trace()
  107. << common::TraceElement < Time >(type::get_name(), t,
  108. common::OUTPUT)
  109. << ": IMM = " << IMM.to_string();
  110. common::Trace < Time >::trace().flush();
  111. #endif
  112. for (auto & model : IMM) {
  113. model->output(t);
  114. }
  115. #ifdef WITH_TRACE
  116. common::Trace < Time >::trace()
  117. << common::TraceElement < Time >(type::get_name(), t,
  118. common::OUTPUT)
  119. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  120. << type::_tn << " ; scheduler = " << _event_table.to_string();
  121. common::Trace < Time >::trace().flush();
  122. #endif
  123. }
  124. /*******************************************************************
  125. * when x-message(t)
  126. * receivers = { r | r in children, N in Ir, Z(N,r)(x) isn't empty
  127. * for each r in receivers
  128. * send x-message(Z(N,r)(x), t) with input value Z(N,r)(x) to r
  129. * for each r in IMM and not in receivers
  130. * send x-message(empty, t) to r
  131. * sort event list acocrding to tn
  132. * tl = t
  133. * tn = min(tn_d | d in D)
  134. *******************************************************************/
  135. typename Time::type transition(typename Time::type t)
  136. {
  137. #ifdef WITH_TRACE
  138. common::Trace < Time >::trace()
  139. << common::TraceElement < Time >(type::get_name(), t,
  140. common::S_MESSAGE)
  141. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  142. << type::_tn << " ; scheduler = " << _event_table.to_string();
  143. common::Trace < Time >::trace().flush();
  144. #endif
  145. assert(t >= type::_tl and t <= type::_tn);
  146. common::Models < Time, SchedulerHandle > receivers =
  147. _event_table.get_current_models(t);
  148. add_models_with_inputs(receivers);
  149. #ifdef WITH_TRACE
  150. common::Trace < Time >::trace()
  151. << common::TraceElement < Time >(type::get_name(), t,
  152. common::S_MESSAGE)
  153. << ": receivers = " << receivers.to_string();
  154. common::Trace < Time >::trace().flush();
  155. #endif
  156. for (auto & model : receivers) {
  157. _event_table.put(model->transition(t), model);
  158. }
  159. update_event_table(t);
  160. type::_tl = t;
  161. type::_tn = _event_table.get_current_time();
  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 = "
  168. << type::_tn << " ; scheduler = " << _event_table.to_string();
  169. common::Trace < Time >::trace().flush();
  170. #endif
  171. return type::_tn;
  172. }
  173. void post_event(typename Time::type t,
  174. const common::ExternalEvent < Time,
  175. SchedulerHandle >& event)
  176. {
  177. #ifdef WITH_TRACE
  178. common::Trace < Time >::trace()
  179. << common::TraceElement < Time >(type::get_name(), t,
  180. common::POST_EVENT)
  181. << ": BEFORE => " << event.to_string();
  182. common::Trace < Time >::trace().flush();
  183. #endif
  184. type::add_event(event);
  185. _graph_manager.post_event(t, event);
  186. update_event_table(t);
  187. type::_tn = _event_table.get_current_time();
  188. #ifdef WITH_TRACE
  189. common::Trace < Time >::trace()
  190. << common::TraceElement < Time >(type::get_name(), t,
  191. common::POST_EVENT)
  192. << ": AFTER => " << event.to_string();
  193. common::Trace < Time >::trace().flush();
  194. #endif
  195. }
  196. /*******************************************************************
  197. * when y-message(y_d, t) with output y_d from d
  198. *******************************************************************/
  199. typename Time::type dispatch_events(
  200. common::Bag < Time, SchedulerHandle > bag, typename Time::type t)
  201. {
  202. #ifdef WITH_TRACE
  203. common::Trace < Time >::trace()
  204. << common::TraceElement < Time >( type::get_name(), t,
  205. common::Y_MESSAGE)
  206. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  207. << type::_tn << " ; bag = " << bag.to_string()
  208. << " ; " << _event_table.to_string();
  209. common::Trace < Time >::trace().flush();
  210. #endif
  211. _graph_manager.dispatch_events(bag, t);
  212. update_event_table(t);
  213. type::_tn = _event_table.get_current_time();
  214. #ifdef WITH_TRACE
  215. common::Trace < Time >::trace()
  216. << common::TraceElement < Time >(type::get_name(), t,
  217. common::Y_MESSAGE)
  218. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  219. << " ; " << _event_table.to_string();
  220. common::Trace < Time >::trace().flush();
  221. #endif
  222. return type::_tn;
  223. }
  224. void observation(std::ostream& file) const
  225. {
  226. for (auto & child : _graph_manager.children()) {
  227. child->observation(file);
  228. }
  229. }
  230. void add_models_with_inputs(
  231. common::Models < Time, SchedulerHandle >& receivers)
  232. {
  233. for (auto & model : _graph_manager.children()) {
  234. if (model->event_number() > 0) {
  235. if (std::find(receivers.begin(), receivers.end(), model) ==
  236. receivers.end()) {
  237. receivers.push_back(model);
  238. }
  239. }
  240. }
  241. }
  242. void update_event_table(typename Time::type t)
  243. {
  244. for (auto & model : _graph_manager.children()) {
  245. if (model->event_number() > 0) {
  246. _event_table.put(t, model);
  247. }
  248. }
  249. }
  250. protected:
  251. GraphManager _graph_manager;
  252. Scheduler _event_table;
  253. };
  254. } } // namespace paradevs pdevs
  255. #endif