Coordinator.hpp 14 KB

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