GraphStream Tutorials

Let's get some coding done



 
DGS0004
cg "id":"GraphStream Team" 
an "Stefan Balev"
an "Antoine Dutot" 
an "Yoann Pigné"
an "Guilhelm Savin"

Outlook

  1. Installation of GraphStream
  2. Tutorial 1: Basic tasks with GraphStream
  3. Tutorial 2: A first dynamic graph

Installation of GraphStream

Basic steps to install GraphStream

  1. Get a working java SDK installed
  2. Get Eclipse installed
  3. Get the GraphStream tutorial sources
  4. Get GraphStream binaries

Get Eclipse Installed

Get the tutorial workspace

Import into Eclipse

Get GraphStream binaries

Tutorial 1



Basic tasks with GraphStream

Create and display

Open src > org > graphstream > demo > tutorial1 > Tutorial1.java

import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;

public class Tutorial1 {
	public static void main(String args[]) {
		Graph graph = new SingleGraph("Tutorial 1");
		
		graph.display();
		
		graph.addNode("A");
		graph.addNode("B");
		graph.addEdge("AB", "A", "B");
		graph.addNode("C");
		graph.addEdge("BC", "B", "C", true); // Directed edge.
		graph.addEdge("CA", "C", "A");
	}
}

Maven : mvn exec:java -Dexec.mainClass="org.graphstream.demo.tutorial1.Tutorial1"

Basic tasks with GS

We can improve the display with some CSS:

...
graph.display();

graph.addAttribute("ui.quality");
graph.addAttribute("ui.antialias");
graph.addAttribute("ui.stylesheet",
	"edge { fill-color: grey; }");

graph.addNode("A");
...

Basic tasks with GS

Basic tasks with GS

Basic tasks with GS

Edge ab = graph.getEdge("AB");
Edge bc = graph.getEdge("BC");
Edge ca = graph.getEdge("CA");

ab.setAttribute("ui.label", "AB");
bc.setAttribute("ui.label", "BC");
ca.setAttribute("ui.label", "CA");

Basic tasks with GS

ab.setAttribute("aNumber", 10);
bc.setAttribute("anObject", new Double(10));
ca.setAttribute("anArrayOfThings", 1, 2, 3);

Basic tasks with GS

int value1 = ((Number) ab.getAttribute("aNumber")).intValue();
double value2 = bc.getAttribute("anObject");
Object[] value3 = ca.getAttribute("anArrayOfThings");

double value4 = ab.getNumber("aNumber");
double value5 = bc.getNumber("anObject");

Basic tasks with GS

for(Node n: graph) {
	System.out.println(n.getId());
}

for(Edge e: graph.getEachEdge()) {
	System.out.println(e.getId());
}

Basic tasks with GS

Iterator<? extends Node> nodes = graph.getNodeIterator();

while(nodes.hasNext()) {
	System.out.println(nodes.next().getId());
}

Iterator<? extends Edge> edges = graph.getEdgeIterator();

while(edges.hasNext()) {
	System.out.println(edges.next().getId());
}		

Basic tasks with GS

int n = graph.getNodeCount();
for(int i=0; i<n; i++) {
	System.out.println(graph.getNode(i).getId());
}

int n = graph.getEdgeCount();
for(int i=0; i<n; i++) {
	System.out.println(graph.getEdge(i).getId());
}

Be careful: indices remain the same as long as the graph is unchanged. But as soon as an addition or removal occurs, indices are no longer tied to their old node or edge.

Basic tasks with GS

import static org.graphstream.algorithm.Toolkit.*;
...
Node node = randomNode(graph);

for(Edge e: node.getEachEdge()) {
	System.out.printf("neighbor %s via %s%n",
		e.getOpposite(node).getId(),
		e.getId() );
}

Basic tasks with GS

Node node = getRandomNode(graph);
Iterator<? extends Edge> edges = node.getLeavingEdgeIterator();

Iterator<? extends Edge> edges = node.getEnteringEdgeIterator();

System.out.println(“Node degree %d (entering %d, leaving %d)%n”,
	node.getDegree(),
	node.getInDegree(),
	node.getOutDegree());

Tutorial 2



A first dynamic graph

A first dynamic graph

A first dynamic graph

An element sink must follow the interface:

public interface ElementSink {
	void nodeAdded( ... );
	void nodeRemoved( ... );
	void edgeAdded( ... );
	void edgeRemoved( ... );
	void graphCleared( ... );
	void stepBegins( ... );
}

A first dynamic graph

An attribute sink must follow the interface:

public interface AttributeSink {
	void graphAttributeAdded( ... );
	void graphAttributeChanged( ... );
	void graphAttributeRemoved( ... );

	void nodeAttributeAdded( ... );
	void nodeAttributeChanged( ... );
	void nodeAttributeRemoved( ... );

	void edgeAttributeAdded( ... );
	void edgeAttributeChanged( ... );
	void edgeAttributeRemoved( ... );
}

A first dynamic graph

A source is an interface that only defines methods to handle a set of sinks.

public interface Source {
	void addSink(Sink sink);
	void removeSink(Sink sink);
	void addAttributeSink(AttributeSink sink);
	void removeAttributeSink(AttributeSink sink);
	void addElementSink(ElementSink sink);
	void removeElementSink(ElementSink sink);
	void clearElementSinks();
	void clearAttributeSinks();
	void clearSinks();
}

A first dynamic graph





A first dynamic graph

A first dynamic graph

A DGS file looks like this. You can find it in the data > tutorial2.dgs

DGS003
"Tutorial 2" 0 0
an "A"
an "B"
an "C"
ae "AB" "A" "B"
ae "BC" "B" "C"
ae "CA" "C" "A"
ce "AB" label="AB"
ce "BC" label="BC"
ce "CA" label="CA"
  • an add a node and ae an edge.
  • ae "AB" "A" > "B" adds a directed edge.
  • cn, ce and cg change or add one or more attributes on a node, edge or graph.
  • dn and de allow to remove nodes, edges.

A first dynamic graph

A first dynamic graph

st 2
an "D"
an "E"
ae "BD" "B" "D"
ae "CE" "C" "E"
ae "DE" "D" "E"
de "AB"
dn "A"
st 3

A first dynamic graph

graph.read("tutorial2.dgs");

A first dynamic graph

public class Tutorial2 {
	public static void main(String args[]) {
		Graph graph = new SingleGraph("Tutorial2");
		graph.display();
		graph.addAttribute("ui.antialias");
		try {
			FileSource source = FileSourceFactory.sourceFor(
				"tutorial2.dgs");
			source.addSink(graph);
			source.begin("tutorial2.dgs");
			while(source.nextEvents());
			source.end();
		} catch(Exception e) { e.printStackTrace(); }
	}
}

A first dynamic graph

		while(source.nextEvents()) { Thread.sleep(1000); }

  		while(source.nextStep()) { Thread.sleep(1000); }

A first dynamic graph

an "A" x=0 y=1
an "B" x=1 y=-1
an "C" x=-1 y=-1

an "D" x=1 y=1 
an "E" x=-1 y=1

A first dynamic graph

	graph.display(false);

Slides and Material



Get the Slides and Materials online:

http://graphstream-project.org/doc/Tutorials/Lab-Sessions/Simtools-2013/

https://github.com/graphstream/gs-talk-simtools