Tulip  5.2.1
Large graphs analysis and drawing
TLPBExportImport.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef TLPBIMPORTEXPORT_H
22 #define TLPBIMPORTEXPORT_H
23 
24 #include <iostream>
25 #include <tulip/TulipPluginHeaders.h>
26 #include <tulip/ExportModule.h>
27 #include <tulip/ImportModule.h>
28 
29 // some compilation environments define major and minor as preprocessor macros
30 // undefine them in that case
31 #ifdef major
32 #undef major
33 #endif
34 
35 #ifdef minor
36 #undef minor
37 #endif
38 
39 /**
40  * The plugins below export/import a Tulip graph
41  * using the following binary format:
42  * format header = <magic_number, major, minor> (uint16 + uint8 +uint8)
43  * nb_nodes = uint32
44  * nb_edges = uint32
45  * edges = nb_edges * <source, target> (uint32+uint32)
46  * nb_subgraphs = uint32
47  * subgraphs = nb_subgraphs * <subgraph_id, parent_graph_id, nodes_desc, edges_desc>
48  * nodes_desc = nb_nodes_intervals * <first_node, last_node>
49  * edges_desc = nb_edges_intervals *<first_edge, last_edge>
50  * nb_properties = uint32
51  * properties = <prop_name, graph_id, type, default_node_val, default_edge_val, nodes_val,
52  * edges_val>
53  * prop_name = length + utf8 text
54  * graph_id = uint32
55  * type = length + utf8 text
56  * default_node_val = type dependant (method readb)
57  * default_edge_val = type dependant (method readb)
58  * nb_nodes_val = uint32
59  * nodes_val = nb_nodes_val * <node, node_val> (uint32 + type dependant)
60  * nb_edges_val = uint32
61  * edges_val = nb_edges_val * <edge, edge_val> (uint32 + type dependant)
62  * graph_attributes = (nb_subgraphs + 1) * <graph_id, graph_attributes_list>*
63  */
64 
65 /*@{*/
66 /// Export plugin for TLPB format
67 /**
68  *
69  * \brief This plugin saves a Tulip graph using a binary format
70  *
71  */
72 class TLPBExport : public tlp::ExportModule {
73 public:
74  PLUGININFORMATION("TLPB Export", "David Auber, Patrick Mary", "13/07/2012",
75  "<p>Supported extensions: tlpb, tlpbz (compressed), tlpb.gz "
76  "(compressed)</p><p>Exports a graph in a file using the Tulip binary format.",
77  "1.2", "File")
78 
79  std::string fileExtension() const override {
80  return "tlpb";
81  }
82 
83  std::list<std::string> gzipFileExtensions() const override {
84  std::list<std::string> ext;
85  ext.push_back("tlpb.gz");
86  ext.push_back("tlpbz");
87  return ext;
88  }
89 
90  TLPBExport(const tlp::PluginContext *context) : ExportModule(context) {}
91  ~TLPBExport() override {}
92 
93  bool exportGraph(std::ostream &) override;
94 
95  std::string icon() const override {
96  return ":/tulip/gui/icons/tlpb32x32.png";
97  }
98 
99  inline tlp::node getNode(tlp::node n) {
100  assert(graph->isElement(n));
101  return tlp::node(graph->nodePos(n));
102  }
103 
104  inline tlp::edge getEdge(tlp::edge e) {
105  assert(graph->isElement(e));
106  return tlp::edge(graph->edgePos(e));
107  }
108 
109  void getSubGraphs(tlp::Graph *, std::vector<tlp::Graph *> &);
110 
111  void writeAttributes(std::ostream &, tlp::Graph *);
112 };
113 
114 /// Import plugin for TLPB format
115 /**
116  *
117  * \brief This plugin reads a Tulip graph using a binary format
118  *
119  */
120 class TLPBImport : public tlp::ImportModule {
121 public:
122  PLUGININFORMATION("TLPB Import", "David Auber, Patrick Mary", "13/07/2012",
123  "<p>Supported extensions: tlpb, tlpb.gz (compressed), tlpbz "
124  "(compressed)</p><p>Imports a graph recorded in a file using the Tulip binary "
125  "format.</p>",
126  "1.2", "File")
127 
128  TLPBImport(tlp::PluginContext *context);
129  ~TLPBImport() override {}
130 
131  std::string icon() const override {
132  return ":/tulip/gui/icons/tlpb32x32.png";
133  }
134 
135  std::list<std::string> fileExtensions() const override {
136  std::list<std::string> l;
137  l.push_back("tlpb");
138  return l;
139  }
140 
141  std::list<std::string> gzipFileExtensions() const override {
142  std::list<std::string> ext;
143  ext.push_back("tlpb.gz");
144  ext.push_back("tlpbz");
145  return ext;
146  }
147 
148  bool importGraph() override;
149 };
150 
151 /*@}*/
152 
153 // Don't ask why it is David favorite 9 digit number.
154 #define TLPB_MAGIC_NUMBER 578374683
155 #define TLPB_MAJOR 1
156 #define TLPB_MINOR 2
157 
158 // structures used in both tlpb import/export plugins
159 struct TLPBHeader {
160  unsigned int magicNumber;
161  unsigned char major;
162  unsigned char minor;
163  unsigned int numNodes;
164  unsigned int numEdges;
165 
166  TLPBHeader(unsigned int nbN = 0, unsigned int nbE = 0)
167  : magicNumber(TLPB_MAGIC_NUMBER), major(TLPB_MAJOR), minor(TLPB_MINOR), numNodes(nbN),
168  numEdges(nbE) {}
169 
170  bool checkCompatibility() {
171  return ((magicNumber == TLPB_MAGIC_NUMBER) && (major == TLPB_MAJOR) && (minor <= TLPB_MINOR));
172  }
173 };
174 
175 #define MAX_EDGES_TO_WRITE 64000
176 #define MAX_EDGES_TO_READ MAX_EDGES_TO_WRITE
177 #define MAX_RANGES_TO_WRITE MAX_EDGES_TO_WRITE
178 #define MAX_RANGES_TO_READ MAX_RANGES_TO_WRITE
179 #define MAX_VALUES_TO_WRITE MAX_EDGES_TO_WRITE
180 #define MAX_VALUES_TO_READ MAX_VALUES_TO_WRITE
181 
182 #endif // TLPBIMPORTEXPORT_H
183 ///@endcond
Graph * importGraph(const std::string &format, DataSet &dataSet, PluginProgress *progress=nullptr, Graph *newGraph=nullptr)
Imports a graph using the specified import plugin with the parameters stored in the DataSet...
The edge struct represents an edge in a Graph object.
Definition: Edge.h:40
The node struct represents a node in a Graph object.
Definition: Node.h:40
#define PLUGININFORMATION(NAME, AUTHOR, DATE, INFO, RELEASE, GROUP)
Declare meta-information for a plugin This is an helper macro that defines every function related to ...
Definition: Plugin.h:233
Contains runtime parameters for a plugin.
Definition: PluginContext.h:42
bool exportGraph(Graph *graph, std::ostream &outputStream, const std::string &format, DataSet &dataSet, PluginProgress *progress=nullptr)
Exports a graph using the specified export plugin with parameters stored in the DataSet.