Simulator.hpp 11 KB

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