GraphManager.hpp 5.3 KB

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