Simulator.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @file kernel/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-2015 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 <paradevs/common/Coordinator.hpp>
  28. #include <paradevs/common/Parameters.hpp>
  29. #include <paradevs/common/Simulator.hpp>
  30. #include <paradevs/common/utils/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::Model < Time, SchedulerHandle >(name),
  44. common::Simulator < Time, SchedulerHandle >(name),
  45. _dynamics(name, parameters),
  46. _time_step(time_step)
  47. { }
  48. ~Simulator()
  49. { }
  50. typename Time::type start(typename Time::type t)
  51. {
  52. #ifdef WITH_TRACE
  53. common::Trace < Time >::trace()
  54. << common::TraceElement < Time >(type::get_name(), t,
  55. common::I_MESSAGE)
  56. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  57. << type::_tn;
  58. common::Trace < Time >::trace().flush();
  59. #endif
  60. _dynamics.start(t);
  61. type::_tl = t;
  62. type::_tn = t;
  63. #ifdef WITH_TRACE
  64. common::Trace < Time >::trace()
  65. << common::TraceElement < Time >(type::get_name(), t,
  66. common::I_MESSAGE)
  67. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  68. << type::_tn;
  69. common::Trace < Time >::trace().flush();
  70. #endif
  71. return type::_tn;
  72. }
  73. void observation(std::ostream &file) const
  74. {
  75. _dynamics.observation(file);
  76. }
  77. void output(typename Time::type t)
  78. {
  79. #ifdef WITH_TRACE
  80. common::Trace < Time >::trace()
  81. << common::TraceElement < Time >(type::get_name(), t,
  82. common::OUTPUT) << ": BEFORE";
  83. common::Trace < Time >::trace().flush();
  84. #endif
  85. if (t == type::_tn) {
  86. common::Bag < Time, SchedulerHandle > bag = _dynamics.lambda(t);
  87. if (not bag.empty()) {
  88. for (auto & event : bag) {
  89. event.set_model(this);
  90. }
  91. dynamic_cast < common::Coordinator < Time, SchedulerHandle >* >(
  92. type::get_parent())->dispatch_events(bag, t);
  93. }
  94. }
  95. #ifdef WITH_TRACE
  96. common::Trace < Time >::trace()
  97. << common::TraceElement < Time >(type::get_name(), t,
  98. common::OUTPUT) << ": AFTER";
  99. common::Trace < Time >::trace().flush();
  100. #endif
  101. }
  102. void post_event(typename Time::type t,
  103. const common::ExternalEvent < Time,
  104. SchedulerHandle >& event)
  105. {
  106. #ifndef WITH_TRACE
  107. (void)t;
  108. #endif
  109. #ifdef WITH_TRACE
  110. common::Trace < Time >::trace()
  111. << common::TraceElement < Time >(type::get_name(), t,
  112. common::POST_EVENT)
  113. << ": BEFORE => " << event.to_string();
  114. common::Trace < Time >::trace().flush();
  115. #endif
  116. type::add_event(event);
  117. #ifdef WITH_TRACE
  118. common::Trace < Time >::trace()
  119. << common::TraceElement < Time >(type::get_name(), t,
  120. common::POST_EVENT)
  121. << ": AFTER => " << event.to_string();
  122. common::Trace < Time >::trace().flush();
  123. #endif
  124. }
  125. typename Time::type transition(typename Time::type t)
  126. {
  127. #ifdef WITH_TRACE
  128. common::Trace < Time >::trace()
  129. << common::TraceElement < Time >(type::get_name(), t,
  130. common::S_MESSAGE)
  131. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  132. << type::_tn;
  133. common::Trace < Time >::trace().flush();
  134. #endif
  135. assert(t == type::_tn);
  136. _dynamics.transition(type::get_bag(), t);
  137. type::_tl = t;
  138. type::_tn = t + _time_step;
  139. type::clear_bag();
  140. #ifdef WITH_TRACE
  141. common::Trace < Time >::trace()
  142. << common::TraceElement < Time >(type::get_name(), t,
  143. common::S_MESSAGE)
  144. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  145. << type::_tn;
  146. common::Trace < Time >::trace().flush();
  147. #endif
  148. return type::_tn;
  149. }
  150. private :
  151. Dynamics _dynamics;
  152. typename Time::type _time_step;
  153. };
  154. } } // namespace paradevs dtss
  155. #endif