Simulator.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file kernel/pdevs/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 PDEVS_SIMULATOR
  26. #define PDEVS_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 <artis-star/kernel/pdevs/Context.hpp>
  33. namespace artis {
  34. namespace pdevs {
  35. template<class Time, class Dynamics,
  36. class Parameters = common::NoParameters>
  37. class Simulator : public common::Simulator<Time>
  38. {
  39. typedef Simulator<Time, Dynamics, Parameters> type;
  40. public :
  41. Simulator(const std::string &name, const Parameters &parameters)
  42. :
  43. common::Model<Time>(name),
  44. common::Simulator<Time>(name),
  45. _dynamics(name, Context<Time, Dynamics, Parameters>(parameters, this))
  46. {}
  47. ~Simulator()
  48. {}
  49. const Dynamics &dynamics() const
  50. { return _dynamics; }
  51. virtual void restore(const common::context::State<Time> &state)
  52. {
  53. common::Simulator<Time>::restore(state);
  54. _dynamics.restore(state);
  55. }
  56. virtual void save(common::context::State<Time> &state) const
  57. {
  58. common::Simulator<Time>::save(state);
  59. _dynamics.save(state);
  60. }
  61. virtual std::string to_string(int level) const
  62. {
  63. std::ostringstream ss;
  64. ss << common::String::make_spaces(level * 2) << "p-devs simulator \""
  65. << type::get_name() << "\"" << std::endl;
  66. return ss.str();
  67. }
  68. virtual void finish(const typename Time::type &t)
  69. {
  70. #ifndef WITH_TRACE
  71. (void) t;
  72. #else
  73. common::Trace<Time>::trace()
  74. << common::TraceElement<Time>(type::get_name(), t,
  75. common::FormalismType::PDEVS,
  76. common::FunctionType::FINISH,
  77. common::LevelType::FORMALISM);
  78. common::Trace<Time>::trace().flush();
  79. #endif
  80. }
  81. typename Time::type start(const typename Time::type &t)
  82. {
  83. // When i-message(t)
  84. // tl = t - e
  85. // tn = tl + ta(s)
  86. // End
  87. #ifdef WITH_TRACE
  88. common::Trace<Time>::trace()
  89. << common::TraceElement<Time>(type::get_name(), t,
  90. common::FormalismType::PDEVS,
  91. common::FunctionType::I_MESSAGE,
  92. common::LevelType::FORMALISM)
  93. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  94. << type::_tn;
  95. common::Trace<Time>::trace().flush();
  96. #endif
  97. _dynamics.start(t);
  98. type::_tl = t;
  99. typename Time::type duration = _dynamics.ta(t);
  100. assert(duration >= 0);
  101. type::_tn = type::_tl + duration;
  102. #ifdef WITH_TRACE
  103. common::Trace<Time>::trace()
  104. << common::TraceElement<Time>(type::get_name(), t,
  105. common::FormalismType::PDEVS,
  106. common::FunctionType::I_MESSAGE,
  107. common::LevelType::FORMALISM)
  108. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  109. << type::_tn;
  110. common::Trace<Time>::trace().flush();
  111. #endif
  112. return type::_tn;
  113. }
  114. common::Value observe(const typename Time::type &t, unsigned int index) const
  115. {
  116. return _dynamics.observe(t, index);
  117. }
  118. virtual std::string observable_name(unsigned int observable_index) const
  119. {
  120. return _dynamics.observable_name(observable_index);
  121. }
  122. void output(const typename Time::type &t)
  123. {
  124. // When *-message(t)
  125. // if (t = tn) then
  126. // y = lambda(s)
  127. // send y-message(y,t) to parent
  128. // End
  129. #ifdef WITH_TRACE
  130. common::Trace<Time>::trace()
  131. << common::TraceElement<Time>(type::get_name(), t,
  132. common::FormalismType::PDEVS,
  133. common::FunctionType::OUTPUT,
  134. common::LevelType::FORMALISM)
  135. << ": BEFORE";
  136. common::Trace<Time>::trace().flush();
  137. #endif
  138. if (t == type::_tn) {
  139. common::Bag<Time> bag = _dynamics.lambda(t);
  140. if (not bag.empty()) {
  141. for (auto &event : bag) {
  142. event.set_model(this);
  143. }
  144. dynamic_cast < common::Coordinator<Time> * >(
  145. type::get_parent())->dispatch_events(bag, t);
  146. }
  147. }
  148. #ifdef WITH_TRACE
  149. common::Trace<Time>::trace()
  150. << common::TraceElement<Time>(type::get_name(), t,
  151. common::FormalismType::PDEVS,
  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. {
  160. #ifndef WITH_TRACE
  161. (void) t;
  162. #else
  163. common::Trace<Time>::trace()
  164. << common::TraceElement<Time>(type::get_name(), t,
  165. common::FormalismType::PDEVS,
  166. common::FunctionType::POST_EVENT,
  167. common::LevelType::FORMALISM)
  168. << ": BEFORE => " << event.to_string();
  169. common::Trace<Time>::trace().flush();
  170. #endif
  171. type::add_event(event);
  172. #ifdef WITH_TRACE
  173. common::Trace<Time>::trace()
  174. << common::TraceElement<Time>(type::get_name(), t,
  175. common::FormalismType::PDEVS,
  176. common::FunctionType::POST_EVENT,
  177. common::LevelType::FORMALISM)
  178. << ": AFTER => " << event.to_string();
  179. common::Trace<Time>::trace().flush();
  180. #endif
  181. }
  182. typename Time::type transition(const typename Time::type &t)
  183. {
  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::PDEVS,
  199. common::FunctionType::S_MESSAGE,
  200. common::LevelType::FORMALISM)
  201. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  202. << type::_tn;
  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.dint(t);
  209. } else {
  210. _dynamics.dconf(t, t - type::_tl, type::get_bag());
  211. }
  212. } else {
  213. _dynamics.dext(t, t - type::_tl, type::get_bag());
  214. }
  215. type::_tl = t;
  216. typename Time::type duration = _dynamics.ta(t);
  217. assert(duration >= 0);
  218. type::_tn = type::_tl + duration;
  219. type::clear_bag();
  220. #ifdef WITH_TRACE
  221. common::Trace<Time>::trace()
  222. << common::TraceElement<Time>(type::get_name(), t,
  223. common::FormalismType::PDEVS,
  224. common::FunctionType::S_MESSAGE,
  225. common::LevelType::FORMALISM)
  226. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
  227. common::Trace<Time>::trace().flush();
  228. #endif
  229. return type::_tn;
  230. }
  231. typename Time::type lookahead(const typename Time::type &t) const
  232. {
  233. return _dynamics.lookahead(t);
  234. }
  235. private :
  236. Dynamics _dynamics;
  237. };
  238. }
  239. } // namespace artis pdevs
  240. #endif