15. TUTORIAL 009 : ObservableGraph
Prev | Chapter 3. Tulip Library | Next

## 15. TUTORIAL 009 : ObservableGraph

In this tutorial, we will show you how to use the class ObservableGraph that enables to receive notification when a graph is updated.

First, we will create a class that inherits from ObservableGraph, and then use it and see what this new class is able to "do".

### 1. Our new class, GraphObserverTest :

Header files and namespaces :

To continue this tutorial, we need to include the following files, and namespaces.

    #include <iostream>
    #include <set>
    //- - - - - - - - - - - - -
    #include <tulip/TlpTools.h>
    #include <tulip/Graph.h>
    #include <tulip/ObservableGraph.h>
    //- - - - - - - - - - - - -

    using namespace std;
    using namespace tlp;

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

Following is the class GraphObserverTest :

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

Those methods are redefinitions of their homologues in the super class.

### 2. The Main function

Following in the main function with some explanations :

    int main(int argc, char **argv) {
      Graph *graph = tlp::newGraph();

Nothing really new here, just the creation of the graph.

      //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      GraphObserverTest graphObserverTest;
      graph->addObserver(&graphObserverTest);
      //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

We add the instance of our class GraphObserverTest with the function `Graph::addObserver` .

We then perform some modifications on the graph :

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

Following is the output of this program :

    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

Prev | Up | Next
14. TUTORIAL 008 : Graph Tests | Home | Chapter 4. Tulip Open GL Library