Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3de90b11a51db15882eb23fae341d14169fdea9f
[mapping.git] / src / and / Mapping / DefaultMapping.java
1 package and.Mapping ;
2
3
4 import java.util.ArrayList;
5 import java.util.Random;
6
7
8 /**
9  * Implementation of JaceP2P default mapping
10  * @author Sébastien Miquée
11  * @version 1.0
12  */
13 public class DefaultMapping extends Algo
14 {
15         private static final long serialVersionUID = 1L;
16
17         
18         private ArrayList<GTask> atraiter ;
19         private ArrayList<GNode> archi ;
20         
21         /**
22          * Default constructor.
23          */
24         public DefaultMapping()
25         {
26                 super() ;
27                 name = "DefaultMapping" ;
28         }
29         
30
31         /**
32          * Constructor.
33          * @param _gr Tasks graph to be mapped
34          * @param _gd Grid graph
35          */
36         public DefaultMapping( Graph _gr, Grid _gd, ArrayList<GNode> _gnodes )
37         {
38                 super( _gr, _gd ) ;
39                 archi = _gnodes ;
40                 name = "DefaultMapping" ;
41         }
42         
43
44
45         @Override
46         public void map() 
47         {
48                 /** If the mapping is possible ... **/
49                 if( gr.getNbGTask() <= gl.getNbGNode() )
50                 {
51                         atraiter = gr.getGraph() ;
52                         
53                         System.out.println( "*******************************************" ) ;
54                         System.out.println( "* Launching the Default Mapping algorithm *" ) ;
55                         System.out.println( "*******************************************\n\n" ) ;
56                         
57                         /** Save the Mapping **/
58                         for( int i = 0 ; i < atraiter.size() ; i++ )
59                         {
60                                 mp.addMapping( new Association( archi.get( i ), atraiter.get( i ) ) ) ;
61                         }
62                 
63                 } else {
64                         System.err.println( "\n\n!!! Unable to map application !\n\n" ) ;
65                 }
66         }
67
68
69         @Override
70         public GNode replaceNode( GNode _dead, ArrayList<GNode> _ag ) 
71         {
72                 GNode ret = null ;
73                 
74                 if( _dead != null && _ag != null )
75                 {
76                         int pos = 0 ;
77                         pos = mp.getIdOfAssociation( _dead ) ;
78                         
79                         if( pos == -1 )
80                         {
81                                 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
82                                 return null ;
83                         }
84                         
85                         if( _ag.size() > 0 )
86                         {
87                                 Random r = new Random() ;
88                                 ret = _ag.get( r.nextInt( _ag.size() ) ) ;
89                                 
90                         } else {
91                                 System.err.println( "Not enought available nodes in gnodes to replace one !" ) ;
92                                 return null ;
93                         }       
94                         
95                         nb_fault++ ;
96                 }
97                 
98                 return ret ;
99         }
100
101
102         @Override
103         public GNode getOtherGNode( ArrayList<GNode> _ag ) 
104         {
105                 /** Returning the first node in the list **/
106                 if( _ag.size() > 0 )
107                 {
108                         return _ag.get( 0 ) ;
109                 } 
110                 
111                 return null ;
112         }
113 }
114
115
116
117 /** La programmation est un art, respectons ceux qui la pratiquent !! **/