Simulator.hpp 5.3 KB

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