GraphManager.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file kernel/sss/GraphManager.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2022 ULCO http://www.univ-littoral.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_MANAGER
  26. #define SSS_GRAPH_MANAGER
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/GraphManager.hpp>
  29. #include <artis-star/common/Links.hpp>
  30. #include <artis-star/common/Parameters.hpp>
  31. #include <artis-star/kernel/sss/Model.hpp>
  32. namespace artis::sss {
  33. template<typename Time, typename GraphParameters = common::NoParameters>
  34. class GraphManager : public common::GraphManager<Time> {
  35. public:
  36. GraphManager(common::Coordinator<Time> *coordinator,
  37. const GraphParameters & /* parameters */)
  38. :
  39. common::GraphManager<Time>(coordinator) {}
  40. virtual ~GraphManager() {}
  41. void add_link(common::Model<Time> *src_model,
  42. const std::string &src_port_name,
  43. common::Model<Time> *dst_model,
  44. const std::string &dst_port_name) {
  45. assert((src_model != common::GraphManager<Time>::_coordinator and
  46. dst_model != common::GraphManager<Time>::_coordinator and
  47. src_model->exist_out_port(src_port_name) and
  48. dst_model->exist_in_port(dst_port_name)) or
  49. (src_model == common::GraphManager<Time>::_coordinator and
  50. dst_model != common::GraphManager<Time>::_coordinator and
  51. src_model->exist_in_port(src_port_name) and
  52. dst_model->exist_in_port(dst_port_name)) or
  53. (src_model != common::GraphManager<Time>::_coordinator and
  54. dst_model == common::GraphManager<Time>::_coordinator and
  55. src_model->exist_out_port(src_port_name) and
  56. dst_model->exist_out_port(dst_port_name)));
  57. _link_list.add(src_model, src_port_name, dst_model, dst_port_name);
  58. }
  59. void dispatch_events(common::event::Bag<Time> bag,
  60. typename Time::type t) {
  61. for (auto &ymsg: bag) {
  62. typename common::Links<Time>::Result result_model =
  63. _link_list.find(ymsg.get_model(),
  64. ymsg.get_port_name());
  65. for (typename common::Links<Time>::const_iterator it =
  66. result_model.first; it != result_model.second; ++it) {
  67. // event on output port of coupled model
  68. if (it->second.get_model() == common::GraphManager<Time>::_coordinator) {
  69. common::event::Bag<Time> ymessages;
  70. ymessages.push_back(
  71. common::event::ExternalEvent<Time>(it->second, ymsg.data()));
  72. if (common::GraphManager<Time>::_coordinator->get_parent()) {
  73. dynamic_cast < common::Coordinator<
  74. Time> * >(common::GraphManager<Time>::_coordinator->get_parent())
  75. ->dispatch_events(ymessages, t);
  76. }
  77. } else { // event on input port of internal model
  78. it->second.get_model()->post_event(
  79. t, common::event::ExternalEvent<Time>(
  80. it->second,
  81. ymsg.data()));
  82. }
  83. }
  84. }
  85. }
  86. void post_event(typename Time::type t,
  87. const common::event::ExternalEvent<Time> &event) {
  88. typename common::Links<Time>::Result result =
  89. _link_list.find(common::GraphManager<Time>::_coordinator, event.get_port_name());
  90. for (typename common::Links<Time>::const_iterator it_r =
  91. result.first; it_r != result.second; ++it_r) {
  92. it_r->second.get_model()->post_event(
  93. t, common::event::ExternalEvent<Time>(it_r->second,
  94. event.data()));
  95. }
  96. }
  97. private:
  98. common::Links<Time> _link_list;
  99. };
  100. } // namespace artis sss
  101. #endif