Coordinator.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file common/Coordinator.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 COMMON_COORDINATOR
  26. #define COMMON_COORDINATOR
  27. #include "artis-star/common/event/Bag.hpp"
  28. #include "artis-star/common/event/ExternalEvent.hpp"
  29. #include <artis-star/common/Links.hpp>
  30. #include <artis-star/common/Model.hpp>
  31. #include <artis-star/common/Node.hpp>
  32. #include <sstream>
  33. namespace artis::common {
  34. template<typename Time>
  35. class Model;
  36. template<typename Time>
  37. class GraphManager;
  38. template<typename Time>
  39. class Coordinator : public virtual Model<Time> {
  40. public :
  41. Coordinator(const std::string &name)
  42. : Model<Time>(name) {}
  43. virtual ~Coordinator() {}
  44. virtual GraphManager<Time> &get_graph_manager() = 0;
  45. virtual const GraphManager<Time> &get_graph_manager() const = 0;
  46. virtual const Model<Time> *get_sub_model(unsigned int index) const {
  47. const GraphManager<Time> &graph_manager = get_graph_manager();
  48. typename common::ModelMap<Time>::const_iterator it = graph_manager.child_map().find(
  49. index);
  50. if (it != graph_manager.child_map().end()) {
  51. return it->second;
  52. } else {
  53. return nullptr;
  54. }
  55. }
  56. virtual const Model<Time> *get_sub_model(unsigned int index, unsigned int rank) const {
  57. const GraphManager<Time> &graph_manager = get_graph_manager();
  58. typename common::ModelsMap<Time>::const_iterator it = graph_manager.children_map().find(
  59. index);
  60. if (it != graph_manager.children_map().end() and it->second.size() > rank) {
  61. return it->second.at(rank);
  62. } else {
  63. return nullptr;
  64. }
  65. }
  66. virtual unsigned int get_sub_model_number(unsigned int index) const {
  67. const GraphManager<Time> &graph_manager = get_graph_manager();
  68. typename common::ModelsMap<Time>::const_iterator it = graph_manager.children_map().find(
  69. index);
  70. if (it != graph_manager.children_map().end()) {
  71. return it->second.size();
  72. } else {
  73. return 0;
  74. }
  75. }
  76. void input_port(common::Port p) { Coordinator<Time>::add_in_port(p); }
  77. void input_ports(std::initializer_list<common::Port> list) {
  78. for (typename std::initializer_list<common::Port>::iterator it = list.begin();
  79. it != list.end();
  80. ++it) {
  81. Coordinator<Time>::add_in_port(*it);
  82. }
  83. }
  84. virtual bool is_atomic() const { return false; }
  85. void output_port(common::Port p) { Coordinator<Time>::add_out_port(p); }
  86. void output_ports(std::initializer_list<common::Port> list) {
  87. for (typename std::initializer_list<common::Port>::iterator it = list.begin();
  88. it != list.end();
  89. ++it) {
  90. Coordinator<Time>::add_out_port(*it);
  91. }
  92. }
  93. virtual void remove_model(const typename Time::type &t, common::Model<Time> *model) {
  94. model->finish(t);
  95. }
  96. void restore(const common::context::State<Time> &state) {
  97. Model<Time>::restore(state);
  98. get_graph_manager().restore(state);
  99. }
  100. void save(common::context::State<Time> &state) const {
  101. Model<Time>::save(state);
  102. get_graph_manager().save(state);
  103. }
  104. virtual std::string to_string(int /* level */) const {
  105. std::ostringstream ss;
  106. ss << "Coordinator " << Coordinator<Time>::get_name();
  107. return ss.str();
  108. }
  109. // DEVS methods
  110. virtual void finish(const typename Time::type &t) = 0;
  111. virtual common::event::Value observe(const typename Time::type &t, unsigned int index) const = 0;
  112. virtual void output(const typename Time::type &t) = 0;
  113. virtual void post_event(const typename Time::type &t, const common::event::ExternalEvent<Time> &event) = 0;
  114. virtual typename Time::type dispatch_events(const common::event::Bag<Time> &bag,
  115. const typename Time::type &t) = 0;
  116. virtual typename Time::type start(const typename Time::type &t) = 0;
  117. virtual typename Time::type transition(const typename Time::type &t) = 0;
  118. };
  119. } // namespace artis common
  120. #endif