Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Creation of Mapping repository.
[mapping.git] / src / and / Mapping / Grid.java
1 package and.Mapping ;
2
3
4 import java.io.Serializable;
5 import java.util.ArrayList;
6
7
8
9 /**
10  * Class representing a computing grid, composed of multiple clusters
11  * @author Sébastien Miquée
12  */
13 public class Grid implements Serializable
14 {
15         private static final long serialVersionUID = 1L;
16
17         
18         private int nb_cluster ;
19         private int nb_node ;
20         private ArrayList<Cluster> clusters ;
21         private ArrayList<GNode> gnodesList;
22         private boolean gnodesList_done;
23         
24         
25         /**
26          * Default constructor
27          */
28         public Grid()
29         {
30                 nb_cluster = 0 ;
31                 nb_node = 0 ;
32                 clusters = new ArrayList<Cluster>() ;
33                 gnodesList = null ;
34                 gnodesList_done = false ;
35         }
36         
37         
38         /**
39          * Add a cluster in the grid.
40          * @param c Cluster to be add
41          */
42         public void addCluster( Cluster c )
43         {
44                 if( c != null )
45                 {
46                         clusters.add( c ) ;
47                         nb_cluster ++ ;
48                         nb_node += c.getNbGNode() ;
49                 }
50         }
51         
52         
53         /**
54          * Add a clusters list in the grid.
55          * @param al List of clusters to be add
56          */
57         public void addClusters( ArrayList<Cluster> al )
58         {
59                 if( al != null )
60                 {
61                         for( int i = 0 ; i < al.size() ; i++ )
62                         {
63                                 clusters.add( al.get( i ) ) ;
64                                 nb_cluster ++ ;
65                                 nb_node += al.get( i ).getNbGNode() ;
66                         }
67                 }
68         }
69
70         
71         /**
72          * Return the amount of clusters in the grid.
73          * @return The amount of clusters
74          */
75         public int getNbCluster()
76         {
77                 return nb_cluster ;
78         }
79         
80         
81         /**
82          * Return the amount of computing nodes in the grid.
83          * @return The amount of computing nodes
84          */
85         public int getNbGNode()
86         {
87                 return nb_node ;
88         }
89         
90         
91         /**
92          * Return the grid in a clusters list view.
93          * @return Clusters list
94          */
95         public ArrayList<Cluster> getClusters()
96         {
97                 return clusters ;
98         }
99         
100         
101         /**
102          * Initialization of clusters.
103          */
104         public void initClusters()
105         {
106                 for( int i = 0 ; i < nb_cluster ; i++ )
107                 {
108                         clusters.get( i ).initIndice() ;
109                 }
110         }
111         
112         
113         /**
114          * Compute and return the distance between two clusters.
115          * @param _g1 First cluster
116          * @param _g2 Second cluster
117          * @return The distance between the two clusters
118          */
119         public double getDistance( GNode _g1, GNode _g2 )
120         {
121                 double d = 0 ;
122                 
123                 if( _g1.equals( _g2 ) ) 
124                 {
125                         return d ;
126                 }
127                 
128                 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
129                 
130                 for( int i = 0 ; i < clusters.size() ; i++ )
131                 {
132                         if( clusters.get( i ).isIn( _g1) )
133                         {
134                                 cluster1 = clusters.get( i ).getName() ;
135                                 site1 = clusters.get( i ).getSite() ;
136                         }
137                         
138                         if( clusters.get( i ).isIn( _g2) )
139                         {
140                                 cluster2 = clusters.get( i ).getName() ;
141                                 site2 = clusters.get( i ).getSite() ;
142                         }
143                 }
144                 
145                 //
146                 
147                 if( cluster1.compareTo( cluster2 ) == 0 )
148                 {
149                         d = 1 ;
150                 } else {
151                         if( site1.compareTo( site2 ) == 0 )
152                         {
153                                 d = 2 ;
154                         } else {
155                                 d = 3 ;
156                         }
157                 }
158                 
159                 return d ;
160         }
161         
162         
163         /**
164          * Return the list of computing nodes in the grid.
165          * @return The list of computing nodes
166          */
167         public ArrayList<GNode> getGNodes()
168         {
169                 if( ! gnodesList_done )
170                 {
171                         gnodesList = new ArrayList<GNode>() ;
172                         
173                         for( int i = 0 ; i < clusters.size() ; i++ )
174                         {
175                                 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
176                                 
177                                 for( int j = 0 ; j < ar.size() ; j++ )
178                                 {
179                                         gnodesList.add( ar.get( j ) ) ;
180                                 }
181                         }
182                         
183                         gnodesList_done = true ;
184                 }
185                 
186                 return gnodesList ;
187         }
188         
189         
190         /**
191          * Plop !!
192          * @param _gnodes
193          */
194         public void updateGrid( ArrayList<GNode> _gnodes )
195         {
196                 if( _gnodes != null && _gnodes.size() != 0 )
197                 {
198                         for( int i = 0 ; i < _gnodes.size() ; i++ )
199                         {
200                                 
201                         }
202                         
203                         gnodesList_done = false ;
204                 }
205         }
206         
207         
208         /**
209          * Remove a computing node from the grid.
210          * @param _dead The node to be removed
211          */
212         public void removeGNode( GNode _dead ) 
213         {
214                 if( _dead != null )
215                 {
216                         if( _dead.getMapped() )
217                         {
218                                 String site = "", cluster = "" ;
219                         
220                                 site = _dead.getSite() ;
221                                 cluster = _dead.getCluster() ;
222                         
223                                 /* Removing GNode from its cluster */                   
224                                 for( int j = 0 ; j < clusters.size() ; j++ )
225                                 {
226                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
227                                         {
228                                                 clusters.get( j ).removeGNode( _dead ) ;
229                                         
230                                                 break ;
231                                         }
232                                 }
233                         }
234                         
235                         /* Removing the dead node from the global list */
236                         for( int i = 0 ; i < nb_node ; i++ )
237                         {
238                                 if( _dead.getId() == gnodesList.get( i ).getId() )
239                                 {
240                                         gnodesList.remove( i ) ;
241                                         break ;
242                                 }
243                         }
244                 }
245                 
246                 nb_node-- ;
247                 gnodesList_done = false ;
248         }
249         
250         
251         /**
252          * Compute the heterogeneity degree of the grid.
253          * This is based on a ratio between the average and the 
254          * standard deviation of computing nodes' power.
255          * @return The heterogeneity degree of the grid
256          */
257         public double getHeterogenityDegre()
258         {
259                 double hd = -1 ;
260                 
261                 
262                 return hd ;
263         }
264         
265         
266         /**
267          * Print a comprehensible text version of the grid.
268          */
269         public void print()
270         {
271                 System.out.println();
272                 System.out.println( "\t=> Grid composition :\n" ) ; 
273                 for( int i = 0 ; i < nb_cluster ; i++ )
274                 {
275                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
276                 }
277                 
278                 System.out.println();
279         }
280         
281 }
282
283
284 /** La programmation est un art, respectons ceux qui la pratiquent !! **/