Simulator.hpp 8.9 KB

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