Demonstrations of Sample Code Projects
Politechnika Poznańska – April 24th 2018
In that project, we want the Demos/
folder.
public class Demo1 {
public static void main(String args[]) {
System.setProperty("org.graphstream.ui", "swing");
Graph graph = new SingleGraph("Demo 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");
}
}
We can improve the display with some CSS:
They are accessible at creation time:
Or after creation:
Nodes and edges are removed the same way:
There are many other ways to modify the graph.
Data stored in the graph, on nodes and edges, are called “attributes”. An attribute is a pair (name,value).
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");
for(String id : new String[]{"A", "B", "C"}){
graph.getNode(id).setAttribute("ui.label", id);
}
graph.setAttribute("ui.stylesheet", "url(data/style.css);");
setAttribute()
and a variable number of arguments:Several ways to retrieve attributes:
int value1 = ((Number) ab.getAttribute("aNumber")).intValue();
double value2 = (double) bc.getAttribute("anObject");
Object[] value3 = (Object[]) ca.getAttribute("anArrayOfThings");
Special methods are here to simplify access:
GraphStream 2.0 uses Java 8 streams.
Access all nodes:
Equally for edges:
Indices for nodes:
int n = graph.getNodeCount();
for(int i=0; i<n; i++) {
System.out.println(graph.getNode(i).getId());
}
Indices for edges:
int n = graph.getEdgeCount();
for(int i=0; i<n; i++) {
System.out.println(graph.getEdge(i).getId());
}
⚠ indices remain the same as long as the graph is unchanged. ⚠
You can also travel the graph using nodes:
import static org.graphstream.algorithm.Toolkit.*;
//...
Node node = randomNode(graph);
node.edges().forEach(e -> {
System.out.printf("neighbor %s via %s%n",
e.getOpposite(node).getId(),
e.getId() );
})
Toolkit
is set of often used functions and small algorithms (see the API).Directed edges stream from a given node:
Get a node’s degree, entering degree or leaving degree:
Source.addSink(Sink)
method.addElementSink(ElementSink)
. Nodes and edges are Elements.addAttributeSink(AttributeSink)
. Data attributes are stored on every element.Sink
is both an ElementSink
and AttributeSink
.ElementSink is an interface
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 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();
}
Since Graph is a sink let’s create a graph from a set of events generated by a source.
an
for “add node”.ae
for “add edge”. ae "AB" "A" > "B"
adds a directed edge between nodes A
and B
.cn
, ce
and cg
change or add one or more attributes on a node, an edge or the graph.dn
and de
allow to remove nodes, edges.st <number>
The ability to remove nodes and edges makes the format support dynamic.
The file can be read entirely :
We can read the DGS file event by event using an input source:
nextEvents()
method reads the file event by event (line by line in the file)nextStep()
methods reads events up to the next st
command (all the lines between two st
lines)x
and y
attributes:Then one have to tell the viewer not to compute nodes positions:
The gs-geography
project brings the opportunity to read geographical data file format and produce graphs.
For instance, one can read an OpenStreetMap map to produce a graph of the road network, where nodes would be intersections and edges would be roads.