GraphManager.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, class GraphParameters = common::NoParameters >
  33. class GraphManager
  34. {
  35. public:
  36. GraphManager(common::Coordinator < Time >* coordinator,
  37. const GraphParameters& /* parameters */) :
  38. _coordinator(coordinator)
  39. { }
  40. virtual ~GraphManager()
  41. { }
  42. void add_child(common::Model < Time >* child)
  43. {
  44. _child_list.push_back(child);
  45. child->set_parent(_coordinator);
  46. }
  47. void add_link(common::Model < Time >* out_model,
  48. const std::string& out_port_name,
  49. common::Model < Time >* in_model,
  50. const std::string& in_port_name)
  51. {
  52. _link_list.add(out_model, out_port_name, in_model, in_port_name);
  53. }
  54. const common::Models < Time >& children() const
  55. { return _child_list; }
  56. void dispatch_events(common::Bag < Time > bag, typename Time::type t)
  57. {
  58. for (auto & ymsg : bag) {
  59. typename common::Links < Time >::Result result_model =
  60. _link_list.find(ymsg.get_model(),
  61. ymsg.get_port_name());
  62. for (typename common::Links < Time >::const_iterator it =
  63. result_model.first; it != result_model.second; ++it) {
  64. // event on output port of coupled model
  65. if (it->second.get_model() == _coordinator) {
  66. common::Bag < Time > ymessages;
  67. ymessages.push_back(
  68. common::ExternalEvent < Time >(it->second,
  69. ymsg.get_content()));
  70. dynamic_cast < common::Coordinator < Time >* >(
  71. _coordinator->get_parent())->dispatch_events(ymessages,
  72. t);
  73. } else { // event on input port of internal model
  74. it->second.get_model()->post_event(
  75. t, common::ExternalEvent < Time >(it->second,
  76. ymsg.get_content()));
  77. }
  78. }
  79. }
  80. }
  81. void post_event(typename Time::type t,
  82. const common::ExternalEvent < Time >& event)
  83. {
  84. typename common::Links < Time >::Result result =
  85. _link_list.find(_coordinator, event.get_port_name());
  86. for (typename common::Links < Time >::const_iterator it_r =
  87. result.first; it_r != result.second; ++it_r) {
  88. it_r->second.get_model()->post_event(
  89. t, common::ExternalEvent < Time >(it_r->second,
  90. event.get_content()));
  91. }
  92. }
  93. private:
  94. common::Links < Time > _link_list;
  95. common::Models < Time > _child_list;
  96. common::Coordinator < Time >* _coordinator;
  97. };
  98. } } // namespace paradevs dtss
  99. #endif