Coordinator.hpp 9.8 KB

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