Simulator.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @file dtss/Simulator.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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 DTSS_SIMULATOR
  26. #define DTSS_SIMULATOR 1
  27. #include <common/Coordinator.hpp>
  28. #include <common/Parameters.hpp>
  29. #include <common/Simulator.hpp>
  30. #include <common/Trace.hpp>
  31. #include <cassert>
  32. namespace paradevs { namespace dtss {
  33. template < class Time, class Dynamics,
  34. class SchedulerHandle =
  35. paradevs::common::scheduler::NoSchedulerHandle,
  36. class Parameters = common::NoParameters >
  37. class Simulator : public common::Simulator < Time, SchedulerHandle >
  38. {
  39. typedef Simulator < Time, Dynamics, SchedulerHandle, Parameters > type;
  40. public:
  41. Simulator(const std::string& name, typename Time::type time_step,
  42. const Parameters& parameters) :
  43. common::Simulator < Time, SchedulerHandle >(name),
  44. _dynamics(name, parameters),
  45. _time_step(time_step)
  46. { }
  47. ~Simulator()
  48. { }
  49. typename Time::type start(typename Time::type t)
  50. {
  51. #ifdef WITH_TRACE
  52. common::Trace < Time >::trace()
  53. << common::TraceElement < Time >(type::get_name(), t,
  54. common::I_MESSAGE)
  55. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  56. << type::_tn;
  57. common::Trace < Time >::trace().flush();
  58. #endif
  59. _dynamics.start(t);
  60. type::_tl = t;
  61. type::_tn = t;
  62. #ifdef WITH_TRACE
  63. common::Trace < Time >::trace()
  64. << common::TraceElement < Time >(type::get_name(), t,
  65. common::I_MESSAGE)
  66. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  67. << type::_tn;
  68. common::Trace < Time >::trace().flush();
  69. #endif
  70. return type::_tn;
  71. }
  72. void observation(std::ostream &file) const
  73. {
  74. _dynamics.observation(file);
  75. }
  76. void output(typename Time::type t)
  77. {
  78. #ifdef WITH_TRACE
  79. common::Trace < Time >::trace()
  80. << common::TraceElement < Time >(type::get_name(), t,
  81. common::OUTPUT) << ": BEFORE";
  82. common::Trace < Time >::trace().flush();
  83. #endif
  84. if (t == type::_tn) {
  85. common::Bag < Time, SchedulerHandle > bag = _dynamics.lambda(t);
  86. if (not bag.empty()) {
  87. for (auto & event : bag) {
  88. event.set_model(this);
  89. }
  90. dynamic_cast < common::Coordinator < Time, SchedulerHandle >* >(
  91. type::get_parent())->dispatch_events(bag, t);
  92. }
  93. }
  94. #ifdef WITH_TRACE
  95. common::Trace < Time >::trace()
  96. << common::TraceElement < Time >(type::get_name(), t,
  97. common::OUTPUT) << ": AFTER";
  98. common::Trace < Time >::trace().flush();
  99. #endif
  100. }
  101. void post_event(typename Time::type t,
  102. const common::ExternalEvent < Time,
  103. SchedulerHandle >& event)
  104. {
  105. #ifndef WITH_TRACE
  106. (void)t;
  107. #endif
  108. #ifdef WITH_TRACE
  109. common::Trace < Time >::trace()
  110. << common::TraceElement < Time >(type::get_name(), t,
  111. common::POST_EVENT)
  112. << ": BEFORE => " << event.to_string();
  113. common::Trace < Time >::trace().flush();
  114. #endif
  115. type::add_event(event);
  116. #ifdef WITH_TRACE
  117. common::Trace < Time >::trace()
  118. << common::TraceElement < Time >(type::get_name(), t,
  119. common::POST_EVENT)
  120. << ": AFTER => " << event.to_string();
  121. common::Trace < Time >::trace().flush();
  122. #endif
  123. }
  124. typename Time::type transition(typename Time::type t)
  125. {
  126. #ifdef WITH_TRACE
  127. common::Trace < Time >::trace()
  128. << common::TraceElement < Time >(type::get_name(), t,
  129. common::S_MESSAGE)
  130. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  131. << type::_tn;
  132. common::Trace < Time >::trace().flush();
  133. #endif
  134. assert(t == type::_tn);
  135. _dynamics.transition(type::get_bag(), t);
  136. type::_tl = t;
  137. type::_tn = t + _time_step;
  138. type::clear_bag();
  139. #ifdef WITH_TRACE
  140. common::Trace < Time >::trace()
  141. << common::TraceElement < Time >(type::get_name(), t,
  142. common::S_MESSAGE)
  143. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  144. << type::_tn;
  145. common::Trace < Time >::trace().flush();
  146. #endif
  147. return type::_tn;
  148. }
  149. private :
  150. Dynamics _dynamics;
  151. typename Time::type _time_step;
  152. };
  153. } } // namespace paradevs dtss
  154. #endif