GraphManager.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file dtss/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 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 DTSS_GRAPH_MANANGER
  26. #define DTSS_GRAPH_MANANGER 1
  27. #include <common/Coordinator.hpp>
  28. #include <common/Links.hpp>
  29. #include <common/Model.hpp>
  30. #include <common/Parameters.hpp>
  31. namespace paradevs { namespace dtss {
  32. template < class Time,
  33. class SchedulerHandle =
  34. paradevs::common::scheduler::NoSchedulerHandle,
  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,
  80. SchedulerHandle >::const_iterator it =
  81. result_model.first; 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 < Time,
  87. SchedulerHandle >(
  88. it->second,
  89. ymsg.get_content()));
  90. dynamic_cast < common::Coordinator <
  91. Time, SchedulerHandle >* >(_coordinator->get_parent())
  92. ->dispatch_events(ymessages, t);
  93. } else { // event on input port of internal model
  94. it->second.get_model()->post_event(
  95. t, common::ExternalEvent < Time,
  96. SchedulerHandle >(
  97. it->second,
  98. ymsg.get_content()));
  99. }
  100. }
  101. }
  102. }
  103. void post_event(typename Time::type t,
  104. const common::ExternalEvent < Time,
  105. SchedulerHandle >& event)
  106. {
  107. typename common::Links < Time, SchedulerHandle >::Result result =
  108. _link_list.find(_coordinator, event.get_port_name());
  109. for (typename common::Links < Time,
  110. SchedulerHandle >::const_iterator it_r =
  111. result.first; it_r != result.second; ++it_r) {
  112. it_r->second.get_model()->post_event(
  113. t, common::ExternalEvent < Time, SchedulerHandle >(it_r->second,
  114. event.get_content()));
  115. }
  116. }
  117. private:
  118. common::Links < Time, SchedulerHandle > _link_list;
  119. common::Models < Time, SchedulerHandle > _child_list;
  120. common::Coordinator < Time, SchedulerHandle >* _coordinator;
  121. };
  122. } } // namespace paradevs dtss
  123. #endif