Coordinator.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. void output(const typename Time::type& t)
  132. {
  133. #ifdef WITH_TRACE
  134. common::Trace<Time>::trace()
  135. << common::TraceElement<Time>(type::get_name(), t,
  136. common::FormalismType::DTSS,
  137. common::FunctionType::LAMBDA,
  138. common::LevelType::FORMALISM)
  139. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  140. << type::_tn;
  141. common::Trace<Time>::trace().flush();
  142. #endif
  143. if (t == type::_tn) {
  144. for (auto& model : _graph_manager.children()) {
  145. model->output(t);
  146. }
  147. }
  148. #ifdef WITH_TRACE
  149. common::Trace<Time>::trace()
  150. << common::TraceElement<Time>(type::get_name(), t,
  151. common::FormalismType::DTSS,
  152. common::FunctionType::LAMBDA,
  153. common::LevelType::FORMALISM)
  154. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  155. common::Trace<Time>::trace().flush();
  156. #endif
  157. }
  158. void post_event(const typename Time::type& t,
  159. const common::ExternalEvent<Time>& event)
  160. {
  161. #ifdef WITH_TRACE
  162. common::Trace<Time>::trace()
  163. << common::TraceElement<Time>(type::get_name(), t,
  164. common::FormalismType::DTSS,
  165. common::FunctionType::POST_EVENT,
  166. common::LevelType::FORMALISM)
  167. << ": BEFORE => " << event.to_string();
  168. common::Trace<Time>::trace().flush();
  169. #endif
  170. if (t == type::_tn) {
  171. _graph_manager.post_event(t, event);
  172. } else {
  173. _policy(t, event, type::_tl, type::_tn);
  174. }
  175. #ifdef WITH_TRACE
  176. common::Trace<Time>::trace()
  177. << common::TraceElement<Time>(type::get_name(), t,
  178. common::FormalismType::DTSS,
  179. common::FunctionType::POST_EVENT,
  180. common::LevelType::FORMALISM)
  181. << ": AFTER => " << event.to_string();
  182. common::Trace<Time>::trace().flush();
  183. #endif
  184. }
  185. typename Time::type transition(const typename Time::type& t)
  186. {
  187. #ifdef WITH_TRACE
  188. common::Trace<Time>::trace()
  189. << common::TraceElement<Time>(type::get_name(), t,
  190. common::FormalismType::DTSS,
  191. common::FunctionType::S_MESSAGE,
  192. common::LevelType::FORMALISM)
  193. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  194. << type::_tn;
  195. common::Trace<Time>::trace().flush();
  196. #endif
  197. if (t == type::_tn) {
  198. for (auto& event : _policy.bag()) {
  199. post_event(t, event);
  200. }
  201. _policy.clear();
  202. for (auto& model : _graph_manager.children()) {
  203. model->transition(t);
  204. }
  205. type::_tl = t;
  206. type::_tn = t + _time_step;
  207. }
  208. type::clear_bag();
  209. #ifdef WITH_TRACE
  210. common::Trace<Time>::trace()
  211. << common::TraceElement<Time>(type::get_name(), t,
  212. common::FormalismType::DTSS,
  213. common::FunctionType::S_MESSAGE,
  214. common::LevelType::FORMALISM)
  215. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  216. common::Trace<Time>::trace().flush();
  217. #endif
  218. return type::_tn;
  219. }
  220. private:
  221. GraphManager _graph_manager;
  222. typename Time::type _time_step;
  223. Policy _policy;
  224. };
  225. }
  226. } // namespace artis dtss
  227. #endif