GraphManager.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file kernel/devs/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 DEVS_GRAPH_MANAGER
  26. #define DEVS_GRAPH_MANAGER
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/GraphManager.hpp>
  29. #include <artis-star/common/Model.hpp>
  30. #include <artis-star/common/Parameters.hpp>
  31. #include <artis-star/common/utils/String.hpp>
  32. #include <sstream>
  33. namespace artis {
  34. namespace devs {
  35. template<class Time,
  36. class Parameters = common::NoParameters,
  37. class GraphParameters = common::NoParameters>
  38. class GraphManager : public common::GraphManager<Time> {
  39. public:
  40. typedef GraphManager<Time, Parameters, GraphParameters> type;
  41. typedef std::pair<common::Model<Time>*, common::Model<Time>*> Link;
  42. typedef std::vector<Link> Links;
  43. GraphManager(common::Coordinator<Time>* coordinator,
  44. const Parameters& /* parameters */,
  45. const GraphParameters& /* graph_parameters */)
  46. :
  47. common::GraphManager<Time>(coordinator) { }
  48. ~GraphManager() override = default;
  49. void dispatch_events(common::Bag<Time> bag, typename Time::type t)
  50. {
  51. for (auto& ymsg : bag) {
  52. auto it = std::find_if(_links.begin(), _links.end(),
  53. [&ymsg](const Link& link) { return link.first == ymsg.get_model(); });
  54. if (it != _links.end()) {
  55. // event on output port of coupled Model
  56. if (it->second == common::GraphManager<Time>::_coordinator) {
  57. dispatch_events_to_parent(ymsg.data(), t);
  58. } else { // event on input port of internal model
  59. it->second->post_event(t, common::ExternalEvent<Time>(ymsg.data()));
  60. }
  61. }
  62. }
  63. }
  64. virtual void
  65. dispatch_events_to_parent(const common::Value& content, typename Time::type t)
  66. {
  67. common::Bag<Time> ymessages;
  68. ymessages.push_back(common::ExternalEvent<Time>(content));
  69. if (common::GraphManager<Time>::_coordinator->get_parent()) {
  70. dynamic_cast < common::Coordinator<Time>* >(common::GraphManager<Time>::_coordinator->get_parent())
  71. ->dispatch_events(ymessages, t);
  72. }
  73. }
  74. bool exist_link(common::Model<Time>* src_model, common::Model<Time>* dst_model) const
  75. {
  76. return _links.find(std::make_pair(src_model, dst_model));
  77. }
  78. void post_event(typename Time::type t, const common::ExternalEvent<Time>& event)
  79. {
  80. auto it = std::find_if(_links.begin(), _links.end(),
  81. [this](const Link& link) {
  82. return link.first == common::GraphManager<Time>::_coordinator;
  83. });
  84. if (it != _links.end()) {
  85. it->second->post_event(t,
  86. common::ExternalEvent<Time>(event.data()));
  87. }
  88. }
  89. virtual std::string to_string(int level) const
  90. {
  91. std::ostringstream ss;
  92. ss << common::String::make_spaces(level * 2) << "Children :" << std::endl;
  93. for (auto& child : common::GraphManager<Time>::_children) {
  94. ss << child->to_string(level + 1);
  95. }
  96. ss << common::String::make_spaces(level * 2) << "Links:" << std::endl;
  97. for (typename Links::const_iterator it = _links.begin(); it != _links.end(); ++it) {
  98. ss << common::String::make_spaces((level + 1) * 2)
  99. << it->first->get_name() << " -> "
  100. << it->second->get_name() << std::endl;
  101. }
  102. return ss.str();
  103. }
  104. void add_link(common::Model<Time>* src_model, common::Model<Time>* dst_model)
  105. {
  106. assert((src_model != common::GraphManager<Time>::_coordinator and
  107. dst_model != common::GraphManager<Time>::_coordinator and
  108. src_model != dst_model) or
  109. (src_model == common::GraphManager<Time>::_coordinator and
  110. dst_model != common::GraphManager<Time>::_coordinator) or
  111. (src_model != common::GraphManager<Time>::_coordinator and
  112. dst_model == common::GraphManager<Time>::_coordinator));
  113. _links.push_back(std::make_pair(src_model, dst_model));
  114. }
  115. private:
  116. Links _links;
  117. };
  118. }
  119. } // namespace artis devs
  120. #endif