Coordinator.hpp 12 KB

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