GraphManager.hpp 4.0 KB

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