Coordinator.hpp 9.6 KB

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