Executive.hpp 11 KB

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