Simulator.hpp 7.5 KB

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