Coordinator.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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-2018 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 { namespace dtss {
  33. template < class Time >
  34. class Parameters
  35. {
  36. public:
  37. Parameters(typename Time::type time_step) : _time_step(time_step)
  38. { }
  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. {
  46. typedef Coordinator < Time, Policy, GraphManager,
  47. Parameters, GraphParameters > type;
  48. public:
  49. typedef Parameters parameters_type;
  50. typedef GraphParameters graph_parameters_type;
  51. Coordinator(const std::string& name,
  52. const Parameters& parameters,
  53. const GraphParameters& graph_paramaters) :
  54. common::Model < Time >(name),
  55. common::Coordinator < Time >(name),
  56. _graph_manager(this, graph_paramaters),
  57. _time_step(parameters._time_step)
  58. { }
  59. virtual ~Coordinator()
  60. { }
  61. typename Time::type start(const typename Time::type& t)
  62. {
  63. #ifdef WITH_TRACE
  64. common::Trace < Time >::trace()
  65. << common::TraceElement < Time >(type::get_name(), t,
  66. common::I_MESSAGE)
  67. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  68. << type::_tn;
  69. common::Trace < Time >::trace().flush();
  70. #endif
  71. assert(_graph_manager.children().size() > 0);
  72. for (auto & child : _graph_manager.children()) {
  73. child->start(t);
  74. }
  75. type::_tl = t;
  76. type::_tn = t;
  77. #ifdef WITH_TRACE
  78. common::Trace < Time >::trace()
  79. << common::TraceElement < Time >(type::get_name(), t,
  80. common::I_MESSAGE)
  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::Y_MESSAGE)
  94. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  95. << type::_tn << " ; bag = " << bag.to_string();
  96. common::Trace < Time >::trace().flush();
  97. #endif
  98. _graph_manager.dispatch_events(bag, t);
  99. #ifdef WITH_TRACE
  100. common::Trace < Time >::trace()
  101. << common::TraceElement < Time >(type::get_name(), t,
  102. common::Y_MESSAGE)
  103. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  104. << type::_tn;
  105. common::Trace < Time >::trace().flush();
  106. #endif
  107. return type::_tn;
  108. }
  109. void observation(std::ostream& file) const
  110. {
  111. for (auto & child : _graph_manager.children()) {
  112. child->observation(file);
  113. }
  114. }
  115. void output(const typename Time::type& t)
  116. {
  117. #ifdef WITH_TRACE
  118. common::Trace < Time >::trace()
  119. << common::TraceElement < Time >(type::get_name(), t,
  120. common::OUTPUT)
  121. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  122. << type::_tn;
  123. common::Trace < Time >::trace().flush();
  124. #endif
  125. if (t == type::_tn) {
  126. for (auto & model : _graph_manager.children()) {
  127. model->output(t);
  128. }
  129. }
  130. #ifdef WITH_TRACE
  131. common::Trace < Time >::trace()
  132. << common::TraceElement < Time >(type::get_name(), t,
  133. common::OUTPUT)
  134. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  135. common::Trace < Time >::trace().flush();
  136. #endif
  137. }
  138. void post_event(const typename Time::type& t,
  139. const common::ExternalEvent < Time >& event)
  140. {
  141. #ifdef WITH_TRACE
  142. common::Trace < Time >::trace()
  143. << common::TraceElement < Time >(type::get_name(), t,
  144. common::POST_EVENT)
  145. << ": BEFORE => " << event.to_string();
  146. common::Trace < Time >::trace().flush();
  147. #endif
  148. if (t == type::_tn) {
  149. _graph_manager.post_event(t, event);
  150. } else {
  151. _policy(t, event, type::_tl, type::_tn);
  152. }
  153. #ifdef WITH_TRACE
  154. common::Trace < Time >::trace()
  155. << common::TraceElement < Time >(type::get_name(), t,
  156. common::POST_EVENT)
  157. << ": AFTER => " << event.to_string();
  158. common::Trace < Time >::trace().flush();
  159. #endif
  160. }
  161. typename Time::type transition(const typename Time::type& t)
  162. {
  163. #ifdef WITH_TRACE
  164. common::Trace < Time >::trace()
  165. << common::TraceElement < Time >(type::get_name(), t,
  166. common::S_MESSAGE)
  167. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  168. << type::_tn;
  169. common::Trace < Time >::trace().flush();
  170. #endif
  171. if (t == type::_tn) {
  172. for (auto & event : _policy.bag()) {
  173. post_event(t, event);
  174. }
  175. for (auto & model : _graph_manager.children()) {
  176. model->transition(t);
  177. }
  178. type::_tl = t;
  179. type::_tn = t + _time_step;
  180. }
  181. type::clear_bag();
  182. #ifdef WITH_TRACE
  183. common::Trace < Time >::trace()
  184. << common::TraceElement < Time >(type::get_name(), t,
  185. common::S_MESSAGE)
  186. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  187. common::Trace < Time >::trace().flush();
  188. #endif
  189. return type::_tn;
  190. }
  191. private:
  192. GraphManager _graph_manager;
  193. typename Time::type _time_step;
  194. Policy _policy;
  195. };
  196. } } // namespace artis dtss
  197. #endif