Simulator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file Simulator.cpp
  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. #include <common/Trace.hpp>
  26. #include <pdevs/Coordinator.hpp>
  27. #include <pdevs/Simulator.hpp>
  28. #include <cassert>
  29. #include <stdexcept>
  30. namespace paradevs { namespace pdevs {
  31. Simulator::Simulator(Dynamics* dynamics) :
  32. Model(dynamics->get_name()), _dynamics(dynamics)
  33. { }
  34. Simulator::~Simulator()
  35. { delete _dynamics; }
  36. common::Time Simulator::i_message(common::Time t)
  37. {
  38. common::Trace::trace() << common::TraceElement(get_name(), t,
  39. common::I_MESSAGE)
  40. << ": BEFORE => "
  41. << "tl = " << _tl << " ; tn = " << _tn;
  42. common::Trace::trace().flush();
  43. _tl = t;
  44. _tn = _tl + _dynamics->start(t);
  45. common::Trace::trace() << common::TraceElement(get_name(), t,
  46. common::I_MESSAGE)
  47. << ": AFTER => "
  48. << "tl = " << _tl << " ; tn = " << _tn;
  49. common::Trace::trace().flush();
  50. return _tn;
  51. }
  52. /*************************************************
  53. * when *-message(t)
  54. * if (t = tn) then
  55. * y = lambda(s)
  56. * send #-message(y,t) to parent
  57. * if (???) then
  58. * s = delta_int(s)
  59. * else
  60. * s = delta_conf(s,x)
  61. * else
  62. * s = delta_ext(s,t-tl,x)
  63. * tn = t + ta(s)
  64. * tl = t
  65. * empty x
  66. * send done to parent
  67. *************************************************/
  68. common::Time Simulator::s_message(common::Time t)
  69. {
  70. common::Trace::trace() << common::TraceElement(get_name(), t,
  71. common::S_MESSAGE)
  72. << ": BEFORE => "
  73. << "tl = " << _tl << " ; tn = " << _tn;
  74. common::Trace::trace().flush();
  75. if(t == _tn) {
  76. common::Messages msgs = _dynamics->lambda(t);
  77. if (not msgs.empty()) {
  78. for (common::Messages::iterator it = msgs.begin(); it != msgs.end();
  79. ++it) {
  80. it->set_model(this);
  81. }
  82. dynamic_cast < Coordinator* >(get_parent())->y_message(msgs, t);
  83. }
  84. if (message_number() == 0) {
  85. _dynamics->dint(t);
  86. } else {
  87. _dynamics->dconf(t, t - _tl, _x_messages);
  88. }
  89. } else {
  90. _dynamics->dext(t, t - _tl, _x_messages);
  91. }
  92. _tn = t + _dynamics->ta(t);
  93. _tl = t;
  94. clear_messages();
  95. common::Trace::trace() << common::TraceElement(get_name(), t,
  96. common::S_MESSAGE)
  97. << ": AFTER => "
  98. << "tl = " << _tl << " ; tn = " << _tn;
  99. common::Trace::trace().flush();
  100. return _tn;
  101. }
  102. void Simulator::observation(std::ostream &file) const
  103. {
  104. _dynamics->observation(file);
  105. }
  106. void Simulator::clear_messages()
  107. {
  108. _x_messages.clear();
  109. }
  110. void Simulator::post_message(common::Time t, const common::Message& message)
  111. {
  112. common::Trace::trace() << common::TraceElement(get_name(), t,
  113. common::POST_MESSAGE)
  114. << ": BEFORE => " << message.to_string();
  115. common::Trace::trace().flush();
  116. _x_messages.push_back(message);
  117. common::Trace::trace() << common::TraceElement(get_name(), t,
  118. common::POST_MESSAGE)
  119. << ": AFTER => " << message.to_string();
  120. common::Trace::trace().flush();
  121. }
  122. } } // namespace paradevs pdevs