Tulip  4.8.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
Graph.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
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 
20 #ifndef Tulip_SUPERGRAPH_H
21 #define Tulip_SUPERGRAPH_H
22 
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
27 #include <climits>
28 #include <tulip/tulipconf.h>
29 #include <tulip/DataSet.h>
30 #include <tulip/Node.h>
31 #include <tulip/Edge.h>
32 #include <tulip/Observable.h>
33 
34 namespace tlp {
35 
36 class PropertyInterface;
37 class BooleanProperty;
38 class PluginProgress;
39 template<class C>struct Iterator;
40 
41 /**
42  * @enum This Enum describes the possible types of an element of the graph.
43  *
44  * It is used in functions that can return an edge or a node, to distinguish between the two cases.
45  **/
46 enum ElementType {
47  /** This element describes a node **/
48  NODE = 0,
49  /** This element describes an edge **/
50  EDGE = 1
51 };
52 
53 /**
54  * @ingroup Graph
55  * @brief Loads a graph from a file (extension can be any of the Tulip supported input graph file format).
56  *
57  * This function loads a graph serialized in a file trough the available Tulip import plugins.
58  * Since Tulip 4.8, the selection of the import plugin is based on the provided filename extension.
59  * The import will fail if the selected import plugin is not loaded.
60  * The graph file formats that can currently be imported are : TLP (*.tlp, *.tlp.gz), TLP Binary (*.tlpb, *.tlpb.gz), TLP JSON (*.json),
61  * Gephi (*.gexf), Pajek (*.net, *.paj), GML (*.gml), Graphviz (*.dot) and UCINET (*.txt)
62  *
63  * Before Tulip 4.8 and as a fallback, the function uses the "TLP Import" import plugin
64  * (always loaded as it is linked into the tulip-core library).
65  *
66  * If the import fails (no such file, parse error, ...) NULL is returned.
67  *
68  * @param filename the file in one of the supported formats to parse.
69  * @return Graph* the imported Graph, NULL if the import failed.
70  **/
71 TLP_SCOPE Graph * loadGraph(const std::string &filename, tlp::PluginProgress* progress = NULL);
72 
73 /**
74  * @ingroup Graph
75  * @brief Saves the corresponding graph to a file (extension can be any of the Tulip supported ouput graph file format)..
76  *
77  * This function serializes the corresponding graph and all its subgraphs (depending on the format) to a file
78  * through the available Tulip export plugins.
79  * Since Tulip 4.8, the selection of the export plugin is based on the provided filename extension.
80  * The export will fail if the selected export plugin is not loaded.
81  * The file formats the graph can be exported to are : TLP (*.tlp, *.tlp.gz), TLP Binary (*.tlpb, *.tlpb.gz),
82  * TLP JSON (*.json) and GML (*.gml)
83  *
84  * This function checks the file name for the '.gz' extension and uses a compressed output if supported (TLP and TLP Binary only).
85  *
86  * Before Tulip 4.8 and as a fallback, this function uses the "TLP Export" export plugin
87  * (always loaded as it is linked into the tulip-core library).
88  *
89  * @param graph the graph to save.
90  * @param filename the file to save the graph to.
91  * @return bool whether the export was successfull or not.
92  **/
93 TLP_SCOPE bool saveGraph(Graph* graph, const std::string &filename, tlp::PluginProgress* progress = NULL);
94 
95 /**
96  * @ingroup Graph
97  * @brief Exports a graph using the specified export plugin with parameters stored in the DataSet.
98  *
99  * You determine the destination, whether by using a fstream, or by saving the contents of the stream to the destination of your choice.
100  *
101  * @param graph The graph to export.
102  * @param outputStream The stream to export to. Can be a standard ostream, an ofstream, or even a gzipped ostream.
103  * @param format The format to use to export the Graph.
104  * @param dataSet The parameters to pass to the export plugin (e.g. additional data, options for the format)
105  * @param progress A PluginProgress to report the progress of the operation, as well as final state. Defaults to NULL.
106  * @return bool Whether the export was successfull or not.
107  **/
108 TLP_SCOPE bool exportGraph(Graph *graph, std::ostream &outputStream, const std::string &format, DataSet &dataSet, PluginProgress *progress=NULL);
109 
110 /**
111  * @ingroup Graph
112  * @brief Imports a graph using the specified import plugin with the parameters stored in the DataSet.
113  *
114  * If no graph is passed, then a new graph will be created. You can pass a graph in order to import data into it.
115  * Returns the graph with imported data, or NULL if the import failed. In this case, the Pluginprogress should have an error that can be displayed.
116  *
117  * @param format The format to use to import the graph.
118  * @param dataSet The parameters to pass to the import plugin (file to read, ...)
119  * @param progress A PluginProgress to report the progress of the operation, as well as final state. Defaults to NULL.
120  * @param newGraph The graph to import the data into. This can be usefull to import data into a subgraph. Defaults to NULL.
121  * @return :Graph* The graph containing the imported data, or NULL in case of failure.
122  **/
123 TLP_SCOPE Graph* importGraph(const std::string &format, DataSet &dataSet, PluginProgress *progress=NULL,Graph *newGraph=NULL);
124 
125 /**
126  * @ingroup Graph
127  * @brief Creates a new, empty graph.
128  *
129  * This is a simple method factory to create a Graph implementation (remember, Graph is only an interface).
130  *
131  * This is the recommended way to create a new Graph.
132  *
133  * @return :Graph* A new, empty graph.
134  **/
135 TLP_SCOPE Graph* newGraph();
136 
137 /**
138  * @ingroup Graph
139  * Appends the selected part of the graph inG (properties, nodes and edges) into the graph outG.
140  * If no selection is done (inSel=NULL), the whole inG graph is appended.
141  * The output selection is used to select the appended nodes & edges
142  * \warning The input selection is extended to all selected edge ends.
143  */
144 TLP_SCOPE void copyToGraph(Graph *outG, const Graph *inG, BooleanProperty* inSelection=NULL, BooleanProperty* outSelection=NULL );
145 
146 /**
147  * @ingroup Graph
148  * Removes the selected part of the graph ioG (properties values, nodes and edges).
149  * If no selection is done (inSel=NULL), the whole graph is reseted to default value.
150  * \warning The selection is extended to all selected edge ends.
151  */
152 TLP_SCOPE void removeFromGraph(Graph * ioG, BooleanProperty* inSelection=NULL);
153 
154 
155 /**
156  * @ingroup Graph
157  * Gets an iterator over the root graphs. That is all the currently existing graphs which have been created using the tlp::newGraph, tlp::loadGraph or tlp::importGraph functions and are the root graphs of an existing graph hierarchy.
158  * @return An iterator over all the root graphs. The caller of this function is responsible of the deletion of the returned iterator.
159  */
160 TLP_SCOPE Iterator<Graph*>* getRootGraphs();
161 
162 
163 /**
164  * @ingroup Graph
165  * The class Graph is the interface of a Graph in the Tulip Library.
166  *
167  * There are a few principles to know when working with a Tulip Graph.
168  *
169  * @chapter Directed
170  * Every edge is directed in a Tulip Graph.
171  * You can choose to ignore this, but every edge has a source and destination.
172  *
173  * @chapter Inheritance
174  *
175  * Subgraphs inherit from their parent graph.
176  * This is true of nodes and edges; every node and edge in a subgraph also exists in each of its parent graphs.
177  * This is also true of properties; every property in a graph exist in all of its subgraphs, except if it has been replaced
178  * by a local property.
179  *
180  * For instance, if you have the following graph hierarchy:
181  * root
182  * / \
183  * A B
184  *
185  * Every node in A is in root, and every node in B is in root.
186  * Nodes can be in A and root but not B; or in B and root but not A.
187  *
188  * For instance, imagine a graph. You want to compare it to its Delaunay triangulation.
189  * You need to create a subgraph that is a clone of the original (say this is A) to keep the original graph,
190  * and another copy (say this one is B) on which you will perform the delaunay triangulation.
191  *
192  * B will have none of the original edges, and A will have only the original edges.
193  *
194  * As for properties; let's imagine the same graph hierarchy.
195  * You want to compare two different layouts on the same graph.
196  * You need to create two clone subgraphs, on each you make the 'viewLayout' property local.
197  * This results in A and B having different values for the layout, but everything else in common.
198  * You then can apply two different algorithms on A and B (e.g. Bubble Tree and Tree Radial).
199  *
200  * @chapter Meta Nodes
201  * A meta node is a node representing a subgraph of the current graph.
202  *
203  * @chapter Undo Redo
204  * The Tulip Graph object supports for undo and redo of modifications.
205  *The operations affect the whole graph hierarchy, and cannot be limited to a subgraph.
206  *
207  */
208 class TLP_SCOPE Graph : public Observable {
209 
210  friend class GraphAbstract;
211  friend class GraphUpdatesRecorder;
212  friend class GraphDecorator;
213  friend class PropertyManager;
214  friend class PropertyInterface;
215 
216 public:
217  Graph():id(0) {}
218  virtual ~Graph() {}
219 
220  /**
221  * @brief Applies an algorithm plugin, identified by its name.
222  * Algorithm plugins are subclasses of the tlp::Algorithm interface.
223  * Parameters are transmitted to the algorithm trough the DataSet.
224  * To determine a plugin's parameters, you can either:
225  *
226  * * refer to its documentation
227  *
228  * * use buildDefaultDataSet on the plugin object if you have an instance of it
229  *
230  * * call getPluginParameters() with the name of the plugin on the PluginLister
231  *
232  *
233  * If an error occurs, a message describing the error should be stored in errorMessage.
234  *
235  * @param algorithm The algorithm to apply.
236  * @param errorMessage A string that will be modified to contain an error message should an error occur.
237  * @param dataSet The parameters to the algorithm. Defaults to NULL.
238  * @param progress A PluginProgress to report the progress of the operation, as well as final state. Defaults to NULL.
239  * @return bool Whether the algorithm was successfully applied.
240  **/
241  bool applyAlgorithm(const std::string &algorithm, std::string &errorMessage, DataSet *dataSet=NULL, PluginProgress *progress=NULL);
242 
243  //=========================================================================
244  // Graph hierarchy access and building
245  //=========================================================================
246 
247  /**
248  * @brief Removes all nodes, edges and sub-graphs from this graph.
249  *
250  * Contrarily to creating a new Graph, this keeps attributes and properties.
251  *
252  * @return void
253  **/
254  virtual void clear()=0;
255 
256  /**
257  * @brief Creates and returns a new sub-graph of this graph.
258  *
259  * If a BooleanProperty is provided, all the nodes and edges for which it is true will be added to the subgraph.
260  * If none is provided, then the subgraph will be empty.
261  *
262  * The id parameter should only be provided if you know exactly what you are doing; as Tulip will manage the subgraphs IDs when left to 0.
263  * It is only used by the Graph loading as subgraphs ids are preserved when saving/loading a Graph.
264  *
265  * @param selection The elements to add to the new subgraph. Defaults to 0.
266  * @param name The name of the newly created subgraph. Defaults to "unnamed".
267  * @return :Graph* The newly created subgraph.
268  **/
269  virtual Graph *addSubGraph(BooleanProperty *selection=NULL,
270  const std::string& name = "unnamed")=0;
271 
272  /**
273  * @brief Creates and returns a new named sub-graph of this graph.
274  *
275  * @param name The name of the newly created subgraph.
276  * @return :Graph* The newly created subgraph.
277  **/
278  Graph *addSubGraph(const std::string& name);
279 
280  /**
281  * @brief Creates and returns a subgraph that contains all the elements of this graph.
282  *
283  * @param name The name of the newly created subgraph. Defaults to "unnamed".
284  * @param addSibling if true the clone subgraph will be a sibling of this graph, if false (the default) it will be a subgraph of this graph
285  * @return :Graph* The newly created clone subgraph. NULL will be returned if addSibling is set to true and this graph is a root graph.
286  **/
287  virtual Graph* addCloneSubGraph(const std::string& name = "unnamed", bool addSibling = false);
288 
289  /**
290  * @brief Creates and returns a new sub-graph of the graph induced by a set of nodes.
291  * Every node contained in the given set of nodes is added to the subgraph.
292  * Every edge connecting any two nodes in the set of given nodes is also added.
293  * @param nodeSet The nodes to add to the subgraph. All the edges between these nodes are added too.
294  * @param parentSubGraph If provided, is used as parent graph for the newly created subgraph instead of the graph this method is called on.
295  * @return The newly created subgraph.
296  */
297  Graph *inducedSubGraph(const std::set<node>& nodeSet,
298  Graph* parentSubGraph = NULL);
299 
300  /**
301  * @brief Deletes a sub-graph of this graph.
302  * All subgraphs of the removed graph are re-parented to this graph.
303  * For instance, with a graph hierarchy as follows :
304  * root
305  * / \
306  * A B
307  * /|\
308  * C D E
309  *
310  * @code root->delSubGraph(B);
311  * would result in the following hierarchy:
312  * root
313  * / | \\
314  * A C D E
315  *
316  * @param graph The subgraph to delete.
317  *
318  * @see delAllSubGraphs() if you want to remove all descendants of a graph.
319  */
320  virtual void delSubGraph(Graph *graph)=0;
321 
322  /**
323  * @brief Deletes a sub-graph of this graph and all of its sub-graphs.
324  ** For instance, with a graph hierarchy as follows :
325  * root
326  * / \
327  * A B
328  * /|\
329  * C D E
330  *
331  * @codeline root->delSubGraph(B); @endcode
332  * would result in the following hierarchy:
333  * root
334  * |
335  * A
336  *
337  * @param graph The subgraph to delete.
338  * @see delSubGraph() if you want to keep the descendants of the subgraph to remove.
339  */
340  virtual void delAllSubGraphs(Graph *graph)=0;
341 
342  /**
343  * @brief Returns the parent of the graph. If called on the root graph, it returns itself.
344  * @return The parent of this graph (or itself if it is the root graph).
345  * @see getRoot() to directly retrieve the root graph.
346  */
347  virtual Graph* getSuperGraph()const =0;
348 
349  /**
350  * @brief Gets the root graph of the graph hierarchy.
351  * @return The root graph of the graph hierarchy.
352  */
353  virtual Graph* getRoot() const =0;
354 
355  /**
356  * @cond DOXYGEN_HIDDEN
357  * @brief Sets the parent of a graph.
358  * @warning ONLY USE IF YOU KNOW EXACTLY WHAT YOU ARE DOING.
359  * @endcond
360  */
361  virtual void setSuperGraph(Graph *)=0;
362 
363  /**
364  * @brief Gets an iterator over all the sub-graphs of the graph.
365  * For instance, in the following graph hierarchy:
366  ** root
367  * / \
368  * A B
369  * /|\
370  * C D E
371  *
372  * @codeline root->getSubGraphs(); @endcode
373  * Will return an iterator over A and B, but not C, D and E.
374  * @return An iterator over this graph's direct subgraphs.
375  */
376  virtual Iterator<Graph *> * getSubGraphs() const=0;
377 
378  /**
379  * @brief This method returns the nth subgraph.
380  * Since subgraphs order cannot be ensured in every implementation, this method should be equivalent to:
381  * @code
382  int i=0;
383  Iterator<Graph *> *it = g->getSubGraphs();
384  while (it->hasNext()) {
385  Graph *result = it->next();
386  if (i++ == n) {
387  delete it;
388  return result;
389  }
390  }
391  delete it;
392  return NULL;
393  * @endcode
394  * @param n the index of the subgraph to retrieve.
395  * @return The n-th subgraph.
396  */
397  virtual Graph *getNthSubGraph(unsigned int n) const;
398 
399  /**
400  * @brief Return the number of direct sub-graphs.
401  * For instance, in the following graph hierarchy:
402  * root
403  * / \
404  * A B
405  * /|\
406  * C D E
407  *
408  * @codeline root->numberOfSubGraphs(); @endcode
409  * Will return 2.
410  * @return The number of direct subgraphs.
411  * @see numberOfDescendantGraphs() to count in the whole hierarchy.
412  */
413  virtual unsigned int numberOfSubGraphs() const=0;
414 
415  /**
416  * @brief Return the number of descendant sub-graphs.
417  * For instance, in the following graph hierarchy:
418  * root
419  * / \
420  * A B
421  * /|\
422  * C D E
423  *
424  * @codeline root->numberOfSubGraphs(); @endcode
425  * Will return 5.
426  * @return The number of descendants subgraphs.
427  * @see numberOfSubGraphs() to count only direct subgraphs.
428  */
429  virtual unsigned int numberOfDescendantGraphs() const=0;
430 
431  /**
432  * @brief Indicates if the graph argument is a direct sub-graph.
433  * @param subGraph The graph to check is a subgraph of this graph.
434  * @return Whether subGraph is a direct subgraph of this graph.
435  * @see isDescendantGraph() to search in the whole hierarchy.
436  */
437  virtual bool isSubGraph(const Graph* subGraph) const=0;
438 
439  /**
440  * @brief Indicates if the graph argument is a descendant of this graph.
441  * @param subGraph The graph to check is a descendant of this graph.
442  * @return Whether subGraph is a descendant of this graph.
443  * @see isSubGraph to search only in direct subgraphs.
444  */
445  virtual bool isDescendantGraph(const Graph* subGraph) const=0;
446 
447  /**
448  * @brief Returns a pointer on the sub-graph with the corresponding id
449  * or NULL if there is no sub-graph with that id.
450  * @param id The id of the subgraph to retrieve.
451  * @return A subgraph of the given id, or null if no such subgraph exists on this graph.
452  * @see getDescendantGraph(unsigned int) to search in the whole hierarchy.
453  */
454  virtual Graph* getSubGraph(unsigned int id) const=0;
455 
456  /**
457  * @brief Returns a pointer on the sub-graph with the corresponding name
458  * or NULL if there is no sub-graph with that name.
459  * @param name The name of the subgraph to retrieve.
460  * @return A Graph named name, or NULL if no such subgraph exists on this graph.
461  * @see getDescendantGraph(const std::string &) to search in the whole hierarchy.
462  */
463  virtual Graph* getSubGraph(const std::string &name) const=0;
464 
465  /**
466  * @brief Returns a pointer on the descendant with the corresponding id
467  * or NULL if there is no descendant with that id.
468  * @param id The id of the descendant graph to retrieve.
469  * @return A graph with the given id, or NULL if no such graph exists in this graph's descendants.
470  * @see getSubGraph(unsigned int) to search only in direct subgraphs.
471  */
472  virtual Graph* getDescendantGraph(unsigned int id) const=0;
473 
474  /**
475  * @brief Returns a pointer on the first descendant graph with the corresponding name
476  * or NULL if there is no descendant graph with that name.
477  * @param name The name of the descendant graph to look for.
478  * @return A graph named name, or NULL if there is no such graph in this graph's descendants.
479  * @see getSubGraph(const std::string &) to search only in direct subgraphs.
480  */
481  virtual Graph* getDescendantGraph(const std::string &name) const=0;
482 
483  /**
484  * @brief Gets an iterator over all the descendant sub-graphs of the graph.
485  * For instance, in the following graph hierarchy:
486  ** root
487  * / \
488  * A B
489  * /|\
490  * C D E
491  *
492  * @codeline root->getSubGraphs(); @endcode
493  * Will return an iterator over A B, C, D and E.
494  * @return An iterator over this graph's descendant subgraphs.
495  */
496  Iterator<Graph *> * getDescendantGraphs() const;
497 
498  //==============================================================================
499  // Modification of the graph structure
500  //==============================================================================
501  /**
502  * @brief Adds a new node in the graph and returns it. This node is also added in all
503  * the ancestor graphs.
504  * @return The newly added node.
505  * @see addNodes() if you want to add more than one node.
506  */
507  virtual node addNode()=0;
508 
509  /**
510  * @brief Adds new nodes in the graph and returns them in the addedNodes vector.
511  * The new nodes are also added in all the ancestor graphs.
512  *
513  * @param nbNodes The number of nodes to add.
514  * @param addedNodes The newly added nodes. This vector is cleared before being filled.
515  * @see addNode() to add a single node.
516  */
517  virtual void addNodes(unsigned int nbNodes, std::vector<node>& addedNodes)=0;
518 
519  /**
520  * @brief Adds an existing node in the graph. This node is also added in all the ancestor graphs.
521  * This node must exists in the graph hierarchy (which means it must exist in the root graph).
522  * You cannot add a node to the root graph this way (as it must already be an element of the root graph).
523  * @warning Using this method on the root graph will display a warning on the console.
524  *
525  * @param n The node to add to a subgraph. This node must exist in the root graph.
526  * @see addNode() to add a new node to a graph.
527  */
528  virtual void addNode(const node n)=0;
529 
530  /**
531  * @brief Adds existing nodes in the graph. The nodes are also added in all the ancestor graphs.
532  * as with addNode(const tlp::node), the nodes must exist in the graph hierarchy and thus exist in the root graph,
533  * and node cannot be added this way to the root graph.
534 
535  * @warning Using this method on the root graph will display a warning on the console.
536  * @warning The graph does not take ownership of the Iterator.
537  *
538  * @param nodes An iterator over nodes to add to this subgraph. The graph does not takes ownership of this iterator.
539  */
540  virtual void addNodes(Iterator<node>* nodes)=0;
541 
542  /**
543  * @brief Adds existing nodes in the graph. The nodes are also added in all the ancestor graphs.
544  * as with addNode(const tlp::node), the nodes must exist in the graph hierarchy and thus exist in the root graph,
545  * and nodes cannot be added this way to the root graph.
546 
547  * @warning Using this method on the root graph will display a warning on the console.
548  *
549  * @param nodes a vector of nodes to add to this subgraph.
550  */
551  void addNodes(const std::vector<node>& nodes);
552 
553  /**
554  * @brief Deletes a node in the graph.
555  * This node is also removed in the sub-graphs hierarchy of the current graph.
556  * @param n The node to delete.
557  * @param deleteInAllGraphs Whether to delete in all its parent graphs or only in this graph. By default only removes in the current graph.
558  * @see delNodes() to remove multiple nodes.
559  */
560  virtual void delNode(const node n, bool deleteInAllGraphs = false)=0;
561 
562  /**
563  * @brief Deletes nodes in the graph.
564  * These nodes are also removed in the sub-graphs hierarchy of the current graph.
565  * @warning the graph does not take ownership of the Iterator.
566  * @param it The nodes to delete.
567  * @param deleteInAllGraphs Whether to delete in all its parent graphs or only in this graph. By default only removes in the current graph.
568  * @see delNode() to remove a single node.
569  */
570  virtual void delNodes(Iterator<node>* it, bool deleteInAllGraphs = false)=0;
571 
572  /**
573  * @brief Deletes nodes in the graph.
574  * These nodes are also removed in the sub-graphs hierarchy of the current graph.
575  * @warning the graph does not take ownership of the Iterator.
576  * @param nodes a vector of the nodes to delete.
577  * @param deleteInAllGraphs Whether to delete in all its parent graphs or only in this graph. By default only removes in the current graph.
578  * @see delNode() to remove a single node.
579  */
580  void delNodes(const std::vector<node>& nodes, bool deleteInAllGraphs = false);
581 
582  /**
583  * @brief Adds a new edge in the graph
584  * This edge is also added in all the super-graph of the graph.
585  * @param source The source of the edge.
586  * @param target The target of the edge.
587  * @return The newly added edge.
588  * @see addEdges() to add multiple edges at once.
589  */
590  virtual edge addEdge(const node source, const node target)=0;
591 
592  /**
593  * @brief Adds new edges in the graph and returns them the addedEdges vector.
594  * The new edges are also added in all the graph ancestors.
595  *
596  * @warning If the edges vector contains a node that does not belong to this graph,
597  * undefined behavior will ensue.
598  * @param edges A vector describing between which nodes to add edges.
599  * The first element of the pair is the source, the second is the destination.
600  * @param addedEdges The newly added edges. This vector is cleared before being filled.
601  *
602  */
603  virtual void addEdges(const std::vector<std::pair<node, node> >& edges,
604  std::vector<edge>& addedEdges)=0;
605 
606  /**
607  * @brief Adds an existing edge in the graph. This edge is also added in all
608  * the ancestor graphs.
609  * The edge must be an element of the graph hierarchy, thus it must be
610  * an element of the root graph.
611  * @warning Using this method on the root graph will display a warning on the console.
612  * @param e The edge to add to this subgraph.
613  * @see addEgdes() to add more than one edge at once.
614  * @see addNode() to add nodes.
615  */
616  virtual void addEdge(const edge e)=0;
617 
618  /**
619  * @brief Adds existing edges in the graph. The edges are also added in all
620  * the ancestor graphs.
621  * The added edges must be elements of the graph hierarchy,
622  * thus they must be elements of the root graph.
623  * @warning Using this method on the root graph will display a warning on the console.
624  * @warning The graph does not take ownership of the iterator.
625  * @param edges The edges to add on this subgraph.
626  */
627  virtual void addEdges(Iterator<edge>* edges)=0;
628 
629  /**
630  * @brief Adds existing edges in the graph. The edges are also added in all
631  * the ancestor graphs.
632  * The added edges must be elements of the graph hierarchy,
633  * thus they must be elements of the root graph.
634  * @warning Using this method on the root graph will display a warning on the console.
635  * @param edges a vector of the edges to add on this subgraph.
636  */
637  void addEdges(const std::vector<edge>& edges);
638 
639  /**
640  * @brief Deletes an edge in the graph. The edge is also removed in
641  * the sub-graphs hierarchy.
642  * The ordering of remaining edges is preserved.
643  * @param e The edge to delete.
644  * @param deleteInAllGraphs Whether to delete in all its parent graphs or only in this graph. By default only removes in the current graph.
645  */
646  virtual void delEdge(const edge e, bool deleteInAllGraphs = false)=0;
647 
648  /**
649  * @brief Deletes edges in the graph. These edges are also removed in the sub-graphs hierarchy.
650  * The ordering of remaining edges is preserved.
651  * @warning The graph does not take ownership of the Iterator.
652  * @param itE
653  * @param deleteInAllGraphs Whether to delete in all its parent graphs or only in this graph. By default only removes in the current graph.
654  */
655  virtual void delEdges(Iterator<edge>* itE, bool deleteInAllGraphs = false)=0;
656 
657  /**
658  * @brief Deletes edges in the graph. These edges are also removed in the sub-graphs hierarchy.
659  * The ordering of remaining edges is preserved.
660  * @warning The graph does not take ownership of the Iterator.
661  * @param edges a vector of the edges to delete
662  * @param deleteInAllGraphs Whether to delete in all its parent graphs or only in this graph. By default only removes in the current graph.
663  */
664  void delEdges(const std::vector<edge>& edges, bool deleteInAllGraphs = false);
665 
666  /**
667  * @brief Sets the order of the edges around a node.
668  * This operation ensures that adjacent edges of a node will
669  * be ordered as they are in the vector of edges given in parameter.
670  *
671  * This can be useful if you want to make sure you retrieve the edges in a specific order when iterating upon them.
672  * @param n The node whose edges to order.
673  * @param edges The edges, in the order you want them.
674  */
675  virtual void setEdgeOrder(const node n,const std::vector<edge> &edges)=0;
676 
677  /**
678  * @brief Swaps two edges in the adjacency list of a node.
679  * @param n The node on whoch to swap the edges order.
680  * @param e1 The first edge, that will take the second edge's position.
681  * @param e2 The second edge, that will take the first edge's position.
682  */
683  virtual void swapEdgeOrder(const node n,const edge e1, const edge e2)=0;
684 
685  /**
686  * @brief Sets the source of an edge to be the given node.
687  * @param e The edge to change the source of.
688  * @param source The new source of the edge.
689  */
690  virtual void setSource(const edge e, const node source) = 0;
691 
692  /**
693  * @brief Sets the target of an edge to be the given node.
694  * @param e The edge to change the target of.
695  * @param target The new target of the edge.
696  */
697  virtual void setTarget(const edge e, const node target) = 0;
698 
699  /**
700  * @brief Sets both the source and the target of an edge.
701  * @param e The edge to set the source and target of.
702  * @param source The new source of the edge.
703  * @param target The new target of the edge.
704  */
705  virtual void setEnds(const edge e, const node source, const node target) = 0;
706 
707  /**
708  * @brief Reverses the direction of an edge, the source becomes the target and the target
709  * becomes the source.
710  * @warning The ordering is global to the entire graph hierarchy. Thus, by changing
711  * the ordering of a graph you change the ordering of the hierarchy.
712  * @param e The edge top reverse.
713  */
714  virtual void reverse(const edge e)=0;
715  // Attempts to reserve enough space to store nodes.
716  // Only defined on root graph.
717  virtual void reserveNodes(unsigned int nbNodes) = 0;
718  // Attempts to reserve enough space to store edges.
719  // Only defined on root graph.
720  virtual void reserveEdges(unsigned int nbEdges) = 0;
721  //================================================================================
722  //Iterators on the graph structure.
723  //================================================================================
724  /**
725  * @brief Finds the first node whose input degree equals 0.
726  *
727  * @return tlp::node The first encountered node with input degree of 0, or an invalid node if none was found.
728  **/
729  virtual tlp::node getSource() const;
730 
731  /**
732  * @brief Returns the first node in the graph.
733  *
734  */
735  virtual node getOneNode() const =0;
736 
737  /**
738  * @brief Returns a random node in the graph.
739  *
740  * @since Tulip 4.8
741  *
742  */
743  virtual node getRandomNode() const =0;
744 
745  /**
746  * @brief Gets an iterator over this graph's nodes.
747  * @return An iterator over all the nodes of this graph.
748  * @see getInNodes()
749  * @see getOutNodes()
750  * @see getInOutNodes()
751  * @see getEdges()
752  */
753  virtual Iterator<node>* getNodes() const =0;
754 
755  /**
756  * @brief Gets the i-th node in the input nodes of a given node.
757  * An input node 'in' of a node 'N' is the source of an edge going from
758  * 'in' to 'N'. e.g.
759  * @code
760  * node in = graph->addNode();
761  * node N = graph->addNode();
762  * graph->addEdge(in, N);
763  * //in == graph->getInNode(N, 1);
764  * @endcode
765  *
766  * If you have 5 input nodes on a node N, then
767  * @codeline graph->getInNode(2); @endcode
768  * will return the second of those nodes.
769  * It will ignore the output nodes of this node.
770  * @param n The node to get an input node of.
771  * @param i The index of the input node to get.
772  * @return The i-th input node of the given node.
773  * @see getInNodes()
774  * @see getInEdges()
775  */
776  virtual node getInNode(const node n,unsigned int i)const =0;
777 
778  /**
779  * @brief Gets an iterator over the input nodes of a node.
780  * @param n The node to get the input nodes of.
781  * @return An iterator over the input nodes of a node.
782  * @see getInNode()
783  * @see getInOutNodes()
784  * @see getInEdges()
785  */
786  virtual Iterator<node>* getInNodes(const node n) const =0;
787 
788  /**
789  * @brief Gets the i-th node in the output nodes of a given node.
790  * An output node 'out' of a node 'N' is the target of an edge going from
791  * 'N' to 'out'. e.g.
792  * @code
793  * node N = graph->addNode();
794  * node out = graph->addNode();
795  * graph->addEdge(N, out);
796  * //out == graph->getOutNode(N, 1);
797  * @endcode
798  *
799  * If you have 5 output nodes on a node N, then
800  * @codeline graph->getOutNode(2); @endcode
801  * will return the second of those nodes.
802  * It will ignore the input nodes of this node.
803  * @param n The node to get an output node of.
804  * @param i The index of the output node to get.
805  * @return The i-th output node of the given node.
806  * @see getOutNodes()
807  * @see getOutEdges()
808  */
809  virtual node getOutNode(const node n,unsigned int i) const =0;
810 
811  /**
812  * @brief Gets an iterator over the output nodes of a node.
813  * @param n The node to get the output nodes of.
814  * @return An iterator over the output nodes of a node.
815  * @see getOutNode()
816  * @see getInOutNodes()
817  * @see getOutEdges()
818  */
819  virtual Iterator<node>* getOutNodes(const node n) const =0;
820 
821  /**
822  * @brief Gets an iterator over the neighbors of a given node.
823  * @param n The node to retrieve the neighbors of.
824  * @return An iterator over the node's neighbors.
825  */
826  virtual Iterator<node>* getInOutNodes(const node n) const =0;
827 
828  /**
829  * @brief Gets an iterator performing a breadth-first search on the graph.
830  * @param root The node from whom to start the BFS. If not provided, the root
831  * node will be assigned to a source node in the graph (node with input degree equals to 0).
832  * If there is no source node in the graph, a random node will be picked.
833  * @return A stable iterator over the graph nodes in the BFS order.
834  */
835  virtual Iterator<node>* bfs(const node root = node()) const = 0;
836 
837  /**
838  * @brief Gets an iterator performing a depth-first search on the graph.
839  * @param root The node from whom to start the DFS. If not provided, the root
840  * node will be assigned to a source node in the graph (node with input degree equals to 0).
841  * If there is no source node in the graph, a random node will be picked.
842  * @return A stable iterator over the graph nodes in the DFS order.
843  */
844  virtual Iterator<node>* dfs(const node root = node()) const = 0;
845 
846  /**
847  * @brief Gets the underlying graph of a meta node.
848  * @param metaNode The metanode.
849  * @return The Graph pointed to by the metanode.
850  * @see getEdgeMetaInfo()
851  */
852  virtual Graph* getNodeMetaInfo(const node metaNode) const = 0;
853 
854  /**
855  * @brief Get an iterator over all the graph's edges.
856  * @return An iterator over all the graph's edges.
857  * @see getInEdges()
858  * @see getOutEdges()
859  * @see getInOutEdges()
860  */
861  virtual Iterator<edge>* getEdges() const =0;
862 
863  /**
864  * @brief Returns the first edge in the graph.
865  *
866  */
867  virtual edge getOneEdge() const =0;
868 
869  /**
870  * @brief Returns a random edge in the graph.
871  *
872  * @since Tulip 4.8
873  *
874  */
875  virtual edge getRandomEdge() const =0;
876 
877  /**
878  * @brief Gets an iterator over the output edges of a node.
879  * @param n The node to get the output edges from.
880  * @return An iterator over the node's output edges.
881  * @see getEdges()
882  * @see getOutEdges()
883  * @see getInOutEdges()
884  */
885  virtual Iterator<edge>* getOutEdges(const node n) const =0;
886 
887  /**
888  * @brief Gets an iterator over the edges of a node.
889  * @param n The node to get the edges from.
890  * @return An iterator over the node's edges.
891  * @see getEdges()
892  * @see getOutEdges()
893  * @see getInEdges()
894  */
895  virtual Iterator<edge>* getInOutEdges(const node n) const =0;
896 
897  /**
898  * @brief Gets an iterator over the input edges of a node.
899  * @param n The node to get the input edges from.
900  * @return An iterator over the node's input edges.
901  * @see getEdges()
902  * @see getOutEdges()
903  * @see getInOutEdges()
904  */
905  virtual Iterator<edge>* getInEdges(const node n) const =0;
906 
907  /**
908  * @brief Gets an iterator over the edges composing a meta edge.
909  * @param metaEdge The metaEdge to get the real edges of.
910  * @return An Iterator over the edges composing the metaEdge.
911  * @see getNodeMetaInfo()
912  */
913  virtual Iterator<edge>* getEdgeMetaInfo(const edge metaEdge) const =0;
914 
915  //================================================================================
916  // Graph, nodes and edges information about the graph stucture
917  //================================================================================
918  /**
919  * @brief Gets the unique identifier of the graph.
920  * @return The unique identifier of this graph.
921  */
922  unsigned int getId() const {
923  return id;
924  }
925 
926  /**
927  * @brief Gets the number of nodes in this graph.
928  * @return The number of nodes in this graph.
929  * @see numberOfEdges()
930  */
931  virtual unsigned int numberOfNodes()const =0;
932 
933  /**
934  * @brief Gets the number of edges in this graph.
935  * @return The number of edges in this graph.
936  * @see numberOfNodes()
937  */
938  virtual unsigned int numberOfEdges()const =0;
939 
940  /**
941  * @param n The node to get the degree of.
942  * @return The degree of the given node.
943  */
944  virtual unsigned int deg(const node n)const =0;
945 
946  /**
947  * @brief Get the input degree of a node.
948  * @param n The node to get the input degree of.
949  * @return The input degree of the given node.
950  */
951  virtual unsigned int indeg(const node n)const =0;
952 
953  /**
954  * @brief Get the output degree of a node.
955  * @param n The node to get the output degree of.
956  * @return The output degree of the given node.
957  */
958  virtual unsigned int outdeg(const node n)const =0;
959 
960  /**
961  * @brief Gets the source of an edge.
962  * @param e The edge to get the source of.
963  * @return The source of the given edge.
964  */
965  virtual node source(const edge e)const =0;
966 
967  /**
968  * @brief Gets the target of an edge.
969  * @param e The edge to get the target of.
970  * @return The target of the given edge.
971  */
972  virtual node target(const edge e)const =0;
973 
974  /**
975  * @brief Gets the source and the target of an edge.
976  * @param e The edge to get the ends of.
977  * @return A pair whose first element is the source, and second is the target.
978  */
979  virtual const std::pair<node, node>& ends(const edge e)const=0;
980 
981  /**
982  * @brief Gets the opposite node of n through e.
983  * @param e The edge linking the two nodes.
984  * @param n The node at one end of e.
985  * @return The node at the other end of e.
986  */
987  virtual node opposite(const edge e, const node n)const =0;
988 
989  /**
990  * @brief Checks if an element belongs to this graph.
991  * @param n The node to check if it is an element of the graph.
992  * @return Whether or not the element belongs to the graph.
993  */
994  virtual bool isElement(const node n) const =0;
995 
996  /**
997  * @brief Checks if a node is a meta node.
998  * @param n The node to check if it is a meta node.
999  * @return Whether or not the node is a meta node.
1000  */
1001  virtual bool isMetaNode(const node n) const =0;
1002 
1003  /**
1004  * @brief Checks if an element belongs to this graph.
1005  * @param e The edge to check if it is an element of the graph.
1006  * @return Whether or not the element belongs to the graph.
1007  */
1008  virtual bool isElement(const edge e) const =0;
1009 
1010  /**
1011  * @brief Checks if an edge is a meta edge.
1012  * @param e The edge to check if it is a meta edge.
1013  * @return Whether or not the edge is a meta edge.
1014  */
1015  virtual bool isMetaEdge(const edge e) const =0;
1016 
1017  /**
1018  * @brief Checks if an edge exists between two given nodes.
1019  * @param source The source of the hypothetical edge.
1020  * @param target The target of the hypothetical edge.
1021  * @param directed When set to false edges from target to source are also considered
1022  * @return true if such an edge exists
1023  */
1024  virtual bool hasEdge(const node source, const node target,
1025  bool directed = true) const {
1026  return existEdge(source, target, directed).isValid();
1027  }
1028 
1029 
1030  /**
1031  * @brief Returns all the edges between two nodes.
1032  * @param source The source of the hypothetical edges.
1033  * @param target The target of the hypothetical edges.
1034  * @param directed When set to false edges from target to source are also considered
1035  * @return a vector of existing edges
1036  */
1037  virtual std::vector<edge> getEdges(const node source, const node target,
1038  bool directed = true) const=0;
1039 
1040  /**
1041  * @brief Returns the first edge found between the two given nodes.
1042  * @warning This function always returns an edge,
1043  * you need to check if this edge is valid with edge::isValid().
1044  * @param source The source of the hypothetical edge.
1045  * @param target The target of the hypothetical edge.
1046  * @param directed When set to false
1047  * an edge from target to source may also be returned
1048  * @return An edge that is only valid if it exists.
1049  */
1050  virtual edge existEdge(const node source, const node target,
1051  bool directed = true) const=0;
1052 
1053  //================================================================================
1054  // Access to the graph attributes and to the node/edge property.
1055  //================================================================================
1056  /**
1057  * @brief Sets the name of the graph.
1058  * The name does not have to be unique, it is used for convenience.
1059  * @param name The new name of the graph.
1060  */
1061  virtual void setName(const std::string &name) = 0;
1062 
1063  /**
1064  * @brief Retrieves the name of the graph.
1065  * @return The name of the graph.
1066  */
1067  virtual std::string getName() const = 0;
1068 
1069  /**
1070  * @brief Gets the attributes of the graph.
1071  *
1072  * The attributes contains the name and any user-defined value.
1073  * @return The attributes of the graph.
1074  */
1075  const DataSet & getAttributes() const {
1076  return (const_cast<Graph *>(this))->getNonConstAttributes();
1077  }
1078 
1079  /**
1080  * @brief Gets an attribute on the graph.
1081  * @param name The name of the attribute to set.
1082  * @param value The value to set.
1083  * @return Whether the setting of the attribute was sucessful.
1084  */
1085  template<typename ATTRIBUTETYPE>
1086  bool getAttribute(const std::string &name, ATTRIBUTETYPE& value) const;
1087 
1088  /**
1089  * @brief Gets a copy of the attribute.
1090  * @param name The name of the attribute to retrieve.
1091  * @return A copy of the attribute to retrieve.
1092  */
1093  DataType* getAttribute(const std::string& name) const;
1094 
1095  /**
1096  * @brief Sets an attribute on the graph.
1097  * @param name The name of the attribute to set.
1098  * @param value The value to set on this attribute.
1099  */
1100  template<typename ATTRIBUTETYPE>
1101  void setAttribute(const std::string &name,const ATTRIBUTETYPE &value);
1102 
1103  /**
1104  * @brief Sets an attribute on the graph.
1105  * @param name The name of the attribute to set.
1106  * @param value The value to set.
1107  */
1108  void setAttribute(const std::string &name, const DataType* value);
1109 
1110  /**
1111  * @brief Removes an attribute on the graph.
1112  * @param name The name of the attribute to remove.
1113  */
1114  void removeAttribute(const std::string &name) {
1115  notifyRemoveAttribute(name);
1116  getNonConstAttributes().remove(name);
1117  }
1118 
1119  /**
1120  * @brief Checks if an attribute exists.
1121  * @param name The name of the attribute to check for.
1122  * @return Whether the attribute exists.
1123  */
1124  bool attributeExist(const std::string &name) {
1125  return getAttributes().exist(name);
1126  }
1127 
1128  /**
1129  * @brief Adds a property to the graph.
1130  * The graph takes ownership of the property. If you want to delete it, use
1131  * Graph::delLocalProperty().
1132  * @param name The unique identifier of the property.
1133  * @param prop The property to add.
1134  */
1135  virtual void addLocalProperty(const std::string &name, PropertyInterface *prop)=0;
1136 
1137  /**
1138  * @brief Gets an existing property.
1139  * In DEBUG mode an assertion checks the existence of the property.
1140  *
1141  * The graph keeps ownership of the property, if you wish to remove it from the graph use
1142  * Graph::delLocalProperty().
1143  *
1144  * @param name The unique identifier of the property.
1145  * @return An existing property, or NULL if no property with the given name exists.
1146  */
1147  virtual PropertyInterface* getProperty(const std::string& name) const = 0;
1148 
1149  /**
1150  * @brief Gets a property on this graph.
1151  * The name of a property indentifies it uniquely.
1152  * Either there already exists a property with the given name, in which case it is returned.
1153  * Either no such porperty exists and it is created.
1154  *
1155  * The graph keeps ownership of the property, if you wish to remove it fgrom the graph use
1156  * Graph::delLocalProperty().
1157  * @warning using the wrong template parameter will cause a segmentation fault.
1158  * @param The unique identifier of the property.
1159  * @return The property of given name.
1160  */
1161  template<typename PropertyType>
1162  PropertyType* getLocalProperty(const std::string &name);
1163 
1164  /**
1165  * @brief Gets a property on this graph or one of its ancestors.
1166  * If the property already exists on the graph or in one of its ancestors, it is returned.
1167  * Otherwise a new property is created on this graph.
1168  *
1169  * The graph keeps ownership of the property, if you wish to remove it from the graph use
1170  * Graph::delLocalProperty().
1171  *
1172  * @warning using the wrong propertyType will result in a segmentation fault. Using an invalid property type will always return NULL.
1173  * @param name The unique identifier of the property.
1174  * @return An existing property, or a new one if none exists with the given name.
1175  */
1176  template<typename PropertyType>
1177  PropertyType* getProperty(const std::string &name);
1178 
1179  /**
1180  * @brief Gets a property on this graph, and this graph only.
1181  * This forwards the call to the template version of getLocalProperty(), with the correct template parameter deduced from the propertyType parameter.
1182  *
1183  * The graph keeps ownership of the property, if you wish to remove it from the graph use
1184  * Graph::delLocalProperty().
1185  *
1186  * @warning using the wrong propertyType will result in a segmentation fault. Using an invalid property type will always return NULL.
1187  * @param propertyName The unique identifier of the property.
1188  * @param propertyType A string describing the type of the property.
1189  * @return The property of given name.
1190  * @see getLocalProperty().
1191  */
1192  PropertyInterface *getLocalProperty(const std::string& propertyName, const std::string& propertyType);
1193 
1194  /**
1195  * @brief Gets a property on this graph or one of its ancestors.
1196  * This forwards the call to the template version of getProperty(), with the correct template parameter deduced from the propertyType parameter.
1197  *
1198  * The graph keeps ownership of the property, if you wish to remove it from the graph use
1199  * Graph::delLocalProperty().
1200  *
1201  * @warning using the wrong propertyType will result in a segmentation fault. Using an invalid property type will always return NULL.
1202  * @param propertyName The unique identifier of the property.
1203  * @param propertyType A string describing the type of the property.
1204  * @return The property of given name.
1205  * @see getProperty().
1206  */
1207  PropertyInterface *getProperty(const std::string& propertyName, const std::string& propertyType);
1208 
1209  /**
1210  * @brief Checks if a property exists in this graph or one of its ancestors.
1211  * @param The unique identifier of the property.
1212  * @return Whether a property with the given name exists.
1213  */
1214  virtual bool existProperty(const std::string& name) const = 0;
1215 
1216  /**
1217  * @brief Checks if a property exists in this graph.
1218  * @param The unique identifier of the property.
1219  * @return Whether a property with the given name exists.
1220  */
1221  virtual bool existLocalProperty(const std::string& name) const = 0;
1222 
1223  /**
1224  * @brief Removes and deletes a property from this graph.
1225  * The property is removed from the graph's property pool, meaning its name can now be used by another property.
1226  * The object is deleted and the memory freed.
1227  * @param name The unique identifier of the property.
1228  */
1229  virtual void delLocalProperty(const std::string& name)=0;
1230 
1231  /**
1232  * @brief Gets an iterator over the names of the local properties of this graph.
1233  * @return An iterator over this graph's properties names.
1234  */
1235  virtual Iterator<std::string>* getLocalProperties() const=0;
1236 
1237  /**
1238  * @brief Gets an iterator over the local properties of this graph.
1239  * @return An iterator over this graph's properties.
1240  */
1241  virtual Iterator<PropertyInterface*>* getLocalObjectProperties() const=0;
1242 
1243  /**
1244  * @brief Gets an iterator over the names of the properties inherited from this graph's ancestors,
1245  * excluding this graph's local properties.
1246  * @return An iterator over the names of the properties this graph inherited.
1247  */
1248  virtual Iterator<std::string>* getInheritedProperties() const=0;
1249 
1250  /**
1251  * @brief Gets an iterator over the properties inherited from this graph's ancestors,
1252  * excluding this graph's local properties.
1253  * @return An iterator over the properties this graph inherited.
1254  */
1255  virtual Iterator<PropertyInterface*>* getInheritedObjectProperties() const=0;
1256 
1257  /**
1258  * @brief Gets an iterator over the names of all the properties attached to this graph,
1259  * whether they are local or inherited.
1260  * @return An iterator over the names of all the properties attached to this graph.
1261  */
1262  virtual Iterator<std::string>* getProperties() const=0;
1263 
1264  /**
1265  * @brief Gets an iterator over the properties attached to this graph,
1266  * whether they are local or inherited.
1267  * @return An iterator over all of the properties attached to this graph.
1268  */
1269  virtual Iterator<PropertyInterface*>* getObjectProperties() const=0;
1270 
1271  /**
1272  * @brief Runs a plugin on the graph, whose result is a property.
1273  *
1274  * @param algorithm The name of the plugin to run.
1275  * @param result The property in which to put the results. All previous values will be erased.
1276  * @param errorMessage Stores the error message if the plugin fails.
1277  * @param progress A means of feedback during the plugin execution. Some plugins support being stopped or cancelled through this.
1278  * @param parameters The parameters of the algorithm. Some algorithms use this DataSet to output some additional information.
1279  * @return Whether the plugin executed successfully or not. If not, check the error message.
1280  *
1281  * @see PluginLister::getPluginParameters() to retrieve the list of default parameters for the pligin.
1282  */
1283  bool applyPropertyAlgorithm(const std::string &algorithm,
1284  PropertyInterface* result,
1285  std::string &errorMessage,
1286  PluginProgress *progress=NULL,
1287  DataSet *parameters=NULL);
1288 
1289  // updates management
1290  /**
1291  * @brief Saves the current state of the whole graph hierarchy and allows to revert to this state later on, using pop().
1292  * All modifications except those altering the ordering of the edges will be undone.
1293  *
1294  * This allows to undo/redo modifications on a graph.
1295  * This is mostly useful from a user interface point of view, but some algorithms use this mechanism to clean up before finishing.
1296  * For instance:
1297  * @code
1298  * Graph* graph = tlp::newGraph();
1299  * DoubleProperty* prop = graph->getProperty<DoubleProperty>("metric");
1300  * string errorMessage;
1301  *
1302  * //our super metric stuff we want to kee
1303  * DoubleProperty* superProperty = graph->getProperty<DoubleProperty>("superStuff");
1304  * vector<PropertyInterface*> propertiesToKeep;
1305  * propertiesToKeep.push_back(superProperty);
1306  *
1307  *
1308  * //apply some metric
1309  * graph->applyPropertyAlgorithm("Degree", prop, errorMessage);
1310  *
1311  * // save this state to be able to revert to it later
1312  * //however we do not want to allow to unpop(), which would go forward again to the state where prop contains 'Depth'.
1313  * //this saves some memory.
1314  * //Also we always want to keep the value of our super property, so we pass it in the collection of properties to leave unaffected by the pop().
1315  * graph->push(false, propertiesToKeep);
1316  *
1317  * //compute the quality of this metric, or whatever makes sense
1318  * int degreeQuality = prop->getMax();
1319  *
1320  * //compute another metric
1321  * graph->applyPropertyAlgorithm("Depth", prop, errorMessage);
1322  *
1323  * //compute our secret metric, that depends on depth
1324  * graph->applyPropertyAlgorithm("MySuperSecretAlgorithm", superProperty, errorMessage);
1325  *
1326  * //compute its quality
1327  * int depthQuality = prop->getMax();
1328  *
1329  * //if the degree was better, revert back to the state where its contents were in prop.
1330  * if(degreeQuality > depthQuality) {
1331  * //this does not affect superProperty, as we told the system not to consider it when recording modifications to potentially revert.
1332  * graph->pop();
1333  * }
1334  *
1335  * //do some stuff using our high quality metric
1336  * ColorProperty* color = graph->getProperty("viewColor");
1337  * graph->applyPropertyAlgorithm("Color Mapping", color, errorMessage);
1338  *
1339  * @endcode
1340  *
1341  * @param unpopAllowed Whether or not to allow to re-do the modifications once they are undone.
1342  * @param propertiesToPreserveOnPop A collection of properties whose state to preserve when using pop().
1343  * @see pop()
1344  * @see unpop()
1345  * @see canPop()
1346  * @see canUnPop()
1347  * @see canPopThenUnPop()
1348  */
1349  virtual void push(bool unpopAllowed = true,
1350  std::vector<PropertyInterface*>* propertiesToPreserveOnPop= NULL)=0;
1351 
1352  /**
1353  * @brief Undoes modifications and reverts the whole graph hierarchy back to a previous state.
1354  *
1355  * @param unpopAllowed Whether or not it is possible to redo what will be undoe by this call.
1356  */
1357  virtual void pop(bool unpopAllowed = true)=0;
1358 
1359  /**
1360  * @brief Re-perform actions that were undone using pop().
1361  *
1362  * For instance:
1363  * @code
1364  * DoubleProperty* prop = graph->getProperty<DoubleProperty>("metric");
1365  * string errorMessage;
1366  *
1367  * //apply some metric
1368  * graph->applyPropertyAlgorithm("Degree", prop, errorMessage);
1369  *
1370  * // save this state to be able to revert to it later
1371  * graph->push();
1372  *
1373  * //compute the quality of this metric, or whatever makes sense
1374  * int degreeQuality = prop->getMax();
1375  *
1376  * //compute another metric
1377  * graph->applyPropertyAlgorithm("Depth", prop, errorMessage);
1378  *
1379  * //compute its quality
1380  * int depthQuality = prop->getMax();
1381  *
1382  * //if the degree was better, revert back to the state where its contents were in prop.
1383  * if(degreeQuality > depthQuality) {
1384  * graph->pop();
1385  * }
1386  *
1387  * ...
1388  *
1389  * //revert back to the depth for some reason.
1390  * graph->unpop();
1391  * @endcode
1392  */
1393  virtual void unpop()=0;
1394 
1395  /**
1396  * @brief Checks if there is a state to revert to.
1397  * @return Whether there was a previous call to push() that was not yet pop()'ed.
1398  */
1399  virtual bool canPop()=0;
1400 
1401  /**
1402  * @brief Checks if the last undone modifications can be redone.
1403  * @return Whether it is possible to re-do modifications that have been undone by pop().
1404  */
1405  virtual bool canUnpop()=0;
1406 
1407  /**
1408  * @brief Checks if it is possible to call pop() and then unPop(), to undo then re-do modifications.
1409  * @return Whether it is possible to undo and then redo.
1410  */
1411  virtual bool canPopThenUnpop()=0;
1412 
1413  // meta nodes management
1414  /**
1415  * @brief Creates a meta-node from a set of nodes.
1416  * Every edges from any node in the set to another node of the graph will be replaced with meta edges
1417  * from the meta node to the other nodes.
1418  * @warning This method will fail when called on the root graph.
1419  *
1420  * @param nodeSet The nodes to put into the meta node.
1421  * @param multiEdges Whether a meta edge should be created for each underlying edge.
1422  * @param delAllEdge Whether the underlying edges will be removed from the whole hierarchy.
1423  * @return The newly created meta node.
1424  */
1425  node createMetaNode(const std::set<node> &nodeSet, bool multiEdges = true, bool delAllEdge = true);
1426 
1427  /**
1428  * @brief Populates a quotient graph with one meta node
1429  * for each iterated graph.
1430  *
1431  * @param itS a Graph iterator, (typically a subgraph iterator)
1432  * @param quotientGraph the graph that will contain the meta nodes
1433  * @param metaNodes will contains all the added meta nodes after the call
1434  *
1435  */
1436  void createMetaNodes(Iterator<Graph *> *itS, Graph *quotientGraph,
1437  std::vector<node>& metaNodes);
1438  /**
1439  * @brief Closes an existing subgraph into a metanode. Edges from nodes
1440  * in the subgraph to nodes outside the subgraph are replaced with
1441  * edges from the metanode to the nodes outside the subgraph.
1442  * @warning this method will fail when called on the root graph.
1443  *
1444  * @param subGraph an existing subgraph
1445  * @param multiEdges indicates if a meta edge will be created for each underlying edge
1446  * @param delAllEdge indicates if the underlying edges will be removed from the entire hierarchy
1447  */
1448  node createMetaNode(Graph* subGraph, bool multiEdges = true, bool delAllEdge = true);
1449 
1450  /**
1451  * @brief Opens a metanode and replaces all edges between that
1452  * meta node and other nodes in the graph.
1453  *
1454  * @warning this method will fail when called on the root graph.
1455  *
1456  * @param n The meta node to open.
1457  * @param updateProperties If set to true, open meta node will update inner nodes layout, color, size, etc
1458  */
1459  void openMetaNode(node n, bool updateProperties=true);
1460 
1461 protected:
1462  virtual DataSet &getNonConstAttributes() = 0;
1463  // designed to reassign an id to a previously deleted elt
1464  // used by GraphUpdatesRecorder
1465  virtual node restoreNode(node)=0;
1466  virtual void restoreNodes(const std::vector<node>& nodes)=0;
1467  virtual edge restoreEdge(edge, node source, node target)=0;
1468  virtual void restoreEdges(const std::vector<edge>& edges,
1469  const std::vector<std::pair<node, node> >& ends)=0;
1470  // designed to only update own structures
1471  // used by GraphUpdatesRecorder
1472  virtual void removeNode(const node)=0;
1473  virtual void removeEdge(const edge)=0;
1474 
1475  // to check if a property can be deleted
1476  // used by PropertyManager
1477  virtual bool canDeleteProperty(Graph* g, PropertyInterface *prop) {
1478  return getRoot()->canDeleteProperty(g, prop);
1479  }
1480 
1481  // local property renaming
1482  // can failed if a property with the same name already exists
1483  virtual bool renameLocalProperty(PropertyInterface* prop,
1484  const std::string& newName)=0;
1485 
1486  // internally used to deal with sub graph deletion
1487  virtual void removeSubGraph(Graph*)=0;
1488  virtual void clearSubGraphs()=0;
1489  virtual void restoreSubGraph(Graph*)=0;
1490  virtual void setSubGraphToKeep(Graph*)=0;
1491 
1492  // for notification of GraphObserver
1493  void notifyAddNode(const node n);
1494  void notifyAddNode(Graph*, const node n) {
1495  notifyAddNode(n);
1496  }
1497  void notifyAddEdge(const edge e);
1498  void notifyAddEdge(Graph*, const edge e) {
1499  notifyAddEdge(e);
1500  }
1501  void notifyBeforeSetEnds(const edge e);
1502  void notifyBeforeSetEnds(Graph*, const edge e) {
1503  notifyBeforeSetEnds(e);
1504  }
1505  void notifyAfterSetEnds(const edge e);
1506  void notifyAfterSetEnds(Graph*, const edge e) {
1507  notifyAfterSetEnds(e);
1508  }
1509  void notifyDelNode(const node n);
1510  void notifyDelNode(Graph*, const node n) {
1511  notifyDelNode(n);
1512  }
1513  void notifyDelEdge(const edge e);
1514  void notifyDelEdge(Graph*, const edge e) {
1515  notifyDelEdge(e);
1516  }
1517  void notifyReverseEdge(const edge e);
1518  void notifyReverseEdge(Graph*, const edge e) {
1519  notifyReverseEdge(e);
1520  }
1521  void notifyBeforeAddSubGraph(const Graph*);
1522  void notifyAfterAddSubGraph(const Graph*);
1523  void notifyBeforeAddSubGraph(Graph*, const Graph* sg) {
1524  notifyBeforeAddSubGraph(sg);
1525  }
1526  void notifyAfterAddSubGraph(Graph*, const Graph* sg) {
1527  notifyAfterAddSubGraph(sg);
1528  }
1529  void notifyBeforeDelSubGraph(const Graph*);
1530  void notifyAfterDelSubGraph(const Graph*);
1531  void notifyBeforeDelSubGraph(Graph*, const Graph* sg) {
1532  notifyBeforeDelSubGraph(sg);
1533  }
1534  void notifyAfterDelSubGraph(Graph*, const Graph* sg) {
1535  notifyAfterDelSubGraph(sg);
1536  }
1537 
1538  void notifyBeforeAddDescendantGraph(const Graph*);
1539  void notifyAfterAddDescendantGraph(const Graph*);
1540  void notifyBeforeDelDescendantGraph(const Graph*);
1541  void notifyAfterDelDescendantGraph(const Graph*);
1542 
1543  void notifyBeforeAddLocalProperty(const std::string&);
1544  void notifyAddLocalProperty(const std::string&);
1545  void notifyAddLocalProperty(Graph*, const std::string& name) {
1546  notifyAddLocalProperty(name);
1547  }
1548  void notifyBeforeDelLocalProperty(const std::string&);
1549  void notifyAfterDelLocalProperty(const std::string&);
1550  void notifyDelLocalProperty(Graph*, const std::string& name) {
1551  notifyBeforeDelLocalProperty(name);
1552  }
1553  void notifyBeforeSetAttribute(const std::string&);
1554  void notifyBeforeSetAttribute(Graph*, const std::string& name) {
1555  notifyBeforeSetAttribute(name);
1556  }
1557  void notifyAfterSetAttribute(const std::string&);
1558  void notifyAfterSetAttribute(Graph*, const std::string& name) {
1559  notifyAfterSetAttribute(name);
1560  }
1561  void notifyRemoveAttribute(const std::string&);
1562  void notifyRemoveAttribute(Graph*, const std::string& name) {
1563  notifyRemoveAttribute(name);
1564  }
1565  void notifyDestroy();
1566  void notifyDestroy(Graph*) {
1567  notifyDestroy();
1568  }
1569 
1570  unsigned int id;
1571  TLP_HASH_MAP<std::string, tlp::PropertyInterface*> circularCalls;
1572 };
1573 
1574 /**
1575  * @ingroup Observation
1576  * Event class for specific events on Graph
1577  **/
1578 class TLP_SCOPE GraphEvent :public Event {
1579 public:
1580  // be careful about the ordering of the constants
1581  // in the enum below because it is used in some assertions
1582  enum GraphEventType {
1583  TLP_ADD_NODE = 0,
1584  TLP_DEL_NODE = 1,
1585  TLP_ADD_EDGE = 2,
1586  TLP_DEL_EDGE = 3,
1587  TLP_REVERSE_EDGE = 4,
1588  TLP_BEFORE_SET_ENDS = 5,
1589  TLP_AFTER_SET_ENDS = 6,
1590  TLP_ADD_NODES = 7,
1591  TLP_ADD_EDGES = 8,
1592  TLP_BEFORE_ADD_DESCENDANTGRAPH = 9,
1593  TLP_AFTER_ADD_DESCENDANTGRAPH = 10,
1594  TLP_BEFORE_DEL_DESCENDANTGRAPH = 11,
1595  TLP_AFTER_DEL_DESCENDANTGRAPH = 12,
1596  TLP_BEFORE_ADD_SUBGRAPH = 13,
1597  TLP_AFTER_ADD_SUBGRAPH = 14,
1598  TLP_BEFORE_DEL_SUBGRAPH = 15,
1599  TLP_AFTER_DEL_SUBGRAPH = 16,
1600  TLP_ADD_LOCAL_PROPERTY = 17,
1601  TLP_BEFORE_DEL_LOCAL_PROPERTY = 18,
1602  TLP_AFTER_DEL_LOCAL_PROPERTY = 19,
1603  TLP_ADD_INHERITED_PROPERTY = 20,
1604  TLP_BEFORE_DEL_INHERITED_PROPERTY = 21,
1605  TLP_AFTER_DEL_INHERITED_PROPERTY = 22,
1606  TLP_BEFORE_RENAME_LOCAL_PROPERTY = 23,
1607  TLP_AFTER_RENAME_LOCAL_PROPERTY = 24,
1608  TLP_BEFORE_SET_ATTRIBUTE = 25,
1609  TLP_AFTER_SET_ATTRIBUTE = 26,
1610  TLP_REMOVE_ATTRIBUTE = 27,
1611  TLP_BEFORE_ADD_LOCAL_PROPERTY = 28,
1612  TLP_BEFORE_ADD_INHERITED_PROPERTY = 29
1613  };
1614 
1615  // constructor for node/edge events
1616  GraphEvent(const Graph& g, GraphEventType graphEvtType, unsigned int id,
1617  Event::EventType evtType = Event::TLP_MODIFICATION)
1618  : Event(g, evtType), evtType(graphEvtType) {
1619  info.eltId = id;
1620  }
1621  // constructor for nodes events
1622  GraphEvent(const Graph& g, GraphEventType graphEvtType,
1623  const std::vector<node>& nodes,
1624  Event::EventType evtType = Event::TLP_MODIFICATION)
1625  : Event(g, evtType), evtType(graphEvtType) {
1626  info.nodes = &nodes;
1627  }
1628  // constructor for edges events
1629  GraphEvent(const Graph& g, GraphEventType graphEvtType,
1630  const std::vector<edge>& edges,
1631  Event::EventType evtType = Event::TLP_MODIFICATION)
1632  : Event(g, evtType), evtType(graphEvtType) {
1633  info.edges = &edges;
1634  }
1635  // constructor for subgraph events
1636  GraphEvent(const Graph& g, GraphEventType graphEvtType,
1637  const Graph* sg)
1638  : Event(g, Event::TLP_MODIFICATION), evtType(graphEvtType) {
1639  info.subGraph = sg;
1640  }
1641 
1642  // constructor for attribute/property events
1643  GraphEvent(const Graph& g, GraphEventType graphEvtType,
1644  const std::string& str,
1645  Event::EventType evtType = Event::TLP_MODIFICATION)
1646  : Event(g, evtType), evtType(graphEvtType) {
1647  info.name = new std::string(str);
1648  }
1649 
1650  // constructor for rename property events
1651  GraphEvent(const Graph& g, GraphEventType graphEvtType,
1652  PropertyInterface* prop,
1653  const std::string& newName)
1654  : Event(g, Event::TLP_MODIFICATION), evtType(graphEvtType) {
1655  info.renamedProp =
1656  new std::pair<PropertyInterface*,std::string>(prop, newName);
1657  }
1658 
1659  // destructor needed to cleanup name if any
1660  ~GraphEvent() {
1661  if (evtType > TLP_AFTER_DEL_SUBGRAPH) {
1662  if (evtType == TLP_BEFORE_RENAME_LOCAL_PROPERTY ||
1663  evtType == TLP_AFTER_RENAME_LOCAL_PROPERTY)
1664  delete info.renamedProp;
1665  else
1666  delete info.name;
1667  }
1668  }
1669 
1670  Graph* getGraph() const {
1671  return static_cast<Graph *>(sender());
1672  }
1673 
1674  node getNode() const {
1675  assert(evtType < TLP_ADD_EDGE);
1676  return node(info.eltId);
1677  }
1678 
1679  edge getEdge() const {
1680  assert(evtType > TLP_DEL_NODE && evtType < TLP_ADD_NODES);
1681  return edge(info.eltId);
1682  }
1683 
1684  const std::vector<node>& getNodes() const {
1685  assert(evtType == TLP_ADD_NODES);
1686  return *(info.nodes);
1687  }
1688 
1689  const std::vector<edge>& getEdges() const {
1690  assert(evtType == TLP_ADD_EDGES);
1691  return *(info.edges);
1692  }
1693 
1694  const Graph* getSubGraph() const {
1695  assert(evtType > TLP_ADD_EDGES && evtType < TLP_ADD_LOCAL_PROPERTY);
1696  return info.subGraph;
1697  }
1698 
1699  const std::string& getAttributeName() const {
1700  assert(evtType > TLP_AFTER_DEL_INHERITED_PROPERTY);
1701  return *(info.name);
1702  }
1703 
1704  const std::string& getPropertyName() const;
1705 
1706  PropertyInterface* getProperty() const {
1707  assert(evtType == TLP_BEFORE_RENAME_LOCAL_PROPERTY ||
1708  evtType == TLP_AFTER_RENAME_LOCAL_PROPERTY);
1709  return info.renamedProp->first;
1710  }
1711 
1712  const std::string& getPropertyNewName() const {
1713  assert(evtType == TLP_BEFORE_RENAME_LOCAL_PROPERTY);
1714  return info.renamedProp->second;
1715  }
1716 
1717  const std::string& getPropertyOldName() const {
1718  assert(evtType == TLP_AFTER_RENAME_LOCAL_PROPERTY);
1719  return info.renamedProp->second;
1720  }
1721 
1722  GraphEventType getType() const {
1723  return evtType;
1724  }
1725 
1726 protected:
1727  GraphEventType evtType;
1728  union {
1729  unsigned int eltId;
1730  const Graph* subGraph;
1731  std::string* name;
1732  const std::vector<node>* nodes;
1733  const std::vector<edge>* edges;
1734  std::pair<PropertyInterface*, std::string>* renamedProp;
1735  } info;
1736 };
1737 
1738 }
1739 
1740 ///Print the graph (only nodes and edges) in ostream, in the tulip format
1741 TLP_SCOPE std::ostream& operator<< (std::ostream &,const tlp::Graph *);
1742 
1743 //================================================================================
1744 // these functions allow to use tlp::Graph as a key in a hash-based data structure (e.g. hashmap).
1745 //================================================================================
1746 #ifndef DOXYGEN_NOTFOR_DEVEL
1747 
1748 TLP_BEGIN_HASH_NAMESPACE {
1749  template <>
1750  struct TLP_SCOPE hash<const tlp::Graph *> {
1751  size_t operator()(const tlp::Graph *s) const {return size_t(s->getId());}
1752  };
1753  template <>
1754  struct TLP_SCOPE hash<tlp::Graph *> {
1755  size_t operator()(tlp::Graph *s) const {return size_t(s->getId());}
1756  };
1757 } TLP_END_HASH_NAMESPACE
1758 
1759 #endif // DOXYGEN_NOTFOR_DEVEL
1760 #include "cxx/Graph.cxx"
1761 #endif
A graph property that maps a boolean value to graph elements.
Graph * newGraph()
Creates a new, empty graph.
PropertyInterface describes the interface of a graph property.
void copyToGraph(Graph *outG, const Graph *inG, BooleanProperty *inSelection=NULL, BooleanProperty *outSelection=NULL)
virtual bool hasEdge(const node source, const node target, bool directed=true) const
Checks if an edge exists between two given nodes.
Definition: Graph.h:1024
Describes a value of any type.
Definition: DataSet.h:57
A container that can store data from any type.
Definition: DataSet.h:172
bool attributeExist(const std::string &name)
Checks if an attribute exists.
Definition: Graph.h:1124
bool saveGraph(Graph *graph, const std::string &filename, tlp::PluginProgress *progress=NULL)
Saves the corresponding graph to a file (extension can be any of the Tulip supported ouput graph file...
const DataSet & getAttributes() const
Gets the attributes of the graph.
Definition: Graph.h:1075
Graph * importGraph(const std::string &format, DataSet &dataSet, PluginProgress *progress=NULL, Graph *newGraph=NULL)
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:39
The node struct represents a node in a Graph object.
Definition: Node.h:39
Iterator< Graph * > * getRootGraphs()
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:47
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...
void removeAttribute(const std::string &name)
Removes an attribute on the graph.
Definition: Graph.h:1114
unsigned int getId() const
Gets the unique identifier of the graph.
Definition: Graph.h:922
bool exportGraph(Graph *graph, std::ostream &outputStream, const std::string &format, DataSet &dataSet, PluginProgress *progress=NULL)
Exports a graph using the specified export plugin with parameters stored in the DataSet.
The Observable class is the base of Tulip's observation system.
Definition: Observable.h:123
void removeFromGraph(Graph *ioG, BooleanProperty *inSelection=NULL)
Graph * loadGraph(const std::string &filename, tlp::PluginProgress *progress=NULL)
Loads a graph from a file (extension can be any of the Tulip supported input graph file format)...