Coordinator.hpp 10 KB

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