Coordinator.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. common::GraphManager<Time>& get_graph_manager() { return _graph_manager; }
  57. const common::GraphManager<Time>& get_graph_manager() const { return _graph_manager; }
  58. typename Time::type start(const typename Time::type& t)
  59. {
  60. #ifdef WITH_TRACE
  61. common::Trace<Time>::trace()
  62. << common::TraceElement<Time>(type::get_name(), t,
  63. common::FormalismType::DTSS,
  64. common::FunctionType::I_MESSAGE,
  65. common::LevelType::FORMALISM)
  66. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  67. << type::_tn;
  68. common::Trace<Time>::trace().flush();
  69. #endif
  70. assert(_graph_manager.children().size() > 0);
  71. for (auto& child : _graph_manager.children()) {
  72. child->start(t);
  73. }
  74. type::_tl = t;
  75. type::_tn = t;
  76. #ifdef WITH_TRACE
  77. common::Trace<Time>::trace()
  78. << common::TraceElement<Time>(type::get_name(), t,
  79. common::FormalismType::DTSS,
  80. common::FunctionType::I_MESSAGE,
  81. common::LevelType::FORMALISM)
  82. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  83. << type::_tn;
  84. common::Trace<Time>::trace().flush();
  85. #endif
  86. return type::_tn;
  87. }
  88. typename Time::type dispatch_events(const common::Bag<Time>& bag,
  89. const typename Time::type& t)
  90. {
  91. #ifdef WITH_TRACE
  92. common::Trace<Time>::trace()
  93. << common::TraceElement<Time>(type::get_name(), t,
  94. common::FormalismType::DTSS,
  95. common::FunctionType::Y_MESSAGE,
  96. common::LevelType::FORMALISM)
  97. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  98. << type::_tn << " ; bag = " << bag.to_string();
  99. common::Trace<Time>::trace().flush();
  100. #endif
  101. _graph_manager.dispatch_events(bag, t);
  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;
  110. common::Trace<Time>::trace().flush();
  111. #endif
  112. return type::_tn;
  113. }
  114. common::Value observe(const typename Time::type& /* t */,
  115. unsigned int /* index */) const
  116. {
  117. assert(false);
  118. return common::Value();
  119. }
  120. void output(const typename Time::type& t)
  121. {
  122. #ifdef WITH_TRACE
  123. common::Trace<Time>::trace()
  124. << common::TraceElement<Time>(type::get_name(), t,
  125. common::FormalismType::DTSS,
  126. common::FunctionType::LAMBDA,
  127. common::LevelType::FORMALISM)
  128. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  129. << type::_tn;
  130. common::Trace<Time>::trace().flush();
  131. #endif
  132. if (t == type::_tn) {
  133. for (auto& model : _graph_manager.children()) {
  134. model->output(t);
  135. }
  136. }
  137. #ifdef WITH_TRACE
  138. common::Trace<Time>::trace()
  139. << common::TraceElement<Time>(type::get_name(), t,
  140. common::FormalismType::DTSS,
  141. common::FunctionType::LAMBDA,
  142. common::LevelType::FORMALISM)
  143. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  144. common::Trace<Time>::trace().flush();
  145. #endif
  146. }
  147. void post_event(const typename Time::type& t,
  148. const common::ExternalEvent<Time>& event)
  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::POST_EVENT,
  155. common::LevelType::FORMALISM)
  156. << ": BEFORE => " << event.to_string();
  157. common::Trace<Time>::trace().flush();
  158. #endif
  159. if (t == type::_tn) {
  160. _graph_manager.post_event(t, event);
  161. } else {
  162. _policy(t, event, type::_tl, type::_tn);
  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. << ": AFTER => " << event.to_string();
  171. common::Trace<Time>::trace().flush();
  172. #endif
  173. }
  174. typename Time::type transition(const typename Time::type& t)
  175. {
  176. #ifdef WITH_TRACE
  177. common::Trace<Time>::trace()
  178. << common::TraceElement<Time>(type::get_name(), t,
  179. common::FormalismType::DTSS,
  180. common::FunctionType::S_MESSAGE,
  181. common::LevelType::FORMALISM)
  182. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  183. << type::_tn;
  184. common::Trace<Time>::trace().flush();
  185. #endif
  186. if (t == type::_tn) {
  187. for (auto& event : _policy.bag()) {
  188. post_event(t, event);
  189. }
  190. _policy.clear();
  191. for (auto& model : _graph_manager.children()) {
  192. model->transition(t);
  193. }
  194. type::_tl = t;
  195. type::_tn = t + _time_step;
  196. }
  197. type::clear_bag();
  198. #ifdef WITH_TRACE
  199. common::Trace<Time>::trace()
  200. << common::TraceElement<Time>(type::get_name(), t,
  201. common::FormalismType::DTSS,
  202. common::FunctionType::S_MESSAGE,
  203. common::LevelType::FORMALISM)
  204. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  205. common::Trace<Time>::trace().flush();
  206. #endif
  207. return type::_tn;
  208. }
  209. private:
  210. GraphManager _graph_manager;
  211. typename Time::type _time_step;
  212. Policy _policy;
  213. };
  214. }
  215. } // namespace artis dtss
  216. #endif