GraphManager.hpp 5.5 KB

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