Coordinator.hpp 12 KB

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