Coordinator.hpp 13 KB

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