Coordinator.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @file dtss/Coordinator.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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 <common/Coordinator.hpp>
  28. #include <common/Parameters.hpp>
  29. #include <common/Trace.hpp>
  30. #include <cassert>
  31. #include <iostream>
  32. namespace paradevs { 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, Parameters,
  47. 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::Coordinator < Time >(name),
  55. _graph_manager(this, graph_paramaters),
  56. _time_step(parameters._time_step)
  57. { }
  58. virtual ~Coordinator()
  59. { }
  60. typename Time::type start(typename Time::type t)
  61. {
  62. #ifdef WITH_TRACE
  63. common::Trace < Time >::trace()
  64. << common::TraceElement < Time >(type::get_name(), t,
  65. common::I_MESSAGE)
  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(type::_tn);
  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::I_MESSAGE)
  80. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  81. << type::_tn;
  82. common::Trace < Time >::trace().flush();
  83. #endif
  84. return type::_tn;
  85. }
  86. typename Time::type dispatch_events(common::Bag < Time > bag,
  87. typename Time::type t)
  88. {
  89. #ifdef WITH_TRACE
  90. common::Trace < Time >::trace()
  91. << common::TraceElement < Time >(type::get_name(), t,
  92. common::Y_MESSAGE)
  93. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  94. << type::_tn << " ; bag = " << bag.to_string();
  95. common::Trace < Time >::trace().flush();
  96. #endif
  97. _graph_manager.dispatch_events(bag, t);
  98. #ifdef WITH_TRACE
  99. common::Trace < Time >::trace()
  100. << common::TraceElement < Time >(type::get_name(), t,
  101. common::Y_MESSAGE)
  102. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  103. << type::_tn;
  104. common::Trace < Time >::trace().flush();
  105. #endif
  106. return type::_tn;
  107. }
  108. void observation(std::ostream& file) const
  109. {
  110. for (auto & child : _graph_manager.children()) {
  111. child->observation(file);
  112. }
  113. }
  114. void output(typename Time::type t)
  115. {
  116. #ifdef WITH_TRACE
  117. common::Trace < Time >::trace()
  118. << common::TraceElement < Time >(type::get_name(), t,
  119. common::OUTPUT)
  120. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  121. << type::_tn;
  122. common::Trace < Time >::trace().flush();
  123. #endif
  124. if (t == type::_tn) {
  125. for (auto & model : _graph_manager.children()) {
  126. model->output(t);
  127. }
  128. }
  129. #ifdef WITH_TRACE
  130. common::Trace < Time >::trace()
  131. << common::TraceElement < Time >(type::get_name(), t,
  132. common::OUTPUT)
  133. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  134. common::Trace < Time >::trace().flush();
  135. #endif
  136. }
  137. void post_event(typename Time::type t,
  138. const common::ExternalEvent < Time >& event)
  139. {
  140. #ifdef WITH_TRACE
  141. common::Trace < Time >::trace()
  142. << common::TraceElement < Time >(type::get_name(), t,
  143. common::POST_EVENT)
  144. << ": BEFORE => " << event.to_string();
  145. common::Trace < Time >::trace().flush();
  146. #endif
  147. if (t == type::_tn) {
  148. _graph_manager.post_event(t, event);
  149. } else {
  150. _policy(t, event, type::_tl, type::_tn);
  151. }
  152. #ifdef WITH_TRACE
  153. common::Trace < Time >::trace()
  154. << common::TraceElement < Time >(type::get_name(), t,
  155. common::POST_EVENT)
  156. << ": AFTER => " << event.to_string();
  157. common::Trace < Time >::trace().flush();
  158. #endif
  159. }
  160. typename Time::type transition(typename Time::type t)
  161. {
  162. #ifdef WITH_TRACE
  163. common::Trace < Time >::trace()
  164. << common::TraceElement < Time >(type::get_name(), t,
  165. common::S_MESSAGE)
  166. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  167. << type::_tn;
  168. common::Trace < Time >::trace().flush();
  169. #endif
  170. if (t == type::_tn) {
  171. for (auto & event : _policy.bag()) {
  172. post_event(t, event);
  173. }
  174. for (auto & model : _graph_manager.children()) {
  175. model->transition(t);
  176. }
  177. type::_tl = t;
  178. type::_tn = t + _time_step;
  179. }
  180. type::clear_bag();
  181. #ifdef WITH_TRACE
  182. common::Trace < Time >::trace()
  183. << common::TraceElement < Time >(type::get_name(), t,
  184. common::S_MESSAGE)
  185. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  186. common::Trace < Time >::trace().flush();
  187. #endif
  188. return type::_tn;
  189. }
  190. private:
  191. GraphManager _graph_manager;
  192. typename Time::type _time_step;
  193. Policy _policy;
  194. };
  195. } } // namespace paradevs dtss
  196. #endif