Coordinator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 const GraphManager<Time>& get_graph_manager() const = 0;
  46. virtual const Model<Time>* get_submodel(unsigned int index) const
  47. {
  48. const GraphManager<Time>& graph_manager = get_graph_manager();
  49. typename common::ModelMap<Time>::const_iterator it = graph_manager.child_map().find(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_submodel(unsigned int index, unsigned int rank) const
  57. {
  58. const GraphManager<Time>& graph_manager = get_graph_manager();
  59. typename common::ModelsMap<Time>::const_iterator it = graph_manager.children_map().find(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_submodel_number(unsigned int index) const
  67. {
  68. const GraphManager<Time>& graph_manager = get_graph_manager();
  69. typename common::ModelsMap<Time>::const_iterator it = graph_manager.children_map().find(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)
  77. {
  78. Coordinator<Time>::add_in_port(p);
  79. }
  80. void input_ports(std::initializer_list<common::Port> list)
  81. {
  82. for (typename std::initializer_list<common::Port>::iterator it = list.begin(); it != list.end(); ++it) {
  83. Coordinator<Time>::add_in_port(*it);
  84. }
  85. }
  86. virtual bool is_atomic() const { return false; }
  87. void output_port(common::Port p)
  88. {
  89. Coordinator<Time>::add_out_port(p);
  90. }
  91. void output_ports(std::initializer_list<common::Port> list)
  92. {
  93. for (typename std::initializer_list<common::Port>::iterator it = list.begin(); it != list.end(); ++it) {
  94. Coordinator<Time>::add_out_port(*it);
  95. }
  96. }
  97. virtual std::string to_string(int /* level */) const
  98. {
  99. std::ostringstream ss;
  100. ss << "Coordinator " << Coordinator<Time>::get_name();
  101. return ss.str();
  102. }
  103. // DEVS methods
  104. virtual common::Value observe(const typename Time::type& t, unsigned int index) const = 0;
  105. virtual void output(const typename Time::type& t) = 0;
  106. virtual void post_event(const typename Time::type& t, const common::ExternalEvent<Time>& event) = 0;
  107. virtual typename Time::type dispatch_events(const common::Bag<Time>& bag,
  108. const typename Time::type& t) = 0;
  109. virtual typename Time::type start(const typename Time::type& t) = 0;
  110. virtual typename Time::type transition(const typename Time::type& t) = 0;
  111. };
  112. }
  113. } // namespace artis common
  114. #endif