Coordinator.hpp 15 KB

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