Coordinator.hpp 9.6 KB

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