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    * <p>Patches: </p>
9    * <p>  v1.2 (30/11/2005) In order to reflect direction, 
10   *                                              if we use a SpaceGraph elemet, we need to set the 
11   *                                              reflect direction to true in both Elements. 
12   *                                              Plus, the Generator returns the Algorithm used.  </p>
13   * @author Illya Stepanov
14   * @author v1.2 Jerome Haerri (haerri@ieee.org)
15   * @version 1.2
16   */
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  import java.util.Set;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  import de.uni_stuttgart.informatik.canu.mobisim.core.ExtensionModule;
27  import de.uni_stuttgart.informatik.canu.mobisim.core.Node;
28  import de.uni_stuttgart.informatik.canu.mobisim.core.Notification;
29  import de.uni_stuttgart.informatik.canu.mobisim.core.Position3D;
30  import de.uni_stuttgart.informatik.canu.mobisim.extensions.Graph;
31  import de.uni_stuttgart.informatik.canu.mobisim.notifications.LoaderNotification;
32  import de.uni_stuttgart.informatik.canu.mobisim.notifications.RouteChoiceNotification;
33  import de.uni_stuttgart.informatik.canu.senv.core.Vertex;
34  import de.uni_stuttgart.informatik.canu.spatialmodel.core.SpatialModel;
35  import de.uni_stuttgart.informatik.canu.spatialmodel.geometry.Point;
36  import de.uni_stuttgart.informatik.canu.tripmodel.core.PathSearchingAlgorithm;
37  import de.uni_stuttgart.informatik.canu.tripmodel.core.Trip;
38  import de.uni_stuttgart.informatik.canu.tripmodel.core.TripGenerator;
39  import de.uni_stuttgart.informatik.canu.tripmodel.pathalgorithms.Dijkstra;
40  import de.uni_stuttgart.informatik.canu.tripmodel.pathalgorithms.PedestrianStochPathSelection;
41  import de.uni_stuttgart.informatik.canu.tripmodel.pathalgorithms.SpeedPathSelection;
42  import de.uni_stuttgart.informatik.canu.uomm.CommunicationsManager;
43  
44  
45  /**
46   * This class generates random trips for a node
47   * <p>Patches: </p>
48   * <p>  <i> Version 1.2 by Jerome Haerri (haerri@ieee.org) on 11/30/2005: 
49   * <br> &nbsp;&nbsp;&nbsp;&nbsp; In order to reflect direction, 
50   *                                              if we use a SpaceGraph element with differentiated flows, we need to set the 
51   *                                              reflect direction to true in both Elements. 
52   * <br>  &nbsp;&nbsp;&nbsp;&nbsp; The Generator returns the Algorithm used.  </i></p>
53   * @author 1.0-1.1 Illya Stepanov 
54   * @author 1.2 Jerome Haerri
55   * @version 1.2
56   */
57  public class RandomTripGenerator extends ExtensionModule
58                                         implements TripGenerator
59  {
60    
61      /**
62      * Spatial Model
63      */
64      protected SpatialModel spatialModel=null;
65      
66      /**
67     * Minimal stay duration at destination (ms)
68     */
69    protected int minStay = 0;        // in ms
70    /**
71     * Maximal stay duration at destination (ms)
72     */
73    protected int maxStay = 0;        // in ms
74  
75    /**
76     * Set of points to be used as destinations during node movements
77     */
78    protected java.util.ArrayList points = new java.util.ArrayList();
79    
80    /**
81     * Path searching algorithm
82     */
83    protected PathSearchingAlgorithm algo = new Dijkstra();
84    
85    /**
86     * Flag to reflect or ignore the road directions during the path calculation
87     */
88    protected int reflect_directions = PathSearchingAlgorithm.FLAG_IGNORE_DIRECTIONS;
89      private ConcurrentHashMap<String,ArrayList> pathPoints = new ConcurrentHashMap<String,ArrayList>();
90    protected boolean routeFlag = false;
91    public boolean isRouteFlag() {
92      return routeFlag;
93  }
94  
95  public void setRouteFlag(boolean routeFlag) {
96      this.routeFlag = routeFlag;
97  }
98  public ConcurrentHashMap<String,ArrayList> getPathPoints() {
99      return pathPoints;
100 }
101 
102 public void setPathPoints(ConcurrentHashMap<String,ArrayList> pathPoints) {
103     this.pathPoints = pathPoints;
104 }
105 
106 /**
107    * Constructor
108    */
109   public RandomTripGenerator()
110   {
111       super("RandomTripGenerator");
112         
113         u.addNotificationListener(this);    
114   }
115 
116   /**
117    * Returns the module's description. <br>
118    * <br>
119    * @return extension module's description
120    */
121   public String getDescription()
122   {
123     return "Random Trip Generation module";
124   }
125     
126     /**
127    * Returns the PathSearch Algorithm used. <br>
128    * <br>
129    * @return algo Algorithm used for Path Searching
130      * @since 1.2 
131    */
132     public  PathSearchingAlgorithm getAlgo()
133   {
134     return algo;
135   }
136     
137     public void sendNotification(Notification notification)
138       {
139         if(notification instanceof RouteChoiceNotification){
140             boolean myFlag = ((RouteChoiceNotification) notification).isRouteFlag();
141             setRouteFlag(myFlag);
142                 
143             ConcurrentHashMap<String,ArrayList> routeMap = ((RouteChoiceNotification) notification).getPathPoints();
144             setPathPoints(routeMap);
145         }
146         
147       }
148   /**
149    * Performs the module initialization. <br>
150    * <br>
151    * The method is called after finishing the scenario file processing.
152    */
153   public void initialize()
154   {
155     //SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
156         // JHNote (06/02/2006): Now the SpatialModel is loaded at the beginning.
157         
158         Graph graph = spatialModel.getGraph(); 
159         
160     // check the points
161     java.util.Iterator iter = points.iterator();
162     while (iter.hasNext())
163     {
164       Point p = (Point)iter.next();
165 
166       if (graph==null)
167       { 
168         if((p.getX()<0.0)||(p.getY()<0.0)||
169            (p.getX()>u.getDimensionX())||(p.getY()>u.getDimensionY()))
170         {
171           System.err.println("Fatal error: Position is outside Universe dimensions: Position3D("+p.getX()+","+p.getY()+")");
172           System.exit(1);
173         }
174       }
175       else
176       {
177         if((p.getX()<graph.getLeftmostCoordinate())||(p.getY()<graph.getLowermostCoordinate())||
178            (p.getX()>graph.getRightmostCoordinate())||(p.getY()>graph.getUppermostCoordinate()))
179         {
180           System.err.println("Fatal error: Position is outside movement area graph: Position3D("+p.getX()+","+p.getY()+")");
181           System.exit(1);
182         }
183       }
184     }
185   }
186 
187   /**
188    * Executes the extension. <br>
189    * <br>
190    * The method is called on every simulation timestep. 
191    * @return 0 - the module should be executed on next timesteps,
192    *        -1 - the module should not be executed on further timesteps and should be removed from the extensions' list
193    */
194   public int act()
195   {
196     return 0;
197   }
198 
199   public Trip genTrip(Node node)
200   {
201       int vCnt = 0;
202         Trip trip = null;
203         ArrayList<Vertex> verticesList = new ArrayList<Vertex>();
204         ConcurrentHashMap getFlagVal = u.returnFlagMap();
205         boolean nodeListed = false;
206         boolean foundFlag = false;
207         boolean shortestPath = false;
208         //CommunicationsManager.addVehPos(node.getID(), node.getPosition());
209         
210         if(node.getID().equals("#9")){
211         
212         }
213         
214       
215         
216         Position3D destPos = CommunicationsManager.retrieveDestPos(node.getID());
217         if(pathPoints!= null && pathPoints.size()>0){   
218             
219             
220             
221             
222         Set entrySet = pathPoints.entrySet();
223         for(Iterator i= entrySet.iterator();i.hasNext();){
224             Map.Entry entry = (Entry)i.next();
225             String agtName = (String) entry.getKey();
226             verticesList = (ArrayList<Vertex>) entry.getValue();
227             if(agtName!=null && agtName.equals(node.getID())){
228                 foundFlag = true;
229                 nodeListed = true;
230                 Iterator iter = verticesList.iterator();
231                 while(iter.hasNext())
232                 {
233                     Vertex vdNext = (Vertex) iter.next();
234                 //  System.out.println("node.getPosition().getX() == vdNext.getX() && node.getPosition().getY() == vdNext.getY()"+node.getPosition().getX()+"space vdNext.getX()= "+ vdNext.getX()+"space node.getPosition().getY()"+node.getPosition().getY()+"space vdNext.getY()"+vdNext.getY());
235                     if(node.getPosition().getX() == vdNext.getX() && node.getPosition().getY() == vdNext.getY())
236                     {
237                         
238                         CommunicationsManager.addVehPos(node.getID(), node.getPosition());
239                         vCnt++; 
240                     /*if(node.getPosition().getX() == 800.0 && node.getPosition().getY() == 300.0)
241                                trip = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);*/
242                     }else if(vCnt==1){
243                         trip = algo.getPath(spatialModel, node,
244                                 new Point(node.getPosition().getX(), node.getPosition().getY()),
245                                 new Point(vdNext.getX(), vdNext.getY()), reflect_directions);
246                         
247                         CommunicationsManager.addVehPos(node.getID(), new Position3D("dest",vdNext.getX(),vdNext.getY(),0.0));
248                         
249                         
250                         break;
251                     }
252                 }//end while
253                 if(vCnt==0)
254                     return generateTripToNearestNode(node);
255                 
256                 break;
257             }//end if
258             
259         }//end for
260         
261         if(!foundFlag){
262             trip = algo.getPath(spatialModel, node, new Point(node.getPosition().getX(),node.getPosition().getY()), new Point(destPos.getX(),destPos.getY()), reflect_directions);
263         }
264         }//if - null check, pathpoints check
265         
266         else{
267             System.out.println("NO PATH POINTS");
268             if(node.getPosition().getX() == 0.0 && node.getPosition().getY() == 340.0)
269                    trip = algo.getPath(spatialModel, node, (new Point(0.00,340.00)), (new Point(0.0,240.0)), reflect_directions);   
270 
271     
272             if(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") || node.getID().equals("#67") || node.getID().equals("#68") || node.getID().equals("#69")){
273                 if(node.getPosition().getX() == 240.0 && node.getPosition().getY() == 240.0){
274                     trip = algo.getPath(spatialModel, node, (new Point(240.00,240.00)), (new Point(300.0,240.0)), reflect_directions);
275                 }
276             }
277             if(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") || node.getID().equals("#77") || node.getID().equals("#78") || node.getID().equals("#79")){
278                 if(node.getPosition().getX() == 240.0 && node.getPosition().getY() == 240.0){
279                     trip = algo.getPath(spatialModel, node, (new Point(240.00,240.00)), (new Point(300.0,240.0)), reflect_directions);
280                 }
281             }
282             if(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") || node.getID().equals("#87") || node.getID().equals("#88") || node.getID().equals("#89")){
283                 if(node.getPosition().getX() == 240.0 && node.getPosition().getY() == 240.0){
284                     trip = algo.getPath(spatialModel, node, (new Point(240.00,240.00)), (new Point(300.0,240.0)), reflect_directions);
285                 }
286             }
287             
288               
289            
290   
291             //commented on 10th aug
292             CommunicationsManager.addVehPos(node.getID(), node.getPosition());
293             trip = algo.getPath(spatialModel, node, new Point(node.getPosition().getX(),node.getPosition().getY()), new Point(destPos.getX(),destPos.getY()), reflect_directions);
294              if(node.getPosition().getX() == destPos.getX() && node.getPosition().getY() == destPos.getY())
295                    trip = algo.getPath(spatialModel, node, (new Point(destPos.getX(),destPos.getY())), (new Point(destPos.getX(),destPos.getY())), reflect_directions);
296             
297             
298                  
299         
300         }//end outer else
301         
302         return trip; 
303   }
304   private Trip generateTripToNearestNode(Node node) {
305      // System.out.println("In generateTripToNearestNode "+node.getPosition().getX());
306       Trip trip = null;
307         ArrayList<Vertex> verticesList = new ArrayList<Vertex>();
308         Set entrySet = pathPoints.entrySet();
309         for(Iterator i= entrySet.iterator();i.hasNext();){
310             Map.Entry entry = (Entry)i.next();
311             String agtName = (String) entry.getKey();
312             verticesList = (ArrayList<Vertex>) entry.getValue();
313             if(agtName!=null && agtName.equals(node.getID())){
314                 Iterator iter = verticesList.iterator();
315                 while(iter.hasNext())
316                 {
317                     Vertex vdNext = (Vertex) iter.next();
318                     if(node.getPosition().getX() < vdNext.getX())
319                     {
320                       //    CommunicationsManager.addVehPos(node.getID(), new Position3D("dest",vdNext.getX(),vdNext.getY(),0.0));
321                     
322                         trip = algo.getPath(spatialModel, node,
323                                 new Point(node.getPosition().getX(), node.getPosition().getY()),
324                                 new Point(vdNext.getX(), vdNext.getY()), reflect_directions);
325                         
326                         return trip;
327                     }
328                 }//end while
329                 
330                 break;
331             }//end if
332         }//end for
333     return trip;
334 }
335 
336 /**
337    * Generates a new trip for the node. <br>
338    * <br>
339    * @param node node
340    * @return new trip for node
341    */
342   //public Trip genTrip(Node node)
343  // {
344         
345       /*
346       Trip trip = null;
347       Vertex vd = null;
348       SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
349       java.util.ArrayList vertices = spatialModel.getGraph().getVertices();
350     if(!routeFlag)
351     {
352         
353           if(node.getID().equals("#0")||node.getID().equals("#1")||node.getID().equals("#2")||node.getID().equals("#3")||node.getID().equals("#4")||node.getID().equals("#5"))
354           {
355             try {
356                 vd = new Vertex("dest1","","800.0","300.0");
357             } catch (Exception e) {
358                 // TODO Auto-generated catch block
359                 e.printStackTrace();
360             }
361           }else if(node.getID().equals("#6")||node.getID().equals("#7")||node.getID().equals("#8")||node.getID().equals("#9")||node.getID().equals("#10"))
362           {
363             try {
364                 vd = new Vertex("dest2","","900.0","400.0");
365             } catch (Exception e) {
366                 // TODO Auto-generated catch block
367                 e.printStackTrace();
368             }
369           }else
370           {
371             try {
372                 vd = new Vertex("dest1","","800.0","300.0");
373             } catch (Exception e) {
374                 // TODO Auto-generated catch block
375                 e.printStackTrace();
376             }
377           }
378     
379   
380         
381       // get a path from the current location to the destination
382     
383     trip = algo.getPath(spatialModel, node,
384         new Point(node.getPosition().getX(), node.getPosition().getY()),
385         new Point(vd.getX(), vd.getY()), reflect_directions);
386     }
387     //my code
388     else
389     {
390         
391         if(node.getID().equals("#1"))
392         {
393         ArrayList<String> verticesList = new ArrayList<String>();
394         verticesList.add("120.0,300.0");
395         verticesList.add("150.0,300.0");
396         verticesList.add("300.0,300.0");
397         verticesList.add("400.0,200.0");
398         verticesList.add("650.0,300.0");
399         verticesList.add("800.0,300.0");
400         
401         Iterator iter = verticesList.iterator();
402         while(iter.hasNext())
403         {
404             Vertex vdNext = (Vertex) iter.next();   
405             if(node.getPosition().getX() == vdNext.getX() && node.getPosition().getY() == vdNext.getY())
406                 continue;
407             trip = algo.getPath(spatialModel, node,
408                       new Point(node.getPosition().getX(), node.getPosition().getY()),
409                       new Point(vdNext.getX(), vdNext.getY()), reflect_directions);
410             break;
411         }
412         }       
413         if(node.getID().equals("#0"))
414           {
415             try {
416                 vd = new Vertex("dest1","","800.0","300.0");
417                 trip = algo.getPath(spatialModel, node,
418                           new Point(node.getPosition().getX(), node.getPosition().getY()),
419                           new Point(vd.getX(), vd.getY()), reflect_directions);
420             } catch (Exception e) {
421                 // TODO Auto-generated catch block
422                 e.printStackTrace();
423             }
424             
425         
426           }
427     }
428     //my code
429       if (trip==null)
430       {
431         // add an empty trip
432         trip = new Trip();
433 
434         java.util.ArrayList path = trip.getPath();
435         path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
436         path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
437       }
438      
439       return trip;
440     }*/ 
441       /*
442     // my code
443       Trip trip1 = new Trip();
444      
445       
446       
447    if(node.getID()!=null && node.getID().equals("#0"))
448    {
449      
450        if(isRouteFlag()){
451        if(node.getPosition().getX() == 100.0 && node.getPosition().getY() == 300.0)
452        trip1 = algo.getPath(spatialModel, node, (new Point(100.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
453        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
454            trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(500.0,400.0)), reflect_directions);
455        if(node.getPosition().getX() == 500.0 && node.getPosition().getY() == 400.0)
456            trip1 = algo.getPath(spatialModel, node, (new Point(500.00,400.00)), (new Point(700.0,600.0)), reflect_directions);
457        if(node.getPosition().getX() == 700.0 && node.getPosition().getY() == 650.0)
458            trip1 = algo.getPath(spatialModel, node, (new Point(700.00,650.00)), (new Point(800.0,300.0)), reflect_directions);
459        if(node.getPosition().getX() == 800.0 && node.getPosition().getY() == 300.0)
460            trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
461        }else{
462       if(node.getPosition().getX() == 100.0 && node.getPosition().getY() == 300.0)
463            trip1 = algo.getPath(spatialModel, node, (new Point(100.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
464            if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
465                trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(650.0,300.0)), reflect_directions);
466            if(node.getPosition().getX() == 650.0 && node.getPosition().getY() == 300.0)
467                trip1 = algo.getPath(spatialModel, node, (new Point(650.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
468            if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
469                trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
470       }
471        return trip1;
472    }
473    if(!isRouteFlag()){
474    if(node.getID()!=null && node.getID().equals("#1"))
475    {
476        
477        if(node.getPosition().getX() == 120.0 && node.getPosition().getY() == 300.0)
478        trip1 = algo.getPath(spatialModel, node, (new Point(120.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
479        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
480            trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(650.0,300.0)), reflect_directions);
481       if(node.getPosition().getX() == 650.0 && node.getPosition().getY() == 300.0)
482            trip1 = algo.getPath(spatialModel, node, (new Point(650.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
483        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
484            trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
485            
486        return trip1;
487    }
488    }else 
489    {
490       int vCnt = 0;
491        
492         if(node.getID().equals("#1"))
493         {
494         ArrayList<Vertex> verticesList = new ArrayList<Vertex>();
495         try {
496             verticesList.add(new Vertex("dest1","","120.0","300.0"));
497             verticesList.add(new Vertex("dest1","","150.0","300.0"));
498             verticesList.add(new Vertex("dest1","","300.0","300.0"));
499             verticesList.add(new Vertex("dest1","","400.0","200.0"));
500             verticesList.add(new Vertex("dest1","","650.0","300.0"));
501             verticesList.add(new Vertex("dest1","","800.0","300.0"));
502         } catch (Exception e) {
503             // TODO Auto-generated catch block
504             e.printStackTrace();
505         }
506         
507         
508         Iterator iter = verticesList.iterator();
509         while(iter.hasNext())
510         {
511             Vertex vdNext = (Vertex) iter.next();
512             
513             if(node.getPosition().getX() == vdNext.getX() && node.getPosition().getY() == vdNext.getY())
514             {
515                 vCnt++; 
516                         
517             
518             }else if(vCnt==1){
519             trip1 = algo.getPath(spatialModel, node,
520                       new Point(node.getPosition().getX(), node.getPosition().getY()),
521                       new Point(vdNext.getX(), vdNext.getY()), reflect_directions);
522             break;
523             }
524         }
525         }   
526         return trip1;
527        /*if(node.getPosition().getX() == 120.0 && node.getPosition().getY() == 300.0)
528            trip1 = algo.getPath(spatialModel, node, (new Point(120.00,300.00)), (new Point(150.0,300.0)), reflect_directions);
529        if(node.getPosition().getX() == 150.0 && node.getPosition().getY() == 300.0)
530            trip1 = algo.getPath(spatialModel, node, (new Point(150.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
531            if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
532                trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(400.0,200.0)), reflect_directions);
533           if(node.getPosition().getX() == 400.0 && node.getPosition().getY() == 200.0)
534                trip1 = algo.getPath(spatialModel, node, (new Point(400.00,200.00)), (new Point(650.0,300.0)), reflect_directions);
535            if(node.getPosition().getX() == 650.0 && node.getPosition().getY() == 300.0)
536                trip1 = algo.getPath(spatialModel, node, (new Point(650.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
537            if(node.getPosition().getX() == 800.0 && node.getPosition().getY() == 300.0)
538                trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
539            return trip1;*/
540   /* }
541    if(node.getID()!=null && node.getID().equals("#2"))
542    {
543        if(node.getPosition().getX() == 150.0 && node.getPosition().getY() == 300.0)
544        trip1 = algo.getPath(spatialModel, node, (new Point(150.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
545        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
546            trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(400.0,200.0)), reflect_directions);
547        if(node.getPosition().getX() == 400.0 && node.getPosition().getY() == 200.0)
548            trip1 = algo.getPath(spatialModel, node, (new Point(400.00,200.00)), (new Point(600.0,150.0)), reflect_directions);
549        if(node.getPosition().getX() == 650.0 && node.getPosition().getY() == 150.0)
550            trip1 = algo.getPath(spatialModel, node, (new Point(650.00,150.00)), (new Point(700.0,200.0)), reflect_directions);
551        if(node.getPosition().getX() == 700.0 && node.getPosition().getY() == 200.0)
552            trip1 = algo.getPath(spatialModel, node, (new Point(700.00,200.00)), (new Point(800.0,300.0)), reflect_directions);
553        if(node.getPosition().getX() == 800.0 && node.getPosition().getY() == 300.0)
554            trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
555            
556        return trip1;
557    }else  if(node.getID()!=null && node.getID().equals("#3"))
558    {
559        if(node.getPosition().getX() == 120.0 && node.getPosition().getY() == 300.0)
560        trip1 = algo.getPath(spatialModel, node, (new Point(120.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
561        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
562            trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(650.0,300.0)), reflect_directions);
563       // if(node.getPosition().getX() == 600.0 && node.getPosition().getY() == 300.0)
564         //   trip1 = algo.getPath(spatialModel, node, (new Point(600.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
565        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
566            trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
567            
568        return trip1;
569    }else  if(node.getID()!=null && node.getID().equals("#4"))
570    {
571        if(node.getPosition().getX() == 120.0 && node.getPosition().getY() == 300.0)
572        trip1 = algo.getPath(spatialModel, node, (new Point(120.00,300.00)), (new Point(300.0,300.0)), reflect_directions);
573        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
574            trip1 = algo.getPath(spatialModel, node, (new Point(300.00,300.00)), (new Point(650.0,300.0)), reflect_directions);
575       // if(node.getPosition().getX() == 600.0 && node.getPosition().getY() == 300.0)
576         //   trip1 = algo.getPath(spatialModel, node, (new Point(600.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
577        if(node.getPosition().getX() == 300.0 && node.getPosition().getY() == 300.0)
578            trip1 = algo.getPath(spatialModel, node, (new Point(800.00,300.00)), (new Point(800.0,300.0)), reflect_directions);
579            
580        return trip1;
581    } 
582    return trip1;
583         
584   }*/
585      /*
586      java.util.Random rand = u.getRandom();
587 
588     SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
589         // JHNote (06/02/2006): Now the SpatialModel is loaded at the beginning.
590         
591         Graph graph = spatialModel.getGraph();
592         
593     // check if the set of points is defined
594     if (points.size()>0)
595     {
596       int size = 0;
597       Point p = (Point)points.get(rand.nextInt(points.size()));
598       while(size!=points.size()){
599            p = (Point)points.get(size);
600         if(p.getX()==spatialModel.max_x_clip)
601         {
602            
603         break;
604         }
605         size++;
606       }
607       if (graph==null)
608       {
609         Trip trip = new Trip();
610 
611         java.util.ArrayList path = trip.getPath();
612         path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
613         path.add(new Point(p.getX(), p.getY()));
614 
615         return trip;
616       }
617       else
618       {
619         // get a path from the current location to the destination
620         Trip trip = algo.getPath(spatialModel, node,
621           new Point(node.getPosition().getX(), node.getPosition().getY()),
622           new Point(p.getX(), p.getY()), reflect_directions);
623         if (trip==null)
624         {
625           // add an empty trip
626           trip = new Trip();
627 
628           java.util.ArrayList path = trip.getPath();
629           path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
630           path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
631         }
632         
633         return trip;
634       }
635     }
636     else
637     {
638       if (graph==null)
639       {
640         // generate a path to a randomly chosen point
641         double x = rand.nextDouble()*u.getDimensionX();
642         double y = rand.nextDouble()*u.getDimensionY();
643 
644         Trip trip = new Trip();
645 
646         java.util.ArrayList path = trip.getPath();
647         path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
648         path.add(new Point(x, y));
649       
650         return trip;
651       }
652       else
653       {
654         
655         java.util.ArrayList vertices = spatialModel.getGraph().getVertices();
656 
657         
658         // choose a random destination
659        
660       //  Vertex vd = (Vertex)vertices.get(rand.nextInt(vertices.size()));
661       
662         
663         /*
664         Iterator iter = vertices.iterator();
665         Vertex vd = null;
666         while(iter.hasNext())
667         {
668              vd = (Vertex)iter.next();
669             
670              if(vd.getX()==spatialModel.max_x_clip)
671             {
672                
673             break;
674             }
675         }
676         --*/
677      /* 
678        //my code
679         boolean vdFlag =false;
680         Vertex vd;
681         
682         Trip trip = null;
683         if(!routeFlag)
684         {
685             vd = (Vertex)vertices.get(rand.nextInt(vertices.size()));
686             if(node.getID().equals("#0")||node.getID().equals("#1")||node.getID().equals("#2")||node.getID().equals("#3")||node.getID().equals("#4")||node.getID().equals("#5"))
687             {
688                 try {
689                     vd = new Vertex("dest1","","800.0","300.0");
690                 } catch (Exception e) {
691                     // TODO Auto-generated catch block
692                     e.printStackTrace();
693                 }
694             }else if(node.getID().equals("#6")||node.getID().equals("#7")||node.getID().equals("#8")||node.getID().equals("#9")||node.getID().equals("#10"))
695             {
696                 try {
697                     vd = new Vertex("dest2","","900.0","400.0");
698                 } catch (Exception e) {
699                     // TODO Auto-generated catch block
700                     e.printStackTrace();
701                 }
702             }else
703             {
704                 try {
705                     vd = new Vertex("dest1","","800.0","300.0");
706                 } catch (Exception e) {
707                     // TODO Auto-generated catch block
708                     e.printStackTrace();
709                 }
710             }
711         
712     
713           
714         // get a path from the current location to the destination
715         
716         trip = algo.getPath(spatialModel, node,
717           new Point(node.getPosition().getX(), node.getPosition().getY()),
718           new Point(vd.getX(), vd.getY()), reflect_directions);
719         }
720         //my code
721         else
722         {
723             
724             if(node.getID().equals("#1"))
725             {
726             ArrayList<String> verticesList = new ArrayList<String>();
727             verticesList.add("120.0,300.0");
728             verticesList.add("150.0,300.0");
729             verticesList.add("300.0,300.0");
730             verticesList.add("400.0,200.0");
731             verticesList.add("650.0,300.0");
732             verticesList.add("800.0,300.0");
733             
734             Iterator iter = verticesList.iterator();
735             while(iter.hasNext())
736             {
737                 Vertex vdNext = (Vertex) iter.next();   
738                 if(node.getPosition().getX() == vdNext.getX() && node.getPosition().getY() == vdNext.getY())
739                     continue;
740                 trip = algo.getPath(spatialModel, node,
741                           new Point(node.getPosition().getX(), node.getPosition().getY()),
742                           new Point(vdNext.getX(), vdNext.getY()), reflect_directions);
743                 break;
744             }
745             }       
746             if(node.getID().equals("#0"))
747             {
748                 try {
749                     vd = new Vertex("dest1","","800.0","300.0");
750                     trip = algo.getPath(spatialModel, node,
751                           new Point(node.getPosition().getX(), node.getPosition().getY()),
752                           new Point(vd.getX(), vd.getY()), reflect_directions);
753                 } catch (Exception e) {
754                     // TODO Auto-generated catch block
755                     e.printStackTrace();
756                 }
757                 
758             
759             }
760         }
761         //my code
762         if (trip==null)
763         {
764           // add an empty trip
765           trip = new Trip();
766 
767           java.util.ArrayList path = trip.getPath();
768           path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
769           path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
770         }
771        
772         return trip;
773       }
774     }
775   }*/
776   
777   /**
778    * Chooses a time of staying at the current position. <br>
779    * <br>
780    * @param node node
781    * @return stay duration (in ms)
782    */
783   public int chooseStayDuration(Node node)
784   {
785    // return (int)(minStay+(maxStay-minStay)*u.getRandom().nextFloat());
786       return 0;
787   }
788   
789   /**
790    * Initializes the object from XML tag. <br>
791      * <i> Version 1.2 by  Jerome Haerri (haerri@ieee.org) on 11/30/2005: 
792      * <br> &nbsp;&nbsp;&nbsp;&nbsp; Patch to reflect the stong links between reflect_direction in SpaceGraph and here.
793      * <br> &nbsp;&nbsp;&nbsp;&nbsp; In order to reflect direction here, if we use a space graph, 
794      *                                                  we need to set the reflect direction to true in both case. </i>
795    * <br>
796    * @param element source tag
797    * @throws Exception Exception if parameters are invalid
798    */
799   public void load(org.w3c.dom.Element element) throws Exception
800   {
801     u.sendNotification(new LoaderNotification(this, u,
802       "Loading RandomTripGenerator extension"));
803 
804     super.load(element);
805         
806         // JHNote (06/02/2006): Now the SpatialModel is loaded at the beginning.
807         String sm = element.getAttribute("spatial_model");
808     if (sm.length()>0) {
809       spatialModel = (SpatialModel)u.getExtension(sm);
810     }
811         
812         else {
813             spatialModel = (SpatialModel)u.getExtension("SpatialModel");
814         }
815         
816         if(spatialModel==null)
817       throw new Exception("A SpatialModel is missing!");
818         
819     org.w3c.dom.Node n;
820 
821     String classTag = element.getAttribute("path_algorithm").trim();
822     if (classTag.length()!=0)
823     {
824       algo = (PathSearchingAlgorithm)Class.forName(classTag).newInstance();
825       
826       // handle stoch path selection parameters
827       if (algo instanceof PedestrianStochPathSelection && !(algo instanceof SpeedPathSelection))
828       {
829         String param = element.getAttribute("theta").trim();
830         if(param.length()==0)
831           throw new Exception("\"theta\" attribute of path selection is missing!");
832         float theta = Float.parseFloat(param);
833         
834         ((PedestrianStochPathSelection)algo).setTheta(theta);
835       }
836 
837       // handle speed path selection parameters
838       if (algo instanceof SpeedPathSelection)
839       {
840         String param = element.getAttribute("speedWeight").trim();
841         if(param.length()==0)
842           throw new Exception("\"speedWeight\" attribute of path selection is missing!");
843         float speedWeight = Float.parseFloat(param);
844         
845         ((SpeedPathSelection)algo).setSpeedWeight(speedWeight);
846       }
847     }
848 
849     n = element.getElementsByTagName("reflect_directions").item(0);
850     if((n!=null)&&(Boolean.valueOf(n.getFirstChild().getNodeValue()).booleanValue()))
851     {
852       reflect_directions = PathSearchingAlgorithm.FLAG_REFLECT_DIRECTIONS;
853     }
854         
855         // JHNote (30/11/2005) code added to reflect the stong links between reflect_direction in SpaceGraph and here
856         // In order to reflect direction here, if we use a space graph, we need to set the 
857         // reflect direction to true in both case.
858         // JHNote (15/09/2006): the directions are now in SpatialModel
859         if ((spatialModel != null) && (spatialModel.getDirections() != Boolean.valueOf(n.getFirstChild().getNodeValue()).booleanValue())) {
860             throw new Exception("\"reflect_direction\" attribute of path selection need to be identical to the one on the SpatialModel!");
861         }
862             
863     n = element.getElementsByTagName("points").item(0);
864     if(n!=null)
865     {
866       String fileSource = n.getFirstChild().getNodeValue();
867 
868       java.io.BufferedReader source = new java.io.BufferedReader(new java.io.FileReader(fileSource));
869 
870       String s;
871       // read next record
872       while ((s = source.readLine())!=null)
873       {
874         String ss[] = s.split(" ");
875 
876         double x = Double.parseDouble(ss[0]);
877         double y = Double.parseDouble(ss[1]);
878         
879         points.add(new Point(x, y));
880       }
881     }
882 
883     n = element.getElementsByTagName("minstay").item(0);
884     if(n==null)
885       throw new Exception("<minstay> is missing!");
886     minStay = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
887 
888     n = element.getElementsByTagName("maxstay").item(0);
889     if(n==null)
890       throw new Exception("<maxstay> is missing!");
891     maxStay = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
892 
893     // checkout
894     if ( (minStay<0)||(maxStay<minStay) )
895       throw new Exception("Trip generation parameters are invalid:\n"
896         +"minStay="+(float)minStay/1000+"(s), maxStay="+(float)maxStay/1000+"(s)");
897 
898     u.sendNotification(new LoaderNotification(this, u,
899       "Finished loading RandomTripGenerator extension"));
900   }//proc
901 }
902