Coordinator.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @file 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 <cassert>
  29. #include <iostream>
  30. namespace paradevs { namespace dtss {
  31. template < class Time >
  32. class Parameters
  33. {
  34. public:
  35. Parameters(typename Time::type time_step) : _time_step(time_step)
  36. { }
  37. typename Time::type _time_step;
  38. };
  39. template < class Time, class Policy, class GraphManager >
  40. class Coordinator : public common::Coordinator < Time >
  41. {
  42. public:
  43. typedef Parameters < Time > parameters_type;
  44. Coordinator(const std::string& name,
  45. const Parameters < Time >& parameters) :
  46. common::Coordinator < Time >(name), _graph_manager(this),
  47. _time_step(parameters._time_step)
  48. { }
  49. virtual ~Coordinator()
  50. { }
  51. typename Time::type start(typename Time::type t)
  52. {
  53. #ifdef WITH_TRACE
  54. common::Trace < Time >::trace()
  55. << common::TraceElement < Time >(
  56. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  57. common::I_MESSAGE)
  58. << ": BEFORE => "
  59. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  60. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  61. common::Trace < Time >::trace().flush();
  62. #endif
  63. assert(_graph_manager.children().size() > 0);
  64. for (auto & child : _graph_manager.children()) {
  65. child->start(Coordinator < Time, Policy, GraphManager >::_tn);
  66. }
  67. Coordinator < Time, Policy, GraphManager >::_tl = t;
  68. Coordinator < Time, Policy, GraphManager >::_tn = t;
  69. #ifdef WITH_TRACE
  70. common::Trace < Time >::trace()
  71. << common::TraceElement < Time >(
  72. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  73. common::I_MESSAGE)
  74. << ": AFTER => "
  75. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  76. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  77. common::Trace < Time >::trace().flush();
  78. #endif
  79. return Coordinator < Time, Policy, GraphManager >::_tn;
  80. }
  81. typename Time::type dispatch_events(common::Bag < Time > bag,
  82. typename Time::type t)
  83. {
  84. #ifdef WITH_TRACE
  85. common::Trace < Time >::trace()
  86. << common::TraceElement < Time >(
  87. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  88. common::Y_MESSAGE)
  89. << ": BEFORE => "
  90. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  91. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn
  92. << " ; bag = " << bag.to_string();
  93. common::Trace < Time >::trace().flush();
  94. #endif
  95. _graph_manager.dispatch_events(bag, t);
  96. #ifdef WITH_TRACE
  97. common::Trace < Time >::trace()
  98. << common::TraceElement < Time >(
  99. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  100. common::Y_MESSAGE)
  101. << ": BEFORE => "
  102. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  103. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  104. common::Trace < Time >::trace().flush();
  105. #endif
  106. return Coordinator < Time, Policy, GraphManager >::_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 >(
  119. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  120. common::OUTPUT)
  121. << ": BEFORE => "
  122. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  123. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  124. common::Trace < Time >::trace().flush();
  125. #endif
  126. if (t == Coordinator < Time, Policy, GraphManager >::_tn) {
  127. for (auto & model : _graph_manager.children()) {
  128. model->output(t);
  129. }
  130. }
  131. #ifdef WITH_TRACE
  132. common::Trace < Time >::trace()
  133. << common::TraceElement < Time >(
  134. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  135. common::OUTPUT)
  136. << ": AFTER => "
  137. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  138. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  139. common::Trace < Time >::trace().flush();
  140. #endif
  141. }
  142. void post_event(typename Time::type t,
  143. const common::ExternalEvent < Time >& event)
  144. {
  145. #ifdef WITH_TRACE
  146. common::Trace < Time >::trace()
  147. << common::TraceElement < Time >(
  148. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  149. common::POST_EVENT)
  150. << ": BEFORE => " << event.to_string();
  151. common::Trace < Time >::trace().flush();
  152. #endif
  153. if (t == Coordinator < Time, Policy, GraphManager >::_tn) {
  154. _graph_manager.post_event(t, event);
  155. } else {
  156. _policy(t, event, Coordinator < Time, Policy, GraphManager >::_tl,
  157. Coordinator < Time, Policy, GraphManager >::_tn);
  158. }
  159. #ifdef WITH_TRACE
  160. common::Trace < Time >::trace()
  161. << common::TraceElement < Time >(
  162. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  163. common::POST_EVENT)
  164. << ": AFTER => " << event.to_string();
  165. common::Trace < Time >::trace().flush();
  166. #endif
  167. }
  168. typename Time::type transition(typename Time::type t)
  169. {
  170. #ifdef WITH_TRACE
  171. common::Trace < Time >::trace()
  172. << common::TraceElement < Time >(
  173. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  174. common::S_MESSAGE)
  175. << ": BEFORE => "
  176. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  177. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  178. common::Trace < Time >::trace().flush();
  179. #endif
  180. if (t == Coordinator < Time, Policy, GraphManager >::_tn) {
  181. for (auto & event : _policy.bag()) {
  182. post_event(t, event);
  183. }
  184. for (auto & model : _graph_manager.children()) {
  185. model->transition(t);
  186. }
  187. Coordinator < Time, Policy, GraphManager >::_tl = t;
  188. Coordinator < Time, Policy, GraphManager >::_tn = t + _time_step;
  189. }
  190. Coordinator < Time, Policy, GraphManager >::clear_bag();
  191. #ifdef WITH_TRACE
  192. common::Trace < Time >::trace()
  193. << common::TraceElement < Time >(
  194. Coordinator < Time, Policy, GraphManager >::get_name(), t,
  195. common::S_MESSAGE)
  196. << ": AFTER => "
  197. << "tl = " << Coordinator < Time, Policy, GraphManager >::_tl
  198. << " ; tn = " << Coordinator < Time, Policy, GraphManager >::_tn;
  199. common::Trace < Time >::trace().flush();
  200. #endif
  201. return Coordinator < Time, Policy, GraphManager >::_tn;
  202. }
  203. private:
  204. GraphManager _graph_manager;
  205. typename Time::type _time_step;
  206. Policy _policy;
  207. };
  208. } } // namespace paradevs dtss
  209. #endif