Coordinator.hpp 12 KB

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