Coordinator.hpp 10 KB

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