Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[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 )
37         {
38                 super( _gr, _gd ) ;
39                 name = "DefaultMapping" ;
40         }
41         
42
43
44         @Override
45         public void map() 
46         {
47                 /** If the mapping is possible ... **/
48                 if( gr.getNbGTask() <= gl.getNbGNode() )
49                 {
50                         atraiter = gr.getGraph() ;
51                         archi = gl.getFreeGNodes() ;
52                         
53                         
54                         System.out.println( "*******************************************" ) ;
55                         System.out.println( "* Launching the Default Mapping algorithm *" ) ;
56                         System.out.println( "*******************************************\n\n" ) ;
57                         
58                         /** Save the Mapping **/
59                         for( int i = 0 ; i < atraiter.size() ; i++ )
60                         {
61                                 Random r = new Random() ;
62                                 int ret = r.nextInt( archi.size() ) ;
63                                 mp.addMapping( archi.remove( ret ), atraiter.get( i ) ) ;
64                         }
65                 
66                 } else {
67                         System.err.println( "\n\n!!! Unable to map application !\n\n" ) ;
68                 }
69         }
70
71
72         @Override
73         public GNode replaceNode( GNode _dead, ArrayList<GNode> _ag ) 
74         {
75                 GNode ret = null ;
76                 
77                 if( _dead != null && _ag != null )
78                 {
79                         int pos = 0 ;
80                         pos = mp.getIdOfAssociation( _dead ) ;
81                         
82                         if( pos == -1 )
83                         {
84                                 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
85                                 return null ;
86                         }
87                         
88                         if( _ag.size() > 0 )
89                         {
90                                 Random r = new Random() ;
91                                 ret = _ag.get( r.nextInt( _ag.size() ) ) ;
92                                 
93                         } else {
94                                 System.err.println( "Not enought available nodes in gnodes to replace one !" ) ;
95                                 return null ;
96                         }       
97                         
98                         nb_fault++ ;
99                 }
100                 
101                 return ret ;
102         }
103
104
105         @Override
106         public GNode getOtherGNode( ArrayList<GNode> _ag ) 
107         {
108                 /** Returning the first node in the list **/
109                 if( _ag.size() > 0 )
110                 {
111                         return _ag.get( 0 ) ;
112                 } 
113                 
114                 return null ;
115         }
116
117
118         @Override
119         public boolean setParams(Object[] _params) {
120                 return true ;
121         }
122 }
123
124
125
126 /** La programmation est un art, respectons ceux qui la pratiquent !! **/