Coordinator.hpp 14 KB

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