Simulator.hpp 6.6 KB

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