Coordinator.hpp 12 KB

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