Coordinator.hpp 12 KB

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