Simulator.hpp 8.2 KB

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