GraphManager.hpp 6.2 KB

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