Simulator.hpp 8.1 KB

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