-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticGraph2.java
More file actions
115 lines (95 loc) · 3.79 KB
/
Copy pathStaticGraph2.java
File metadata and controls
115 lines (95 loc) · 3.79 KB
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
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
import java.util.Iterator;
public class StaticGraph2 {
private MultiGraph graph;
public StaticGraph2() {
/* set the graph renderer */
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
/* instantiate a multigraph - capable of having bidirectional edges between two nodes */
graph = new MultiGraph("Graph");
graph.addAttribute("ui.quality");
graph.addAttribute("ui.antialias");
/* add CSS styles */
graph.addAttribute("ui.stylesheet",
"edge { fill-color: grey; }");
/* display the graph */
graph.display(false);
generateGraph();
}
public void generateGraph() {
addNodes();
addEdges();
setNodeAttributes();
printNodeInfo();
}
public void addNodes() {
/* add nodes;
1st parameter: name of node
*/
Node nodeA = graph.addNode("A");
Node nodeB = graph.addNode("B");
Node nodeC = graph.addNode("C");
/* set nodes' position in space */
nodeA.setAttribute("xy", 0, 1);
nodeB.setAttribute("xy", 1, 1);
nodeC.setAttribute("xy", -1, -1);
}
public void addEdges() {
/* add edges;
1st parameter: name of edge
2nd parameter: source node
3rd parameter: target node
4th parameter: classifies the edge as either directed (boolean = true) or undirected (boolean = false) */
Edge ab = graph.addEdge("A_B", "A", "B", true);
Edge bc = graph.addEdge("B_C", "B", "C", true);
Edge ca = graph.addEdge("C_A", "C", "A", true);
Edge ba = graph.addEdge("B_A", "B", "A", true);
Edge cb = graph.addEdge("C_B", "C", "B", true);
Edge ac = graph.addEdge("A_C", "A", "C", true);
/* set label of the edge to be displayed on the graph */
ab.setAttribute("ui.label", "AB");
bc.setAttribute("ui.label", "BC");
ca.setAttribute("ui.label", "CA");
ba.setAttribute("ui.label", "BA");
cb.setAttribute("ui.label", "CB");
ac.setAttribute("ui.label", "AC");
}
public void setNodeAttributes() {
/* Iterate over all the nodes in the graph*/
Iterator<? extends Node> nodes = graph.getNodeIterator();
int i = 1;
while (nodes.hasNext()) {
Node currentNode = nodes.next();
int multiplier = 100;
/* We can also add other attributes to a node/edge like node sensor type, node sensor values, etc. */
currentNode.setAttribute("id", i++);
currentNode.setAttribute("sensors", "Humidity", "Temperature"); // array of sensors
currentNode.setAttribute("currentSensorValues", Math.random() * multiplier, Math.random() * multiplier); // array of sensor values
currentNode.setAttribute("batteryLevel", Math.random() * multiplier);
}
}
public void printNodeInfo() {
Iterator<? extends Node> nodes = graph.getNodeIterator();
while (nodes.hasNext()) {
Node currentNode = nodes.next();
/* We can also access the nodes' attributes. */
String nodeName = currentNode.getId();
int nodeId = (int) currentNode.getNumber("id");
Object[] nodeSensors = currentNode.getAttribute("sensors");
Object[] nodeCurrentSensorValues = currentNode.getAttribute("currentSensorValues");
double nodeBatteryLevel = currentNode.getNumber("batteryLevel");
/* Print node's information */
System.out.printf("Node %s id: %d\n", nodeName, nodeId);
System.out.printf("Node %s battery level: %f\n", nodeName, nodeBatteryLevel);
System.out.printf("Node %s (sensor : currentValue)\n", nodeName);
for (int i = 0; i < nodeSensors.length; i++) {
System.out.printf("\t %s : %f \n", nodeSensors[i], nodeCurrentSensorValues[i]);
}
System.out.println("");
}
}
public static void main(String[] args) {
StaticGraph2 staticGraph = new StaticGraph2();
}
}