GraphManager.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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-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 DSDE_GRAPH_MANAGER
  26. #define DSDE_GRAPH_MANAGER
  27. #include <artis-star/kernel/pdevs/GraphManager.hpp>
  28. #include <vector>
  29. namespace artis {
  30. namespace dsde {
  31. template<class Time,
  32. class Parameters = common::NoParameters,
  33. class GraphParameters = common::NoParameters>
  34. class GraphManager : public pdevs::GraphManager<Time, Parameters, GraphParameters> {
  35. public:
  36. typedef pdevs::GraphManager<Time, Parameters, GraphParameters> super;
  37. typedef GraphManager<Time, Parameters, GraphParameters> type;
  38. GraphManager(common::Coordinator<Time> *coordinator,
  39. const Parameters &parameters,
  40. const GraphParameters &graph_parameters)
  41. :
  42. pdevs::GraphManager<Time, Parameters, GraphParameters>(coordinator, parameters,
  43. graph_parameters) {}
  44. ~GraphManager() override {
  45. for (auto m: _models) {
  46. delete m;
  47. }
  48. }
  49. virtual void add_dynamic_child(unsigned int index, common::Model<Time> *child) {
  50. super::add_child(index, child);
  51. _models.push_back(child);
  52. _new_models.push_back(child);
  53. }
  54. virtual void add_dynamic_children(unsigned int index, common::Model<Time> *child) {
  55. super::add_children(index, child);
  56. _models.push_back(child);
  57. _new_models.push_back(child);
  58. }
  59. void add_link(unsigned int source_model_index, unsigned int source_port_index,
  60. unsigned int destination_model_index, unsigned int destination_port_index) {
  61. common::Model<Time> *source_model = super::child_map(source_model_index);
  62. common::Model<Time> *destination_model = super::child_map(destination_model_index);
  63. assert(source_model and destination_model);
  64. this->out({source_model, source_port_index})
  65. >> this->in({destination_model, destination_port_index});
  66. }
  67. void clear_new_models() { _new_models.clear(); }
  68. std::vector<common::Model<Time> *> &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> *> _models;
  91. std::vector<common::Model<Time> *> _new_models;
  92. };
  93. }
  94. }
  95. #endif