Executive.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * @file kernel/dsde/Executive.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-2022 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 DSDE_EXECUTIVE
  26. #define DSDE_EXECUTIVE
  27. #include <artis-star/kernel/dsde/GraphManager.hpp>
  28. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  29. namespace artis::dsde {
  30. template<typename Time, typename Dynamics, typename Parameters, typename GraphParameters>
  31. class ExecutiveSimulator;
  32. template<typename Time, typename Dynamics,
  33. typename Parameters = common::NoParameters,
  34. typename GraphParameters = common::NoParameters>
  35. class ExecutiveContext {
  36. typedef ExecutiveSimulator<Time, Dynamics, Parameters, GraphParameters> Simulator;
  37. public:
  38. ExecutiveContext(const Parameters &parameters, const GraphParameters &graph_parameters,
  39. Simulator *simulator)
  40. :
  41. _parameters(parameters), _graph_parameters(graph_parameters),
  42. _simulator(simulator) {}
  43. virtual ~ExecutiveContext() {}
  44. const GraphParameters &graph_parameters() const { return _graph_parameters; }
  45. const Parameters &parameters() const { return _parameters; }
  46. Simulator *simulator() const { return _simulator; }
  47. private:
  48. const Parameters &_parameters;
  49. const GraphParameters &_graph_parameters;
  50. Simulator *_simulator;
  51. };
  52. template<typename Time, typename Dynamics,
  53. typename Parameters = common::NoParameters,
  54. typename GraphParameters = common::NoParameters>
  55. class ExecutiveSimulator : public common::Simulator<Time> {
  56. typedef ExecutiveSimulator<Time, Dynamics, Parameters, GraphParameters> type;
  57. public :
  58. ExecutiveSimulator(const Parameters &parameters,
  59. const GraphParameters &graph_parameters,
  60. GraphManager<Time, Parameters, GraphParameters> &graph_manager)
  61. :
  62. common::Model<Time>("executive"),
  63. common::Simulator<Time>("executive"),
  64. _dynamics(ExecutiveContext<Time, Dynamics, Parameters, GraphParameters>(
  65. parameters, graph_parameters, this), graph_manager) {}
  66. ~ExecutiveSimulator() {}
  67. const Dynamics &dynamics() const { return _dynamics; }
  68. virtual void restore(const common::context::State<Time> &state) {
  69. common::Simulator<Time>::restore(state);
  70. _dynamics.restore(state);
  71. }
  72. virtual void save(common::context::State<Time> &state) const {
  73. common::Simulator<Time>::save(state);
  74. _dynamics.save(state);
  75. }
  76. virtual std::string to_string(int level) const {
  77. std::ostringstream ss;
  78. ss << common::String::make_spaces(level * 2) << "executive simulator \""
  79. << type::get_name() << "\"" << std::endl;
  80. return ss.str();
  81. }
  82. virtual void finish(const typename Time::type &t) {
  83. #ifndef WITH_TRACE
  84. (void) t;
  85. #endif
  86. #ifdef WITH_TRACE
  87. common::Trace<Time>::trace()
  88. << common::TraceElement<Time>(type::get_name(), t,
  89. common::FormalismType::DSDE,
  90. common::FunctionType::FINISH,
  91. common::LevelType::FORMALISM);
  92. common::Trace<Time>::trace().flush();
  93. #endif
  94. }
  95. typename Time::type start(const typename Time::type &t) {
  96. // When i-message(t)
  97. // tl = t - e
  98. // tn = tl + ta(s)
  99. // End
  100. #ifdef WITH_TRACE
  101. common::Trace<Time>::trace()
  102. << common::TraceElement<Time>(type::get_name(), t,
  103. common::FormalismType::DSDE,
  104. common::FunctionType::I_MESSAGE,
  105. common::LevelType::FORMALISM)
  106. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  107. << type::_tn;
  108. common::Trace<Time>::trace().flush();
  109. #endif
  110. type::_tl = t;
  111. type::_tn = type::_tl + _dynamics.start(t);
  112. #ifdef WITH_TRACE
  113. common::Trace<Time>::trace()
  114. << common::TraceElement<Time>(type::get_name(), t,
  115. common::FormalismType::DSDE,
  116. common::FunctionType::I_MESSAGE,
  117. common::LevelType::FORMALISM)
  118. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  119. << type::_tn;
  120. common::Trace<Time>::trace().flush();
  121. #endif
  122. return type::_tn;
  123. }
  124. common::event::Value observe(const typename Time::type &t,
  125. unsigned int index) const { return _dynamics.observe(t, index); }
  126. void output(const typename Time::type &t) {
  127. // When *-message(t)
  128. // if (t = tn) then
  129. // y = lambda(s)
  130. // send y-message(y,t) to parent
  131. // End
  132. #ifdef WITH_TRACE
  133. common::Trace<Time>::trace()
  134. << common::TraceElement<Time>(type::get_name(), t,
  135. common::FormalismType::DSDE,
  136. common::FunctionType::OUTPUT,
  137. common::LevelType::FORMALISM)
  138. << ": BEFORE";
  139. common::Trace<Time>::trace().flush();
  140. #endif
  141. if (t == type::_tn) {
  142. common::event::Bag<Time> bag = _dynamics.lambda(t);
  143. if (not bag.empty()) {
  144. for (auto &event: bag) {
  145. event.set_model(this);
  146. }
  147. dynamic_cast < common::Coordinator<Time> * >(
  148. type::get_parent())->dispatch_events(bag, t);
  149. }
  150. }
  151. #ifdef WITH_TRACE
  152. common::Trace<Time>::trace()
  153. << common::TraceElement<Time>(type::get_name(), t,
  154. common::FormalismType::DSDE,
  155. common::FunctionType::OUTPUT,
  156. common::LevelType::FORMALISM)
  157. << ": AFTER";
  158. common::Trace<Time>::trace().flush();
  159. #endif
  160. }
  161. void post_event(const typename Time::type &t,
  162. const common::event::ExternalEvent<Time> &event) {
  163. #ifndef WITH_TRACE
  164. (void) t;
  165. #endif
  166. #ifdef WITH_TRACE
  167. common::Trace<Time>::trace()
  168. << common::TraceElement<Time>(type::get_name(), t,
  169. common::FormalismType::DSDE,
  170. common::FunctionType::POST_EVENT,
  171. common::LevelType::FORMALISM)
  172. << ": BEFORE => " << event.to_string();
  173. common::Trace<Time>::trace().flush();
  174. #endif
  175. type::add_event(event);
  176. #ifdef WITH_TRACE
  177. common::Trace<Time>::trace()
  178. << common::TraceElement<Time>(type::get_name(), t,
  179. common::FormalismType::DSDE,
  180. common::FunctionType::POST_EVENT,
  181. common::LevelType::FORMALISM)
  182. << ": AFTER => " << event.to_string();
  183. common::Trace<Time>::trace().flush();
  184. #endif
  185. }
  186. typename Time::type transition(const typename Time::type &t) {
  187. // When x-message(t)
  188. // if (x is empty and t = tn) then
  189. // s = delta_int(s)
  190. // else if (x isn't empty and t = tn)
  191. // s = delta_conf(s,x)
  192. // else if (x isn't empty and t < tn)
  193. // e = t - tl
  194. // s = delta_ext(s,e,x)
  195. // tn = t + ta(s)
  196. // tl = t
  197. // End
  198. #ifdef WITH_TRACE
  199. common::Trace<Time>::trace()
  200. << common::TraceElement<Time>(type::get_name(), t,
  201. common::FormalismType::DSDE,
  202. common::FunctionType::S_MESSAGE,
  203. common::LevelType::FORMALISM)
  204. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  205. << type::_tn;
  206. common::Trace<Time>::trace().flush();
  207. #endif
  208. assert(type::_tl <= t and t <= type::_tn);
  209. if (t == type::_tn) {
  210. if (type::event_number() == 0) {
  211. _dynamics.dint(t);
  212. } else {
  213. _dynamics.dconf(t, t - type::_tl, type::get_bag());
  214. }
  215. } else {
  216. _dynamics.dext(t, t - type::_tl, type::get_bag());
  217. }
  218. type::_tn = t + _dynamics.ta(t);
  219. type::_tl = t;
  220. type::clear_bag();
  221. #ifdef WITH_TRACE
  222. common::Trace<Time>::trace()
  223. << common::TraceElement<Time>(type::get_name(), t,
  224. common::FormalismType::DSDE,
  225. common::FunctionType::S_MESSAGE,
  226. common::LevelType::FORMALISM)
  227. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  228. common::Trace<Time>::trace().flush();
  229. #endif
  230. return type::_tn;
  231. }
  232. private :
  233. Dynamics _dynamics;
  234. };
  235. template<typename Time, typename Dyn, class Parameters = common::NoParameters, class GraphParameters = common::NoParameters>
  236. class ExecutiveDynamics : public common::state::States<Time, Dyn> {
  237. typedef dsde::ExecutiveSimulator<Time, Dyn, Parameters, GraphParameters> Simulator;
  238. public:
  239. ExecutiveDynamics(
  240. const ExecutiveContext<Time, Dyn, Parameters, GraphParameters> &context)
  241. :
  242. _simulator(context.simulator()), _name("executive") {}
  243. virtual ~ExecutiveDynamics() {}
  244. virtual void dconf(typename Time::type /* t */, typename Time::type /* e */,
  245. const common::event::Bag<Time> & /* bag */) {}
  246. virtual void dint(typename Time::type /* t */) {}
  247. virtual void dext(typename Time::type /* t */, typename Time::type /* e */,
  248. const common::event::Bag<Time> & /* bag */) {}
  249. virtual typename Time::type
  250. start(typename Time::type /* time */) { return Time::infinity; }
  251. virtual typename Time::type
  252. ta(typename Time::type /* time */) const { return Time::infinity; }
  253. virtual common::event::Bag<Time>
  254. lambda(typename Time::type /* time */) const { return common::event::Bag<Time>(); }
  255. virtual common::event::Value observe(const typename Time::type & /* t */,
  256. unsigned int /* index */) const { return common::event::Value(); }
  257. const std::string &get_name() const { return _name; }
  258. void restore(const common::context::State<Time> &state) {
  259. common::state::States<Time, Dyn>::restore(static_cast<Dyn *>(this), state);
  260. }
  261. void save(common::context::State<Time> &state) const {
  262. common::state::States<Time, Dyn>::save(static_cast<const Dyn *>(this), state);
  263. }
  264. private:
  265. Simulator *_simulator;
  266. std::string _name;
  267. };
  268. template<typename Time, typename Dyn, typename Parameters = common::NoParameters, typename GraphParameters = common::NoParameters>
  269. class Executive : public dsde::ExecutiveDynamics<Time, Dyn, Parameters, GraphParameters> {
  270. public:
  271. Executive(const ExecutiveContext<Time, Dyn, Parameters, GraphParameters> &context,
  272. dsde::GraphManager<Time, Parameters, GraphParameters> &graph_manager)
  273. :
  274. dsde::ExecutiveDynamics<Time, Dyn, Parameters, GraphParameters>(context),
  275. _graph_manager(graph_manager) {}
  276. ~Executive() override = default;
  277. protected:
  278. dsde::GraphManager<Time, Parameters, GraphParameters> &graph_manager() { return _graph_manager; }
  279. private:
  280. dsde::GraphManager<Time, Parameters, GraphParameters> &_graph_manager;
  281. };
  282. }
  283. #endif