Executive.hpp 11 KB

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