Coordinator.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. const common::GraphManager<Time>& get_graph_manager() const { return _graph_manager; }
  56. typename Time::type start(const typename Time::type& t)
  57. {
  58. #ifdef WITH_TRACE
  59. common::Trace<Time>::trace()
  60. << common::TraceElement<Time>(type::get_name(), t,
  61. common::I_MESSAGE)
  62. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  63. << type::_tn;
  64. common::Trace<Time>::trace().flush();
  65. #endif
  66. assert(_graph_manager.children().size() > 0);
  67. for (auto& child : _graph_manager.children()) {
  68. child->start(t);
  69. }
  70. type::_tl = t;
  71. type::_tn = t;
  72. #ifdef WITH_TRACE
  73. common::Trace<Time>::trace()
  74. << common::TraceElement<Time>(type::get_name(), t,
  75. common::I_MESSAGE)
  76. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  77. << type::_tn;
  78. common::Trace<Time>::trace().flush();
  79. #endif
  80. return type::_tn;
  81. }
  82. typename Time::type dispatch_events(const common::Bag<Time>& bag,
  83. const typename Time::type& t)
  84. {
  85. #ifdef WITH_TRACE
  86. common::Trace<Time>::trace()
  87. << common::TraceElement<Time>(type::get_name(), t,
  88. common::Y_MESSAGE)
  89. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  90. << type::_tn << " ; bag = " << bag.to_string();
  91. common::Trace<Time>::trace().flush();
  92. #endif
  93. _graph_manager.dispatch_events(bag, t);
  94. #ifdef WITH_TRACE
  95. common::Trace<Time>::trace()
  96. << common::TraceElement<Time>(type::get_name(), t,
  97. common::Y_MESSAGE)
  98. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  99. << type::_tn;
  100. common::Trace<Time>::trace().flush();
  101. #endif
  102. return type::_tn;
  103. }
  104. common::Value observe(const typename Time::type& /* t */,
  105. unsigned int /* index */) const
  106. {
  107. assert(false);
  108. return common::Value();
  109. }
  110. void output(const typename Time::type& t)
  111. {
  112. #ifdef WITH_TRACE
  113. common::Trace<Time>::trace()
  114. << common::TraceElement<Time>(type::get_name(), t,
  115. common::OUTPUT)
  116. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  117. << type::_tn;
  118. common::Trace<Time>::trace().flush();
  119. #endif
  120. if (t == type::_tn) {
  121. for (auto& model : _graph_manager.children()) {
  122. model->output(t);
  123. }
  124. }
  125. #ifdef WITH_TRACE
  126. common::Trace<Time>::trace()
  127. << common::TraceElement<Time>(type::get_name(), t,
  128. common::OUTPUT)
  129. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  130. common::Trace<Time>::trace().flush();
  131. #endif
  132. }
  133. void post_event(const typename Time::type& t,
  134. const common::ExternalEvent<Time>& event)
  135. {
  136. #ifdef WITH_TRACE
  137. common::Trace<Time>::trace()
  138. << common::TraceElement<Time>(type::get_name(), t,
  139. common::POST_EVENT)
  140. << ": BEFORE => " << event.to_string();
  141. common::Trace<Time>::trace().flush();
  142. #endif
  143. if (t == type::_tn) {
  144. _graph_manager.post_event(t, event);
  145. } else {
  146. _policy(t, event, type::_tl, type::_tn);
  147. }
  148. #ifdef WITH_TRACE
  149. common::Trace<Time>::trace()
  150. << common::TraceElement<Time>(type::get_name(), t,
  151. common::POST_EVENT)
  152. << ": AFTER => " << event.to_string();
  153. common::Trace<Time>::trace().flush();
  154. #endif
  155. }
  156. typename Time::type transition(const typename Time::type& t)
  157. {
  158. #ifdef WITH_TRACE
  159. common::Trace<Time>::trace()
  160. << common::TraceElement<Time>(type::get_name(), t,
  161. common::S_MESSAGE)
  162. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  163. << type::_tn;
  164. common::Trace<Time>::trace().flush();
  165. #endif
  166. if (t == type::_tn) {
  167. for (auto& event : _policy.bag()) {
  168. post_event(t, event);
  169. }
  170. for (auto& model : _graph_manager.children()) {
  171. model->transition(t);
  172. }
  173. type::_tl = t;
  174. type::_tn = t + _time_step;
  175. }
  176. type::clear_bag();
  177. #ifdef WITH_TRACE
  178. common::Trace<Time>::trace()
  179. << common::TraceElement<Time>(type::get_name(), t,
  180. common::S_MESSAGE)
  181. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  182. common::Trace<Time>::trace().flush();
  183. #endif
  184. return type::_tn;
  185. }
  186. private:
  187. GraphManager _graph_manager;
  188. typename Time::type _time_step;
  189. Policy _policy;
  190. };
  191. }
  192. } // namespace artis dtss
  193. #endif