aboutsummaryrefslogtreecommitdiff
path: root/structure/Graph.java
blob: 1c2dc0fee4f31dc2471a561e0914f494ad036a8e (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package structure;

import java.util.*;

import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.layout.springbox.EdgeSpring;
import org.graphstream.ui.swingViewer.basicRenderer.EdgeRenderer;
import org.graphstream.ui.util.EdgePoints;


public class MyGraph extends SingleGraph{

	
	private Grid grid;
	
	private int miss=0;
	private int success=0;
	
	public MyGraph(String title, Grid grid) {
		super(title);
		// Allow CSS on view
		System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
		// Set graph CSS
		this.addAttribute("ui.stylesheet", "url('resources/style.css')");

		// Assign grid
		this.grid=grid;
		
		int i=0;
		int ll=this.grid.getGrid().size();
		// Build node
		for(Router r : this.grid.getGrid()){
			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++;
			
		}
		
		// Build Edges
		this.buildEdges();
		
		//Build bestLink
		this.showBestLink();
		
	}

	
	
	public void buildEdges(){
		
		/*Iterator<Edge> edges=this.getEdgeIterator();
		while(edges.hasNext()){
			Edge edge=edges.next();
			this.removeEdge(edge);
		}*/
		
		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;
				 try{
						 Edge toAdd=this.addEdge(current+currentRouterName, current, currentRouterName);
						 toAdd.setAttribute("ui.label", relier.get(currentRouter));
				 }
				 catch(Exception e){
					// System.out.println("Bug de merde.");
				 }
				 
			 }
			 
		 }
	}
	
	public void showBestLink(){
		ArrayList<Integer> bestLink=this.grid.getLinks().get(this.grid.getBestLinkByProtocol());
		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)){
						int destIndex=bestLink.get(i+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;");
						}
					}
					
				}
			}
		}
	}

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

		if(this.grid.getBestLinkByProtocol()==this.grid.getBestLinkIndex()){
			this.success++;
		}
		else{
			this.miss++;
		}
		System.out.println("Success = " + this.success + "  Miss = " + this.miss + " try number :"+(this.success+this.miss)) ;
		
		//Build bestLink
		this.showBestLink();
	}
	
}