Coordinator.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * @file kernel/dtss/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 DTSS_COORDINATOR
  26. #define DTSS_COORDINATOR
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Parameters.hpp>
  29. #include <artis-star/common/utils/Trace.hpp>
  30. #include <cassert>
  31. namespace artis {
  32. namespace dtss {
  33. template<class Time>
  34. struct Parameters
  35. {
  36. typename Time::type time_step;
  37. };
  38. template<class Time, class Policy, class GraphManager,
  39. class Parameters = Parameters<Time>,
  40. class GraphParameters = common::NoParameters>
  41. class Coordinator : public common::Coordinator<Time>
  42. {
  43. typedef Coordinator<Time, Policy, GraphManager, 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. _time_step(parameters.time_step)
  54. {}
  55. virtual ~Coordinator()
  56. {}
  57. GraphManager &get_graph_manager()
  58. { return _graph_manager; }
  59. const GraphManager &get_graph_manager() const
  60. { return _graph_manager; }
  61. virtual void finish(const typename Time::type &t)
  62. {
  63. #ifndef WITH_TRACE
  64. (void) t;
  65. #endif
  66. #ifdef WITH_TRACE
  67. common::Trace<Time>::trace()
  68. << common::TraceElement<Time>(type::get_name(), t,
  69. common::FormalismType::DTSS,
  70. common::FunctionType::FINISH,
  71. common::LevelType::FORMALISM);
  72. common::Trace<Time>::trace().flush();
  73. #endif
  74. }
  75. typename Time::type start(const typename Time::type &t)
  76. {
  77. #ifdef WITH_TRACE
  78. common::Trace<Time>::trace()
  79. << common::TraceElement<Time>(type::get_name(), t,
  80. common::FormalismType::DTSS,
  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. child->start(t);
  90. }
  91. type::_tl = t;
  92. type::_tn = t;
  93. #ifdef WITH_TRACE
  94. common::Trace<Time>::trace()
  95. << common::TraceElement<Time>(type::get_name(), t,
  96. common::FormalismType::DTSS,
  97. common::FunctionType::I_MESSAGE,
  98. common::LevelType::FORMALISM)
  99. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  100. << type::_tn;
  101. common::Trace<Time>::trace().flush();
  102. #endif
  103. return type::_tn;
  104. }
  105. typename Time::type dispatch_events(const common::Bag<Time> &bag,
  106. const typename Time::type &t)
  107. {
  108. #ifdef WITH_TRACE
  109. common::Trace<Time>::trace()
  110. << common::TraceElement<Time>(type::get_name(), t,
  111. common::FormalismType::DTSS,
  112. common::FunctionType::Y_MESSAGE,
  113. common::LevelType::FORMALISM)
  114. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  115. << type::_tn << " ; bag = " << bag.to_string();
  116. common::Trace<Time>::trace().flush();
  117. #endif
  118. _graph_manager.dispatch_events(bag, t);
  119. #ifdef WITH_TRACE
  120. common::Trace<Time>::trace()
  121. << common::TraceElement<Time>(type::get_name(), t,
  122. common::FormalismType::DTSS,
  123. common::FunctionType::Y_MESSAGE,
  124. common::LevelType::FORMALISM)
  125. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  126. << type::_tn;
  127. common::Trace<Time>::trace().flush();
  128. #endif
  129. return type::_tn;
  130. }
  131. common::Value observe(const typename Time::type & /* t */,
  132. unsigned int /* index */) const
  133. {
  134. assert(false);
  135. return common::Value();
  136. }
  137. typename Time::type lookahead(const typename Time::type &t) const
  138. {
  139. return _graph_manager.lookahead(t);
  140. }
  141. void output(const typename Time::type &t)
  142. {
  143. #ifdef WITH_TRACE
  144. common::Trace<Time>::trace()
  145. << common::TraceElement<Time>(type::get_name(), t,
  146. common::FormalismType::DTSS,
  147. common::FunctionType::LAMBDA,
  148. common::LevelType::FORMALISM)
  149. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  150. << type::_tn;
  151. common::Trace<Time>::trace().flush();
  152. #endif
  153. if (t == type::_tn) {
  154. for (auto &model : _graph_manager.children()) {
  155. model->output(t);
  156. }
  157. }
  158. #ifdef WITH_TRACE
  159. common::Trace<Time>::trace()
  160. << common::TraceElement<Time>(type::get_name(), t,
  161. common::FormalismType::DTSS,
  162. common::FunctionType::LAMBDA,
  163. common::LevelType::FORMALISM)
  164. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  165. common::Trace<Time>::trace().flush();
  166. #endif
  167. }
  168. void post_event(const typename Time::type &t,
  169. const common::ExternalEvent<Time> &event)
  170. {
  171. #ifdef WITH_TRACE
  172. common::Trace<Time>::trace()
  173. << common::TraceElement<Time>(type::get_name(), t,
  174. common::FormalismType::DTSS,
  175. common::FunctionType::POST_EVENT,
  176. common::LevelType::FORMALISM)
  177. << ": BEFORE => " << event.to_string();
  178. common::Trace<Time>::trace().flush();
  179. #endif
  180. if (t == type::_tn) {
  181. _graph_manager.post_event(t, event);
  182. } else {
  183. _policy(t, event, type::_tl, type::_tn);
  184. }
  185. #ifdef WITH_TRACE
  186. common::Trace<Time>::trace()
  187. << common::TraceElement<Time>(type::get_name(), t,
  188. common::FormalismType::DTSS,
  189. common::FunctionType::POST_EVENT,
  190. common::LevelType::FORMALISM)
  191. << ": AFTER => " << event.to_string();
  192. common::Trace<Time>::trace().flush();
  193. #endif
  194. }
  195. typename Time::type transition(const typename Time::type &t)
  196. {
  197. #ifdef WITH_TRACE
  198. common::Trace<Time>::trace()
  199. << common::TraceElement<Time>(type::get_name(), t,
  200. common::FormalismType::DTSS,
  201. common::FunctionType::S_MESSAGE,
  202. common::LevelType::FORMALISM)
  203. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  204. << type::_tn;
  205. common::Trace<Time>::trace().flush();
  206. #endif
  207. if (t == type::_tn) {
  208. for (auto &event : _policy.bag()) {
  209. post_event(t, event);
  210. }
  211. _policy.clear();
  212. for (auto &model : _graph_manager.children()) {
  213. model->transition(t);
  214. }
  215. type::_tl = t;
  216. type::_tn = t + _time_step;
  217. }
  218. type::clear_bag();
  219. #ifdef WITH_TRACE
  220. common::Trace<Time>::trace()
  221. << common::TraceElement<Time>(type::get_name(), t,
  222. common::FormalismType::DTSS,
  223. common::FunctionType::S_MESSAGE,
  224. common::LevelType::FORMALISM)
  225. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  226. common::Trace<Time>::trace().flush();
  227. #endif
  228. return type::_tn;
  229. }
  230. private:
  231. GraphManager _graph_manager;
  232. typename Time::type _time_step;
  233. Policy _policy;
  234. };
  235. }
  236. } // namespace artis dtss
  237. #endif