Coordinator.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * @file kernel/pdevs/Coordinator.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.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 <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Parameters.hpp>
  29. #include <artis-star/common/Scheduler.hpp>
  30. #include <artis-star/common/utils/String.hpp>
  31. #include <artis-star/common/utils/Trace.hpp>
  32. #include <cassert>
  33. namespace artis {
  34. namespace pdevs {
  35. template<class Time,
  36. class GraphManager,
  37. class Parameters = common::NoParameters,
  38. class GraphParameters = common::NoParameters>
  39. class Coordinator : public common::Coordinator<Time> {
  40. typedef Coordinator<Time, GraphManager, Parameters, GraphParameters> type;
  41. public:
  42. typedef Parameters parameters_type;
  43. typedef GraphParameters graph_parameters_type;
  44. Coordinator(const std::string& name, const Parameters& parameters,
  45. const GraphParameters& graph_parameters)
  46. :
  47. common::Model<Time>(name),
  48. common::Coordinator<Time>(name),
  49. _graph_manager(this, parameters, graph_parameters) { }
  50. virtual ~Coordinator() { }
  51. common::GraphManager<Time>& get_graph_manager() { return _graph_manager; }
  52. const common::GraphManager<Time>& get_graph_manager() const { return _graph_manager; }
  53. virtual std::string to_string(int level) const
  54. {
  55. std::ostringstream ss;
  56. ss << common::String::make_spaces(level * 2) << "p-devs coordinator \""
  57. << type::get_name() << "\":" << std::endl;
  58. ss << _graph_manager.to_string(level + 1);
  59. return ss.str();
  60. }
  61. void restore(const common::context::State<Time>& state)
  62. {
  63. common::Coordinator<Time>::restore(state);
  64. for (auto& child : _graph_manager.children()) {
  65. _event_table.init(child->get_tn(), child);
  66. }
  67. }
  68. typename Time::type start(const typename Time::type& t)
  69. {
  70. // When receive (START,t)
  71. // send (START,t) to {I | I ∈ C}
  72. // tl ← t
  73. // tn ← min{t N,I | I ∈ C}
  74. // End
  75. #ifdef WITH_TRACE
  76. common::Trace<Time>::trace()
  77. << common::TraceElement<Time>(type::get_name(), t,
  78. common::FormalismType::PDEVS,
  79. common::FunctionType::I_MESSAGE,
  80. common::LevelType::FORMALISM)
  81. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  82. << type::_tn;
  83. common::Trace<Time>::trace().flush();
  84. #endif
  85. assert(_graph_manager.children().size() > 0);
  86. for (auto& child : _graph_manager.children()) {
  87. _event_table.init(child->start(t), child);
  88. }
  89. type::_tl = t;
  90. type::_tn = _event_table.get_current_time();
  91. #ifdef WITH_TRACE
  92. common::Trace<Time>::trace()
  93. << common::TraceElement<Time>(type::get_name(), t,
  94. common::FormalismType::PDEVS,
  95. common::FunctionType::I_MESSAGE,
  96. common::LevelType::FORMALISM)
  97. << ": AFTER => " << "tl = " << type::_tl
  98. << " ; tn = " << type::_tn;
  99. common::Trace<Time>::trace().flush();
  100. #endif
  101. return type::_tn;
  102. }
  103. void output(const typename Time::type& t)
  104. {
  105. // When *-message(t)
  106. // calculate IMM (models with tn = t in scheduler
  107. // calculate INF from IMM
  108. // for each e in IMM U INF
  109. // calculate influencer
  110. // ...
  111. // send done to parent
  112. // End
  113. #ifdef WITH_TRACE
  114. common::Trace<Time>::trace()
  115. << common::TraceElement<Time>(type::get_name(), t,
  116. common::FormalismType::PDEVS,
  117. common::FunctionType::OUTPUT,
  118. common::LevelType::FORMALISM)
  119. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  120. << type::_tn << " ; scheduler = " << _event_table.to_string();
  121. common::Trace<Time>::trace().flush();
  122. #endif
  123. assert(t == type::_tn);
  124. common::Models<Time> IMM =
  125. _event_table.get_current_models(t);
  126. #ifdef WITH_TRACE
  127. common::Trace<Time>::trace()
  128. << common::TraceElement<Time>(type::get_name(), t,
  129. common::FormalismType::PDEVS,
  130. common::FunctionType::OUTPUT,
  131. common::LevelType::FORMALISM)
  132. << ": IMM = " << IMM.to_string();
  133. common::Trace<Time>::trace().flush();
  134. #endif
  135. for (auto& model : IMM) {
  136. model->output(t);
  137. }
  138. #ifdef WITH_TRACE
  139. common::Trace<Time>::trace()
  140. << common::TraceElement<Time>(type::get_name(), t,
  141. common::FormalismType::PDEVS,
  142. common::FunctionType::OUTPUT,
  143. common::LevelType::FORMALISM)
  144. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  145. << type::_tn << " ; scheduler = " << _event_table.to_string();
  146. common::Trace<Time>::trace().flush();
  147. #endif
  148. }
  149. typename Time::type transition(const typename Time::type& t)
  150. {
  151. // When x-message(t)
  152. // receivers = { r | r in children, N in Ir, Z(N,r)(x) isn't empty }
  153. // for each r in receivers
  154. // send x-message(Z(N,r)(x), t) with input value Z(N,r)(x) to r
  155. // for each r in IMM and not in receivers
  156. // send x-message(empty, t) to r
  157. // sort event list according to tn
  158. // tl = t
  159. // tn = min(tn_d | d in D)
  160. // End
  161. #ifdef WITH_TRACE
  162. common::Trace<Time>::trace()
  163. << common::TraceElement<Time>(type::get_name(), t,
  164. common::FormalismType::PDEVS,
  165. common::FunctionType::S_MESSAGE,
  166. common::LevelType::FORMALISM)
  167. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  168. << type::_tn << " ; scheduler = " << _event_table.to_string();
  169. common::Trace<Time>::trace().flush();
  170. #endif
  171. assert(t >= type::_tl and t <= type::_tn);
  172. common::Models<Time> receivers =
  173. _event_table.get_current_models(t);
  174. add_models_with_inputs(receivers);
  175. #ifdef WITH_TRACE
  176. common::Trace<Time>::trace()
  177. << common::TraceElement<Time>(type::get_name(), t,
  178. common::FormalismType::PDEVS,
  179. common::FunctionType::S_MESSAGE,
  180. common::LevelType::FORMALISM)
  181. << ": receivers = " << receivers.to_string();
  182. common::Trace<Time>::trace().flush();
  183. #endif
  184. for (auto& model : receivers) {
  185. _event_table.put(model->transition(t), model);
  186. }
  187. update_event_table(t);
  188. type::_tl = t;
  189. type::_tn = _event_table.get_current_time();
  190. type::clear_bag();
  191. #ifdef WITH_TRACE
  192. common::Trace<Time>::trace()
  193. << common::TraceElement<Time>(type::get_name(), t,
  194. common::FormalismType::PDEVS,
  195. common::FunctionType::S_MESSAGE,
  196. common::LevelType::FORMALISM)
  197. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  198. << type::_tn << " ; scheduler = " << _event_table.to_string();
  199. common::Trace<Time>::trace().flush();
  200. #endif
  201. return type::_tn;
  202. }
  203. void post_event(const typename Time::type& t,
  204. const common::ExternalEvent<Time>& event)
  205. {
  206. #ifdef WITH_TRACE
  207. common::Trace<Time>::trace()
  208. << common::TraceElement<Time>(type::get_name(), t,
  209. common::FormalismType::PDEVS,
  210. common::FunctionType::POST_EVENT,
  211. common::LevelType::FORMALISM)
  212. << ": BEFORE => " << event.to_string();
  213. common::Trace<Time>::trace().flush();
  214. #endif
  215. type::add_event(event);
  216. _graph_manager.post_event(t, event);
  217. update_event_table(t);
  218. type::_tn = _event_table.get_current_time();
  219. #ifdef WITH_TRACE
  220. common::Trace<Time>::trace()
  221. << common::TraceElement<Time>(type::get_name(), t,
  222. common::FormalismType::PDEVS,
  223. common::FunctionType::POST_EVENT,
  224. common::LevelType::FORMALISM)
  225. << ": AFTER => " << event.to_string();
  226. common::Trace<Time>::trace().flush();
  227. #endif
  228. }
  229. /*******************************************************************
  230. * when y-message(y_d, t) with output y_d from d
  231. *******************************************************************/
  232. typename Time::type dispatch_events(
  233. const common::Bag<Time>& bag,
  234. const typename Time::type& t)
  235. {
  236. #ifdef WITH_TRACE
  237. common::Trace<Time>::trace()
  238. << common::TraceElement<Time>(type::get_name(), t,
  239. common::FormalismType::PDEVS,
  240. common::FunctionType::Y_MESSAGE,
  241. common::LevelType::FORMALISM)
  242. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  243. << type::_tn << " ; bag = " << bag.to_string()
  244. << " ; " << _event_table.to_string();
  245. common::Trace<Time>::trace().flush();
  246. #endif
  247. _graph_manager.dispatch_events(bag, t);
  248. update_event_table(t);
  249. type::_tn = _event_table.get_current_time();
  250. #ifdef WITH_TRACE
  251. common::Trace<Time>::trace()
  252. << common::TraceElement<Time>(type::get_name(), t,
  253. common::FormalismType::PDEVS,
  254. common::FunctionType::Y_MESSAGE,
  255. common::LevelType::FORMALISM)
  256. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  257. << " ; " << _event_table.to_string();
  258. common::Trace<Time>::trace().flush();
  259. #endif
  260. return type::_tn;
  261. }
  262. common::Value observe(const typename Time::type& /* t */,
  263. unsigned int /* index */) const
  264. {
  265. assert(false);
  266. return common::Value();
  267. }
  268. void add_models_with_inputs(
  269. common::Models<Time>& receivers)
  270. {
  271. for (auto& model : _graph_manager.children()) {
  272. if (model->event_number() > 0) {
  273. if (std::find(receivers.begin(), receivers.end(),
  274. model) == receivers.end()) {
  275. receivers.push_back(model);
  276. }
  277. }
  278. }
  279. }
  280. void update_event_table(typename Time::type t)
  281. {
  282. for (auto& model : _graph_manager.children()) {
  283. if (model->event_number() > 0) {
  284. _event_table.put(t, model);
  285. }
  286. }
  287. }
  288. protected:
  289. GraphManager _graph_manager;
  290. common::SchedulerType _event_table;
  291. };
  292. }
  293. } // namespace artis pdevs
  294. #endif