GraphManager.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. {
  46. for (auto m: _models) {
  47. delete m;
  48. }
  49. }
  50. virtual void add_dynamic_child(unsigned int index, common::Model<Time>* child)
  51. {
  52. super::add_child(index, child);
  53. _models.push_back(child);
  54. _new_models.push_back(child);
  55. }
  56. virtual void add_dynamic_children(unsigned int index, common::Model<Time>* child)
  57. {
  58. super::add_children(index, child);
  59. _models.push_back(child);
  60. _new_models.push_back(child);
  61. }
  62. void add_link(unsigned int source_model_index, unsigned int source_port_index,
  63. unsigned int destination_model_index, unsigned int destination_port_index)
  64. {
  65. common::Model<Time>* source_model = super::child_map(source_model_index);
  66. common::Model<Time>* destination_model = super::child_map(destination_model_index);
  67. assert(source_model and destination_model);
  68. this->out({source_model, source_port_index})
  69. >> this->in({destination_model, destination_port_index});
  70. }
  71. void clear_new_models() { _new_models.clear(); }
  72. std::vector<common::Model<Time>*>& new_models() { return _new_models; }
  73. void remove_dynamic_child(const typename Time::type& t, unsigned int index)
  74. {
  75. size_t new_index = index
  76. - (pdevs::GraphManager<Time, Parameters, GraphParameters>::_children.size()
  77. - _models.size());
  78. new_index -= std::count_if(_models.begin(), _models.end(),
  79. [](common::Model<Time>* model) { return model == nullptr; });
  80. type::coordinator()->remove_model(t, _models[new_index]);
  81. super::remove_child(index);
  82. this->remove_links(_models[new_index]);
  83. delete _models[new_index];
  84. _models[new_index] = nullptr;
  85. }
  86. void remove_link(unsigned int source_model_index, unsigned int source_port_index,
  87. unsigned int destination_model_index, unsigned int destination_port_index)
  88. {
  89. common::Model<Time>* source_model = super::child_map(source_model_index);
  90. common::Model<Time>* destination_model = super::child_map(destination_model_index);
  91. assert(source_model and destination_model);
  92. super::remove_link(source_model, source_port_index, destination_model,
  93. destination_port_index);
  94. }
  95. private:
  96. std::vector<common::Model<Time>*> _models;
  97. std::vector<common::Model<Time>*> _new_models;
  98. };
  99. }
  100. }
  101. #endif