GraphManager.hpp 5.5 KB

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