GraphManager.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file kernel/dsde/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-2022 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 DSDE_GRAPH_MANAGER
  26. #define DSDE_GRAPH_MANAGER
  27. #include <artis-star/kernel/pdevs/GraphManager.hpp>
  28. #include <vector>
  29. namespace artis::dsde {
  30. template<typename Time,
  31. typename Parameters = common::NoParameters,
  32. typename GraphParameters = common::NoParameters>
  33. class GraphManager : public pdevs::GraphManager<Time, Parameters, GraphParameters> {
  34. public:
  35. typedef pdevs::GraphManager <Time, Parameters, GraphParameters> super;
  36. typedef GraphManager<Time, Parameters, GraphParameters> type;
  37. GraphManager(common::Coordinator <Time> *coordinator,
  38. const Parameters &parameters,
  39. const GraphParameters &graph_parameters)
  40. :
  41. pdevs::GraphManager<Time, Parameters, GraphParameters>(coordinator, parameters,
  42. graph_parameters) {}
  43. ~GraphManager() override {
  44. for (auto m: _models) {
  45. delete m;
  46. }
  47. }
  48. virtual void add_dynamic_child(unsigned int index, common::Model <Time> *child) {
  49. super::add_child(index, child);
  50. _models.push_back(child);
  51. _new_models.push_back(child);
  52. }
  53. virtual void add_dynamic_children(unsigned int index, common::Model <Time> *child) {
  54. super::add_children(index, child);
  55. _models.push_back(child);
  56. _new_models.push_back(child);
  57. }
  58. void add_link(unsigned int source_model_index, unsigned int source_port_index,
  59. unsigned int destination_model_index, unsigned int destination_port_index) {
  60. common::Model <Time> *source_model = super::child_map(source_model_index);
  61. common::Model <Time> *destination_model = super::child_map(destination_model_index);
  62. assert(source_model and destination_model);
  63. this->out({source_model, source_port_index})
  64. >> this->in({destination_model, destination_port_index});
  65. }
  66. void clear_new_models() { _new_models.clear(); }
  67. std::vector<common::Model < Time> *> &
  68. new_models() { return _new_models; }
  69. void remove_dynamic_child(const typename Time::type &t, unsigned int index) {
  70. size_t new_index = index
  71. - (pdevs::GraphManager<Time, Parameters, GraphParameters>::_children.size()
  72. - _models.size());
  73. new_index -= std::count_if(_models.begin(), _models.end(),
  74. [](common::Model <Time> *model) { return model == nullptr; });
  75. type::coordinator()->remove_model(t, _models[new_index]);
  76. super::remove_child(index);
  77. this->remove_links(_models[new_index]);
  78. delete _models[new_index];
  79. _models[new_index] = nullptr;
  80. }
  81. void remove_link(unsigned int source_model_index, unsigned int source_port_index,
  82. unsigned int destination_model_index, unsigned int destination_port_index) {
  83. common::Model <Time> *source_model = super::child_map(source_model_index);
  84. common::Model <Time> *destination_model = super::child_map(destination_model_index);
  85. assert(source_model and destination_model);
  86. super::remove_link(source_model, source_port_index, destination_model,
  87. destination_port_index);
  88. }
  89. private:
  90. std::vector<common::Model < Time> *>
  91. _models;
  92. std::vector<common::Model < Time> *>
  93. _new_models;
  94. };
  95. }
  96. #endif