1   package de.uni_stuttgart.informatik.canu.tripmodel.generators;
2   
3   /**
4    * <p>Title: Trip Model</p>
5    * <p>Description: </p>
6    * <p>Copyright: Copyright (c) 2002-2003</p>
7    * <p>Company: University of Stuttgart</p>
8    * @author Illya Stepanov
9    * @version 1.1
10   */
11  
12  import de.uni_stuttgart.informatik.canu.mobisim.core.*;
13  import de.uni_stuttgart.informatik.canu.mobisim.notifications.*;
14  import de.uni_stuttgart.informatik.canu.mobisim.extensions.Graph;
15  import de.uni_stuttgart.informatik.canu.senv.core.*;
16  import de.uni_stuttgart.informatik.canu.spatialmodel.core.*;
17  import de.uni_stuttgart.informatik.canu.spatialmodel.geometry.*;
18  import de.uni_stuttgart.informatik.canu.tripmodel.core.*;
19  import de.uni_stuttgart.informatik.canu.uomm.CommunicationsManager;
20  
21  /**
22   * This class randomly chooses initial position for a node
23   * @author Illya Stepanov
24   */
25  public class RandomInitialPositionGenerator extends ExtensionModule
26                                              implements InitialPositionGenerator
27  {
28  
29      /**
30      * Spatial Model
31      */
32      protected SpatialModel spatialModel = null;
33  
34      /**
35     * Set of points to be used as node initial positions
36     */
37    protected java.util.ArrayList points = new java.util.ArrayList();
38  
39    /**
40     * Constructor
41     */
42    public RandomInitialPositionGenerator()
43    {
44    }
45  
46    /**
47     * Returns the module's description. <br>
48     * <br>
49     * @return extension module's description
50     */
51    public String getDescription()
52    {
53      return "Random Initial Position Generation module";
54    }
55  
56    /**
57     * Performs the module initialization. <br>
58     * <br>
59     * The method is called after finishing the scenario file processing.
60     */
61    public void initialize()
62    {
63      //SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
64          // JHNote (06/02/2006): Now the SpatialModel is loaded at the beginning.
65  
66          Graph graph = spatialModel.getGraph();
67  
68      // check the points
69      java.util.Iterator iter = points.iterator();
70      while (iter.hasNext())
71      {
72        Point p = (Point)iter.next();
73  
74        if (graph==null)
75        {
76          if((p.getX()<0.0)||(p.getY()<0.0)||
77             (p.getX()>u.getDimensionX())||(p.getY()>u.getDimensionY()))
78          {
79            System.err.println("Fatal error: Position is outside Universe dimensions: Position3D("+p.getX()+","+p.getY()+")");
80            System.exit(1);
81          }
82        }
83        else
84        {
85          if((p.getX()<graph.getLeftmostCoordinate())||(p.getY()<graph.getLowermostCoordinate())||
86             (p.getX()>graph.getRightmostCoordinate())||(p.getY()>graph.getUppermostCoordinate()))
87          {
88            System.err.println("Fatal error: Position is outside movement area graph: Position3D("+p.getX()+","+p.getY()+")");
89            System.exit(1);
90          }
91        }
92      }
93    }
94  
95    /**
96     * Executes the extension. <br>
97     * <br>
98     * The method is called on every simulation timestep.
99     * @return 0 - the module should be executed on next timesteps,
100    *        -1 - the module should not be executed on further timesteps and should be removed from the extensions' list
101    */
102   public int act()
103   {
104     return 0;
105   }
106 
107   /**
108    * Chooses the node's initial position. <br>
109    * <br>
110    * @param node node
111    * @return node's initial position
112    */
113   public Point getInitialPosition(Node node)
114   {
115       Position3D ret = CommunicationsManager.storeInitPos(node.getID());
116       
117       if(ret!=null){
118         /*if(node!=null && (node.getID().equals("#0") || node.getID().equals("#1") || node.getID().equals("#2") || node.getID().equals("#3")|| node.getID().equals("#4")|| node.getID().equals("#5")|| node.getID().equals("#6") || node.getID().equals("#7") || node.getID().equals("#8") || node.getID().equals("#9"))){
119             
120           return new Point(108.0, 175.0);
121           }
122           if(node!=null && (node.getID().equals("#10") || node.getID().equals("#12")))
123           {
124                 
125               return new Point(120.0, 180.0);
126               }  
127           if(node!=null && (node.getID().equals("#11"))){
128                     
129                   return new Point(145.0, 188.0);
130                   }*/
131          /* if(node!=null && (node.getID().equals("#0") || node.getID().equals("#1")||node.getID().equals("#2")) ||node.getID().equals("#3") ||node.getID().equals("#4")){
132                 
133              return new Point(120.0, 140.0);
134           }
135           if(node!=null && (node.getID().equals("#22") || node.getID().equals("#10")||node.getID().equals("#8")) ||node.getID().equals("#7") ||node.getID().equals("#6")){
136                 
137                  return new Point(120.0, 140.0);
138               }
139           if(node!=null && (node.getID().equals("#12") || node.getID().equals("#13")||node.getID().equals("#14"))){
140                 
141                  return new Point(120.0, 140.0);
142               }
143           
144           if(node!=null && (node.getID().equals("#11") || node.getID().equals("#5") || node.getID().equals("#6") || node.getID().equals("#17")||node.getID().equals("#20")) ||node.getID().equals("#21") ||node.getID().equals("#23")){
145                 
146                  return new Point(125.0, 123.0);
147               }
148           if(node!=null && (node.getID().equals("#19") || node.getID().equals("#18")||node.getID().equals("#16")) ||node.getID().equals("#15") ||node.getID().equals("#24")){
149                 
150                  return new Point(125.0, 123.0);
151               }
152           if(node!=null && (node.getID().equals("#9"))){
153                 
154                  return new Point(182.0, 155.0);
155               }*/
156 
157 
158           
159           if(node!=null && (node.getID().equals("#5") || node.getID().equals("#6")||node.getID().equals("#7")) ||node.getID().equals("#8") ||node.getID().equals("#9")||node.getID().equals("#10")||node.getID().equals("#12")){
160                 
161               return new Point(0.0,0.0);
162               }
163           if(node!=null && (node.getID().equals("#0") || node.getID().equals("#1")||node.getID().equals("#2")) ||node.getID().equals("#3") ||node.getID().equals("#4")||node.getID().equals("#11")){
164                 
165               return new Point(0.0,0.0);
166               }
167           if(node!=null && (node.getID().equals("#13") || node.getID().equals("#14")||node.getID().equals("#15")) ||node.getID().equals("#16") ||node.getID().equals("#17") ||node.getID().equals("#18") ||node.getID().equals("#19")){
168                 
169               return new Point(0.0,0.0);
170               }
171           if(node!=null && (node.getID().equals("#20") || node.getID().equals("#21")||node.getID().equals("#22")) ||node.getID().equals("#23") ||node.getID().equals("#24")){
172                 
173               return new Point(0.0,0.0);
174               }
175           if(node!=null && (node.getID().equals("#25") || node.getID().equals("#26")||node.getID().equals("#27")) ||node.getID().equals("#28") ||node.getID().equals("#29")){
176                 
177               return new Point(0.0,0.0);
178               } 
179           if(node!=null && (node.getID().equals("#30") || node.getID().equals("#31")||node.getID().equals("#32")) ||node.getID().equals("#33") ||node.getID().equals("#34")){
180                 
181               return new Point(0.0,0.0);
182               }
183           if(node!=null && (node.getID().equals("#35") || node.getID().equals("#36")||node.getID().equals("#37")) ||node.getID().equals("#38") ||node.getID().equals("#39")){
184               
185               return new Point(26.0,34.0);
186               }
187           
188           if(node!=null && (node.getID().equals("#40") || node.getID().equals("#41")||node.getID().equals("#42")) ||node.getID().equals("#43") ||node.getID().equals("#44") ||node.getID().equals("#45") ||node.getID().equals("#46")){
189                 
190               return new Point(26.0,34.0);
191               }
192           if(node!=null && (node.getID().equals("#47") || node.getID().equals("#48")||node.getID().equals("#49"))){
193                 
194               return new Point(26.0,34.0);
195               }
196           if(node!=null && (node.getID().equals("#50") || node.getID().equals("#51")||node.getID().equals("#52")) ||node.getID().equals("#53") ||node.getID().equals("#54") ||node.getID().equals("#55") ||node.getID().equals("#56")){
197                 
198               return new Point(26.0,34.0);
199               }
200           if(node!=null && (node.getID().equals("#57") || node.getID().equals("#58")||node.getID().equals("#59"))){
201                 
202               return new Point(26.0,34.0);
203               }
204       
205           if(node!=null && (node.getID().equals("#60") || node.getID().equals("#61")||node.getID().equals("#62")) ||node.getID().equals("#63") ||node.getID().equals("#64") ||node.getID().equals("#65") ||node.getID().equals("#66")){
206                 
207               return new Point(26.0,34.0);
208               }
209           if(node!=null && (node.getID().equals("#67") || node.getID().equals("#68")||node.getID().equals("#69"))){
210                 
211               return new Point(26.0,34.0);
212               }
213           
214           if(node!=null && (node.getID().equals("#70") || node.getID().equals("#71")||node.getID().equals("#72")) ||node.getID().equals("#73") ||node.getID().equals("#74") ||node.getID().equals("#75") ||node.getID().equals("#76")){
215                 
216               return new Point(26.0,34.0);
217               }
218           if(node!=null && (node.getID().equals("#77") || node.getID().equals("#78")||node.getID().equals("#79"))){
219                 
220               return new Point(26.0,34.0);
221               }
222           if(node!=null && (node.getID().equals("#80") || node.getID().equals("#81")||node.getID().equals("#82")) ||node.getID().equals("#83") ||node.getID().equals("#84") ||node.getID().equals("#85") ||node.getID().equals("#86")){
223                 
224               return new Point(26.0,34.0);
225               }
226           if(node!=null && (node.getID().equals("#87") || node.getID().equals("#88")||node.getID().equals("#89"))){
227                 
228               return new Point(26.0,34.0);
229               }
230           
231          return new Point(ret.getX(), ret.getY()); 
232         // return new Point(0.0, 240.0);
233          
234          // return new Point(135.0, 97.0);
235       }
236       else{
237           return new Point(180.0, 240.0);
238           //return new Point(ret.getX(), ret.getY());
239         //  return new Point(135.0, 97.0);
240       }
241         //  return new Point(ret.getX(), ret.getY()); 
242   }
243 
244   /**
245    * Initializes the object from XML tag. <br>
246    * <br>
247    * @param element source tag
248    * @throws Exception Exception if parameters are invalid
249    */
250   public void load(org.w3c.dom.Element element) throws Exception
251   {
252     u.sendNotification(new LoaderNotification(this, u,
253       "Loading RandomInitialPositionGenerator extension"));
254 
255     super.load(element);
256 
257         // JHNote (06/02/2006): Now the SpatialModel is loaded at the beginning.
258         String sm = element.getAttribute("spatial_model");
259     if (sm.length()>0) {
260       spatialModel = (SpatialModel)u.getExtension(sm);
261     }
262 
263         else {
264             spatialModel = (SpatialModel)u.getExtension("SpatialModel");
265         }
266 
267         if(spatialModel==null)
268       throw new Exception("A SpatialModel is missing!");
269 
270     org.w3c.dom.Node n;
271 
272     n = element.getElementsByTagName("points").item(0);
273     if(n!=null)
274     {
275       String fileSource = n.getFirstChild().getNodeValue();
276 
277       java.io.BufferedReader source = new java.io.BufferedReader(new java.io.FileReader(fileSource));
278 
279       String s;
280       // read next record
281       while ((s = source.readLine())!=null)
282       {
283         String ss[] = s.split(" ");
284 
285         double x = Double.parseDouble(ss[0]);
286         double y = Double.parseDouble(ss[1]);
287 
288         points.add(new Point(x, y));
289       }
290     }
291 
292     u.sendNotification(new LoaderNotification(this, u,
293       "Finished loading RandomInitialPositionGenerator extension"));
294   }
295 }