GraphManager.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @file kernel/pdevs/GraphManager.hpp
  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-2015 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. #ifndef PDEVS_GRAPH_MANANGER
  26. #define PDEVS_GRAPH_MANANGER 1
  27. #include <paradevs/common/Coordinator.hpp>
  28. #include <paradevs/common/Links.hpp>
  29. #include <paradevs/common/Model.hpp>
  30. #include <paradevs/common/Parameters.hpp>
  31. #include <paradevs/common/utils/String.hpp>
  32. #include <sstream>
  33. namespace paradevs { namespace pdevs {
  34. template < class Time, class SchedulerHandle,
  35. class GraphParameters = common::NoParameters >
  36. class GraphManager
  37. {
  38. public:
  39. GraphManager(common::Coordinator < Time, SchedulerHandle >* coordinator,
  40. const GraphParameters& /* parameters */) :
  41. _coordinator(coordinator)
  42. { }
  43. virtual ~GraphManager()
  44. { }
  45. void add_child(common::Model < Time, SchedulerHandle >* child)
  46. {
  47. _child_list.push_back(child);
  48. child->set_parent(_coordinator);
  49. }
  50. void add_link(common::Model < Time, SchedulerHandle >* src_model,
  51. const std::string& src_port_name,
  52. common::Model < Time, SchedulerHandle >* dst_model,
  53. const std::string& dst_port_name)
  54. {
  55. assert((src_model != _coordinator and
  56. dst_model != _coordinator and
  57. src_model->exist_out_port(src_port_name) and
  58. dst_model->exist_in_port(dst_port_name)) or
  59. (src_model == _coordinator and
  60. dst_model != _coordinator and
  61. src_model->exist_in_port(src_port_name) and
  62. dst_model->exist_in_port(dst_port_name)) or
  63. (src_model != _coordinator and
  64. dst_model == _coordinator and
  65. src_model->exist_out_port(src_port_name) and
  66. dst_model->exist_out_port(dst_port_name)));
  67. _link_list.add(src_model, src_port_name, dst_model, dst_port_name);
  68. }
  69. const common::Models < Time, SchedulerHandle >& children() const
  70. { return _child_list; }
  71. void dispatch_events(common::Bag < Time, SchedulerHandle > bag,
  72. typename Time::type t)
  73. {
  74. for (auto & ymsg : bag) {
  75. typename common::Links < Time,
  76. SchedulerHandle >::Result result_model =
  77. _link_list.find(ymsg.get_model(),
  78. ymsg.get_port_name());
  79. for (typename common::Links < Time, SchedulerHandle >::
  80. const_iterator it = result_model.first;
  81. it != result_model.second; ++it) {
  82. // event on output port of coupled Model
  83. if (it->second.get_model() == _coordinator) {
  84. common::Bag < Time, SchedulerHandle > ymessages;
  85. ymessages.push_back(
  86. common::ExternalEvent <
  87. Time, SchedulerHandle >(it->second,
  88. ymsg.get_content()));
  89. dynamic_cast < common::Coordinator <
  90. Time, SchedulerHandle >* >(
  91. _coordinator->get_parent())->dispatch_events(
  92. ymessages, t);
  93. } else { // event on input port of internal model
  94. it->second.get_model()->post_event(
  95. t, common::ExternalEvent <
  96. Time, SchedulerHandle >(
  97. it->second, ymsg.get_content()));
  98. }
  99. }
  100. }
  101. }
  102. bool exist_link(common::Model < Time, SchedulerHandle >* src_model,
  103. const std::string& src_port_name,
  104. common::Model < Time, SchedulerHandle >* dst_model,
  105. const std::string& dst_port_name) const
  106. {
  107. return _link_list.exist(src_model, src_port_name, dst_model,
  108. dst_port_name);
  109. }
  110. common::Coordinator < Time, SchedulerHandle >* get_coordinator() const
  111. { return _coordinator; }
  112. void post_event(typename Time::type t,
  113. const common::ExternalEvent < Time,
  114. SchedulerHandle >& event)
  115. {
  116. typename common::Links < Time, SchedulerHandle >::Result result =
  117. _link_list.find(_coordinator, event.get_port_name());
  118. for (typename common::Links < Time,
  119. SchedulerHandle >::const_iterator it_r =
  120. result.first; it_r != result.second; ++it_r) {
  121. it_r->second.get_model()->post_event(
  122. t, common::ExternalEvent <
  123. Time, SchedulerHandle >(it_r->second,
  124. event.get_content()));
  125. }
  126. }
  127. virtual std::string to_string(int level) const
  128. {
  129. std::ostringstream ss;
  130. ss << common::spaces(level * 2) << "Childs :" << std::endl;
  131. for (auto & child : _child_list) {
  132. ss << child->to_string(level + 1);
  133. }
  134. ss << _link_list.to_string(level);
  135. return ss.str();
  136. }
  137. private:
  138. common::Links < Time, SchedulerHandle > _link_list;
  139. common::Models < Time, SchedulerHandle > _child_list;
  140. common::Coordinator < Time, SchedulerHandle >* _coordinator;
  141. };
  142. } } // namespace paradevs pdevs
  143. #endif