GraphManager.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. for (auto &ymsg : bag) {
  51. auto it = std::find_if(_links.begin(), _links.end(),
  52. [&ymsg](const Link &link) { return link.first == ymsg.get_model(); });
  53. if (it != _links.end()) {
  54. // event on output port of coupled Model
  55. if (it->second == common::GraphManager<Time>::_coordinator) {
  56. dispatch_events_to_parent(ymsg.data(), t);
  57. } else { // event on input port of internal model
  58. it->second->post_event(t, common::ExternalEvent<Time>(ymsg.data()));
  59. }
  60. }
  61. }
  62. }
  63. virtual void
  64. dispatch_events_to_parent(const common::Value &content, typename Time::type t) {
  65. common::Bag<Time> ymessages;
  66. ymessages.push_back(common::ExternalEvent<Time>(content));
  67. if (common::GraphManager<Time>::_coordinator->get_parent()) {
  68. dynamic_cast < common::Coordinator<Time> * >(common::GraphManager<Time>::_coordinator
  69. ->get_parent())
  70. ->dispatch_events(ymessages, t);
  71. }
  72. }
  73. bool exist_link(common::Model<Time> *src_model, common::Model<Time> *dst_model) const {
  74. return _links.find(std::make_pair(src_model, dst_model));
  75. }
  76. virtual typename Time::type lookahead(const typename Time::type &t) const { return t; }
  77. void post_event(typename Time::type t, const common::ExternalEvent<Time> &event) {
  78. auto it = std::find_if(_links.begin(), _links.end(),
  79. [this](const Link &link) {
  80. return link.first == common::GraphManager<Time>::_coordinator;
  81. });
  82. if (it != _links.end()) {
  83. it->second->post_event(t,
  84. common::ExternalEvent<Time>(event.data()));
  85. }
  86. }
  87. virtual std::string to_string(int level) const {
  88. std::ostringstream ss;
  89. ss << common::String::make_spaces(level * 2) << "Children :" << std::endl;
  90. for (auto &child : common::GraphManager<Time>::_children) {
  91. ss << child->to_string(level + 1);
  92. }
  93. ss << common::String::make_spaces(level * 2) << "Links:" << std::endl;
  94. for (typename Links::const_iterator it = _links.begin(); it != _links.end(); ++it) {
  95. ss << common::String::make_spaces((level + 1) * 2)
  96. << it->first->get_name() << " -> "
  97. << it->second->get_name() << std::endl;
  98. }
  99. return ss.str();
  100. }
  101. void add_link(common::Model<Time> *src_model, common::Model<Time> *dst_model) {
  102. assert((src_model != common::GraphManager<Time>::_coordinator and
  103. dst_model != common::GraphManager<Time>::_coordinator and
  104. src_model != dst_model) or
  105. (src_model == common::GraphManager<Time>::_coordinator and
  106. dst_model != common::GraphManager<Time>::_coordinator) or
  107. (src_model != common::GraphManager<Time>::_coordinator and
  108. dst_model == common::GraphManager<Time>::_coordinator));
  109. _links.push_back(std::make_pair(src_model, dst_model));
  110. }
  111. private:
  112. Links _links;
  113. };
  114. }
  115. } // namespace artis devs
  116. #endif