Coordinator.cpp 7.1 KB

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