Executive.hpp 14 KB

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