Simulator.hpp 8.1 KB

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