Simulator.hpp 8.9 KB

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