Simulator.hpp 8.3 KB

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