Coordinator.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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-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 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::dtss {
  32. template<typename Time>
  33. struct Parameters {
  34. typename Time::type time_step;
  35. };
  36. template<typename Time, typename Policy, typename GraphManager,
  37. typename Parameters = Parameters<Time>,
  38. typename GraphParameters = common::NoParameters>
  39. class Coordinator : public common::Coordinator<Time> {
  40. typedef Coordinator<Time, Policy, GraphManager, Parameters, GraphParameters> type;
  41. public:
  42. typedef Parameters parameters_type;
  43. typedef GraphParameters graph_parameters_type;
  44. Coordinator(const std::string &name, const Parameters &parameters,
  45. const GraphParameters &graph_parameters)
  46. :
  47. common::Model<Time>(name),
  48. common::Coordinator<Time>(name),
  49. _graph_manager(this, parameters, graph_parameters),
  50. _time_step(parameters.time_step) {}
  51. virtual ~Coordinator() {}
  52. GraphManager &get_graph_manager() { return _graph_manager; }
  53. const GraphManager &get_graph_manager() const { return _graph_manager; }
  54. virtual void finish(const typename Time::type &t) {
  55. #ifndef WITH_TRACE
  56. (void) t;
  57. #endif
  58. #ifdef WITH_TRACE
  59. common::Trace<Time>::trace()
  60. << common::TraceElement<Time>(type::get_name(), t,
  61. common::FormalismType::DTSS,
  62. common::FunctionType::FINISH,
  63. common::LevelType::FORMALISM);
  64. common::Trace<Time>::trace().flush();
  65. #endif
  66. }
  67. typename Time::type start(const typename Time::type &t) {
  68. #ifdef WITH_TRACE
  69. common::Trace<Time>::trace()
  70. << common::TraceElement<Time>(type::get_name(), t,
  71. common::FormalismType::DTSS,
  72. common::FunctionType::I_MESSAGE,
  73. common::LevelType::FORMALISM)
  74. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  75. << type::_tn;
  76. common::Trace<Time>::trace().flush();
  77. #endif
  78. assert(_graph_manager.children().size() > 0);
  79. for (auto &child: _graph_manager.children()) {
  80. child->start(t);
  81. }
  82. type::_tl = t;
  83. type::_tn = t;
  84. #ifdef WITH_TRACE
  85. common::Trace<Time>::trace()
  86. << common::TraceElement<Time>(type::get_name(), t,
  87. common::FormalismType::DTSS,
  88. common::FunctionType::I_MESSAGE,
  89. common::LevelType::FORMALISM)
  90. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  91. << type::_tn;
  92. common::Trace<Time>::trace().flush();
  93. #endif
  94. return type::_tn;
  95. }
  96. typename Time::type dispatch_events(const common::event::Bag<Time> &bag,
  97. const typename Time::type &t) {
  98. #ifdef WITH_TRACE
  99. common::Trace<Time>::trace()
  100. << common::TraceElement<Time>(type::get_name(), t,
  101. common::FormalismType::DTSS,
  102. common::FunctionType::Y_MESSAGE,
  103. common::LevelType::FORMALISM)
  104. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  105. << type::_tn << " ; bag = " << bag.to_string();
  106. common::Trace<Time>::trace().flush();
  107. #endif
  108. _graph_manager.dispatch_events(bag, t);
  109. #ifdef WITH_TRACE
  110. common::Trace<Time>::trace()
  111. << common::TraceElement<Time>(type::get_name(), t,
  112. common::FormalismType::DTSS,
  113. common::FunctionType::Y_MESSAGE,
  114. common::LevelType::FORMALISM)
  115. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  116. << type::_tn;
  117. common::Trace<Time>::trace().flush();
  118. #endif
  119. return type::_tn;
  120. }
  121. common::event::Value observe(const typename Time::type & /* t */,
  122. unsigned int /* index */) const {
  123. assert(false);
  124. return common::event::Value();
  125. }
  126. typename Time::type lookahead(const typename Time::type &t) const {
  127. return _graph_manager.lookahead(t);
  128. }
  129. void output(const typename Time::type &t) {
  130. #ifdef WITH_TRACE
  131. common::Trace<Time>::trace()
  132. << common::TraceElement<Time>(type::get_name(), t,
  133. common::FormalismType::DTSS,
  134. common::FunctionType::LAMBDA,
  135. common::LevelType::FORMALISM)
  136. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  137. << type::_tn;
  138. common::Trace<Time>::trace().flush();
  139. #endif
  140. if (t == type::_tn) {
  141. for (auto &model: _graph_manager.children()) {
  142. model->output(t);
  143. }
  144. }
  145. #ifdef WITH_TRACE
  146. common::Trace<Time>::trace()
  147. << common::TraceElement<Time>(type::get_name(), t,
  148. common::FormalismType::DTSS,
  149. common::FunctionType::LAMBDA,
  150. common::LevelType::FORMALISM)
  151. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  152. common::Trace<Time>::trace().flush();
  153. #endif
  154. }
  155. void post_event(const typename Time::type &t,
  156. const common::event::ExternalEvent<Time> &event) {
  157. #ifdef WITH_TRACE
  158. common::Trace<Time>::trace()
  159. << common::TraceElement<Time>(type::get_name(), t,
  160. common::FormalismType::DTSS,
  161. common::FunctionType::POST_EVENT,
  162. common::LevelType::FORMALISM)
  163. << ": BEFORE => " << event.to_string();
  164. common::Trace<Time>::trace().flush();
  165. #endif
  166. if (t == type::_tn) {
  167. _graph_manager.post_event(t, event);
  168. } else {
  169. _policy(t, event, type::_tl, type::_tn);
  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. << ": AFTER => " << event.to_string();
  178. common::Trace<Time>::trace().flush();
  179. #endif
  180. }
  181. typename Time::type transition(const typename Time::type &t) {
  182. #ifdef WITH_TRACE
  183. common::Trace<Time>::trace()
  184. << common::TraceElement<Time>(type::get_name(), t,
  185. common::FormalismType::DTSS,
  186. common::FunctionType::S_MESSAGE,
  187. common::LevelType::FORMALISM)
  188. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  189. << type::_tn;
  190. common::Trace<Time>::trace().flush();
  191. #endif
  192. if (t == type::_tn) {
  193. for (auto &event: _policy.bag()) {
  194. post_event(t, event);
  195. }
  196. _policy.clear();
  197. for (auto &model: _graph_manager.children()) {
  198. model->transition(t);
  199. }
  200. type::_tl = t;
  201. type::_tn = t + _time_step;
  202. }
  203. type::clear_bag();
  204. #ifdef WITH_TRACE
  205. common::Trace<Time>::trace()
  206. << common::TraceElement<Time>(type::get_name(), t,
  207. common::FormalismType::DTSS,
  208. common::FunctionType::S_MESSAGE,
  209. common::LevelType::FORMALISM)
  210. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  211. common::Trace<Time>::trace().flush();
  212. #endif
  213. return type::_tn;
  214. }
  215. private:
  216. GraphManager _graph_manager;
  217. typename Time::type _time_step;
  218. Policy _policy;
  219. };
  220. } // namespace artis dtss
  221. #endif