Simulator.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * @file kernel/fddevs/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 FDDEVS_SIMULATOR
  26. #define FDDEVS_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 fddevs {
  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 fddevs::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. #endif
  86. #ifdef WITH_TRACE
  87. common::Trace<Time>::trace()
  88. << common::TraceElement<Time>(type::get_name(), t,
  89. common::FormalismType::FDDEVS,
  90. common::FunctionType::FINISH,
  91. common::LevelType::FORMALISM);
  92. common::Trace<Time>::trace().flush();
  93. #endif
  94. }
  95. typename Time::type start(const typename Time::type& t)
  96. {
  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::FDDEVS,
  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. _dynamics.initial_state(t);
  112. type::_tl = t;
  113. typename Time::type duration = _dynamics.tau(t);
  114. assert(ta >= 0);
  115. type::_tn = type::_tl + duration;
  116. #ifdef WITH_TRACE
  117. common::Trace<Time>::trace()
  118. << common::TraceElement<Time>(type::get_name(), t,
  119. common::FormalismType::FDDEVS,
  120. common::FunctionType::I_MESSAGE,
  121. common::LevelType::FORMALISM)
  122. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  123. << type::_tn;
  124. common::Trace<Time>::trace().flush();
  125. #endif
  126. return type::_tn;
  127. }
  128. common::Value observe(const typename Time::type& t,
  129. unsigned int index) const { return _dynamics.observe(t, index); }
  130. void output(const typename Time::type& t)
  131. {
  132. // When *-message(t)
  133. // if (t = tn) then
  134. // y = lambda(s)
  135. // send y-message(y,t) to parent
  136. // End
  137. #ifdef WITH_TRACE
  138. common::Trace<Time>::trace()
  139. << common::TraceElement<Time>(type::get_name(), t,
  140. common::FormalismType::FDDEVS,
  141. common::FunctionType::OUTPUT,
  142. common::LevelType::FORMALISM)
  143. << ": BEFORE";
  144. common::Trace<Time>::trace().flush();
  145. #endif
  146. if (t == type::_tn) {
  147. common::Bag<Time> bag = _dynamics.lambda(t);
  148. if (not bag.empty()) {
  149. for (auto& event : bag) {
  150. event.set_model(this);
  151. }
  152. #ifdef WITH_TRACE
  153. common::Trace<Time>::trace()
  154. << common::TraceElement<Time>(type::get_name(), t,
  155. common::FormalismType::FDDEVS,
  156. common::FunctionType::OUTPUT,
  157. common::LevelType::FORMALISM)
  158. << ": AFTER" << " => "
  159. << bag.to_string();
  160. common::Trace<Time>::trace().flush();
  161. #endif
  162. dynamic_cast < common::Coordinator<Time>* >(
  163. type::get_parent())->dispatch_events(bag, t);
  164. }
  165. }
  166. }
  167. void post_event(const typename Time::type& t,
  168. const common::ExternalEvent<Time>& event)
  169. {
  170. #ifndef WITH_TRACE
  171. (void) t;
  172. #endif
  173. #ifdef WITH_TRACE
  174. common::Trace<Time>::trace()
  175. << common::TraceElement<Time>(type::get_name(), t,
  176. common::FormalismType::FDDEVS,
  177. common::FunctionType::POST_EVENT,
  178. common::LevelType::FORMALISM)
  179. << ": BEFORE => " << event.to_string();
  180. common::Trace<Time>::trace().flush();
  181. #endif
  182. type::add_event(event);
  183. #ifdef WITH_TRACE
  184. common::Trace<Time>::trace()
  185. << common::TraceElement<Time>(type::get_name(), t,
  186. common::FormalismType::FDDEVS,
  187. common::FunctionType::POST_EVENT,
  188. common::LevelType::FORMALISM)
  189. << ": AFTER => " << event.to_string();
  190. common::Trace<Time>::trace().flush();
  191. #endif
  192. }
  193. typename Time::type transition(const typename Time::type& t)
  194. {
  195. // When x-message(t)
  196. // if (x is empty and t = tn) then
  197. // s = delta_int(s)
  198. // else if (x isn't empty and t = tn)
  199. // s = delta_conf(s,x)
  200. // else if (x isn't empty and t < tn)
  201. // e = t - tl
  202. // s = delta_ext(s,e,x)
  203. // tn = t + ta(s)
  204. // tl = t
  205. // End
  206. #ifdef WITH_TRACE
  207. common::Trace<Time>::trace()
  208. << common::TraceElement<Time>(type::get_name(), t,
  209. common::FormalismType::FDDEVS,
  210. common::FunctionType::S_MESSAGE,
  211. common::LevelType::FORMALISM)
  212. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  213. << " ; S = " << _dynamics.state();
  214. common::Trace<Time>::trace().flush();
  215. #endif
  216. assert(type::_tl <= t and t <= type::_tn);
  217. if (t == type::_tn) {
  218. if (type::event_number() == 0) {
  219. _dynamics.delta_tau(t);
  220. typename Time::type duration = _dynamics.tau(t);
  221. assert(ta >= 0);
  222. type::_tl = t;
  223. type::_tn = type::_tl + duration;
  224. }
  225. } else {
  226. _dynamics.delta_x(t, t - type::_tl, type::get_bag());
  227. if (not _dynamics.rho(t, type::get_bag())) {
  228. typename Time::type duration = _dynamics.tau(t);
  229. assert(ta >= 0);
  230. type::_tl = t;
  231. type::_tn = type::_tl + duration;
  232. }
  233. }
  234. type::clear_bag();
  235. #ifdef WITH_TRACE
  236. common::Trace<Time>::trace()
  237. << common::TraceElement<Time>(type::get_name(), t,
  238. common::FormalismType::FDDEVS,
  239. common::FunctionType::S_MESSAGE,
  240. common::LevelType::FORMALISM)
  241. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  242. << " ; S = " << _dynamics.state();
  243. common::Trace<Time>::trace().flush();
  244. #endif
  245. return type::_tn;
  246. }
  247. private :
  248. Dynamics _dynamics;
  249. };
  250. }
  251. } // namespace artis fddevs
  252. #endif