Simulator.hpp 8.9 KB

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