GraphManager.hpp 5.7 KB

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