GraphManager.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @file kernel/dtss/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-2019 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 DTSS_GRAPH_MANAGER
  26. #define DTSS_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/Model.hpp>
  31. #include <artis-star/common/Parameters.hpp>
  32. namespace artis {
  33. namespace dtss {
  34. template<class Time,
  35. class Parameters = common::NoParameters,
  36. class GraphParameters = common::NoParameters>
  37. class GraphManager : public common::GraphManager<Time> {
  38. public:
  39. typedef GraphManager<Time, Parameters, GraphParameters> type;
  40. struct ModelPort {
  41. common::Model<Time>* model;
  42. unsigned int port_index;
  43. type* graph_manager;
  44. ModelPort(common::Model<Time>* model, unsigned int port_index)
  45. :model(model),
  46. port_index(port_index),
  47. graph_manager(nullptr) { }
  48. void operator>>(const ModelPort& dst)
  49. {
  50. graph_manager->add_link(model, port_index, dst.model, dst.port_index);
  51. }
  52. };
  53. GraphManager(common::Coordinator<Time>* coordinator, const Parameters& /* parameters */,
  54. const GraphParameters& /* graph_parameters */)
  55. :
  56. common::GraphManager<Time>(coordinator) { }
  57. virtual ~GraphManager() { }
  58. ModelPort in(ModelPort p)
  59. {
  60. p.graph_manager = this;
  61. return p;
  62. }
  63. ModelPort out(ModelPort p)
  64. {
  65. p.graph_manager = this;
  66. return p;
  67. }
  68. void dispatch_events(common::Bag<Time> bag,
  69. typename Time::type t)
  70. {
  71. for (auto& ymsg : bag) {
  72. typename common::Links<Time>::Result result_model =
  73. _link_list.find(ymsg.get_model(),
  74. ymsg.port_index());
  75. for (typename common::Links<Time>::const_iterator it =
  76. result_model.first; it != result_model.second; ++it) {
  77. // event on output port of coupled model
  78. if (it->second.get_model() == common::GraphManager<Time>::_coordinator) {
  79. common::Bag<Time> ymessages;
  80. ymessages.push_back(
  81. common::ExternalEvent<Time>(
  82. it->second, ymsg.data()));
  83. if (common::GraphManager<Time>::_coordinator->get_parent()) {
  84. dynamic_cast < common::Coordinator<Time>* >(
  85. common::GraphManager<Time>::_coordinator->get_parent())
  86. ->dispatch_events(ymessages, t);
  87. }
  88. } else { // event on input port of internal model
  89. it->second.get_model()->post_event(
  90. t, common::ExternalEvent<Time>(
  91. it->second, ymsg.data()));
  92. }
  93. }
  94. }
  95. }
  96. void post_event(typename Time::type t, const common::ExternalEvent<Time>& event)
  97. {
  98. typename common::Links<Time>::Result result =
  99. _link_list.find(common::GraphManager<Time>::_coordinator,
  100. event.port_index());
  101. for (typename common::Links<Time>::const_iterator it_r = result.first;
  102. it_r != result.second;
  103. ++it_r) {
  104. it_r->second.get_model()->post_event(t,
  105. common::ExternalEvent<Time>(it_r->second, event.data()));
  106. }
  107. }
  108. private:
  109. void add_link(common::Model<Time>* src_model, unsigned int src_port_index,
  110. common::Model<Time>* dst_model, unsigned int dst_port_index)
  111. {
  112. assert((src_model != common::GraphManager<Time>::_coordinator and
  113. dst_model != common::GraphManager<Time>::_coordinator and
  114. src_model->exist_out_port(src_port_index) and
  115. dst_model->exist_in_port(dst_port_index)) or
  116. (src_model == common::GraphManager<Time>::_coordinator and
  117. dst_model != common::GraphManager<Time>::_coordinator and
  118. src_model->exist_in_port(src_port_index) and
  119. dst_model->exist_in_port(dst_port_index)) or
  120. (src_model != common::GraphManager<Time>::_coordinator and
  121. dst_model == common::GraphManager<Time>::_coordinator and
  122. src_model->exist_out_port(src_port_index) and
  123. dst_model->exist_out_port(dst_port_index)));
  124. _link_list.add(src_model, src_port_index, dst_model, dst_port_index);
  125. }
  126. common::Links<Time> _link_list;
  127. };
  128. }
  129. } // namespace artis dtss
  130. #endif