Coordinator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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
  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. {
  41. typedef Coordinator<Time, GraphManager, Parameters, GraphParameters> type;
  42. public:
  43. typedef Parameters parameters_type;
  44. typedef GraphParameters graph_parameters_type;
  45. Coordinator(const std::string &name, const Parameters &parameters,
  46. const GraphParameters &graph_parameters)
  47. :
  48. common::Model<Time>(name),
  49. common::Coordinator<Time>(name),
  50. _graph_manager(this, parameters, graph_parameters)
  51. {}
  52. virtual ~Coordinator()
  53. {}
  54. GraphManager &get_graph_manager()
  55. { return _graph_manager; }
  56. const GraphManager &get_graph_manager() const
  57. { return _graph_manager; }
  58. virtual std::string to_string(int level) const
  59. {
  60. std::ostringstream ss;
  61. ss << common::String::make_spaces(level * 2) << "p-devs coordinator \""
  62. << type::get_name() << "\":" << std::endl;
  63. ss << _graph_manager.to_string(level + 1);
  64. return ss.str();
  65. }
  66. void restore(const common::context::State<Time> &state)
  67. {
  68. common::Coordinator<Time>::restore(state);
  69. for (auto &child : _graph_manager.children()) {
  70. _event_table.init(child->get_tn(), child);
  71. }
  72. }
  73. virtual void finish(const typename Time::type &t)
  74. {
  75. #ifndef WITH_TRACE
  76. (void) t;
  77. #endif
  78. #ifdef WITH_TRACE
  79. common::Trace<Time>::trace()
  80. << common::TraceElement<Time>(type::get_name(), t,
  81. common::FormalismType::PDEVS,
  82. common::FunctionType::FINISH,
  83. common::LevelType::FORMALISM);
  84. common::Trace<Time>::trace().flush();
  85. #endif
  86. }
  87. typename Time::type start(const typename Time::type &t)
  88. {
  89. // When i-message (i, t) at time t
  90. // for-each d ∈ D do
  91. // send i-message to child d
  92. // sort event-list according to tn,d
  93. // tl ← t
  94. // tn ← min{tn,d | d ∈ D}
  95. // End
  96. #ifdef WITH_TRACE
  97. common::Trace<Time>::trace()
  98. << common::TraceElement<Time>(type::get_name(), t,
  99. common::FormalismType::PDEVS,
  100. common::FunctionType::I_MESSAGE,
  101. common::LevelType::FORMALISM)
  102. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  103. << type::_tn;
  104. common::Trace<Time>::trace().flush();
  105. #endif
  106. assert(_graph_manager.children().size() > 0);
  107. for (auto &child : _graph_manager.children()) {
  108. _event_table.init(child->start(t), child);
  109. }
  110. type::_tl = t;
  111. type::_tn = _event_table.get_current_time();
  112. #ifdef WITH_TRACE
  113. common::Trace<Time>::trace()
  114. << common::TraceElement<Time>(type::get_name(), t,
  115. common::FormalismType::PDEVS,
  116. common::FunctionType::I_MESSAGE,
  117. common::LevelType::FORMALISM)
  118. << ": AFTER => " << "tl = " << type::_tl
  119. << " ; tn = " << type::_tn;
  120. common::Trace<Time>::trace().flush();
  121. #endif
  122. return type::_tn;
  123. }
  124. void output(const typename Time::type &t)
  125. {
  126. // When *-message (*, t)
  127. // if t != tn then Error
  128. // IMM = {d | (d, th,d) ∈ (event-list & tn,d = tn) }
  129. // for-each r ∈ IMM
  130. // send *-message (*, t) to r
  131. // End
  132. #ifdef WITH_TRACE
  133. common::Trace<Time>::trace()
  134. << common::TraceElement<Time>(type::get_name(), t,
  135. common::FormalismType::PDEVS,
  136. common::FunctionType::OUTPUT,
  137. common::LevelType::FORMALISM)
  138. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  139. << type::_tn << " ; scheduler = " << _event_table.to_string();
  140. common::Trace<Time>::trace().flush();
  141. #endif
  142. assert(t == type::_tn);
  143. common::Models<Time> IMM = _event_table.get_current_models(t);
  144. #ifdef WITH_TRACE
  145. common::Trace<Time>::trace()
  146. << common::TraceElement<Time>(type::get_name(), t,
  147. common::FormalismType::PDEVS,
  148. common::FunctionType::OUTPUT,
  149. common::LevelType::FORMALISM)
  150. << ": IMM = " << IMM.to_string();
  151. common::Trace<Time>::trace().flush();
  152. #endif
  153. for (auto &model : IMM) {
  154. model->output(t);
  155. }
  156. #ifdef WITH_TRACE
  157. common::Trace<Time>::trace()
  158. << common::TraceElement<Time>(type::get_name(), t,
  159. common::FormalismType::PDEVS,
  160. common::FunctionType::OUTPUT,
  161. common::LevelType::FORMALISM)
  162. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  163. << type::_tn << " ; scheduler = " << _event_table.to_string();
  164. common::Trace<Time>::trace().flush();
  165. #endif
  166. }
  167. typename Time::type transition(const typename Time::type &t)
  168. {
  169. // When x-message (x, t)
  170. // if not (tl <= t <= tn) then Error
  171. // receivers = { r | r ∈ children, N ∈ Ir, Z(N,r)(x) isn't empty }
  172. // for each r ∈ receivers
  173. // send x-message(Z(N,r)(x), t) with input value Z(N,r)(x) to r
  174. // for each r ∈ IMM and not in receivers
  175. // send x-message(empty, t) to r
  176. // sort event list according to tn
  177. // tl = t
  178. // tn = min(tn,d | d ∈ D)
  179. // End
  180. #ifdef WITH_TRACE
  181. common::Trace<Time>::trace()
  182. << common::TraceElement<Time>(type::get_name(), t,
  183. common::FormalismType::PDEVS,
  184. common::FunctionType::S_MESSAGE,
  185. common::LevelType::FORMALISM)
  186. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  187. << type::_tn << " ; scheduler = " << _event_table.to_string();
  188. common::Trace<Time>::trace().flush();
  189. #endif
  190. assert(t >= type::_tl and t <= type::_tn);
  191. common::Models<Time> receivers = get_receivers();
  192. common::Models<Time> IMM = _event_table.get_current_models(t);
  193. #ifdef WITH_TRACE
  194. common::Trace<Time>::trace()
  195. << common::TraceElement<Time>(type::get_name(), t,
  196. common::FormalismType::PDEVS,
  197. common::FunctionType::S_MESSAGE,
  198. common::LevelType::FORMALISM)
  199. << ": receivers = " << receivers.to_string()
  200. << " ; IMM = " << IMM.to_string();
  201. common::Trace<Time>::trace().flush();
  202. #endif
  203. for (auto &model : receivers) {
  204. _event_table.put(model->transition(t), model);
  205. }
  206. for (auto &model : IMM) {
  207. if (std::find(receivers.begin(), receivers.end(), model) == receivers.end()) {
  208. _event_table.put(model->transition(t), model);
  209. }
  210. }
  211. // update_event_table(t);
  212. type::_tl = t;
  213. type::_tn = _event_table.get_current_time();
  214. type::clear_bag();
  215. #ifdef WITH_TRACE
  216. common::Trace<Time>::trace()
  217. << common::TraceElement<Time>(type::get_name(), t,
  218. common::FormalismType::PDEVS,
  219. common::FunctionType::S_MESSAGE,
  220. common::LevelType::FORMALISM)
  221. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  222. << type::_tn << " ; scheduler = " << _event_table.to_string();
  223. common::Trace<Time>::trace().flush();
  224. #endif
  225. return type::_tn;
  226. }
  227. void post_event(const typename Time::type &t, const common::ExternalEvent<Time> &event)
  228. {
  229. #ifdef WITH_TRACE
  230. common::Trace<Time>::trace()
  231. << common::TraceElement<Time>(type::get_name(), t,
  232. common::FormalismType::PDEVS,
  233. common::FunctionType::POST_EVENT,
  234. common::LevelType::FORMALISM)
  235. << ": BEFORE => " << event.to_string();
  236. common::Trace<Time>::trace().flush();
  237. #endif
  238. type::add_event(event);
  239. _graph_manager.post_event(t, event);
  240. // update_event_table(t);
  241. type::_tn = _event_table.get_current_time();
  242. #ifdef WITH_TRACE
  243. common::Trace<Time>::trace()
  244. << common::TraceElement<Time>(type::get_name(), t,
  245. common::FormalismType::PDEVS,
  246. common::FunctionType::POST_EVENT,
  247. common::LevelType::FORMALISM)
  248. << ": AFTER => " << event.to_string();
  249. common::Trace<Time>::trace().flush();
  250. #endif
  251. }
  252. typename Time::type dispatch_events(const common::Bag<Time> &bag,
  253. const typename Time::type &t)
  254. {
  255. #ifdef WITH_TRACE
  256. common::Trace<Time>::trace()
  257. << common::TraceElement<Time>(type::get_name(), t,
  258. common::FormalismType::PDEVS,
  259. common::FunctionType::Y_MESSAGE,
  260. common::LevelType::FORMALISM)
  261. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  262. << type::_tn << " ; bag = " << bag.to_string()
  263. << " ; " << _event_table.to_string();
  264. common::Trace<Time>::trace().flush();
  265. #endif
  266. _graph_manager.dispatch_events(bag, t);
  267. // update_event_table(t);
  268. type::_tn = _event_table.get_current_time();
  269. #ifdef WITH_TRACE
  270. common::Trace<Time>::trace()
  271. << common::TraceElement<Time>(type::get_name(), t,
  272. common::FormalismType::PDEVS,
  273. common::FunctionType::Y_MESSAGE,
  274. common::LevelType::FORMALISM)
  275. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  276. << " ; " << _event_table.to_string();
  277. common::Trace<Time>::trace().flush();
  278. #endif
  279. return type::_tn;
  280. }
  281. common::Value observe(const typename Time::type & /* t */,
  282. unsigned int /* index */) const
  283. {
  284. assert(false);
  285. return common::Value();
  286. }
  287. typename Time::type lookahead(const typename Time::type &t) const
  288. {
  289. return _graph_manager.lookahead(t);
  290. }
  291. common::Models<Time> get_receivers() const
  292. {
  293. common::Models<Time> receivers;
  294. for (auto &model : _graph_manager.children()) {
  295. if (model->event_number() > 0) {
  296. receivers.push_back(model);
  297. }
  298. }
  299. return receivers;
  300. }
  301. void update_event_table(typename Time::type t)
  302. {
  303. for (auto &model : _graph_manager.children()) {
  304. if (model->event_number() > 0) {
  305. _event_table.put(t, model);
  306. }
  307. }
  308. }
  309. protected:
  310. GraphManager _graph_manager;
  311. common::SchedulerType _event_table;
  312. };
  313. }
  314. } // namespace artis pdevs
  315. #endif