Coordinator.hpp 12 KB

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