graph_manager.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <boost/graph/adjacency_list.hpp>
  28. #include <common/scheduler/HeapScheduler.hpp>
  29. #include <common/scheduler/VectorScheduler.hpp>
  30. #include <pdevs/Coordinator.hpp>
  31. #include <pdevs/GraphManager.hpp>
  32. #include <pdevs/Simulator.hpp>
  33. #include <tests/boost_graph/models.hpp>
  34. namespace paradevs { namespace tests { namespace boost_graph {
  35. struct VertexProperties
  36. {
  37. double _weight;
  38. DynamicsType _type;
  39. VertexProperties() : _weight(0), _type(NORMAL_PIXEL)
  40. { }
  41. VertexProperties(double weight, DynamicsType type) :
  42. _weight(weight), _type(type)
  43. { }
  44. };
  45. typedef boost::property < boost::edge_weight_t, double > EdgeProperty;
  46. typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS,
  47. VertexProperties, EdgeProperty> Graph;
  48. typedef std::vector < Graph > Graphs;
  49. typedef std::pair < int, int > Edge;
  50. typedef std::vector < Edge > Edges;
  51. typedef std::vector < Edges > EdgeList;
  52. struct GraphParameters
  53. {
  54. Graph _graph;
  55. Edges _edges;
  56. GraphParameters(const Graph& graph, const Edges& edges) :
  57. _graph(graph), _edges(edges)
  58. { }
  59. };
  60. template < class Parameters >
  61. class FlatGraphManager :
  62. public paradevs::pdevs::GraphManager < MyTime, Parameters >
  63. {
  64. public:
  65. FlatGraphManager(common::Coordinator < MyTime >* coordinator,
  66. const Parameters& parameters) :
  67. paradevs::pdevs::GraphManager < MyTime, Parameters >(
  68. coordinator, parameters)
  69. { }
  70. virtual ~FlatGraphManager()
  71. {
  72. // TODO
  73. }
  74. void build_flat_graph(const Graph& g)
  75. {
  76. Graph::vertex_iterator vertexIt, vertexEnd;
  77. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  78. for (; vertexIt != vertexEnd; ++vertexIt)
  79. {
  80. std::ostringstream ss;
  81. ss << "a" << *vertexIt;
  82. switch (g[*vertexIt]._type) {
  83. case TOP_PIXEL:
  84. _top_simulators[*vertexIt] =
  85. new pdevs::Simulator <
  86. MyTime, TopPixel, TopPixelParameters >(
  87. ss.str(), TopPixelParameters());
  88. FlatGraphManager < Parameters >::add_child(
  89. _top_simulators[*vertexIt]);
  90. break;
  91. case NORMAL_PIXEL:
  92. unsigned int n = 0;
  93. Graph::adjacency_iterator neighbourIt, neighbourEnd;
  94. boost::tie(neighbourIt, neighbourEnd) =
  95. boost::adjacent_vertices(*vertexIt, g);
  96. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  97. ++n;
  98. }
  99. _normal_simulators[*vertexIt] =
  100. new pdevs::Simulator <
  101. MyTime, NormalPixel, NormalPixelParameters >(
  102. ss.str(), NormalPixelParameters(n));
  103. FlatGraphManager < Parameters >::add_child(
  104. _normal_simulators[*vertexIt]);
  105. break;
  106. };
  107. }
  108. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  109. for (; vertexIt != vertexEnd; ++vertexIt)
  110. {
  111. Graph::adjacency_iterator neighbourIt, neighbourEnd;
  112. boost::tie(neighbourIt, neighbourEnd) =
  113. boost::adjacent_vertices(*vertexIt, g);
  114. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  115. paradevs::common::Model < MyTime >* a = 0;
  116. paradevs::common::Model < MyTime >* b = 0;
  117. if (g[*vertexIt]._type == TOP_PIXEL) {
  118. a = _top_simulators[*vertexIt];
  119. } else {
  120. a = _normal_simulators[*vertexIt];
  121. }
  122. if (g[*neighbourIt]._type == TOP_PIXEL) {
  123. b = _top_simulators[*neighbourIt];
  124. } else {
  125. b = _normal_simulators[*neighbourIt];
  126. }
  127. FlatGraphManager < Parameters >::add_link(b, "out", a, "in");
  128. }
  129. }
  130. }
  131. private:
  132. std::map < int, pdevs::Simulator <
  133. MyTime, TopPixel,
  134. TopPixelParameters >* > _top_simulators;
  135. std::map < int, pdevs::Simulator <
  136. MyTime, NormalPixel,
  137. NormalPixelParameters>* > _normal_simulators;
  138. };
  139. class BuiltFlatGraphManager :
  140. public FlatGraphManager < GraphParameters >
  141. {
  142. public:
  143. BuiltFlatGraphManager(common::Coordinator < MyTime >* coordinator,
  144. const GraphParameters& parameters) :
  145. FlatGraphManager < GraphParameters >(
  146. coordinator, parameters)
  147. {
  148. build_flat_graph(parameters._graph);
  149. }
  150. virtual ~BuiltFlatGraphManager()
  151. { }
  152. };
  153. template < class GraphBuilder >
  154. class InBuildFlatGraphManager :
  155. public FlatGraphManager < paradevs::common::NoParameters >
  156. {
  157. public:
  158. InBuildFlatGraphManager(common::Coordinator < MyTime >* coordinator,
  159. const paradevs::common::NoParameters& parameters) :
  160. FlatGraphManager < paradevs::common::NoParameters >(
  161. coordinator, parameters)
  162. {
  163. GraphBuilder builder;
  164. Graphs graphs;
  165. EdgeList edges;
  166. builder.build(graphs, edges);
  167. build_flat_graph(graphs.front());
  168. }
  169. virtual ~InBuildFlatGraphManager()
  170. { }
  171. };
  172. class FlatGraphBuilder
  173. {
  174. public:
  175. FlatGraphBuilder()
  176. { }
  177. void build(Graphs& graphs, EdgeList& /* edges */)
  178. {
  179. Graph graph;
  180. Graph::vertex_descriptor v0 = boost::add_vertex(graph);
  181. Graph::vertex_descriptor v1 = boost::add_vertex(graph);
  182. Graph::vertex_descriptor v2 = boost::add_vertex(graph);
  183. Graph::vertex_descriptor v3 = boost::add_vertex(graph);
  184. Graph::vertex_descriptor v4 = boost::add_vertex(graph);
  185. Graph::vertex_descriptor v5 = boost::add_vertex(graph);
  186. Graph::vertex_descriptor v6 = boost::add_vertex(graph);
  187. Graph::vertex_descriptor v7 = boost::add_vertex(graph);
  188. Graph::vertex_descriptor v8 = boost::add_vertex(graph);
  189. Graph::vertex_descriptor v9 = boost::add_vertex(graph);
  190. Graph::vertex_descriptor v10 = boost::add_vertex(graph);
  191. boost::add_edge(v0, v1, 1, graph);
  192. boost::add_edge(v0, v2, 1, graph);
  193. boost::add_edge(v0, v3, 1, graph);
  194. boost::add_edge(v1, v2, 1, graph);
  195. boost::add_edge(v1, v4, 1, graph);
  196. boost::add_edge(v1, v5, 1, graph);
  197. boost::add_edge(v1, v6, 1, graph);
  198. boost::add_edge(v2, v6, 1, graph);
  199. boost::add_edge(v2, v3, 1, graph);
  200. boost::add_edge(v3, v9, 1, graph);
  201. boost::add_edge(v3, v10, 1, graph);
  202. boost::add_edge(v4, v5, 1, graph);
  203. boost::add_edge(v5, v6, 1, graph);
  204. boost::add_edge(v4, v7, 1, graph);
  205. boost::add_edge(v4, v8, 1, graph);
  206. boost::add_edge(v7, v8, 1, graph);
  207. boost::add_edge(v9, v10, 1, graph);
  208. graph[v6] = VertexProperties(1, TOP_PIXEL);
  209. graph[v8] = VertexProperties(1, TOP_PIXEL);
  210. graph[v10] = VertexProperties(1, TOP_PIXEL);
  211. graph[v0] = VertexProperties(1, NORMAL_PIXEL);
  212. graph[v1] = VertexProperties(1, NORMAL_PIXEL);
  213. graph[v2] = VertexProperties(1, NORMAL_PIXEL);
  214. graph[v3] = VertexProperties(1, NORMAL_PIXEL);
  215. graph[v4] = VertexProperties(1, NORMAL_PIXEL);
  216. graph[v5] = VertexProperties(1, NORMAL_PIXEL);
  217. graph[v7] = VertexProperties(1, NORMAL_PIXEL);
  218. graph[v9] = VertexProperties(1, NORMAL_PIXEL);
  219. graphs.push_back(graph);
  220. }
  221. };
  222. template < class GraphBuilder >
  223. class HierarchicalGraphManager :
  224. public paradevs::pdevs::GraphManager < MyTime,
  225. paradevs::common::NoParameters >
  226. {
  227. public:
  228. HierarchicalGraphManager(common::Coordinator < MyTime >* coordinator,
  229. const paradevs::common::NoParameters& parameters) :
  230. paradevs::pdevs::GraphManager < MyTime,
  231. paradevs::common::NoParameters >(
  232. coordinator, parameters)
  233. {
  234. GraphBuilder graph_builder;
  235. Graphs graphs;
  236. EdgeList edges;
  237. graph_builder.build(graphs, edges);
  238. // build coordinators (graphs)
  239. for (unsigned int i = 0; i < graphs.size(); ++i) {
  240. Coordinator* coordinator = 0;
  241. std::ostringstream ss;
  242. ss << "S" << i;
  243. coordinator =
  244. new Coordinator(ss.str(), paradevs::common::NoParameters(),
  245. GraphParameters(graphs[i], edges[i]));
  246. _coordinators.push_back(coordinator);
  247. HierarchicalGraphManager < GraphBuilder >::add_child(
  248. coordinator);
  249. }
  250. // builds internal connections (edges)
  251. for (unsigned int i = 0; i < edges.size(); ++i) {
  252. // TODO
  253. }
  254. }
  255. virtual ~HierarchicalGraphManager()
  256. {
  257. // TODO
  258. }
  259. private:
  260. typedef paradevs::pdevs::Coordinator <
  261. MyTime,
  262. paradevs::common::scheduler::VectorScheduler < MyTime >,
  263. BuiltFlatGraphManager, paradevs::common::NoParameters,
  264. GraphParameters > Coordinator;
  265. std::vector < Coordinator* > _coordinators;
  266. };
  267. class HierarchicalGraphBuilder
  268. {
  269. public:
  270. HierarchicalGraphBuilder()
  271. { }
  272. void build(Graphs& graphs, EdgeList& edges)
  273. {
  274. // S1
  275. {
  276. Graph graph;
  277. Graph::vertex_descriptor v1 = boost::add_vertex(graph);
  278. Graph::vertex_descriptor v2 = boost::add_vertex(graph);
  279. Graph::vertex_descriptor v4 = boost::add_vertex(graph);
  280. Graph::vertex_descriptor v5 = boost::add_vertex(graph);
  281. Graph::vertex_descriptor v6 = boost::add_vertex(graph);
  282. Graph::vertex_descriptor v7 = boost::add_vertex(graph);
  283. Graph::vertex_descriptor v8 = boost::add_vertex(graph);
  284. boost::add_edge(v1, v2, 1, graph);
  285. boost::add_edge(v1, v4, 1, graph);
  286. boost::add_edge(v1, v5, 1, graph);
  287. boost::add_edge(v1, v6, 1, graph);
  288. boost::add_edge(v2, v6, 1, graph);
  289. boost::add_edge(v4, v5, 1, graph);
  290. boost::add_edge(v5, v6, 1, graph);
  291. boost::add_edge(v4, v7, 1, graph);
  292. boost::add_edge(v4, v8, 1, graph);
  293. boost::add_edge(v7, v8, 1, graph);
  294. graph[v6] = VertexProperties(1, TOP_PIXEL);
  295. graph[v8] = VertexProperties(1, TOP_PIXEL);
  296. graph[v1] = VertexProperties(1, NORMAL_PIXEL);
  297. graph[v2] = VertexProperties(1, NORMAL_PIXEL);
  298. graph[v4] = VertexProperties(1, NORMAL_PIXEL);
  299. graph[v5] = VertexProperties(1, NORMAL_PIXEL);
  300. graph[v7] = VertexProperties(1, NORMAL_PIXEL);
  301. graphs.push_back(graph);
  302. }
  303. // S2
  304. {
  305. Graph graph;
  306. Graph::vertex_descriptor v0 = boost::add_vertex(graph);
  307. Graph::vertex_descriptor v3 = boost::add_vertex(graph);
  308. Graph::vertex_descriptor v9 = boost::add_vertex(graph);
  309. Graph::vertex_descriptor v10 = boost::add_vertex(graph);
  310. boost::add_edge(v0, v3, 1, graph);
  311. boost::add_edge(v3, v10, 1, graph);
  312. boost::add_edge(v9, v10, 1, graph);
  313. boost::add_edge(v3, v9, 1, graph);
  314. graph[v10] = VertexProperties(1, TOP_PIXEL);
  315. graph[v0] = VertexProperties(1, NORMAL_PIXEL);
  316. graph[v3] = VertexProperties(1, NORMAL_PIXEL);
  317. graph[v9] = VertexProperties(1, NORMAL_PIXEL);
  318. graphs.push_back(graph);
  319. }
  320. {
  321. // S1 -> S2
  322. edges.push_back(std::vector < std::pair < int, int >>());
  323. edges[0].push_back(std::pair < int, int >(1, 0));
  324. edges[0].push_back(std::pair < int, int >(2, 0));
  325. // S2 -> S1
  326. edges.push_back(std::vector < std::pair < int, int >>());
  327. edges[1].push_back(std::pair < int, int >(3, 2));
  328. }
  329. }
  330. };
  331. } } } // namespace paradevs tests boost_graph
  332. #endif