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