graph_manager.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file tests/boost_graph/graph_manager.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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 __TESTS_BOOST_GRAPH_GRAPH_MANAGER_HPP
  26. #define __TESTS_BOOST_GRAPH_GRAPH_MANAGER_HPP 1
  27. #include <common/scheduler/HeapScheduler.hpp>
  28. #include <common/scheduler/VectorScheduler.hpp>
  29. #include <pdevs/Coordinator.hpp>
  30. #include <pdevs/GraphManager.hpp>
  31. #include <pdevs/Simulator.hpp>
  32. #include <tests/boost_graph/models.hpp>
  33. #include <tests/boost_graph/graph_builder.hpp>
  34. namespace paradevs { namespace tests { namespace boost_graph {
  35. struct GraphParameters
  36. {
  37. Graph _graph;
  38. InputEdges _input_edges;
  39. OutputEdges _output_edges;
  40. GraphParameters(const Graph& graph,
  41. const InputEdges& input_edges,
  42. const OutputEdges& output_edges) :
  43. _graph(graph), _input_edges(input_edges), _output_edges(output_edges)
  44. { }
  45. };
  46. template < class Parameters >
  47. class FlatGraphManager :
  48. public paradevs::pdevs::GraphManager < MyTime, Parameters >
  49. {
  50. public:
  51. FlatGraphManager(common::Coordinator < MyTime >* coordinator,
  52. const Parameters& parameters) :
  53. paradevs::pdevs::GraphManager < MyTime, Parameters >(
  54. coordinator, parameters)
  55. { }
  56. virtual ~FlatGraphManager()
  57. {
  58. for (TopSimulators::const_iterator it = _top_simulators.begin();
  59. it != _top_simulators.end(); ++it) {
  60. delete it->second;
  61. }
  62. for (NormalSimulators::const_iterator it = _normal_simulators.begin();
  63. it != _normal_simulators.end(); ++it) {
  64. delete it->second;
  65. }
  66. }
  67. void build_flat_graph(const Graph& g)
  68. {
  69. Graph::vertex_iterator vertexIt, vertexEnd;
  70. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  71. for (; vertexIt != vertexEnd; ++vertexIt)
  72. {
  73. std::ostringstream ss;
  74. ss << "a" << g[*vertexIt]._index;
  75. switch (g[*vertexIt]._type) {
  76. case TOP_PIXEL:
  77. _top_simulators[g[*vertexIt]._index] =
  78. new pdevs::Simulator <
  79. MyTime, TopPixel, TopPixelParameters >(
  80. ss.str(), TopPixelParameters());
  81. FlatGraphManager < Parameters >::add_child(
  82. _top_simulators[g[*vertexIt]._index]);
  83. break;
  84. case NORMAL_PIXEL:
  85. unsigned int n = 0;
  86. Graph::adjacency_iterator neighbourIt, neighbourEnd;
  87. boost::tie(neighbourIt, neighbourEnd) =
  88. boost::adjacent_vertices(*vertexIt, g);
  89. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  90. ++n;
  91. }
  92. _normal_simulators[g[*vertexIt]._index] =
  93. new pdevs::Simulator <
  94. MyTime, NormalPixel, NormalPixelParameters >(
  95. ss.str(), NormalPixelParameters(n));
  96. FlatGraphManager < Parameters >::add_child(
  97. _normal_simulators[g[*vertexIt]._index]);
  98. break;
  99. };
  100. }
  101. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  102. for (; vertexIt != vertexEnd; ++vertexIt)
  103. {
  104. Graph::adjacency_iterator neighbourIt, neighbourEnd;
  105. boost::tie(neighbourIt, neighbourEnd) =
  106. boost::adjacent_vertices(*vertexIt, g);
  107. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  108. paradevs::common::Model < MyTime >* a = 0;
  109. paradevs::common::Model < MyTime >* b = 0;
  110. if (g[*vertexIt]._type == TOP_PIXEL) {
  111. a = _top_simulators[g[*vertexIt]._index];
  112. } else {
  113. a = _normal_simulators[g[*vertexIt]._index];
  114. }
  115. if (g[*neighbourIt]._type == TOP_PIXEL) {
  116. b = _top_simulators[g[*neighbourIt]._index];
  117. } else {
  118. b = _normal_simulators[g[*neighbourIt]._index];
  119. }
  120. FlatGraphManager < Parameters >::add_link(b, "out", a, "in");
  121. }
  122. }
  123. }
  124. protected:
  125. typedef std::map < int, pdevs::Simulator <
  126. MyTime, TopPixel,
  127. TopPixelParameters >* > TopSimulators;
  128. typedef std::map < int, pdevs::Simulator <
  129. MyTime, NormalPixel,
  130. NormalPixelParameters >* > NormalSimulators;
  131. TopSimulators _top_simulators;
  132. NormalSimulators _normal_simulators;
  133. };
  134. class BuiltFlatGraphManager :
  135. public FlatGraphManager < GraphParameters >
  136. {
  137. public:
  138. BuiltFlatGraphManager(common::Coordinator < MyTime >* coordinator,
  139. const GraphParameters& parameters) :
  140. FlatGraphManager < GraphParameters >(
  141. coordinator, parameters)
  142. {
  143. build_flat_graph(parameters._graph);
  144. // input
  145. for (Edges::const_iterator it = parameters._input_edges.begin();
  146. it != parameters._input_edges.end(); ++it) {
  147. std::ostringstream ss_in;
  148. ss_in << "in_" << it->first;
  149. BuiltFlatGraphManager::add_link(coordinator, ss_in.str(),
  150. _normal_simulators[it->second],
  151. "in");
  152. }
  153. // output
  154. for (Edges::const_iterator it = parameters._output_edges.begin();
  155. it != parameters._output_edges.end(); ++it) {
  156. std::ostringstream ss_out;
  157. ss_out << "out_" << it->second;
  158. BuiltFlatGraphManager::add_link(_normal_simulators[it->first],
  159. "out", coordinator, ss_out.str());
  160. }
  161. }
  162. virtual ~BuiltFlatGraphManager()
  163. { }
  164. };
  165. template < class GraphBuilder >
  166. class InBuildFlatGraphManager :
  167. public FlatGraphManager < paradevs::common::NoParameters >
  168. {
  169. public:
  170. InBuildFlatGraphManager(common::Coordinator < MyTime >* coordinator,
  171. const paradevs::common::NoParameters& parameters) :
  172. FlatGraphManager < paradevs::common::NoParameters >(
  173. coordinator, parameters)
  174. {
  175. GraphBuilder builder;
  176. Graphs graphs;
  177. InputEdgeList input_edges;
  178. OutputEdgeList output_edges;
  179. Connections parent_connections;
  180. builder.build(graphs, input_edges, output_edges, parent_connections);
  181. build_flat_graph(graphs.front());
  182. }
  183. virtual ~InBuildFlatGraphManager()
  184. { }
  185. };
  186. template < class GraphBuilder >
  187. class HierarchicalGraphManager :
  188. public paradevs::pdevs::GraphManager < MyTime,
  189. paradevs::common::NoParameters >
  190. {
  191. public:
  192. HierarchicalGraphManager(common::Coordinator < MyTime >* coordinator,
  193. const paradevs::common::NoParameters& parameters) :
  194. paradevs::pdevs::GraphManager < MyTime,
  195. paradevs::common::NoParameters >(
  196. coordinator, parameters)
  197. {
  198. GraphBuilder graph_builder;
  199. Graphs graphs;
  200. InputEdgeList input_edges;
  201. OutputEdgeList output_edges;
  202. Connections parent_connections;
  203. graph_builder.build(graphs, input_edges, output_edges,
  204. parent_connections);
  205. // build coordinators (graphs)
  206. for (unsigned int i = 0; i < graphs.size(); ++i) {
  207. Coordinator* coordinator = 0;
  208. std::ostringstream ss;
  209. ss << "S" << i;
  210. coordinator =
  211. new Coordinator(ss.str(), paradevs::common::NoParameters(),
  212. GraphParameters(graphs[i], input_edges[i],
  213. output_edges[i]));
  214. _coordinators.push_back(coordinator);
  215. HierarchicalGraphManager < GraphBuilder >::add_child(
  216. coordinator);
  217. }
  218. // builds internal connections (edges)
  219. for (Connections::const_iterator it = parent_connections.begin();
  220. it != parent_connections.end(); ++it) {
  221. const Connection& connection = *it;
  222. std::ostringstream ss_out;
  223. std::ostringstream ss_in;
  224. ss_out << "out_" << connection.first.second;
  225. ss_in << "in_" << connection.second.second;
  226. HierarchicalGraphManager < GraphBuilder >::add_link(
  227. _coordinators[connection.first.first], ss_out.str(),
  228. _coordinators[connection.second.first], ss_in.str());
  229. }
  230. }
  231. virtual ~HierarchicalGraphManager()
  232. {
  233. for (Coordinators::const_iterator it = _coordinators.begin();
  234. it != _coordinators.end(); ++it) {
  235. delete *it;
  236. }
  237. }
  238. private:
  239. typedef paradevs::pdevs::Coordinator < MyTime,
  240. common::scheduler::VectorScheduler <
  241. MyTime >,
  242. BuiltFlatGraphManager,
  243. common::NoParameters,
  244. GraphParameters > Coordinator;
  245. typedef std::vector < Coordinator* > Coordinators;
  246. Coordinators _coordinators;
  247. };
  248. } } } // namespace paradevs tests boost_graph
  249. #endif