Coordinator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @file kernel/dsde/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 DSDE_COORDINATOR
  26. #define DSDE_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 <artis-star/kernel/pdevs/Simulator.hpp>
  33. #include <artis-star/kernel/dsde/Executive.hpp>
  34. #include <artis-star/kernel/dsde/GraphManager.hpp>
  35. #include <cassert>
  36. namespace artis {
  37. namespace dsde {
  38. template<class Time,
  39. class GraphManager,
  40. class Executive,
  41. class Parameters = common::NoParameters,
  42. class GraphParameters = common::NoParameters>
  43. class Coordinator : public common::Coordinator<Time>
  44. {
  45. typedef Coordinator<Time, GraphManager, Executive, Parameters, GraphParameters> type;
  46. public:
  47. typedef Parameters parameters_type;
  48. typedef GraphParameters graph_parameters_type;
  49. Coordinator(const std::string &name, const Parameters &parameters,
  50. const GraphParameters &graph_parameters)
  51. :
  52. common::Model<Time>(name),
  53. common::Coordinator<Time>(name),
  54. _graph_manager(this, parameters, graph_parameters),
  55. _executive(parameters, graph_parameters, _graph_manager)
  56. {
  57. _graph_manager.add_child(0, &_executive);
  58. }
  59. virtual ~Coordinator()
  60. {}
  61. GraphManager &get_graph_manager()
  62. { return _graph_manager; }
  63. const GraphManager &get_graph_manager() const
  64. { return _graph_manager; }
  65. virtual std::string to_string(int level) const
  66. {
  67. std::ostringstream ss;
  68. ss << common::String::make_spaces(level * 2) << "dsde coordinator \""
  69. << type::get_name() << "\":" << std::endl;
  70. ss << _graph_manager.to_string(level + 1);
  71. return ss.str();
  72. }
  73. void restore(const common::context::State<Time> &state)
  74. {
  75. common::Coordinator<Time>::restore(state);
  76. for (auto &child : _graph_manager.children()) {
  77. _event_table.init(child->get_tn(), child);
  78. }
  79. }
  80. virtual void finish(const typename Time::type &t)
  81. {
  82. #ifndef WITH_TRACE
  83. (void) t;
  84. #endif
  85. #ifdef WITH_TRACE
  86. common::Trace<Time>::trace()
  87. << common::TraceElement<Time>(type::get_name(), t,
  88. common::FormalismType::DSDE,
  89. common::FunctionType::FINISH,
  90. common::LevelType::FORMALISM);
  91. common::Trace<Time>::trace().flush();
  92. #endif
  93. }
  94. typename Time::type start(const typename Time::type &t)
  95. {
  96. // Network Simulator Start Message
  97. // When receive (START,t)
  98. // send (START,t) to {I | I ∈ C}
  99. // tl ← t
  100. // tn ← min{tn,I | I ∈ C}
  101. // End
  102. #ifdef WITH_TRACE
  103. common::Trace<Time>::trace()
  104. << common::TraceElement<Time>(type::get_name(), t,
  105. common::FormalismType::DSDE,
  106. common::FunctionType::I_MESSAGE,
  107. common::LevelType::FORMALISM)
  108. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  109. << type::_tn;
  110. common::Trace<Time>::trace().flush();
  111. #endif
  112. assert(_graph_manager.children().size() > 0);
  113. for (auto &child : _graph_manager.children()) {
  114. _event_table.init(child->start(t), child);
  115. }
  116. type::_tl = t;
  117. type::_tn = _event_table.get_current_time();
  118. #ifdef WITH_TRACE
  119. common::Trace<Time>::trace()
  120. << common::TraceElement<Time>(type::get_name(), t,
  121. common::FormalismType::DSDE,
  122. common::FunctionType::I_MESSAGE,
  123. common::LevelType::FORMALISM)
  124. << ": AFTER => " << "tl = " << type::_tl
  125. << " ; tn = " << type::_tn;
  126. common::Trace<Time>::trace().flush();
  127. #endif
  128. return type::_tn;
  129. }
  130. void output(const typename Time::type &t)
  131. {
  132. // Network Simulator Output Function
  133. // When receive (OUTPUT,t)
  134. // if t = tn then
  135. // send (OUTPUT,t) to {I | I ∈ C}
  136. // y ← Zn (× I ∈ In (y_t))
  137. // else
  138. // y ← φ
  139. // endIf
  140. // End
  141. #ifdef WITH_TRACE
  142. common::Trace<Time>::trace()
  143. << common::TraceElement<Time>(type::get_name(), t,
  144. common::FormalismType::PDEVS,
  145. common::FunctionType::S_MESSAGE,
  146. common::LevelType::FORMALISM)
  147. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  148. << type::_tn << " ; scheduler = " << _event_table.to_string();
  149. common::Trace<Time>::trace().flush();
  150. #endif
  151. assert(t == type::_tn);
  152. common::Models<Time> I_N = _event_table.get_current_models(t);
  153. #ifdef WITH_TRACE
  154. common::Trace<Time>::trace()
  155. << common::TraceElement<Time>(type::get_name(), t,
  156. common::FormalismType::PDEVS,
  157. common::FunctionType::S_MESSAGE,
  158. common::LevelType::FORMALISM)
  159. << ": I_N = " << I_N.to_string();
  160. common::Trace<Time>::trace().flush();
  161. #endif
  162. for (auto &model : I_N) {
  163. model->output(t);
  164. }
  165. #ifdef WITH_TRACE
  166. common::Trace<Time>::trace()
  167. << common::TraceElement<Time>(type::get_name(), t,
  168. common::FormalismType::PDEVS,
  169. common::FunctionType::S_MESSAGE,
  170. common::LevelType::FORMALISM)
  171. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  172. << type::_tn << " ; scheduler = " << _event_table.to_string();
  173. common::Trace<Time>::trace().flush();
  174. #endif
  175. }
  176. typename Time::type transition(const typename Time::type &t)
  177. {
  178. // Network Simulator Transition
  179. // When receive (TRANSITION,t,x)
  180. // if t ∉ [tl ,tn ] then ERROR endIf
  181. // if t < tn and x = φ then RETURN endIf
  182. // D' ← D
  183. // send (TRANSITION,t,Z_I ( × j ∈ I_I(v_j))) to {I | I ∈ D}
  184. // send (TRANSITION,t,Z_χ ( × j ∈ I_χ(v_j))) to χ
  185. // send (START,t) to {I | I ∈ D − D' }
  186. // tl ← t
  187. // tn ← min{tn,I | I ∈ C}
  188. // End
  189. #ifdef WITH_TRACE
  190. common::Trace<Time>::trace()
  191. << common::TraceElement<Time>(type::get_name(), t,
  192. common::FormalismType::PDEVS,
  193. common::FunctionType::S_MESSAGE,
  194. common::LevelType::FORMALISM)
  195. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  196. << type::_tn << " ; scheduler = " << _event_table.to_string();
  197. common::Trace<Time>::trace().flush();
  198. #endif
  199. assert(t >= type::_tl and t <= type::_tn);
  200. common::Models<Time> receivers = _event_table.get_current_models(t);
  201. add_models_with_inputs(receivers);
  202. #ifdef WITH_TRACE
  203. common::Trace<Time>::trace()
  204. << common::TraceElement<Time>(type::get_name(), t,
  205. common::FormalismType::PDEVS,
  206. common::FunctionType::S_MESSAGE,
  207. common::LevelType::FORMALISM)
  208. << ": receivers = " << receivers.to_string();
  209. common::Trace<Time>::trace().flush();
  210. #endif
  211. bool found = false;
  212. for (auto &model : receivers) {
  213. if (model != &_executive) {
  214. _event_table.put(model->transition(t), model);
  215. } else {
  216. found = true;
  217. }
  218. }
  219. if (found) {
  220. _event_table.put(_executive.transition(t), &_executive);
  221. if (not _graph_manager.new_models().empty()) {
  222. for (auto &child : _graph_manager.new_models()) {
  223. _event_table.init(child->start(t), child);
  224. }
  225. _graph_manager.clear_new_models();
  226. }
  227. }
  228. update_event_table(t);
  229. type::_tl = t;
  230. type::_tn = _event_table.get_current_time();
  231. type::clear_bag();
  232. #ifdef WITH_TRACE
  233. common::Trace<Time>::trace()
  234. << common::TraceElement<Time>(type::get_name(), t,
  235. common::FormalismType::PDEVS,
  236. common::FunctionType::S_MESSAGE,
  237. common::LevelType::FORMALISM)
  238. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  239. << type::_tn << " ; scheduler = " << _event_table.to_string();
  240. common::Trace<Time>::trace().flush();
  241. #endif
  242. return type::_tn;
  243. }
  244. void post_event(const typename Time::type &t,
  245. const common::ExternalEvent<Time> &event)
  246. {
  247. #ifdef WITH_TRACE
  248. common::Trace<Time>::trace()
  249. << common::TraceElement<Time>(type::get_name(), t,
  250. common::FormalismType::PDEVS,
  251. common::FunctionType::POST_EVENT,
  252. common::LevelType::FORMALISM)
  253. << ": BEFORE => " << event.to_string();
  254. common::Trace<Time>::trace().flush();
  255. #endif
  256. type::add_event(event);
  257. _graph_manager.post_event(t, event);
  258. update_event_table(t);
  259. type::_tn = _event_table.get_current_time();
  260. #ifdef WITH_TRACE
  261. common::Trace<Time>::trace()
  262. << common::TraceElement<Time>(type::get_name(), t,
  263. common::FormalismType::PDEVS,
  264. common::FunctionType::POST_EVENT,
  265. common::LevelType::FORMALISM)
  266. << ": AFTER => " << event.to_string();
  267. common::Trace<Time>::trace().flush();
  268. #endif
  269. }
  270. typename Time::type dispatch_events(
  271. const common::Bag<Time> &bag,
  272. const typename Time::type &t)
  273. {
  274. #ifdef WITH_TRACE
  275. common::Trace<Time>::trace()
  276. << common::TraceElement<Time>(type::get_name(), t,
  277. common::FormalismType::PDEVS,
  278. common::FunctionType::Y_MESSAGE,
  279. common::LevelType::FORMALISM)
  280. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  281. << type::_tn << " ; bag = " << bag.to_string()
  282. << " ; " << _event_table.to_string();
  283. common::Trace<Time>::trace().flush();
  284. #endif
  285. _graph_manager.dispatch_events(bag, t);
  286. update_event_table(t);
  287. type::_tn = _event_table.get_current_time();
  288. #ifdef WITH_TRACE
  289. common::Trace<Time>::trace()
  290. << common::TraceElement<Time>(type::get_name(), t,
  291. common::FormalismType::PDEVS,
  292. common::FunctionType::Y_MESSAGE,
  293. common::LevelType::FORMALISM)
  294. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  295. << " ; " << _event_table.to_string();
  296. common::Trace<Time>::trace().flush();
  297. #endif
  298. return type::_tn;
  299. }
  300. common::Value observe(const typename Time::type & /* t */,
  301. unsigned int /* index */) const
  302. {
  303. assert(false);
  304. return common::Value();
  305. }
  306. void add_models_with_inputs(common::Models<Time> &receivers)
  307. {
  308. for (auto &model : _graph_manager.children()) {
  309. if (model->event_number() > 0) {
  310. if (std::find(receivers.begin(), receivers.end(), model)
  311. == receivers.end()) {
  312. receivers.push_back(model);
  313. }
  314. }
  315. }
  316. }
  317. void remove_model(const typename Time::type &t, common::Model<Time> *model) override
  318. {
  319. common::Coordinator<Time>::remove_model(t, model);
  320. _event_table.remove_model(model);
  321. }
  322. void update_event_table(typename Time::type t)
  323. {
  324. for (auto &model : _graph_manager.children()) {
  325. if (model->event_number() > 0) {
  326. _event_table.put(t, model);
  327. }
  328. }
  329. }
  330. protected:
  331. GraphManager _graph_manager;
  332. dsde::ExecutiveSimulator<Time, Executive, Parameters, GraphParameters> _executive;
  333. common::SchedulerType _event_table;
  334. };
  335. }
  336. } // namespace artis dsde
  337. #endif