Simulator.hpp 8.6 KB

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