11. TUTORIAL 005 : Properties and subgraphs

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.

1. Introduction

We will first begin with the creation of the graph and its properties :

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);
        		

Note

The function void buildGraph(Graph *g), is the one implemented in Tutorial 004.

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.

We, then, create two subgraphs out of our selection (the entire graph) :

  //Create a hierarchy of subgraph (they all own the same elements)
  Graph *subgraph1=graph->addSubGraph(select);
  Graph *subgraph2=subgraph1->addSubGraph(select);
        		

And, to finish this section, we add some new properties to those two subgraphs :

   //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");
        		

The property "firstMetric" will be redefined but not the two other ones.

2. Properties of subgraph1

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.

Following is a sample and its output that enables the iteration over local properties :

  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
        		

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.

Following is a sample and its output that enables the iteration over inherited properties :

  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
        		

As you can see, subgraph1 just has two inherited properties since "firstMetric" has been redefined.

Following is a sample of code that lists all the properties of a graph, the inherited properties and local properties :

  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
        		

3. Properties of subgraph2

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.

Following is a sample and its output that enables the iteration over local properties :

  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
        		

The only local properties that has subgraph1 is thirdMetric.

Following is a sample and its output that enables the iteration over inherited properties :

  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
        		

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.

Source Code

#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;
}