Coordinator.hpp 8.0 KB

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