GraphMLWriter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #ifndef GRAPHMLWRITER_H
  2. #define GRAPHMLWRITER_H
  3. #include <QTextStream>
  4. #include <QList>
  5. #include <exception>
  6. #include <boost/graph/graphml.hpp>
  7. #include <boost/graph/subgraph.hpp>
  8. #include <boost/graph/adjacency_list.hpp>
  9. #include <boost/graph/graph_traits.hpp>
  10. #include <boost/graph/graph_utility.hpp>
  11. #include <string>
  12. #include <Common/BoostGraphWrapper.h>
  13. #include <ReaderWriterUtilities/AttributeConstants.h>
  14. #include <ReaderWriterUtilities/DefaultValues.h>
  15. #include <ReaderWriterUtilities/Typedefs.h>
  16. #include <GraphLayoutLibrary_global.h>
  17. #include <QQueue>
  18. using namespace std;
  19. using namespace boost;
  20. /**
  21. * @brief The GraphMLWriter class writes provided boost graph into given file in graphml file format
  22. */
  23. class GRAPHLAYOUTLIBRARYSHARED_EXPORT GraphMLWriter
  24. {
  25. private:
  26. BoostGraphWrapper* m_boostGraphWrapper; /*!< BoostGraphWrapper pointer used to access graph properties */
  27. long int m_iGraphCounter; /*!< Integer to store encountered graph count */
  28. QTextStream m_txtStreamOut; /*!< QTextStream object used to write graphML file */
  29. //*************************************************************
  30. // To Do: Make a separate class for this property mapper
  31. MapGraphElementToAttributeList m_mapGraphElementToAttributeList; /*!< Map between elements to their attribute lists */
  32. //For attributes e.g. "node" "height" - "key0";or "edge" "direction" - "key1"
  33. MapElementNameAndPropertyToKey m_mapElementNameAndPropertyToKey; /*!< Map between element name : property to their keys */
  34. MapVisitedVertices m_mapVisitedVertices; /*!< Map between vertices to their keys. Used to maintain visited vertices. */
  35. MapVisitedEdges m_mapVisitedEdges; /*!< Map between edges to their keys. Used to maintain visited edges. */
  36. /**
  37. This function creates a map of the node, edge, graph and their properties.
  38. This map is later used to generate key declarations for these node, edge or graph.
  39. @pre
  40. @param
  41. @return none
  42. @throw none
  43. */
  44. void populateDataTagProperties();
  45. //*************************************************************
  46. /**
  47. This function marks vertex as visited as the indication that it has been written to the output file.
  48. @pre
  49. -# iVertex != NULL
  50. @param iVertex
  51. a non visited vertex of the graph
  52. @return none
  53. @throw none
  54. */
  55. void markGlobalVertexVisited(int iVertexId);
  56. /**
  57. This function checks if the vertex is visited or not,
  58. as the indication that it has been written to the output file or not.
  59. @pre
  60. -# iVertex != NULL
  61. @param iVertex
  62. a vertex of the graph
  63. @return none
  64. @throw none
  65. */
  66. bool isGlobalVertexVisited(int iVertexId);
  67. /**
  68. This function marks Edge as visited as the indication that it has been written to the output file.
  69. @pre
  70. -# iEdgeId != NULL
  71. @param iEdgeId
  72. an Edge of the graph
  73. @return none
  74. @throw none
  75. */
  76. void markGlobalEdgeVisited(int iEdgeId);
  77. /**
  78. This function checks if the Edge is visited or not, as the indication that it has been written to the output file or not.
  79. @pre
  80. -# iEdgeId != NULL
  81. @param iEdgeId
  82. a Edge of the graph
  83. @return none
  84. @throw none
  85. */
  86. bool isGlobalEdgeVisited(int iEdgeId);
  87. /**
  88. This function generates key declaration for node, edge and graph attributes.
  89. These key ids are used to denote the attribute in the output graphml file.
  90. @pre
  91. -# m_txtStreamOut.device()->isWritable() == true
  92. -# strcmp((const char*)m_txtStreamOut.codec(),"UTF-8")
  93. -# m_mapGraphElementToAttributeList.empty() == false
  94. @param
  95. @return none
  96. @throw LayoutException
  97. -# REQUIRED_PARAMETER_NOT_SET if the output text stream is not set writable or does not have UTF 8 encoding
  98. -# EMPTY_CONTAINER if map GraphElementToAttributeList is empty
  99. */
  100. void generateKeyDeclaration();
  101. /**
  102. This function prints the attribute list of node, edge and graph
  103. which are considered for writing into the output graphml file.
  104. @pre
  105. -# m_mapGraphElementToAttributeList.empty() == false
  106. @param
  107. @return none
  108. @throw none
  109. */
  110. void printAttributeTable();
  111. /**
  112. This function prints data tag for the given attribute with given value
  113. with proper indent space aacording to the level or depth of that tag.
  114. @pre
  115. -# m_txtStreamOut.device()->isWritable() == true
  116. -# strcmp((const char*)m_txtStreamOut.codec(),"UTF-8")
  117. -# sKey.length() > 0
  118. -# iLevel >= 0
  119. @param sKey
  120. key id that describes the attribute of graph
  121. @param sValue
  122. value of the attrbute which is described by the key id
  123. @param iLevel
  124. specifies the nesting depth of current data tag
  125. @return none
  126. @throw LayoutException
  127. -# REQUIRED_PARAMETER_NOT_SET if the output text stream is not set writable or does not have UTF 8 encoding
  128. -# INVALID_PARAMETER if level value negative or Key is invalid
  129. */
  130. void addData(QString sKey , QString sValue, int iLevel);
  131. /**
  132. This function prints data tag for bends in the edges.
  133. @pre
  134. -# m_txtStreamOut.device()->isWritable() == true
  135. -# strcmp((const char*)m_txtStreamOut.codec(),"UTF-8")
  136. -# sKey.length() > 0
  137. -# iLevel >= 0
  138. @param sKey
  139. key id that describes the attribute of edge
  140. @param sValue
  141. value of the attrbute which is described by the key id
  142. @param iLevel
  143. specifies the nesting depth of current data tag
  144. @return none
  145. @throw none
  146. */
  147. void addBendPointsData(QString sKey, int iLevel,EdgeDescriptor& eEdge, SubGraph& gSubgraph);
  148. /**
  149. This function To retrieves unique key assigned to particular attribute of graph, node or edge
  150. @pre
  151. -# sElementName.length() > 0
  152. -# sAttributeName.length() > 0
  153. @param sElementName
  154. describes name of graph element i.e. node, edge or graph
  155. @param sAttributeName
  156. describes the attribute name
  157. @return QString
  158. key id in a QString
  159. @throw LayoutException
  160. -# EMPTY_CONTAINER if an empty key is found
  161. */
  162. QString getKey(QString sElementName , QString sAttributeName);
  163. /**
  164. This function returns indent space according to the given nesting level
  165. @pre
  166. -# iLevel >= 0
  167. @param iLevel
  168. specifies the nesting depth of the any tag
  169. @return none
  170. @throw none
  171. */
  172. QString indent(int iLevel);
  173. public:
  174. /** @name Creators
  175. * The methods under this section are responsible for constructing or
  176. * destructing an instance of type GraphMLWriter.
  177. */
  178. //@{
  179. /**
  180. Constructs new object of GraphMLReader.
  181. @pre none
  182. @param none
  183. @return none
  184. @throw none
  185. */
  186. GraphMLWriter();
  187. //@}
  188. /** @name Modifiers
  189. * The methods under this section are responsible for modifying
  190. * an instance of GraphMLWriter.
  191. */
  192. //@{
  193. /**
  194. This function writes boost graph to graphml file
  195. @pre
  196. -# gBoostGraph != NULL
  197. -# outStreamFile != NULL
  198. -# outStreamFile->isWritable() == true
  199. @param gBoostGraph
  200. reference to input graph
  201. @param outStreamFile
  202. output file stream
  203. @return none
  204. @throw LayoutException
  205. @throw BoostException
  206. @throw MemoryException
  207. */
  208. void writeGraphml(SubGraph& gBoostGraph, QIODevice *outStreamFile);
  209. //@}
  210. private:
  211. /**
  212. This function iterates the graphs recursively and writes them to graphml file stream
  213. @pre
  214. -# gBoostGraph != NULL
  215. -# outStreamFile != NULL
  216. -# outStreamFile->isWritable() == true
  217. @param gBoostGraph
  218. reference to current graph
  219. @param gMainGraph
  220. reference to main graph
  221. @param iLevel
  222. specifies the depth of current graph in graphs-subgraph tree
  223. @return none
  224. @throw none
  225. */
  226. void generateGraphmlRecur(SubGraph& gSubGraph , SubGraph &gMainGraph, int iLevel);
  227. /**
  228. This function generates graphml file from boost subgraph, internally uses generateGraphmlRecur()
  229. @pre
  230. -# gSubGraph != NULL
  231. -# m_txtStreamOut.device()->isWritable() == true
  232. -# strcmp((const char*)m_txtStreamOut.codec(),"UTF-8") == true
  233. @param gSubGraph
  234. reference to input graph
  235. @return none
  236. @throw LayoutException
  237. -# REQUIRED_PARAMETER_NOT_SET if the output text stream is not set writable or does not have UTF 8 encoding
  238. */
  239. void generateGraphml(SubGraph& gSubGraph);
  240. /**
  241. This function iterates the graphs recursively and writes them to graphml file stream
  242. @pre
  243. -# gBoostGraph != NULL
  244. -# outStreamFile != NULL
  245. -# outStreamFile->isWritable() == true
  246. @param gBoostGraph
  247. reference to current graph
  248. @param gMainGraph
  249. reference to main graph
  250. @param iLevel
  251. specifies the depth of current graph in graphs-subgraph tree
  252. @return none
  253. @throw LayoutException
  254. -# REQUIRED_PARAMETER_NOT_SET if the output text stream is not set writable or does not have UTF 8 encoding
  255. */
  256. void addGraphDataAttribute(SubGraph& gSubGraph , int iLevel);
  257. /**
  258. This function writes a node for a graph vertex to the output graphml file
  259. @pre
  260. -# gSubGraph != NULL
  261. -# gMainGraph != NULL
  262. -# iLevel >= 0
  263. -# vVertex >= 0
  264. @param vVertex
  265. reference to the vertex to be written in the output graphml file
  266. @param gSubGraph
  267. reference to current graph
  268. @param gMainGraph
  269. reference to main graph
  270. @param iLevel
  271. specifies the nesting depth of current graph in graphs-subgraph tree
  272. @return none
  273. @throw LayoutException
  274. -# REQUIRED_PARAMETER_NOT_SET if the output text stream is not set writable or does not have UTF 8 encoding
  275. */
  276. void addNode(VertexDescriptor &vVertex, SubGraph &gSubGraph , int iLevel, SubGraph &gMainGraph);
  277. /**
  278. This function writes a edge for a graph edge to the output graphml file
  279. @pre
  280. -# gSubGraph != NULL
  281. -# gMainGraph != NULL
  282. -# iLevel >= 0
  283. -# eEdge >= 0
  284. @param vEdge
  285. reference to the edge to be written in the output graphml file
  286. @param gSubGraph
  287. reference to current graph
  288. @param gMainGraph
  289. reference to main graph
  290. @param iLevel
  291. specifies the nesting depth of current graph in graphs-subgraph tree
  292. @return none
  293. @throw LayoutException
  294. -# REQUIRED_PARAMETER_NOT_SET if the output text stream is not set writable or does not have UTF 8 encoding
  295. */
  296. void addEdge(EdgeDescriptor &eEdge, SubGraph &gSubGraph , int iLevel, SubGraph &gMainGraph);
  297. };
  298. #endif // GRAPHMLWRITER_H