Simulator.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <devs/Coordinator.hpp>
  26. #include <devs/Simulator.hpp>
  27. #include <cassert>
  28. #include <stdexcept>
  29. namespace paradevs { namespace devs {
  30. Simulator::Simulator(Dynamics* dynamics) :
  31. Model(dynamics->get_name()), _dynamics(dynamics)
  32. { }
  33. Simulator::~Simulator()
  34. { delete _dynamics; }
  35. common::Time Simulator::i_message(common::Time t)
  36. {
  37. _tl = t;
  38. _tn = _tl + _dynamics->start();
  39. return _tn;
  40. }
  41. common::Time Simulator::s_message(common::Time t)
  42. {
  43. std::cout << "[" << get_name() << "] at " << t << ": BEFORE - s_message => "
  44. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  45. assert(t == _tn);
  46. common::Messages msgs = _dynamics->lambda();
  47. if (not msgs.empty()) {
  48. for (common::Messages::iterator it = msgs.begin(); it != msgs.end(); ++it) {
  49. it->set_model(this);
  50. }
  51. dynamic_cast < Coordinator* >(get_parent())->y_message(msgs, t);
  52. }
  53. _dynamics->dint(t);
  54. _tl = t;
  55. _tn = t + _dynamics->ta();
  56. std::cout << "[" << get_name() << "] at " << t << ": AFTER - s_message => "
  57. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  58. return _tn;
  59. }
  60. common::Time Simulator::x_message(const common::Message& msg, common::Time t)
  61. {
  62. std::cout << "[" << get_name() << "] at " << t << ": BEFORE - x_message => "
  63. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  64. assert(_tl <= t and t <= _tn);
  65. common::Time e = t - _tl;
  66. _dynamics->dext(e, msg);
  67. _tl = t;
  68. _tn = _tl + _dynamics->ta();
  69. std::cout << "[" << get_name() << "] at " << t << ": AFTER - x_message => "
  70. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  71. return _tn;
  72. }
  73. void Simulator::observation(std::ostream &file) const
  74. { _dynamics->observation(file); }
  75. } } // namespace paradevs devs