Simulator.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-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 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::devs {
  33. template<typename Time, typename Dynamics, typename Parameters>
  34. class Simulator;
  35. template<typename Time, typename Dynamics,
  36. class Parameters = common::NoParameters>
  37. class Context {
  38. typedef devs::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,
  51. typename Parameters = common::NoParameters>
  52. class Simulator : public common::Simulator<Time> {
  53. typedef Simulator<Time, Dynamics, Parameters> type;
  54. public :
  55. Simulator(const std::string &name, const Parameters &parameters)
  56. :
  57. common::Model<Time>(name),
  58. common::Simulator<Time>(name),
  59. _dynamics(name, Context<Time, Dynamics, Parameters>(parameters, this)) {}
  60. ~Simulator() override {}
  61. const Dynamics &dynamics() const { return _dynamics; }
  62. virtual void restore(const common::context::State<Time> &state) {
  63. common::Simulator<Time>::restore(state);
  64. _dynamics.restore(state);
  65. }
  66. virtual void save(common::context::State<Time> &state) const {
  67. common::Simulator<Time>::save(state);
  68. _dynamics.save(state);
  69. }
  70. virtual std::string to_string(int level) const {
  71. std::ostringstream ss;
  72. ss << common::String::make_spaces(level * 2) << "p-devs simulator \""
  73. << type::get_name() << "\"" << std::endl;
  74. return ss.str();
  75. }
  76. virtual void finish(const typename Time::type &t) {
  77. #ifndef WITH_TRACE
  78. (void) t;
  79. #else
  80. common::Trace<Time>::trace()
  81. << common::TraceElement<Time>(type::get_name(), t,
  82. common::FormalismType::DEVS,
  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::DEVS,
  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.start(t);
  104. type::_tl = t;
  105. typename Time::type duration = _dynamics.ta(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::DEVS,
  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. virtual std::string observable_name(unsigned int observable_index) const {
  123. return _dynamics.observable_name(observable_index);
  124. }
  125. void output(const typename Time::type &t) {
  126. // When *-message(t)
  127. // if (t != tn) then Error
  128. // y = lambda(s)
  129. // send y-message(y,t) to parent coordinator
  130. // End
  131. #ifdef WITH_TRACE
  132. common::Trace<Time>::trace()
  133. << common::TraceElement<Time>(type::get_name(), t,
  134. common::FormalismType::DEVS,
  135. common::FunctionType::OUTPUT,
  136. common::LevelType::FORMALISM)
  137. << ": BEFORE";
  138. common::Trace<Time>::trace().flush();
  139. #endif
  140. assert(t == type::_tn);
  141. common::event::ExternalEvent<Time> event(_dynamics.lambda(t));
  142. if (not event.is_void()) {
  143. event.set_model(this);
  144. dynamic_cast < common::Coordinator<Time> * >(
  145. type::get_parent())->dispatch_events(common::event::Bag<Time>(event), t);
  146. }
  147. #ifdef WITH_TRACE
  148. common::Trace<Time>::trace()
  149. << common::TraceElement<Time>(type::get_name(), t,
  150. common::FormalismType::DEVS,
  151. common::FunctionType::OUTPUT,
  152. common::LevelType::FORMALISM)
  153. << ": AFTER";
  154. common::Trace<Time>::trace().flush();
  155. #endif
  156. }
  157. void post_event(const typename Time::type &t, const common::event::ExternalEvent<Time> &event) {
  158. #ifndef WITH_TRACE
  159. (void) t;
  160. #else
  161. common::Trace<Time>::trace()
  162. << common::TraceElement<Time>(type::get_name(), t,
  163. common::FormalismType::DEVS,
  164. common::FunctionType::POST_EVENT,
  165. common::LevelType::FORMALISM)
  166. << ": event = " << event.to_string();
  167. common::Trace<Time>::trace().flush();
  168. #endif
  169. type::add_event(event);
  170. }
  171. typename Time::type transition(const typename Time::type &t) {
  172. // When x-message(t)
  173. // if not (tl <= t <= tn) then Error
  174. // if (x is empty and t = tn) then
  175. // s = delta_int(s)
  176. // else if (x isn't empty and t < tn)
  177. // e = t - tl
  178. // s = delta_ext(s,e,x)
  179. // tl = t
  180. // tn = tl + ta(s)
  181. // End
  182. #ifdef WITH_TRACE
  183. common::Trace<Time>::trace()
  184. << common::TraceElement<Time>(type::get_name(), t,
  185. common::FormalismType::DEVS,
  186. common::FunctionType::S_MESSAGE,
  187. common::LevelType::FORMALISM)
  188. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  189. << type::_tn;
  190. common::Trace<Time>::trace().flush();
  191. #endif
  192. assert(type::_tl <= t and t <= type::_tn);
  193. if (t == type::_tn) {
  194. if (type::event_number() == 0) {
  195. _dynamics.dint(t);
  196. } else {
  197. assert(false);
  198. }
  199. } else {
  200. assert(type::get_bag().size() == 1);
  201. _dynamics.dext(t, t - type::_tl, type::get_bag().at(0));
  202. }
  203. type::_tl = t;
  204. typename Time::type duration = _dynamics.ta(t);
  205. assert(duration >= 0);
  206. type::_tn = type::_tl + duration;
  207. type::clear_bag();
  208. #ifdef WITH_TRACE
  209. common::Trace<Time>::trace()
  210. << common::TraceElement<Time>(type::get_name(), t,
  211. common::FormalismType::DEVS,
  212. common::FunctionType::S_MESSAGE,
  213. common::LevelType::FORMALISM)
  214. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  215. common::Trace<Time>::trace().flush();
  216. #endif
  217. return type::_tn;
  218. }
  219. typename Time::type lookahead(const typename Time::type &t) const {
  220. return _dynamics.lookahead(t);
  221. }
  222. private :
  223. Dynamics _dynamics;
  224. };
  225. } // namespace artis devs
  226. #endif