Coordinator.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <devs/Coordinator.hpp>
  26. #include <devs/Simulator.hpp>
  27. #include <cassert>
  28. namespace paradevs { namespace devs {
  29. Coordinator::Coordinator(const std::string& name) : Model(name)
  30. { }
  31. Coordinator::~Coordinator()
  32. {
  33. for (unsigned int i = 0; i < _child_list.size(); i++)
  34. { delete _child_list[i]; }
  35. }
  36. common::Time Coordinator::i_message(common::Time t)
  37. {
  38. std::cout << "[" << get_name() << "] at " << t << ": BEFORE - i_message => "
  39. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  40. assert(_child_list.size() > 0);
  41. for (unsigned int i = 0; i < _child_list.size(); i++) {
  42. Model* model = _child_list[i];
  43. _event_table.init(model->i_message(_tn), model);
  44. }
  45. _tl = t;
  46. _tn = _event_table.get_current_time();
  47. std::cout << "[" << get_name() << "] at " << t << ": AFTER - i_message => "
  48. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  49. return _tn;
  50. }
  51. common::Time Coordinator::s_message(common::Time t)
  52. {
  53. std::cout << "[" << get_name() << "] at " << t << ": BEFORE - s_message => "
  54. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  55. std::cout << "[" << get_name() << "]: " << _event_table.to_string()
  56. << std::endl;
  57. assert(t == _tn);
  58. Model* current = dynamic_cast < devs::Model* >(
  59. _event_table.get_current_model());
  60. common::Time tn = current->s_message(_tn);
  61. _event_table.put(tn, current);
  62. _tl = t;
  63. _tn = _event_table.get_current_time();
  64. std::cout << "[" << get_name() << "] at " << t << ": AFTER - s_message => "
  65. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  66. std::cout << "[" << get_name() << "]: " << _event_table.to_string()
  67. << std::endl;
  68. std::cout << "**************" << std::endl;
  69. return _tn;
  70. }
  71. common::Time Coordinator::x_message(const common::Message& xmsg, common::Time t)
  72. {
  73. std::cout << "[" << get_name() << "] at " << t << ": BEFORE - x_message on "
  74. << xmsg.get_port_name() << " => "
  75. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  76. std::cout << "[" << get_name() << "] " << _link_list.to_string()
  77. << std::endl;
  78. assert(_tl <= t and t <= _tn);
  79. std::pair < common::Links::iterator, common::Links::iterator > result =
  80. _link_list.equal_range(common::Node(xmsg.get_port_name(), this));
  81. for (common::Links::iterator it_r = result.first; it_r != result.second;
  82. ++it_r) {
  83. _tn = dynamic_cast < devs::Simulator* >(
  84. (*it_r).second.get_model())->x_message(
  85. common::Message(it_r->second.get_port_name(),
  86. it_r->second.get_model(),
  87. xmsg.get_content()), t);
  88. _event_table.put(
  89. _tn,
  90. dynamic_cast < devs::Simulator* >(it_r->second.get_model()));
  91. }
  92. _tl = t;
  93. _tn = _event_table.get_current_time();
  94. std::cout << "[" << get_name() << "] at " << t << ": AFTER - x_message => "
  95. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  96. return _tn;
  97. }
  98. common::Time Coordinator::y_message(common::Messages messages, common::Time t)
  99. {
  100. std::cout << "[" << get_name() << "] at " << t << ": BEFORE - y_message => "
  101. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  102. std::cout << "[" << get_name() << "] at " << t << ": "
  103. << messages.to_string() << std::endl;
  104. if (not messages.empty()) {
  105. bool internal = false;
  106. while (not messages.empty()) {
  107. common::Message ymsg = messages.back();
  108. messages.pop_back();
  109. std::pair < common::Links::iterator ,
  110. common::Links::iterator > result_model =
  111. _link_list.equal_range(common::Node(ymsg.get_port_name(),
  112. ymsg.get_model()));
  113. for (common::Links::iterator it = result_model.first;
  114. it != result_model.second; ++it) {
  115. // event on output port of coupled model
  116. if (it->second.get_model() == this) {
  117. common::Messages ymessages;
  118. ymessages.push_back(
  119. common::Message(it->second.get_port_name(),
  120. it->second.get_model(),
  121. ymsg.get_content()));
  122. dynamic_cast < devs::Coordinator* >(
  123. get_parent())->y_message(ymessages, t);
  124. } else { // event on input port of internal model
  125. common::Message message(
  126. it->second.get_port_name(),
  127. it->second.get_model(),
  128. ymsg.get_content());
  129. common::Time tn = dynamic_cast < devs::Model* >(
  130. it->second.get_model())->x_message(message, t);
  131. _event_table.put(
  132. tn,
  133. dynamic_cast < devs::Model* >(it->second.get_model()));
  134. internal = true;
  135. }
  136. }
  137. }
  138. if (internal) {
  139. _tl = t;
  140. _tn = _event_table.get_current_time();
  141. }
  142. }
  143. std::cout << "[" << get_name() << "] at " << t << ": AFTER - y_message => "
  144. << "tl = " << _tl << " ; tn = " << _tn << std::endl;
  145. return _tn;
  146. }
  147. void Coordinator::observation(std::ostream& file) const
  148. {
  149. for (unsigned i = 0; i < _child_list.size(); i++) {
  150. _child_list[i]->observation(file);
  151. }
  152. }
  153. void Coordinator::add_child(Model* child)
  154. {
  155. _child_list.push_back(child);
  156. child->set_parent(this);
  157. }
  158. void Coordinator::add_link(const common::Node& source,
  159. const common::Node& destination)
  160. {
  161. _link_list.insert(std::pair < common::Node, common::Node >(source,
  162. destination));
  163. }
  164. } } // namespace paradevs devs