Coordinator.hpp 11 KB

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