20 #ifndef Tulip_SUPERGRAPH_H
21 #define Tulip_SUPERGRAPH_H
28 #include <unordered_map>
31 #include <tulip/tulipconf.h>
32 #include <tulip/DataSet.h>
33 #include <tulip/Node.h>
34 #include <tulip/Edge.h>
35 #include <tulip/Observable.h>
39 class PropertyInterface;
40 class BooleanProperty;
101 TLP_SCOPE
bool saveGraph(Graph *graph,
const std::string &filename,
121 TLP_SCOPE
bool exportGraph(Graph *graph, std::ostream &outputStream,
const std::string &format,
122 DataSet &dataSet, PluginProgress *progress =
nullptr);
142 TLP_SCOPE Graph *
importGraph(
const std::string &format, DataSet &dataSet,
143 PluginProgress *progress =
nullptr, Graph *
newGraph =
nullptr);
165 TLP_SCOPE
void copyToGraph(Graph *outG,
const Graph *inG, BooleanProperty *inSelection =
nullptr,
166 BooleanProperty *outSelection =
nullptr);
174 TLP_SCOPE
void removeFromGraph(Graph *ioG, BooleanProperty *inSelection =
nullptr);
236 friend class GraphAbstract;
237 friend class GraphUpdatesRecorder;
238 friend class GraphDecorator;
239 friend class PropertyManager;
270 bool applyAlgorithm(
const std::string &algorithm, std::string &errorMessage,
284 virtual void clear() = 0;
298 const std::string &name =
"unnamed") = 0;
306 Graph *addSubGraph(
const std::string &name);
320 virtual Graph *addCloneSubGraph(
const std::string &name =
"unnamed",
bool addSibling =
false,
321 bool addSiblingProperties =
false);
334 Graph *inducedSubGraph(
const std::vector<node> &nodes,
Graph *parentSubGraph =
nullptr,
335 const std::string &name =
"unnamed");
351 const std::string &name =
"unnamed");
373 virtual void delSubGraph(
Graph *graph) = 0;
393 virtual void delAllSubGraphs(
Graph *graph =
nullptr) = 0;
400 virtual Graph *getSuperGraph()
const = 0;
406 virtual Graph *getRoot()
const = 0;
414 virtual void setSuperGraph(
Graph *) = 0;
436 virtual const std::vector<Graph *> &subGraphs()
const = 0;
458 virtual Graph *getNthSubGraph(
unsigned int n)
const;
474 virtual unsigned int numberOfSubGraphs()
const = 0;
490 virtual unsigned int numberOfDescendantGraphs()
const = 0;
498 virtual bool isSubGraph(
const Graph *subGraph)
const = 0;
506 virtual bool isDescendantGraph(
const Graph *subGraph)
const = 0;
515 virtual Graph *getSubGraph(
unsigned int id)
const = 0;
524 virtual Graph *getSubGraph(
const std::string &name)
const = 0;
534 virtual Graph *getDescendantGraph(
unsigned int id)
const = 0;
543 virtual Graph *getDescendantGraph(
const std::string &name)
const = 0;
569 virtual node addNode() = 0;
578 virtual void addNodes(
unsigned int nbNodes) = 0;
588 virtual void addNodes(
unsigned int nbNodes, std::vector<node> &addedNodes) = 0;
600 virtual void addNode(
const node n) = 0;
626 void addNodes(
const std::vector<node> &nodes);
636 virtual void delNode(
const node n,
bool deleteInAllGraphs =
false) = 0;
647 virtual void delNodes(
Iterator<node> *it,
bool deleteInAllGraphs =
false) = 0;
658 void delNodes(
const std::vector<node> &nodes,
bool deleteInAllGraphs =
false);
668 virtual edge addEdge(
const node source,
const node target) = 0;
680 virtual void addEdges(
const std::vector<std::pair<node, node>> &edges) = 0;
693 virtual void addEdges(
const std::vector<std::pair<node, node>> &edges,
694 std::vector<edge> &addedEdges) = 0;
706 virtual void addEdge(
const edge e) = 0;
727 void addEdges(
const std::vector<edge> &edges);
737 virtual void delEdge(
const edge e,
bool deleteInAllGraphs =
false) = 0;
747 virtual void delEdges(
Iterator<edge> *itE,
bool deleteInAllGraphs =
false) = 0;
757 void delEdges(
const std::vector<edge> &edges,
bool deleteInAllGraphs =
false);
769 virtual void setEdgeOrder(
const node n,
const std::vector<edge> &edges) = 0;
777 virtual void swapEdgeOrder(
const node n,
const edge e1,
const edge e2) = 0;
784 virtual void setSource(
const edge e,
const node source) = 0;
791 virtual void setTarget(
const edge e,
const node target) = 0;
799 virtual void setEnds(
const edge e,
const node source,
const node target) = 0;
808 virtual void reverse(
const edge e) = 0;
811 virtual void reserveNodes(
unsigned int nbNodes) = 0;
814 virtual void reserveEdges(
unsigned int nbEdges) = 0;
838 virtual node getOneNode()
const = 0;
846 virtual node getRandomNode()
const = 0;
853 virtual const std::vector<node> &nodes()
const = 0;
859 virtual unsigned int nodePos(
const node n)
const = 0;
892 virtual node getInNode(
const node n,
unsigned int i)
const = 0;
925 virtual node getOutNode(
const node n,
unsigned int i)
const = 0;
968 virtual Graph *getNodeMetaInfo(
const node metaNode)
const = 0;
975 virtual const std::vector<edge> &edges()
const = 0;
981 virtual unsigned int edgePos(
const edge e)
const = 0;
996 virtual edge getOneEdge()
const = 0;
1004 virtual edge getRandomEdge()
const = 0;
1043 virtual const std::vector<edge> &allEdges(
const node n)
const = 0;
1058 virtual void sortElts() = 0;
1076 return nodes().empty();
1084 virtual unsigned int numberOfNodes()
const = 0;
1091 virtual unsigned int numberOfEdges()
const = 0;
1097 virtual unsigned int deg(
const node n)
const = 0;
1104 virtual unsigned int indeg(
const node n)
const = 0;
1111 virtual unsigned int outdeg(
const node n)
const = 0;
1118 virtual node source(
const edge e)
const = 0;
1125 virtual node target(
const edge e)
const = 0;
1132 virtual const std::pair<node, node> &ends(
const edge e)
const = 0;
1140 virtual node opposite(
const edge e,
const node n)
const = 0;
1147 virtual bool isElement(
const node n)
const = 0;
1154 virtual bool isMetaNode(
const node n)
const = 0;
1161 virtual bool isElement(
const edge e)
const = 0;
1168 virtual bool isMetaEdge(
const edge e)
const = 0;
1178 return existEdge(source, target, directed).isValid();
1188 virtual std::vector<edge> getEdges(
const node source,
const node target,
1189 bool directed =
true)
const = 0;
1201 virtual edge existEdge(
const node source,
const node target,
bool directed =
true)
const = 0;
1211 virtual void setName(
const std::string &name) = 0;
1217 virtual std::string getName()
const = 0;
1226 return const_cast<Graph *
>(
this)->getNonConstAttributes();
1235 template <
typename ATTRIBUTETYPE>
1236 bool getAttribute(
const std::string &name, ATTRIBUTETYPE &value)
const;
1243 DataType *getAttribute(
const std::string &name)
const;
1250 template <
typename ATTRIBUTETYPE>
1251 void setAttribute(
const std::string &name,
const ATTRIBUTETYPE &value);
1258 void setAttribute(
const std::string &name,
const DataType *value);
1265 notifyRemoveAttribute(name);
1266 getNonConstAttributes().remove(name);
1275 return getAttributes().exists(name);
1285 virtual void addLocalProperty(
const std::string &name,
PropertyInterface *prop) = 0;
1311 template <
typename PropertyType>
1312 PropertyType *getLocalProperty(
const std::string &name);
1327 template <
typename PropertyType>
1328 PropertyType *getProperty(
const std::string &name);
1346 const std::string &propertyType);
1363 PropertyInterface *getProperty(
const std::string &propertyName,
const std::string &propertyType);
1370 virtual bool existProperty(
const std::string &name)
const = 0;
1377 virtual bool existLocalProperty(
const std::string &name)
const = 0;
1386 virtual void delLocalProperty(
const std::string &name) = 0;
1444 bool applyPropertyAlgorithm(
const std::string &algorithm,
PropertyInterface *result,
1445 std::string &errorMessage,
DataSet *parameters =
nullptr,
1515 virtual void push(
bool unpopAllowed =
true,
1516 std::vector<PropertyInterface *> *propertiesToPreserveOnPop =
nullptr) = 0;
1523 virtual void pop(
bool unpopAllowed =
true) = 0;
1528 virtual void popIfNoUpdates() = 0;
1564 virtual void unpop() = 0;
1570 virtual bool canPop() = 0;
1576 virtual bool canUnpop() = 0;
1583 virtual bool canPopThenUnpop() = 0;
1598 virtual node createMetaNode(
const std::vector<node> &nodes,
bool multiEdges =
true,
1599 bool delAllEdge =
true);
1611 std::vector<node> &metaNodes);
1622 virtual node createMetaNode(
Graph *subGraph,
bool multiEdges =
true,
bool delAllEdge =
true);
1634 void openMetaNode(
node n,
bool updateProperties =
true);
1638 virtual DataSet &getNonConstAttributes() = 0;
1641 virtual void restoreNode(
node) = 0;
1642 virtual void restoreEdge(
edge,
node source,
node target) = 0;
1645 virtual void removeNode(
const node) = 0;
1646 virtual void removeEdge(
const edge) = 0;
1651 return getRoot()->canDeleteProperty(g, prop);
1656 virtual bool renameLocalProperty(PropertyInterface *prop,
const std::string &newName) = 0;
1659 virtual void removeSubGraph(Graph *) = 0;
1660 virtual void clearSubGraphs() = 0;
1661 virtual void restoreSubGraph(Graph *) = 0;
1662 virtual void setSubGraphToKeep(Graph *) = 0;
1665 void notifyAddNode(
const node n);
1666 void notifyAddNode(Graph *,
const node n) {
1669 void notifyAddEdge(
const edge e);
1670 void notifyAddEdge(Graph *,
const edge e) {
1673 void notifyBeforeSetEnds(
const edge e);
1674 void notifyBeforeSetEnds(Graph *,
const edge e) {
1675 notifyBeforeSetEnds(e);
1677 void notifyAfterSetEnds(
const edge e);
1678 void notifyAfterSetEnds(Graph *,
const edge e) {
1679 notifyAfterSetEnds(e);
1681 void notifyDelNode(
const node n);
1682 void notifyDelNode(Graph *,
const node n) {
1685 void notifyDelEdge(
const edge e);
1686 void notifyDelEdge(Graph *,
const edge e) {
1689 void notifyReverseEdge(
const edge e);
1690 void notifyReverseEdge(Graph *,
const edge e) {
1691 notifyReverseEdge(e);
1693 void notifyBeforeAddSubGraph(
const Graph *);
1694 void notifyAfterAddSubGraph(
const Graph *);
1695 void notifyBeforeAddSubGraph(Graph *,
const Graph *sg) {
1696 notifyBeforeAddSubGraph(sg);
1698 void notifyAfterAddSubGraph(Graph *,
const Graph *sg) {
1699 notifyAfterAddSubGraph(sg);
1701 void notifyBeforeDelSubGraph(
const Graph *);
1702 void notifyAfterDelSubGraph(
const Graph *);
1703 void notifyBeforeDelSubGraph(Graph *,
const Graph *sg) {
1704 notifyBeforeDelSubGraph(sg);
1706 void notifyAfterDelSubGraph(Graph *,
const Graph *sg) {
1707 notifyAfterDelSubGraph(sg);
1710 void notifyBeforeAddDescendantGraph(
const Graph *);
1711 void notifyAfterAddDescendantGraph(
const Graph *);
1712 void notifyBeforeDelDescendantGraph(
const Graph *);
1713 void notifyAfterDelDescendantGraph(
const Graph *);
1715 void notifyBeforeAddLocalProperty(
const std::string &);
1716 void notifyAddLocalProperty(
const std::string &);
1717 void notifyAddLocalProperty(Graph *,
const std::string &name) {
1718 notifyAddLocalProperty(name);
1720 void notifyBeforeDelLocalProperty(
const std::string &);
1721 void notifyAfterDelLocalProperty(
const std::string &);
1722 void notifyDelLocalProperty(Graph *,
const std::string &name) {
1723 notifyBeforeDelLocalProperty(name);
1725 void notifyBeforeSetAttribute(
const std::string &);
1726 void notifyBeforeSetAttribute(Graph *,
const std::string &name) {
1727 notifyBeforeSetAttribute(name);
1729 void notifyAfterSetAttribute(
const std::string &);
1730 void notifyAfterSetAttribute(Graph *,
const std::string &name) {
1731 notifyAfterSetAttribute(name);
1733 void notifyRemoveAttribute(
const std::string &);
1734 void notifyRemoveAttribute(Graph *,
const std::string &name) {
1735 notifyRemoveAttribute(name);
1737 void notifyDestroy();
1738 void notifyDestroy(Graph *) {
1743 std::unordered_map<std::string, tlp::PropertyInterface *> circularCalls;
1755 enum GraphEventType {
1760 TLP_REVERSE_EDGE = 4,
1761 TLP_BEFORE_SET_ENDS = 5,
1762 TLP_AFTER_SET_ENDS = 6,
1765 TLP_BEFORE_ADD_DESCENDANTGRAPH = 9,
1766 TLP_AFTER_ADD_DESCENDANTGRAPH = 10,
1767 TLP_BEFORE_DEL_DESCENDANTGRAPH = 11,
1768 TLP_AFTER_DEL_DESCENDANTGRAPH = 12,
1769 TLP_BEFORE_ADD_SUBGRAPH = 13,
1770 TLP_AFTER_ADD_SUBGRAPH = 14,
1771 TLP_BEFORE_DEL_SUBGRAPH = 15,
1772 TLP_AFTER_DEL_SUBGRAPH = 16,
1773 TLP_ADD_LOCAL_PROPERTY = 17,
1774 TLP_BEFORE_DEL_LOCAL_PROPERTY = 18,
1775 TLP_AFTER_DEL_LOCAL_PROPERTY = 19,
1776 TLP_ADD_INHERITED_PROPERTY = 20,
1777 TLP_BEFORE_DEL_INHERITED_PROPERTY = 21,
1778 TLP_AFTER_DEL_INHERITED_PROPERTY = 22,
1779 TLP_BEFORE_RENAME_LOCAL_PROPERTY = 23,
1780 TLP_AFTER_RENAME_LOCAL_PROPERTY = 24,
1781 TLP_BEFORE_SET_ATTRIBUTE = 25,
1782 TLP_AFTER_SET_ATTRIBUTE = 26,
1783 TLP_REMOVE_ATTRIBUTE = 27,
1784 TLP_BEFORE_ADD_LOCAL_PROPERTY = 28,
1785 TLP_BEFORE_ADD_INHERITED_PROPERTY = 29
1789 GraphEvent(
const Graph &g, GraphEventType graphEvtType,
unsigned int id,
1790 Event::EventType evtType = Event::TLP_MODIFICATION)
1791 :
Event(g, evtType), evtType(graphEvtType) {
1792 if (graphEvtType == TLP_ADD_NODES || graphEvtType == TLP_ADD_EDGES)
1797 vectInfos.addedNodes =
nullptr;
1801 :
Event(g, Event::TLP_MODIFICATION), evtType(graphEvtType) {
1803 vectInfos.addedNodes =
nullptr;
1807 GraphEvent(
const Graph &g, GraphEventType graphEvtType,
const std::string &str,
1808 Event::EventType evtType = Event::TLP_MODIFICATION)
1809 :
Event(g, evtType), evtType(graphEvtType) {
1810 info.name =
new std::string(str);
1811 vectInfos.addedNodes =
nullptr;
1816 const std::string &newName)
1817 :
Event(g, Event::TLP_MODIFICATION), evtType(graphEvtType) {
1818 info.renamedProp =
new std::pair<PropertyInterface *, std::string>(prop, newName);
1819 vectInfos.addedNodes =
nullptr;
1824 Graph *getGraph()
const {
1825 return static_cast<Graph *
>(sender());
1828 node getNode()
const {
1829 assert(evtType < TLP_ADD_EDGE);
1830 return node(info.eltId);
1833 edge getEdge()
const {
1834 assert(evtType > TLP_DEL_NODE && evtType < TLP_ADD_NODES);
1835 return edge(info.eltId);
1838 const std::vector<node> &getNodes()
const;
1840 unsigned int getNumberOfNodes()
const {
1841 assert(evtType == TLP_ADD_NODES);
1845 const std::vector<edge> &getEdges()
const;
1847 unsigned int getNumberOfEdges()
const {
1848 assert(evtType == TLP_ADD_EDGES);
1852 const Graph *getSubGraph()
const {
1853 assert(evtType > TLP_ADD_EDGES && evtType < TLP_ADD_LOCAL_PROPERTY);
1854 return info.subGraph;
1857 const std::string &getAttributeName()
const {
1858 assert(evtType > TLP_AFTER_DEL_INHERITED_PROPERTY);
1859 return *(info.name);
1862 const std::string &getPropertyName()
const;
1865 assert(evtType == TLP_BEFORE_RENAME_LOCAL_PROPERTY ||
1866 evtType == TLP_AFTER_RENAME_LOCAL_PROPERTY);
1867 return info.renamedProp->first;
1870 const std::string &getPropertyNewName()
const {
1871 assert(evtType == TLP_BEFORE_RENAME_LOCAL_PROPERTY);
1872 return info.renamedProp->second;
1875 const std::string &getPropertyOldName()
const {
1876 assert(evtType == TLP_AFTER_RENAME_LOCAL_PROPERTY);
1877 return info.renamedProp->second;
1880 GraphEventType getType()
const {
1885 GraphEventType evtType;
1888 const Graph *subGraph;
1890 unsigned int nbElts;
1891 std::pair<PropertyInterface *, std::string> *renamedProp;
1894 std::vector<node> *addedNodes;
1895 std::vector<edge> *addedEdges;
1901 TLP_SCOPE std::ostream &operator<<(std::ostream &,
const tlp::Graph *);
1903 #include "cxx/Graph.cxx"