routage-optimiste/structure/MyGraph.java

152 lines
3.4 KiB
Java
Raw Normal View History

2016-03-21 17:28:53 +01:00
package structure;
import java.util.*;
2016-04-07 20:36:17 +02:00
import org.graphstream.graph.*;
2016-03-21 17:28:53 +01:00
import org.graphstream.graph.implementations.SingleGraph;
2016-04-07 20:36:17 +02:00
/**
* Class for display graph with GraphStream
* @author loic, adama
*
*/
2016-03-21 17:28:53 +01:00
public class MyGraph extends SingleGraph{
2016-04-07 20:36:17 +02:00
// Grid associate to the graph
2016-03-21 17:28:53 +01:00
private Grid grid;
2016-04-07 20:36:17 +02:00
/**
* Build a graph with a grid
*
* @param title
* @param grid
*/
2016-03-21 17:28:53 +01:00
public MyGraph(String title, Grid grid) {
super(title);
// Allow CSS on view
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
2016-04-07 20:22:05 +02:00
2016-03-21 17:28:53 +01:00
// Set graph CSS
this.addAttribute("ui.stylesheet", "url('resources/style.css')");
// Assign grid
this.grid=grid;
2016-03-21 18:07:12 +01:00
int i=0;
int ll=this.grid.getGrid().size();
2016-03-21 17:28:53 +01:00
// Build node
for(Router r : this.grid.getGrid()){
2016-03-21 18:07:12 +01:00
if(i==0){
this.addNode(r.name).setAttribute("ui.label", "Source");
}
else{
if(i==ll-1){
this.addNode(r.name).setAttribute("ui.label", "Destination");
}
else{
this.addNode(r.name);
}
}
i++;
2016-03-21 17:28:53 +01:00
}
// Build Edges
this.buildEdges();
//Build bestLink
this.showBestLink();
}
2016-04-07 20:36:17 +02:00
/**
* Build edges
*/
2016-03-21 17:28:53 +01:00
public void buildEdges(){
2016-03-21 18:02:31 +01:00
2016-03-21 17:28:53 +01:00
for(Router r : this.grid.getGrid()){
String current=r.name;
HashMap<Router, Integer> relier=r.getLinks();
Set<Router> k=relier.keySet();
Iterator<Router> i=k.iterator();
while(i.hasNext()){
Router currentRouter=i.next();
2016-03-21 18:02:31 +01:00
2016-03-21 17:28:53 +01:00
String currentRouterName=currentRouter.name;
try{
2016-03-21 18:02:31 +01:00
Edge toAdd=this.addEdge(current+currentRouterName, current, currentRouterName);
2016-03-21 17:28:53 +01:00
toAdd.setAttribute("ui.label", relier.get(currentRouter));
}
catch(Exception e){
2016-04-07 20:36:17 +02:00
}
2016-03-21 17:28:53 +01:00
}
}
}
2016-04-07 20:36:17 +02:00
/**
* Update best link on screen
*/
2016-03-21 17:28:53 +01:00
public void showBestLink(){
2016-03-21 20:44:25 +01:00
ArrayList<Integer> bestLink=this.grid.getLinks().get(this.grid.getBestLinkByProtocol());
2016-03-21 17:28:53 +01:00
for(int i=0;i<bestLink.size();i++){
Iterator<Node> nodes= this.getNodeIterator();
while(nodes.hasNext()){
Node node=nodes.next();
Iterator<Edge> edges=node.getEdgeIterator();
while(edges.hasNext()){
Edge edge=edges.next();
if(i<(bestLink.size()-1)){
String src=this.grid.getGrid().get(bestLink.get(i)).name;
String dest=this.grid.getGrid().get(bestLink.get(i+1)).name;
if((edge.getNode0().getId().equals(src) && edge.getNode1().getId().equals(dest))||(edge.getNode1().getId().equals(src) && edge.getNode0().getId().equals(dest))){
edge.setAttribute("ui.style", "fill-color:red;");
}
}
}
}
}
}
2016-04-07 20:36:17 +02:00
/**
* Update all the graph on screen
*/
2016-03-21 18:02:31 +01:00
public void update(){
// Reset color
Iterator<Edge> edges=this.getEdgeIterator();
while(edges.hasNext()){
Edge edge=edges.next();
edge.setAttribute("ui.style", "fill-color:black;");
}
// Update label
edges=this.getEdgeIterator();
while(edges.hasNext()){
Edge edge=edges.next();
for(Router r : this.grid.getGrid()){
String current=r.name;
HashMap<Router, Integer> relier=r.getLinks();
Set<Router> k=relier.keySet();
Iterator<Router> i=k.iterator();
while(i.hasNext()){
Router currentRouter=i.next();
String currentRouterName=currentRouter.name;
if(edge.getId().equals(current+currentRouterName)||edge.getId().equals(currentRouterName+current)){
edge.setAttribute("ui.label", relier.get(currentRouter));
}
}
2016-04-07 20:36:17 +02:00
}
2016-03-21 20:44:25 +01:00
}
2016-03-21 18:02:31 +01:00
this.showBestLink();
}
2016-03-21 17:28:53 +01:00
}