Coordinator.hpp 4.6 KB

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