1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
|
<chapter id="tulip-library"><title>Tulip Library</title>
<sect1 id="tulip-library-intro"><title>Introduction</title>
<para>Efficient visualization of graphs and their usage for data analysis implies
<itemizedlist>
<listitem><para>the manipulation of the structure of the graph</para></listitem>
<listitem><para>the extraction of parts of it</para></listitem>
<listitem><para>association of values to a graph's elements (nodes and edges)</para></listitem>
<listitem><para>the computation of intrinsic parameters (parameters derived from a graph's structure,
e.g. in a filesystem graph (directory tree): number of files in a directory, can be computed by counting
outgoing edges)</para></listitem>
<listitem><para>the computation of extrinsic parameters (parameters derived from external information,
e.g. in a filesystem graph: file size)</para></listitem>
</itemizedlist>
</para>
<para>
This chapter describes the Tulip data structure that takes into account all the requirement
of a graph visualization system. For each part we describe the general principle and
then we give examples explaining how to do it with the Tulip library.
</para>
</sect1>
<sect1 id="graphs"><title>Graphs</title>
<para>The core of the Tulip library provides an interface for the manipulation of graphs. It enables one
to access and modify the structure of a graph. The aim of this library is to be as general
as possible and thus it manipulates a general class of graphs called directed pseudo-graphs.
In a pseudo graph, there can be more than one edge between two nodes, and loops are permitted. A loop is an
edge that links a node to itself. Furthermore, edges are directed, thus an edge u->v is distinct from an edge v->u.
</para>
<para>
Because we use pseudo-graphs, there can be more than one edge u->v, so it is not possible to
distinguish two edges using only source and target (u,v). To make this possible, all the elements in Tulip
are entities (C++ objects). Thus, even if two edges have the same source and the same target, they are distinct.
</para>
<para>
The elements of a graph are encapsulated in the graph. It is therefore not possible to access
the graph's structure through elements, all operations must be done by querying the graph.
For example, to know the source of an edge e of graph G, one must ask G, not e, what e's source is.
This makes the use of the library less intuitive, but it minimizes memory usage for entities and allows to
share them between subgraphs. Building a container of elements is cheap, because to handle elements,
Tulip uses objects which use the same amount of storage as integers.
</para>
<para>
The library supports access and modification of the graph structure. The access to the structure are made by using iterators,
one very important point is that the iterator are not persistent. Thus, if one modify the graph structure all the iterators
on the graph structure can be invalid. This property enables to prevent from cloning the data structure and thus enables
better access to it. For ease of use, Tulip includes mechanism that enables to transform an iterator into stable iterator,
one must keep in mind that it corresponds to clone the data structure and thus, it should be use only if it is necessary.
</para>
<para>
If one uses Tulip only for the manipulation of one graph (no graph hierarchy), the list of available operations on the
graph is given afterward. In the next section we will enhance the set of operations and the actions that they perform
in order to manage a hierarchy of subgraphs
<itemizedlist>
<title>List of available modification operations</title>
<listitem><para><methodname>node addNode()</methodname> : creates a new node in the graph and returns its identifier.</para></listitem>
<listitem><para>
<methodname>edge addEdge(node,node)</methodname> : create a new edge in the graph, given the source and target. </para></listitem>
<listitem><para><methodname>void delNode(node)</methodname> : deletes the given node.</para></listitem>
<listitem><para><methodname>void delEdge(edge)</methodname> : deletes the given edge.</para></listitem>
<listitem><para><methodname>void reverse(edge)</methodname> : reverses an edge (swaps source and target).</para></listitem>
</itemizedlist>
<itemizedlist>
<title>List of available access operations</title>
<listitem><para><methodname>unsigned int deg(node)</methodname> : returns the degree of a node (number of edges).</para></listitem>
<listitem><para><methodname>unsigned int indeg(node)</methodname> : returns the in degree of a node (number of times it is a target).</para></listitem>
<listitem><para><methodname>unsigned int outdeg(node)</methodname> : returns the out degree of a node (number of times it is a source).</para></listitem>
<listitem><para><methodname>node source(edge)</methodname> : returns the source of an edge.</para></listitem>
<listitem><para><methodname>node target(edge)</methodname> : returns the target of an edge.</para></listitem>
<listitem><para><methodname>node opposite(edge,node)</methodname> : it enables to obtain the opposite of a node of an edge.</para></listitem>
<listitem><para><methodname>Iterator * getInNodes(node)</methodname> : returns an iterator on the predecessor nodes of a node.</para></listitem>
<listitem><para><methodname>Iterator * getOutNodes(node)</methodname> : returns an iterator on the successor nodes of a node.</para></listitem>
<listitem><para><methodname>Iterator * getInOutNodes(node)</methodname> : returns an iterator on the neighbor nodes of a node.</para></listitem>
<listitem><para><methodname>Iterator * getInEdges(node)</methodname> : returns an iterator on the predecessor edges of a node.</para></listitem>
<listitem><para><methodname>Iterator * getOutEdges(node)</methodname> : returns an iterator on the successor edges of a node.</para></listitem>
<listitem><para><methodname>Iterator * getInOutEdges(node)</methodname> : returns an iterator on the neighbor edges of a node.</para></listitem>
</itemizedlist>
</para>
</sect1>
<sect1 id="hierarchy-graphs"><title>Hierarchy of graphs</title>
<para> The Tulip library can also manage subgraphs. By definition a subgraph G' of a graph G is part (subset)
of the elements of G such that G' is also a graph (all sources and targets of the edges of G' must be in G').
As a subgraph is also a graph it can itself contain subgraphs. In such a hierarchy,
if a graph G" is a descendant of a graph G, G" is also a subgraph of G.
</para>
<para> One of the strong point of Tulip is to ensure efficiently that all elements are shared between graphs in a hierarchy
of graphs. Thus, if a node n is an element of a graph G and of a graph G', the entity n is the same in both graphs. Of course,
the parameters of the entity can change between graphs. For instance, the degree of n can be smaller for a subgraph,
as it can have less edges.
</para>
<para> The subgraph relation in the hierarchy is preserved when one modifies a graph. This means that
if one adds a node to a graph, this node is automatically added to all its ancestors as well. If one deletes a
node, this node is automatically deleted from all the descendants of the graph. If one reverses an edge, this edge is
reversed in all the graphs of the hierarchy.
</para>
<para> In order to manipulate a hierarchy of graphs, more functions have been added to those introduced above.
They provide navigation and modification for the hierarchy. The access to the hierarchy is provided by iterators, which
are not persistent and thus, if the hierarchy is modified, the iterators are invalid.
<itemizedlist>
<title>List of available modification operations</title>
<listitem><para><methodname>Graph *addSubGraph()</methodname> : returns an empty subgraph of this graph.</para></listitem>
<listitem><para><methodname>Graph *delSubGraph(Graph *)</methodname> : deletes a subgraph. Its descendants
continue to be descendants of this graph.</para></listitem>
<listitem><para><methodname>Graph *delAllSubGraph(Graph *)</methodname> : deletes a subgraph and all its descendants.</para></listitem>
<listitem><para><methodname>edge addEdge(edge)</methodname> : adds an edge element from another graph in the hierarchy.</para></listitem>
<listitem><para><methodname>void addNode(node)</methodname> : adds a node element from another graph in the hierarchy.</para></listitem>
</itemizedlist>
<itemizedlist>
<title>List of available access operations</title>
<listitem><para><methodname>Iterator * getSubGraphs()</methodname> : returns an iterator on the subgraphs.</para></listitem>
<listitem><para><methodname>Graph * getSuperGraph()</methodname> : returns the parent of the graph. If the graph has
no parent, it returns the graph itself.</para></listitem>
</itemizedlist>
</para>
</sect1>
<sect1 id="attributes"><title>Attributes</title>
<para>
An attributes is a kind of property that can be associated to a graph. An attributes has a name (a string) and a value of any type. It can be, for example ,the name of a graph, or a date of creation of the graph.
</para>
<para>
Attributes can be added and accessed with those three following member functions :
</para>
<itemizedlist>
<listitem><para><code>const DataSet getAttributes()</code> : returns the attributes of a graph.
</para></listitem>
<listitem><para><code>template<typename ATTRIBUTETYPE>bool getAttribute(const std::string &name, ATTRIBUTETYPE &value)</code> : get an attribute.
</para></listitem>
<listitem><para><code> template<typename ATTRIBUTETYPE>void setAttribute (const std::string &name, const ATTRIBUTETYPE &value) </code> : set a new attribute value.
</para></listitem>
</itemizedlist>
</sect1>
<sect1 id="properties"><title>Properties</title>
<para> In Tulip, a property is an attribute of an element of a graph. It is called a property in
order to prevent confusion with attributes of a graph: properties are for elements and attributes are for graphs.
In Tulip, a property is always defined for both kinds of elements (nodes and edges), so one can always query
for the value of the property associated with any edge or node.
</para>
<para> To access the value of an elements one must query the graph for a property. This makes
the use of the library less intuitive, but it minimizes memory usage for properties.
</para>
<para>
A property can be seen as an associative table where you can set and get the value for every element.
All property operations have a TYPE argument, so there is no need to cast the result of a property query. The standard
operations of a property are:
<itemizedlist>
<title>List of available modification operations</title>
<listitem><para><methodname>void setNodeValue(node,TYPE)</methodname> : sets the value of a node.</para></listitem>
<listitem><para><methodname>void setAllNodeValue(TYPE)</methodname> : sets the value of all nodes.</para></listitem>
<listitem><para><methodname>void setEdgeValue(edge,TYPE)</methodname> : sets the value of an edge.</para></listitem>
<listitem><para><methodname>void setAllEdgeValue(TYPE)</methodname> : sets the value of all edges.</para></listitem>
</itemizedlist>
<itemizedlist>
<title>List of available access operations</title>
<listitem><para><methodname>TYPE getNodeValue(node)</methodname> : returns the value of a node.</para></listitem>
<listitem><para><methodname>TYPE getEdgeValue(edge)</methodname> : returns the value of an edge.</para></listitem>
</itemizedlist>
</para>
<para> For each property type there is a specific implementation (subclass) that allows operations which
are specific to the property type (see Tulip libraries documentation). For instance, it is possible to obtain
the maximum value of a property if the property type is <code>double</code>.
</para>
<para> A graph includes a set of functions that enables to obtain/create/delete a property. Because
the C++ signature of functions does not include the return type, the syntax for this call is not
very simple. For instance, if one wants to obtain a property containing double (called DoubleProperty in Tulip) one must use
the following syntax : <methodname> DoubleProperty *metric=graph->getProperty<DoubleProperty>("name of the property");</methodname>
In the graph each property is identified by its name which is a std::string, when one asks for a property the type of this
property is checked using the run time type interrogation mechanism of C++. Warning: This test only happens when one
compiles its sources in DEBUG mode (default mode). In order to facilitate the navigation/edition of the set of properties, a
set of functions is accessible through the graph interface.
<itemizedlist>
<title>List of available operations</title>
<listitem><para><methodname>Iterator * getLocalProperties()</methodname> : returns an iterator on all properties of this graph.</para></listitem>
<listitem><para><methodname>void delLocalProperty(const std::string&)</methodname> : deletes a property.</para></listitem>
<listitem><para><methodname>bool existLocalProperty(const std::string&)</methodname> : returns true if the property exists.</para></listitem>
<listitem><para><methodname>PropertyType * getLocalProperty (const std::string&)</methodname> : returns the property.
If it did not exist, creates it first </para></listitem>
</itemizedlist>
</para>
<para> For the property mechanism described above to work with a hierarchy of graphs, a mechanism have been added
to share properties between graphs, which works like this: if a property exists in an ancestor of a graph G, it also
exists in the graph G. Thus, properties of graphs are inherited like members of objects in object-oriented languages.
In order to facilitate the navigation/edition of properties, a set of function is accessible through the graph interface.
<itemizedlist>
<title>List of available operations</title>
<listitem><para><methodname>Iterators * getInheritedProperties()</methodname> : returns an iterator on all properties (both inherited and local).</para></listitem>
<listitem><para><methodname>bool existProperty(const std::string&)</methodname> : returns true if the property exists (inherited or local).</para></listitem>
<listitem><para><methodname>PropertyType * getProperty(const std::string&)</methodname> : returns the property (inherited or local).
If it did not exist, creates it first (locally)</para></listitem>
</itemizedlist>
</para>
</sect1>
<sect1 id="code-example-intro"><title>TUTORIAL Intro.</title>
<para> Before doing any of the tutorials please read the following warnings :
</para>
<itemizedlist>
<listitem><para>
Follow the tutorials, one by one, from the first one to the last one.
</para></listitem>
<listitem><para>
You can find at the end of each tutorial, the integral source code that we used.
</para></listitem>
<listitem><para>
If you want more details on a specific function or class please read the Tulip Graph library <ulink url="../../doxygen/tulip.html">documentation</ulink>.
</para></listitem>
</itemizedlist>
</sect1>
<sect1 id="code-examples-graphs"><title>TUTORIAL 001 : Graphs creation, adding and deleting nodes or edges.</title>
<para>
In this first tutorial, we will show you how to create a graph, add three nodes and three edges, remove an edge and a node, and finally, print the result on the standard output.
</para>
<graphic fileref="images/tuto_001_graphcreated_3.png"/>
<sect2 id="code-examples-graphs-headfile"><title>1. Header files</title>
<para>Let's start with the files we need to include :
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/Graph.h>]]>
</programlisting>
</para>
<itemizedlist>
<listitem><para><code>iostream :</code> This "file" contains the C++ standard declarations for in and out streams. We need it in this tutorial to print the final graph on the standard output.
</para></listitem>
<listitem><para><code>tulip/Graph.h :</code> This file is the core of the tulip graph API. It provides declarations for graphs (edges , nodes) and functions to load one from a file, to save one, and a lot more. You can find a list in the Tulip Graph library <ulink url="../../doxygen/tulip-lib/Graph_8h.html">documentation</ulink>.
</para></listitem>
</itemizedlist>
<para>The namespaces usage declarations, so that we can omit namespace prefix.
<programlisting>
<![CDATA[using namespace std;
using namespace tlp;]]>
</programlisting></para>
</sect2>
<sect2 id="code-examples-graphs-create"><title>2. Creation of a Graph</title>
<para>Create an empty graph with the function <code> Graph* tlp::newGraph( )</code>. This function returns a pointer on a empty Graph.</para>
<programlisting>
<![CDATA[int main() {
//create an empty graph
Graph *graph = tlp::newGraph();]]>
</programlisting>
</sect2>
<sect2 id="code-examples-graphs-addnodes"><title>3. Add nodes</title>
<para>In the following, we are adding three nodes with the member function <code> node Graph::addNode () </code> that will, create an instance of a 'node', add it to the graph, and return it.<note><para>Using this function, the node is also added in all the graph ancestors to maintain the sub-graph relation between graphs.</para></note>
</para>
<programlisting>
<![CDATA[ //add three nodes
node n1 = graph->addNode();
node n2 = graph->addNode();
node n3 = graph->addNode();]]>
</programlisting>
</sect2>
<sect2 id="code-examples-graphs-addedges"><title>4. Add edges</title>
<para>Now that nodes are created, we can create the edges. To do so, we can use the function <code> edge Graph::addEdge ( const node, const node ) </code> that will, add a new edge in the graph and return it.<note><para>The edge is also added in all the super-graph of the graph to maintain the sub-graph relation between graphs.</para></note>
</para>
<para>
The first parameter is the "source node", and, of course, the second is the "target node" (in tulip, every edge are oriented but you can choose not to consider the orientation). We will see later (TUTORIAL 005) that the edges enumeration order is the one in which they are added.
</para>
<programlisting>
<![CDATA[ //add three edges
edge e1 = graph->addEdge(n2,n3);
edge e2 = graph->addEdge(n1,n2);
edge e3 = graph->addEdge(n3,n1);]]>
</programlisting>
<para>Following is a picture of the graph that we just have created. It is being displayed with tulip.</para>
<graphic fileref="images/tuto_001_graphcreated_1.png"/>
</sect2>
<sect2 id="code-examples-graphs-delEdgeNode"><title>5. Delete an edge and a node</title>
<para> The Graph class provides member functions to delete edges and nodes.
<itemizedlist>
<listitem><para><code> void tlp::Graph::delEdge (const edge) </code> :
delete an edge of the graph. This edge is also removed in all the sub-graphs hierarchy to maintain the sub-graph relation between graphs. The ordering of edges is preserved.
</para></listitem>
<listitem><para><code> void tlp::Graph::delNode ( const node ) </code> :
delete a node of the graph. This node is also removed in all the sub-graph of the graph to maintain the sub-graph relation between graphs. When the node is deleted, all its edges are deleted (in and out edges).
</para></listitem>
</itemizedlist></para>
<note><para>The class Graph implements member functions like <code>void delAllNode (const node)</code>, and, <code>void delAllEdge (const edge)</code>.</para></note>
<programlisting>
<![CDATA[ //delete an edge
graph->delEdge(e1);
//delete a node
graph->delNode(n2);]]>
</programlisting>
<para>Following is our graph with node n2 deleted.</para>
<graphic fileref="images/tuto_001_graphcreated_2.png"/>
</sect2>
<sect2 id="code-examples-graphs-print"><title>6. Printing the graph</title>
<para>The class graph has a friend function which is an overload of the stream operator <<. This function will print the graph (only nodes and edges) in an output stream (here, the standard output, "cout"), in the tulip format.</para>
<programlisting>
<![CDATA[ //print the result on the standard output
cout << graph << flush;]]>
</programlisting>
</sect2>
<sect2 id="code-examples-graphs-save"><title>7. Saving a graph</title>
<para>Instead of having our graph printed on the standard output, we can save it in a .tlp (tulip format) suffixed file that can be read by tulip :</para>
<programlisting>
<![CDATA[ //Save the graph :
tlp::saveGraph(graph,"tuto1.tlp");]]>
</programlisting>
</sect2>
<sect2 id="code-examples-graphs-del"><title>8. Graph deletion</title>
<para>Before exiting the main function, do not forget memory leaks, and delete the graph to free memory usages.</para>
<programlisting>
<![CDATA[ //delete the graph
delete graph;
return EXIT_SUCCESS;
}]]>
</programlisting>
</sect2>
<sect2 id="code-examples-graphs-run"><title>9. Compiling and running the program.</title>
<para>
Compile this program with this command :</para>
<para><code>g++ `tulip-config --libs --cxxflags` tutorial.cpp -o tutorial001 </code>
</para>
<para>Run it to have a look :</para>
<para><code> ./tutorial001 </code> </para>
<graphic fileref="images/tuto_001_graphcreated_3.png"/>
</sect2>
<sect2 id="code-examples-graphs-exit"><title>10. Source Code</title>
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/Graph.h>
/**
*
* Tutorial 001
*
* Create a graph
* add three nodes and three edges
* remove an edge and a node
* print the result on the standard output
*
*/
using namespace std;
using namespace tlp;
int main() {
//create an empty graph
Graph *graph = tlp::newGraph();
//add three nodes
node n1 = graph->addNode();
node n2 = graph->addNode();
node n3 = graph->addNode();
//add three edges
edge e1 = graph->addEdge(n2,n3);
edge e2 = graph->addEdge(n1,n2);
edge e3 = graph->addEdge(n3,n1);
//delete an edge
graph->delEdge(e1);
//delete a node
graph->delNode(n2);
//print the result on the standard output
cout << graph << flush ;
tlp::saveGraph(graph,"tuto1.tlp");
//delete the graph
delete graph;
return EXIT_SUCCESS;
}]]>
</programlisting>
<para>
</para>
<para>
</para>
<para>
</para>
</sect2>
</sect1>
<sect1 id="code-examples-iterator-foreach"><title>TUTORIAL 002 : Iterating over a graph (class Iterator and the macro forEach)</title>
<para>
In this tutorial, we will, display on the standard output, all the structure using iterators. For each node, we will display its ancestors, successors, neighbors, and, its incoming and outgoing edges.
</para>
<para> In this tutorial, the graph created is the same that in Tutorial 1 (after the 3 edges were added) see the following picture :
</para>
<graphic fileref="images/tuto_001_graphcreated_1.png"/>
<sect2 id="code-examples-iterator-headfile"><title>1. Header files (Same as Tutorial 1)</title>
<para>Let's start with the files we need to include :
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/Graph.h>]]>
</programlisting>
</para>
<itemizedlist>
<listitem><para><code>iostream :</code> This "file" contains the C++ standard declarations for in and out streams. We need it in this tutorial to print the final graph on the standard output.
</para></listitem>
<listitem><para><code>tulip/Graph.h :</code> This file is the core of the tulip graph API. It provides declarations for graphs (edges , nodes) and functions to load one from a file, to save one, and a lot more. You can find a list in the doxygen <ulink url="../../doxygen/tulip-lib/Graph_8h.html">documentation</ulink>.
</para></listitem>
</itemizedlist>
<para>As you can see, we just need the "Graph.h" header file to create a graph and iterate over its nodes, even though the declaration of the abstract class "Iterator" is in Iterator.h</para>
</sect2>
<sect2 id="code-examples-iterator-allnode"><title>2. Iterating over all nodes</title>
<para> To iterate over all nodes, we need to create an Iterator on the graph nodes with the member function <code>Iterator* Graph::getNodes () const</code>, we will make it point on the graphs nodes.
</para>
<programlisting>
<![CDATA[ Iterator<node> *itNodes = graph->getNodes();]]>
</programlisting>
<para>
The documentation of the interface Iterator can be found <ulink url="http://tulip.labri.fr/doxygen/tulip-lib/structtlp_1_1Iterator.html"> here.</ulink>
</para>
<para>With the functions <code>template <class itType >
bool tlp::Iterator< itType >::hasNext ( )</code> and <code> node next ( )</code>, we can iterate through our graph nodes with a simple while :
</para>
<programlisting role="C">
<![CDATA[ while(itNodes->hasNext()) {
node n = itNodes->next();]]>
</programlisting>
<para>In this <code>while</code> loop, we display some node topological properties :</para>
<programlisting role="C">
<![CDATA[ cout << "node: " << n.id << endl;
cout << " degree: " << graph->deg(n) << endl;
cout << " in-degree: " << graph->indeg(n) << endl;
cout << " out-degree: " << graph->outdeg(n) << endl;]]>
</programlisting>
<para>At the end of the loop, we will need to delete the iterator: <code>delete itNodes;</code> </para>
<para>Following is the output of this simple while loop :</para>
<programlisting>
<![CDATA[[root@atlas tutorial-002]# ./tutorial002
node: 0
degree: 2
in-degree: 1
out-degree: 1
node: 1
degree: 2
in-degree: 1
out-degree: 1
node: 2
degree: 2
in-degree: 1
out-degree: 1]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit-pred"><title>3. Iterating through a node predecessors</title>
<para>To iterate through predecessors of node, we use the same type of Iterator, but, instead of using the function getNodes() of the class Graph, we will use the function <code> Iterator<node>* getInNodes (const node) const</code> that will return an iterator on the predecessors of a node. </para>
<programlisting>
<![CDATA[ //===========================
//iterate all predecessors of a node
cout << " predecessors: {";
Iterator<node> *itN=graph->getInNodes(n);
while(itN->hasNext()) {
cout << itN->next().id;
if (itN->hasNext()) cout << ",";
} delete itN; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit_succs"><title>4. Iterating through a node successors</title>
<para>To iterate through successors of a node, we just need to use the function <code>Iterator<node>* Graph::getOutNodes (const node) const</code> to have an Iterator on its successors.</para>
<programlisting>
<![CDATA[ //===========================
//iterate all successors of a node
cout << " successors: {";
itN = graph->getOutNodes(n);
while (itN->hasNext()) {
cout << itN->next().id;
if (itN->hasNext()) cout << ",";
} delete itN; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit-all"><title>5. Iterating through a node neighbors (predecessors and successors)</title>
<para>For neighbors, we will use the function <code> Iterator<node>* Graph::getInOutNodes (const node) const </code> to have an Iterator on its neighbors.</para>
<programlisting>
<![CDATA[ //===========================
//iterate the neighborhood of a node
cout << " neighborhood: {";
itN = graph->getInOutNodes(n);
while(itN->hasNext()) {
cout << itN->next().id;
if (itN->hasNext()) cout << ",";
} delete itN; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit-inedges"><title>6. Iterating through a node incoming edges</title>
<para>For incoming edges, we will use an Iterator on edges with the member function <code>Iterator<edge>* Graph::getInEdges (const node) const</code>.</para>
<programlisting>
<![CDATA[ //===========================
//iterate the incoming edges
cout << " incoming edges: {";
Iterator<edge> *itE=graph->getInEdges(n);
while(itE->hasNext()) {
cout << itE->next().id;
if (itE->hasNext()) cout << ",";
} delete itE; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
cout << " outcoming edges: {";]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit-outedges"><title>7. Iterating through a node outgoing edges</title>
<para>For outgoing edges, we will use the function <code>Iterator<edge>* Graph::getOutEdges (const node) const</code>.</para>
<programlisting>
<![CDATA[ //===========================
//iterate the outcomming edges
itE = graph->getOutEdges(n);
while(itE->hasNext()) {
cout << itE->next().id;
if (itE->hasNext()) cout << ",";
} delete itE; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit"><title>8. Iterating through a node adjacent edges</title>
<para>For adjacent edges, we will use the function <code>Iterator<edge>* Graph::getInOutEdges (const node) const</code>.</para>
<programlisting>
<![CDATA[ //===========================
//iterate the adjacent edges
cout << " adjacent edges: {";
itE = graph->getInOutEdges(n);
while(itE->hasNext()) {
cout << itE->next().id;
if (itE->hasNext()) cout << ",";
} delete itE; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;]]>
</programlisting>
</sect2>
<sect2 id="code-example-endwhile"><title>Don't forget memory leaks</title>
<para>As we are still in the first while (iterating through all nodes) we need to delete the Iterator on Nodes :</para>
<programlisting>
<![CDATA[}// end while
delete itNodes; //!!!Warning : do not forget to delete iterators (memory leak)]]>
</programlisting>
</sect2>
<sect2 id="code-example-graphit-edges"><title>9. Iterating on edges (all edges).</title>
<para>Some times it can be useful to iterate on edges, for example in the algorithm of Kruskal. That is why the graph class owns the function <code>Iterator<edge>* Graph::getEdges (const node) const</code>, that return a pointer on an Iterator of type edge. Following is an exemple of its use. </para>
<programlisting>
<![CDATA[ //===========================
//Iterate all edges
Iterator<edge> *itEdges=graph->getEdges();
while(itEdges->hasNext()) {
edge e = itEdges->next();
cout << "edge: " << e.id;
cout << " source: " << graph->source(e).id;
cout << " target: " << graph->target(e).id;
cout << endl;
} delete itEdges; //!!!Warning : do not forget to delete iterators (memory leak)]]>
</programlisting>
</sect2>
<sect2 id="code-example-foreach"><title>10. The forEach Macro </title>
<para>To simplify the use of Iterators, the API of tulip provides a macro forEach which is quite similar to the foreach of C# or Java. It takes two parameters :</para>
<itemizedlist>
<listitem><para> A variable :
</para></listitem>
<listitem><para>
An Iterator for the same type as the variable, for example : Variable of type node, Graph::getNodes().
</para></listitem>
</itemizedlist>
<warning><itemizedlist>
<listitem><para>
Must use breakForEach to break iteration
</para></listitem>
<listitem><para> Must use returnForEach to return during iteration
</para></listitem>
</itemizedlist></warning>
<para>This macro function is defined in the header file : tulip/ForEach.h</para>
<para>Following is a small example of its use.
<programlisting>
<![CDATA[#include <tulip/ForEach.h>
//...
//main
//load Graph
//...
node n = graph->getOneNode();
cout << "In Edges :" << endl;
edge e;
forEach(e, graph->getInEdges(n))
{
cout << e.id << ",";
}
//...]]>
</programlisting></para>
</sect2>
<sect2 id="code-example-tuto002"><title>Source Code</title>
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/Graph.h>
/**
* Tutorial 002
*
* Create a graph
* display all the structure using iterators
*
*/
using namespace std;
using namespace tlp;
void buildGraph(Graph *graph) {
//add three nodes
node n0=graph->addNode();
node n1=graph->addNode();
node n2=graph->addNode();
//add three edges
graph->addEdge(n1,n2);
graph->addEdge(n0,n1);
graph->addEdge(n2,n0);
}
int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
buildGraph(graph);
//===========================
//Iterate all nodes and display the structure
Iterator<node> *itNodes = graph->getNodes();
while(itNodes->hasNext()) {
node n = itNodes->next();
cout << "node: " << n.id << endl;
cout << " degree: " << graph->deg(n) << endl;
cout << " in-degree: " << graph->indeg(n) << endl;
cout << " out-degree: " << graph->outdeg(n) << endl;
//===========================
//iterate all ancestors of a node
cout << " ancestors: {";
Iterator<node> *itN=graph->getInNodes(n);
while(itN->hasNext()) {
cout << itN->next().id;
if (itN->hasNext()) cout << ",";
} delete itN; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
//===========================
//iterate all successors of a node
cout << " successors: {";
itN = graph->getOutNodes(n);
while (itN->hasNext()) {
cout << itN->next().id;
if (itN->hasNext()) cout << ",";
} delete itN; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
//===========================
//iterate the neighborhood of a node
cout << " neighborhood: {";
itN = graph->getInOutNodes(n);
while(itN->hasNext()) {
cout << itN->next().id;
if (itN->hasNext()) cout << ",";
} delete itN; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
//===========================
//iterate the incoming edges
cout << " incoming edges: {";
Iterator<edge> *itE=graph->getInEdges(n);
while(itE->hasNext()) {
cout << itE->next().id;
if (itE->hasNext()) cout << ",";
} delete itE; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
cout << " outcoming edges: {";
//===========================
//iterate the outcomming edges
itE = graph->getOutEdges(n);
while(itE->hasNext()) {
cout << itE->next().id;
if (itE->hasNext()) cout << ",";
} delete itE; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
//===========================
//iterate the adjacent edges
cout << " adjacent edges: {";
itE = graph->getInOutEdges(n);
while(itE->hasNext()) {
cout << itE->next().id;
if (itE->hasNext()) cout << ",";
} delete itE; //!!!Warning : do not forget to delete iterators (memory leak)
cout << "}" << endl;
} delete itNodes; //!!!Warning : do not forget to delete iterators (memory leak)
//===========================
//Iterate all edges
Iterator<edge> *itEdges=graph->getEdges();
while(itEdges->hasNext()) {
edge e = itEdges->next();
cout << "edge: " << e.id;
cout << " source: " << graph->source(e).id;
cout << " target: " << graph->target(e).id;
cout << endl;
} delete itEdges; //!!!Warning : do not forget to delete iterators (memory leak)
delete graph; //delete the entire graph
return EXIT_SUCCESS;
}]]>
</programlisting>
</sect2>
</sect1>
<sect1 id="code-examples-properties"><title>TUTORIAL 003 : Properties</title>
<para> This tutorial will show you how to add / create properties to a Graph. For local or inherited properties, see tututorial 005. An instance of a property is owned by a graph and is an association table between the elements of graph (nodes and edges) and values of a predefined type.
</para>
<sect2 id="code-example-headfiles"><title>1. Header files and predefined properties</title>
<para> In tulip API, every type of property is declared in its own header file. Following is a list of those header files and the type of value which can be associated to an element of the graph:
</para>
<itemizedlist>
<listitem><para>DoubleProperty : tulip/DoubleProperty.h /
value type for edge = double, node = double
</para></listitem>
<listitem><para>BooleanProperty : tulip/BooleanProperty.h /
value type for edge = bool, node = bool
</para></listitem>
<listitem><para>IntegerProperty: tulip/IntegerProperty.h /
value type for edge = int, node = int
</para></listitem>
<listitem><para>LayoutProperty : tulip/LayoutProperty.h /
value type for edge = Coord(), node = vector<Coord>()
</para></listitem>
<listitem><para>ColorProperty : tulip/ColorProperty.h /
value type for edge = Color(), node = Color()
</para></listitem>
<listitem><para>SizeProperty : tulip/SizeProperty.h /
value type for edge = Size(), node = Size()
</para></listitem>
<listitem><para>StringProperty : tulip/StringProperty.h /
value type for edge = string, node = string
</para></listitem>
<listitem><para>GraphProperty : tulip/GraphProperty.h /
value type for edge = graph, node = graph
</para></listitem>
</itemizedlist>
</sect2>
<sect2 id="code-example-create-prop"><title>2. Creation of a property.</title>
<para>
The creation of a property is accomplished by the function <code> Graph::getLocalProperty<TypeProperty>("name of the property")</code>. This function returns a pointer to a property. The real type of the property is given with the template parameter. If the property of the given name does not yet exists, a new one is created and returned.
</para>
<warning><para>Using of delete on that property will cause a segmentation violation (use delLocalProperty instead).
</para></warning>
<para> Following is a sample of code that creates 8 properties :
</para>
<programlisting>
<![CDATA[ //Get and create several properties
DoubleProperty *metric = graph->getLocalProperty<DoubleProperty>("firstMetric");
BooleanProperty *select = graph->getLocalProperty<BooleanProperty>("firstSelection");
LayoutProperty *layout = graph->getLocalProperty<LayoutProperty>("firstLayout");
IntegerProperty *integer = graph->getLocalProperty<IntegerProperty>("firstInteger");
ColorProperty *colors = graph->getLocalProperty<ColorProperty>("firstColors");
SizeProperty *sizes = graph->getLocalProperty<SizeProperty>("firstSizes");
GraphProperty *meta = graph->getLocalProperty<GraphProperty>("firstMeta");
StringProperty *strings = graph->getLocalProperty<StringProperty>("firstString");]]>
</programlisting>
</sect2>
<sect2 id="code-example-init-props"><title>3. Initialize all properties.</title>
<para>
One property has to be initialized for both edges and nodes. It is done with the functions <code>setAllNodeValue(value)</code> and <code>setAllEdgeValue(value)</code> which are both member functions of the property.</para>
<para>Following is an example :</para>
<programlisting>
<![CDATA[ //initialize all the properties
metric->setAllNodeValue(0.0);
metric->setAllEdgeValue(0.0);
select->setAllNodeValue(false);
select->setAllEdgeValue(false);
layout->setAllNodeValue(Coord(0,0,0)); //coordinates
layout->setAllEdgeValue(vector<Coord>());//Vector of bends
integer->setAllNodeValue(0);
integer->setAllEdgeValue(0);
sizes->setAllNodeValue(Size(0,0,0)); //width, height, depth
sizes->setAllEdgeValue(Size(0,0,0)); //start_size, end_size, arrow_size
colors->setAllNodeValue(Color(0,0,0,0));//Red, green, blue, alpha
colors->setAllEdgeValue(Color(0,0,0,0));//Red, green, blue, alpha
strings->setAllNodeValue("first");
strings->setAllEdgeValue("first");
meta->setAllNodeValue(graph); //an existing graph]]>
</programlisting>
<para>Following is the display (in the tulip GUI) of the list of a node associated values for the properties previously created :</para>
<graphic fileref="images/tuto_003_properties_1.png"/>
</sect2>
<sect2 id="code-example-iter-props"><title>4. Iterating over properties.</title>
<para>Once again, iteration is made with Iterators. The class graph has a member function <code>Iterator< std::string >* getLocalProperties ()</code> that returns an iterator on the local properties.</para>
<para>Following is an example :</para>
<programlisting>
<![CDATA[ cout << "List of the properties present in the graph:" << endl;
Iterator<string> *it=graph->getLocalProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;]]>
</programlisting>
<para>You can also use the macro forEach.</para>
</sect2>
<sect2 id="code-example-003-codesource"><title>Source Code</title>
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/BooleanProperty.h>
#include <tulip/ColorProperty.h>
#include <tulip/DoubleProperty.h>
#include <tulip/GraphProperty.h>
#include <tulip/IntegerProperty.h>
#include <tulip/LayoutProperty.h>
#include <tulip/SizeProperty.h>
#include <tulip/StringProperty.h>
/**
* Tutorial 003
*
* Create a graph and a properties of each type
* And display properties present in the graph
*/
using namespace std;
using namespace tlp;
void buildGraph(Graph *graph) {
//add three nodes
node n1=graph->addNode();
node n2=graph->addNode();
node n3=graph->addNode();
//add three edges
graph->addEdge(n2,n3);
graph->addEdge(n1,n2);
graph->addEdge(n3,n1);
}
int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
buildGraph(graph);
//Get and create several properties
DoubleProperty *metric=graph->getLocalProperty<DoubleProperty>("firstMetric");
BooleanProperty *select=graph->getLocalProperty<BooleanProperty>("firstSelection");
LayoutProperty *layout=graph->getLocalProperty<LayoutProperty>("firstLayout");
IntegerProperty *integer=graph->getLocalProperty<IntegerProperty>("firstInteger");
ColorProperty *colors=graph->getLocalProperty<ColorProperty>("firstColors");
SizeProperty *sizes=graph->getLocalProperty<SizeProperty>("firstSizes");
GraphProperty *meta=graph->getLocalProperty<GraphProperty>("firstMeta");
StringProperty *strings=graph->getLocalProperty<StringProperty>("firstString");
//initialize all the properties
metric->setAllNodeValue(0.0);
metric->setAllEdgeValue(0.0);
select->setAllNodeValue(false);
select->setAllEdgeValue(false);
layout->setAllNodeValue(Coord(0,0,0)); //coordinates
layout->setAllEdgeValue(vector<Coord>());//Vector of bends
integer->setAllNodeValue(0);
integer->setAllEdgeValue(0);
sizes->setAllNodeValue(Size(0,0,0)); //width, height, depth
sizes->setAllEdgeValue(Size(0,0,0)); //start_size, end_size, arrow_size
colors->setAllNodeValue(Color(0,0,0,0));//Red, green, blue
colors->setAllEdgeValue(Color(0,0,0,0));//Red, green, blue
strings->setAllNodeValue("first");
strings->setAllEdgeValue("first");
meta->setAllNodeValue(graph); //an existing graph
cout << "List of the properties present in the graph:" << endl;
Iterator<string> *it=graph->getLocalProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
tlp::saveGraph (graph, "tutoproper.tlp");
delete graph;
return EXIT_SUCCESS;
}]]>
</programlisting>
</sect2>
</sect1>
<sect1 id="code-examples-subgraph"><title>TUTORIAL 004 : Create your first subgraph.</title>
<para>This tutorial will teach you how to create subgraphs. At the end of it, we will have a hierarchy of 3 graphs.</para>
<para>Before anything consider the following function that creates 3 nodes and 3 edges (same as in tutorial 001 ):</para>
<programlisting>
<![CDATA[void buildGraph(Graph *graph)
{
//add three nodes
node n1=graph->addNode();
node n2=graph->addNode();
node n3=graph->addNode();
//add three edges
graph->addEdge(n2,n3);
graph->addEdge(n1,n2);
graph->addEdge(n3,n1);
}]]>
</programlisting>
<para>The creation of a subgraph is quite simple. You just have to use the function <code>Graph* addSubGraph (BooleanProperty* selection = 0) </code>. It will create and return a new SubGraph of the graph. The elements of the new subgraph are those selected in the selection(selection associated value equals true); if there is no selection an empty subgraph is returned. </para>
<para>In the following sample we create 3 empty subgraphs :</para>
<programlisting>
<![CDATA[ //build three empty subgraphs
Graph *subgraph0=graph->addSubGraph();
Graph *subgraph1=graph->addSubGraph();
Graph *subgraph2=subgraph1->addSubGraph();]]>
</programlisting>
<para>We now need to create some nodes and edges :</para>
<programlisting>
<![CDATA[ //add node inside subgraphs
buildGraph(subgraph0);
buildGraph(subgraph1);
buildGraph(subgraph2);]]>
</programlisting>
<para>Following is the hierarchy we have just created, displayed with tulip :</para>
<graphic fileref="images/tuto_004_subgraph_1.png" />
<para>We can check that by iterating on our graph's subgraphs using the function <code>Iterator< Graph * >* Graph::getSubGraphs()</code> :</para>
<programlisting>
<![CDATA[ //iterate subgraph (0 and 1 normally ) and output them
Iterator<Graph *> *itS=graph->getSubGraphs();
while (itS->hasNext())
cout << itS->next() << endl;
delete itS;]]>
</programlisting>
<sect2 id="code-example-004-code"><title>Source Code</title>
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/Graph.h>
/**
* Tutorial 004
*
* Create a graph and three subgraphs,
* display all the structure using iterators
*/
using namespace std;
using namespace tlp;
void buildGraph(Graph *graph) {
//add three nodes
node n1=graph->addNode();
node n2=graph->addNode();
node n3=graph->addNode();
//add three edges
graph->addEdge(n2,n3);
graph->addEdge(n1,n2);
graph->addEdge(n3,n1);
}
int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
buildGraph(graph);
//build two empty subgraph
Graph *subgraph0=graph->addSubGraph();
Graph *subgraph1=graph->addSubGraph();
Graph *subgraph2=subgraph1->addSubGraph();
//add node inside subgraphs
buildGraph(subgraph0);
buildGraph(subgraph1);
buildGraph(subgraph2);
//iterate subgraph (0 and 1 normally ) and output them
Iterator<Graph *> *itS=graph->getSubGraphs();
while (itS->hasNext())
cout << itS->next() << endl;
delete itS;
delete graph;
return EXIT_SUCCESS;
}]]>
</programlisting>
</sect2>
</sect1>
<sect1 id="code-examples-propertiessub"><title>TUTORIAL 005 : Properties and subgraphs</title>
<para>In this tutorial, we will show you how to use properties with subgraphs, how to deal with properties in a big hierarchy. To do so, we will create a graph with some properties, several subgraphs with other properties and iterate over local and inherited properties.</para>
<sect2 id="code-example-propertiessub-intro"><title>1. Introduction </title>
<para>We will first begin with the creation of the graph and its properties :</para>
<programlisting>
<![CDATA[int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
buildGraph(graph);
//Get and create several properties
BooleanProperty *select=graph->getLocalProperty<BooleanProperty>("firstSelection");
graph->getLocalProperty<ColorProperty>("firstColors");
graph->getLocalProperty<DoubleProperty>("firstMetric");
//init the selection in order to use it for building clone subgraph
select->setAllNodeValue(true);
select->setAllEdgeValue(true);]]>
</programlisting>
<note><para>The function <code>void buildGraph(Graph *g)</code>, is the one implemented in Tutorial 004.</para></note>
<para>In the sample of code above, we create a graph with 3 properties : firstSelection (select), fisrtColors and firstMetric. We then set all nodes and edges "firstSelection" associated value to true which means that all nodes and edges are selected.</para>
<para>We, then, create two subgraphs out of our selection (the entire graph) : </para>
<programlisting>
<![CDATA[ //Create a hierarchy of subgraph (they all own the same elements)
Graph *subgraph1=graph->addSubGraph(select);
Graph *subgraph2=subgraph1->addSubGraph(select);]]>
</programlisting>
<para>And, to finish this section, we add some new properties to those two subgraphs :</para>
<programlisting>
<![CDATA[ //create a property in subgraph1 (redefinition of the one defined in graph)
subgraph1->getLocalProperty<DoubleProperty>("firstMetric");
//create a new property in subgraph1
subgraph1->getLocalProperty<DoubleProperty>("secondMetric");
//create a new property in subgraph3
subgraph2->getLocalProperty<DoubleProperty>("thirdMetric");]]>
</programlisting>
<para>The property "firstMetric" will be redefined but not the two other ones.</para>
</sect2>
<sect2 id="code-example-propertiessub-sub1"><title>2. Properties of subgraph1 </title>
<para>A good way to see what we have created is to iterate over the local properties of subgraph1 and in a second time iterate over inherited properties. </para>
<para>Following is a sample and its output that enables the iteration over local properties :</para>
<programlisting>
<![CDATA[ cout << "List of the local properties present in the subgraph1:" << endl;
Iterator<string> *it=subgraph1->getLocalProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
_________________________________________________________________________
[root@atlas tutorial-005]# ./tutorial
List of the local properties present in the subgraph1:
firstMetric
secondMetric]]>
</programlisting>
<para>As you can see the only local properties that has subgraph1 are "firstMetric" and "secondMetric". Indeed, "firstMetric" has been redefined, and, "thirdMetric" has been created with subgraph2.</para>
<para>Following is a sample and its output that enables the iteration over inherited properties :</para>
<programlisting>
<![CDATA[ cout << "List of the inherited properties present in the subgraph1:" << endl;
it=subgraph1->getInheritedProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
_________________________________________________________________________
List of the local properties present in the subgraph1:
firstColors
firstSelection]]>
</programlisting>
<para>As you can see, subgraph1 just has two inherited properties since "firstMetric" has been redefined.</para>
<para>Following is a sample of code that lists all the properties of a graph, the inherited properties and local properties :</para>
<programlisting>
<![CDATA[ cout << "List of properties present in the subgraph1:" << endl;
it=subgraph1->getProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
_________________________________________________________________________
List of properties present in the subgraph1:
firstMetric
secondMetric
firstColors
firstSelection]]>
</programlisting>
</sect2>
<sect2 id="code-example-propertiessub-sub2"><title>3. Properties of subgraph2 </title>
<para>As we did with subgraph1, we will now iterate over the local properties of subgraph2 in a first time and in a second time iterate over its inherited properties. </para>
<para>Following is a sample and its output that enables the iteration over local properties :</para>
<programlisting>
<![CDATA[ cout << "List of the local properties present in the subgraph2:" << endl;
it=subgraph2->getLocalProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
_________________________________________________________________________
[root@atlas tutorial-005]# ./tutorial
List of the local properties present in the subgraph2:
thirdMetric]]>
</programlisting>
<para>The only local properties that has subgraph1 is thirdMetric.</para>
<para>Following is a sample and its output that enables the iteration over inherited properties :</para>
<programlisting>
<![CDATA[ cout << "List of the inherited properties present in the subgraph2:" << endl;
it=subgraph2->getInheritedProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
_________________________________________________________________________
List of the local properties present in the subgraph2:
firstColors
firstMetric
firstSelection
secondMetric]]>
</programlisting>
<para>As you can see, subgraph2 has a lot of inherited properties since he is the subgraph of subgraph1 which is the subgraph of the root graph. </para>
</sect2>
<sect2 id="code-example-005-code"><title>Source Code</title>
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/BooleanProperty.h>
#include <tulip/ColorProperty.h>
#include <tulip/DoubleProperty.h>
/**
* Tutorial 005
*
* Create a graph hierarchy with several properties
* Display the inherited and local properties in each graph
*/
using namespace std;
using namespace tlp;
void buildGraph(Graph *graph) {
//add three nodes
node n1=graph->addNode();
node n2=graph->addNode();
node n3=graph->addNode();
//add three edges
graph->addEdge(n2,n3);
graph->addEdge(n1,n2);
graph->addEdge(n3,n1);
}
int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
buildGraph(graph);
//Get and create several properties
BooleanProperty *select=graph->getLocalProperty<BooleanProperty>("firstSelection");
graph->getLocalProperty<ColorProperty>("firstColors");
graph->getLocalProperty<DoubleProperty>("firstMetric");
//init the selection in order to use it for building clone subgraph
select->setAllNodeValue(true);
select->setAllEdgeValue(true);
//Create a hierarchy of subgraph (there are all the same)
Graph *subgraph1=graph->addSubGraph(select);
Graph *subgraph2=subgraph1->addSubGraph(select);
//create a property in subgraph1 (redefinition of the one defined in graph)
subgraph1->getLocalProperty<DoubleProperty>("firstMetric");
//create a new property in subgraph1
subgraph1->getLocalProperty<DoubleProperty>("secondMetric");
//create a new property in subgraph3
subgraph2->getLocalProperty<DoubleProperty>("thirdMetric");
cout << "List of the local properties present in the subgraph1:" << endl;
Iterator<string> *it=subgraph1->getLocalProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
cout << "List of inherited properties present in the subgraph1:" << endl;
it=subgraph1->getInheritedProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
cout << "List of properties present in the subgraph1:" << endl;
it=subgraph1->getProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
cout << "List of the local properties present in the subgraph2:" << endl;
it=subgraph2->getLocalProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
cout << "List of inherited properties present in the subgraph2:" << endl;
it=subgraph2->getInheritedProperties();
while (it->hasNext()) {
cout << it->next() << endl;
} delete it;
delete graph;
return EXIT_SUCCESS;
}]]>
</programlisting>
</sect2>
</sect1>
<sect1 id="code-examples-edge-order"><title>TUTORIAL 006 : Edges order.</title>
<para>In this tutorial, we will learn how to change edges order in the graph edges adjacency list (please visit <ulink url="http://en.wikipedia.org/wiki/Acyclic_Graph#Adjacency_and_degree">Wikipedia: Adjacency and degree</ulink> for more details ). Indeed, it can be useful to sort the edges considering a metric.</para>
<sect2 id="code-example-edgesorder-intro"><title>1. Creation of the graph and its edges </title>
<para>We will create a graph with 4 nodes and 4 edges. Their "id number" will start from 0 just like in the figure below :</para>
<graphic fileref="images/tuto_006_edgeorder_1.png" />
<para>Following is the sample of code that created such a graph: </para>
<programlisting>
<![CDATA[int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
node n0=graph->addNode();
node n1=graph->addNode();
node n2=graph->addNode();
node n3=graph->addNode();
//add three edges
edge e0=graph->addEdge(n1,n2);
edge e1=graph->addEdge(n0,n1);
edge e2=graph->addEdge(n2,n0);
edge e3=graph->addEdge(n3,n0);]]>
</programlisting>
<para>As you can see, node 0 has three edges : edge 1,edge 2 and edge 3. And if we display its edges adjacency list (see last section for function <code> void displayAdjacency(node n, Graph *graph) </code> ) we obtain the following output : </para>
<programlisting><![CDATA[ 1 2 3 ]]> </programlisting>
</sect2>
<sect2 id="code-example-edgesorder-"><title>2. Swap edges</title>
<para>Swapping edges can be easily done with the function, <code> void Graph::swapEdgeOrder ( const node, const edge,const edge)</code> that will, as said swap two edges in the adjacent list of a node. Following is an example of its use : </para>
<programlisting>
<![CDATA[ //swap e1 and e3
graph->swapEdgeOrder(n0, e1, e3);]]>
</programlisting>
<para>As you can see, the adjacency list has changed :</para>
<programlisting> <![CDATA[ 3 2 1 ]]> </programlisting>
</sect2>
<sect2 id="code-example-edgesorder-setorder"><title>3. Setting an order</title>
<para>An other way to change the edges order is to use a vector of type edge and the function : <code> void Graph::setEdgeOrder (const node, const std::vector < edge > )</code>, following is an example that will replace e1 and e3 in their original order :</para>
<programlisting>
<![CDATA[ vector<edge> tmp(2);
tmp[0]=e1;
tmp[1]=e3;
graph->setEdgeOrder(n0,tmp);]]>
</programlisting>
<para>And the new order : </para>
<programlisting> <![CDATA[ 1 2 3 ]]> </programlisting>
</sect2>
<sect2 id="code-example-006-code"><title>Source Code</title>
<programlisting>
<![CDATA[#include <iostream>
#include <tulip/Graph.h>
#include <tulip/MutableContainer.h>
/**
* Tutorial 006
*
* Create a graph
* Order the edges around the nodes
*/
using namespace std;
using namespace tlp;
void buildGraph(Graph *graph) {
//add three nodes
node n0=graph->addNode();
node n1=graph->addNode();
node n2=graph->addNode();
//add three edges
graph->addEdge(n1,n2);
graph->addEdge(n0,n1);
graph->addEdge(n2,n0);
}
void displayAdjacency(node n, Graph *graph) {
Iterator<edge>*ite=graph->getInOutEdges(n);
while(ite->hasNext())
cout << ite->next().id << " ";
delete ite;
cout << endl;
}
int main() {
//create an empty graph
Graph *graph=tlp::newGraph();
//build the graph
node n0=graph->addNode();
node n1=graph->addNode();
node n2=graph->addNode();
node n3=graph->addNode();
//add three edges
edge e0=graph->addEdge(n1,n2);
edge e1=graph->addEdge(n0,n1);
edge e2=graph->addEdge(n2,n0);
edge e3=graph->addEdge(n3,n0);
tlp::saveGraph(graph, "adj1.tlp");
//display current order of edge around n1
displayAdjacency(n0,graph);
//swap e1 and e3
graph->swapEdgeOrder(n0,e1,e3);
//display the new order of edge around n1
displayAdjacency(n0,graph);
vector<edge> tmp(2);
tmp[0]=e1;
tmp[1]=e3;
graph->setEdgeOrder(n0,tmp);
//display the new order of edge around n1
displayAdjacency(n0,graph);
delete graph;
return EXIT_SUCCESS;
}]]> </programlisting>
</sect2>
</sect1>
<sect1 id="code-examples-mutablecont"><title>TUTORIAL 007 : Mutable Collection</title>
<para>In this small tutorial, we will learn how to use the Mutable Container (an efficient associative container) of the tulip API that enables :</para>
<itemizedlist>
<listitem><para>
A tradeoff between speed and memory.
</para></listitem>
<listitem><para>
To manage fragmented index
</para></listitem>
</itemizedlist>
<para>The direct access in this container is forbidden, but it exist a getter and a setter :</para>
<itemizedlist>
<listitem><para> <code>const ReturnType<TYPE
>::ConstValue MutableContainer<type>::get(const unsigned int i) const</code> that returns a reference instead of a copy in order to minimize the number copy of objects, user must be aware that calling the set function can devalidate this reference
</para></listitem>
<listitem><para>
<code> void MutableContainer<type>::set( const unsigned int i,const TYPE value).</code>
</para></listitem>
</itemizedlist>
<para>The MutableContainer has two more methods :</para>
<itemizedlist>
<listitem><para>
<code>void setAll (const TYPE value)</code>
</para></listitem>
<listitem><para>
<code>IteratorValue* findAll(const TYPE &value, bool equal = true) const</code>
</para></listitem>
</itemizedlist>
<para>Following is a small example of its use :</para>
<programlisting>
<![CDATA[ //declaration of a new MutableContainer
MutableContainer<int> t;
//set all element to 0
t.setAll(0);
//except element of index 1 set to 1.
t.set(1,1);
//display on standard output
cout << t.get(1) << "and" << t.get(2) << endl;]]>
</programlisting>
</sect1>
<sect1 id="code-examples-graphs-tests"><title>TUTORIAL 008 : Graph Tests</title>
<para>The tulip API has special functions to test if a graph is element of a specific class of graph. Moreover results are buffered and automatically updated if it is possible in constant time. </para>
<para>Specific functions are available for each test.</para>
<para>Each class of graph has its own header file. Following is a list of those header files and an example of their use :</para>
<itemizedlist>
<listitem><para>
tulip/AcyclicTest.h
<programlisting> <![CDATA[AcyclicTest::isAcyclic(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/BiconnectedTest.h
<programlisting> <![CDATA[BiconnectedTest::isBiconnected(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/ConnectedTest.h
<programlisting> <![CDATA[ConnectedTest::isConnected(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/OuterPlanarTest.h
<programlisting> <![CDATA[PlanarityTest::isOuterPlanar(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/PlanarityTest.h
<programlisting> <![CDATA[PlanarityTest::isPlanar(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/SimpleTest.h
<programlisting> <![CDATA[SimpleTest::isSimple(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/TreeTest.h
<programlisting> <![CDATA[ TreeTest::isTree(graph);]]>
</programlisting>
</para></listitem>
<listitem><para>
tulip/TriconnectedTest.h
<programlisting> <![CDATA[TriconnectedTest::isTriconnected(graph);]]>
</programlisting>
</para></listitem>
</itemizedlist>
</sect1>
<sect1 id="code-examples-observablegraph"><title>TUTORIAL 009 : ObservableGraph</title>
<para>
In this tutorial, we will show you how to use the class ObservableGraph that enables to receive notification when a graph is updated.
</para>
<para>First, we will create a class that inherits from ObservableGraph, and then use it and see what this new class is able to "do".</para>
<sect2 id="code-exmple-obser1"><title>1. Our new class, GraphObserverTest :</title>
<para>Header files and namespaces :</para>
<para>To continue this tutorial, we need to include the following files, and namespaces.</para>
<programlisting>
<![CDATA[#include <iostream>
#include <set>
//- - - - - - - - - - - - -
#include <tulip/TlpTools.h>
#include <tulip/Graph.h>
#include <tulip/ObservableGraph.h>
//- - - - - - - - - - - - -
using namespace std;
using namespace tlp;]]>
</programlisting>
<para>As said before, we will create a new class that inherits from ObservableGraph. This class will have several functions to be notified when a node or an edge is deleted or added or reversed (edge).</para>
<para>Following is the class GraphObserverTest :</para>
<programlisting>
<![CDATA[class GraphObserverTest : public GraphObserver {
public:
GraphObserverTest() {
}
private:
void addEdge(Graph * g,const edge e) {
cout << "edge :" << e.id << " has been added" << endl;
}
void delEdge(Graph *,const edge e) {
cout << "edge :" << e.id << " is about to be deleted" << endl;
}
void reverseEdge(Graph *,const edge e) {
cout << "edge :" << e.id << " is about to be reversed" << endl;
}
void addNode(Graph * g,const node n) {
cout << "node :" << n.id << " has been added" << endl;
}
void delNode(Graph *,const node n) {
cout << "node :" << n.id << " is about to be deleted" << endl;
}
void destroy(Graph *g) {
cout << "graph : " << g->getId() << " is about to be deleted" << endl;
}
};]]>
</programlisting>
<para>Those methods are redefinitions of their homologues in the super class.</para>
</sect2>
<sect2 id="code-example-obsmain"><title>2. The Main function</title>
<para>Following in the main function with some explanations :</para>
<programlisting>
<![CDATA[int main(int argc, char **argv) {
Graph *graph = tlp::newGraph();]]>
</programlisting>
<para>Nothing really new here, just the creation of the graph.</para>
<programlisting>
<![CDATA[ //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GraphObserverTest graphObserverTest;
graph->addGraphObserver(&graphObserverTest);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]>
</programlisting>
<para> We add the instance of our class GraphObserverTest with the function <code> Graph::addObserver </code>.
</para>
<para>We then perform some modifications on the graph :</para>
<programlisting>
<![CDATA[ node n1 = graph->addNode();
node n0 = graph->addNode();
edge e0 = graph->addEdge(n0, n1);
graph->reverse(e0);
graph->delNode(n0);
delete graph;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return EXIT_SUCCESS;
}]]>
</programlisting>
<para>Following is the output of this program :</para>
<programlisting>
<![CDATA[node :0 has been added
node :1 has been added
edge :0 has been added
edge :0 is about to be reversed
node :0 is about to be deleted
edge :0 is about to be deleted]]>
</programlisting>
</sect2>
</sect1>
</chapter>
|