Coordinator.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file Coordinator.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 <dtss/Coordinator.hpp>
  27. #include <dtss/Simulator.hpp>
  28. #include <algorithm>
  29. #include <cassert>
  30. namespace paradevs { namespace dtss {
  31. Coordinator::Coordinator(const std::string& name, common::Time time_step) :
  32. common::Coordinator(name), _time_step(time_step)
  33. { }
  34. Coordinator::~Coordinator()
  35. {
  36. for (unsigned int i = 0; i < _child_list.size(); i++)
  37. { delete _child_list[i]; }
  38. }
  39. common::Time Coordinator::start(common::Time t)
  40. {
  41. assert(_child_list.size() > 0);
  42. for (auto & child : _child_list) {
  43. child->start(_tn);
  44. }
  45. _tn = t;
  46. return _tn;
  47. }
  48. common::Time Coordinator::dispatch_events(common::Bag bag, common::Time t)
  49. {
  50. for (auto & ymsg : bag) {
  51. std::pair < common::Links::iterator ,
  52. common::Links::iterator > result_model =
  53. _link_list.equal_range(common::Node(ymsg.get_port_name(),
  54. ymsg.get_model()));
  55. for (common::Links::iterator it = result_model.first;
  56. it != result_model.second; ++it) {
  57. // event on output port of coupled model
  58. if (it->second.get_model() == this) {
  59. common::Bag ymessages;
  60. ymessages.push_back(
  61. common::ExternalEvent(it->second.get_port_name(),
  62. it->second.get_model(),
  63. ymsg.get_content()));
  64. dynamic_cast < Coordinator* >(get_parent())->dispatch_events(
  65. ymessages, t);
  66. } else { // event on input port of internal model
  67. Model* model = dynamic_cast < Model* >(
  68. it->second.get_model());
  69. common::ExternalEvent message(it->second.get_port_name(),
  70. model, ymsg.get_content());
  71. model->post_message(t, message);
  72. }
  73. }
  74. }
  75. return _tn;
  76. }
  77. void Coordinator::observation(std::ostream& file) const
  78. {
  79. for (unsigned i = 0; i < _child_list.size(); i++) {
  80. _child_list[i]->observation(file);
  81. }
  82. }
  83. void Coordinator::output(common::Time t)
  84. {
  85. assert(t == _tn);
  86. for (auto & model : _child_list) {
  87. model->output(t);
  88. }
  89. }
  90. void Coordinator::post_message(common::Time t,
  91. const common::ExternalEvent& message)
  92. {
  93. std::pair < common::Links::iterator, common::Links::iterator > result =
  94. _link_list.equal_range(common::Node(message.get_port_name(), this));
  95. for (common::Links::iterator it_r = result.first;
  96. it_r != result.second; ++it_r) {
  97. Model* model = dynamic_cast < Model* >((*it_r).second.get_model());
  98. model->post_message(t,
  99. common::ExternalEvent(it_r->second.get_port_name(),
  100. model,
  101. message.get_content()));
  102. }
  103. }
  104. common::Time Coordinator::transition(common::Time t)
  105. {
  106. assert(t >= _tl and t <= _tn);
  107. for (auto & model : _child_list) {
  108. model->transition(t);
  109. }
  110. _tn = t + _time_step;
  111. return _tn;
  112. }
  113. void Coordinator::add_child(Model* child)
  114. {
  115. _child_list.push_back(child);
  116. child->set_parent(this);
  117. }
  118. void Coordinator::add_link(const common::Node& source,
  119. const common::Node& destination)
  120. { _link_list.insert(std::pair < common::Node, common::Node >(source,
  121. destination)); }
  122. } } // namespace paradevs dtss