Coordinator.cpp 8.4 KB

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