Coordinator.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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
  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. struct Parameters {
  35. typename Time::type time_step;
  36. };
  37. template<class Time, class Policy, class GraphManager,
  38. class Parameters = Parameters<Time>,
  39. class GraphParameters = common::NoParameters>
  40. class Coordinator : public common::Coordinator<Time> {
  41. typedef Coordinator<Time, Policy, GraphManager, Parameters, GraphParameters> type;
  42. public:
  43. typedef Parameters parameters_type;
  44. typedef GraphParameters graph_parameters_type;
  45. Coordinator(const std::string &name, const Parameters &parameters,
  46. const GraphParameters &graph_parameters)
  47. :
  48. common::Model<Time>(name),
  49. common::Coordinator<Time>(name),
  50. _graph_manager(this, parameters, graph_parameters),
  51. _time_step(parameters.time_step) {}
  52. virtual ~Coordinator() {}
  53. GraphManager &get_graph_manager() { return _graph_manager; }
  54. const GraphManager &get_graph_manager() const { return _graph_manager; }
  55. virtual void finish(const typename Time::type &t) {
  56. #ifndef WITH_TRACE
  57. (void) t;
  58. #endif
  59. #ifdef WITH_TRACE
  60. common::Trace<Time>::trace()
  61. << common::TraceElement<Time>(type::get_name(), t,
  62. common::FormalismType::DTSS,
  63. common::FunctionType::FINISH,
  64. common::LevelType::FORMALISM);
  65. common::Trace<Time>::trace().flush();
  66. #endif
  67. }
  68. typename Time::type start(const typename Time::type &t) {
  69. #ifdef WITH_TRACE
  70. common::Trace<Time>::trace()
  71. << common::TraceElement<Time>(type::get_name(), t,
  72. common::FormalismType::DTSS,
  73. common::FunctionType::I_MESSAGE,
  74. common::LevelType::FORMALISM)
  75. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  76. << type::_tn;
  77. common::Trace<Time>::trace().flush();
  78. #endif
  79. assert(_graph_manager.children().size() > 0);
  80. for (auto &child : _graph_manager.children()) {
  81. child->start(t);
  82. }
  83. type::_tl = t;
  84. type::_tn = t;
  85. #ifdef WITH_TRACE
  86. common::Trace<Time>::trace()
  87. << common::TraceElement<Time>(type::get_name(), t,
  88. common::FormalismType::DTSS,
  89. common::FunctionType::I_MESSAGE,
  90. common::LevelType::FORMALISM)
  91. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  92. << type::_tn;
  93. common::Trace<Time>::trace().flush();
  94. #endif
  95. return type::_tn;
  96. }
  97. typename Time::type dispatch_events(const common::Bag<Time> &bag,
  98. const typename Time::type &t) {
  99. #ifdef WITH_TRACE
  100. common::Trace<Time>::trace()
  101. << common::TraceElement<Time>(type::get_name(), t,
  102. common::FormalismType::DTSS,
  103. common::FunctionType::Y_MESSAGE,
  104. common::LevelType::FORMALISM)
  105. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  106. << type::_tn << " ; bag = " << bag.to_string();
  107. common::Trace<Time>::trace().flush();
  108. #endif
  109. _graph_manager.dispatch_events(bag, t);
  110. #ifdef WITH_TRACE
  111. common::Trace<Time>::trace()
  112. << common::TraceElement<Time>(type::get_name(), t,
  113. common::FormalismType::DTSS,
  114. common::FunctionType::Y_MESSAGE,
  115. common::LevelType::FORMALISM)
  116. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  117. << type::_tn;
  118. common::Trace<Time>::trace().flush();
  119. #endif
  120. return type::_tn;
  121. }
  122. common::Value observe(const typename Time::type & /* t */,
  123. unsigned int /* index */) const {
  124. assert(false);
  125. return common::Value();
  126. }
  127. typename Time::type lookahead(const typename Time::type &t) const {
  128. return _graph_manager.lookahead(t);
  129. }
  130. void output(const typename Time::type &t) {
  131. #ifdef WITH_TRACE
  132. common::Trace<Time>::trace()
  133. << common::TraceElement<Time>(type::get_name(), t,
  134. common::FormalismType::DTSS,
  135. common::FunctionType::LAMBDA,
  136. common::LevelType::FORMALISM)
  137. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  138. << type::_tn;
  139. common::Trace<Time>::trace().flush();
  140. #endif
  141. if (t == type::_tn) {
  142. for (auto &model : _graph_manager.children()) {
  143. model->output(t);
  144. }
  145. }
  146. #ifdef WITH_TRACE
  147. common::Trace<Time>::trace()
  148. << common::TraceElement<Time>(type::get_name(), t,
  149. common::FormalismType::DTSS,
  150. common::FunctionType::LAMBDA,
  151. common::LevelType::FORMALISM)
  152. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  153. common::Trace<Time>::trace().flush();
  154. #endif
  155. }
  156. void post_event(const typename Time::type &t,
  157. const common::ExternalEvent<Time> &event) {
  158. #ifdef WITH_TRACE
  159. common::Trace<Time>::trace()
  160. << common::TraceElement<Time>(type::get_name(), t,
  161. common::FormalismType::DTSS,
  162. common::FunctionType::POST_EVENT,
  163. common::LevelType::FORMALISM)
  164. << ": BEFORE => " << event.to_string();
  165. common::Trace<Time>::trace().flush();
  166. #endif
  167. if (t == type::_tn) {
  168. _graph_manager.post_event(t, event);
  169. } else {
  170. _policy(t, event, type::_tl, type::_tn);
  171. }
  172. #ifdef WITH_TRACE
  173. common::Trace<Time>::trace()
  174. << common::TraceElement<Time>(type::get_name(), t,
  175. common::FormalismType::DTSS,
  176. common::FunctionType::POST_EVENT,
  177. common::LevelType::FORMALISM)
  178. << ": AFTER => " << event.to_string();
  179. common::Trace<Time>::trace().flush();
  180. #endif
  181. }
  182. typename Time::type transition(const typename Time::type &t) {
  183. #ifdef WITH_TRACE
  184. common::Trace<Time>::trace()
  185. << common::TraceElement<Time>(type::get_name(), t,
  186. common::FormalismType::DTSS,
  187. common::FunctionType::S_MESSAGE,
  188. common::LevelType::FORMALISM)
  189. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  190. << type::_tn;
  191. common::Trace<Time>::trace().flush();
  192. #endif
  193. if (t == type::_tn) {
  194. for (auto &event : _policy.bag()) {
  195. post_event(t, event);
  196. }
  197. _policy.clear();
  198. for (auto &model : _graph_manager.children()) {
  199. model->transition(t);
  200. }
  201. type::_tl = t;
  202. type::_tn = t + _time_step;
  203. }
  204. type::clear_bag();
  205. #ifdef WITH_TRACE
  206. common::Trace<Time>::trace()
  207. << common::TraceElement<Time>(type::get_name(), t,
  208. common::FormalismType::DTSS,
  209. common::FunctionType::S_MESSAGE,
  210. common::LevelType::FORMALISM)
  211. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  212. common::Trace<Time>::trace().flush();
  213. #endif
  214. return type::_tn;
  215. }
  216. private:
  217. GraphManager _graph_manager;
  218. typename Time::type _time_step;
  219. Policy _policy;
  220. };
  221. }
  222. } // namespace artis dtss
  223. #endif