Coordinator.hpp 5.4 KB

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