GraphManager.hpp 6.6 KB

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