1   package de.uni_stuttgart.informatik.canu.uomm;
2   
3   /**
4    * <p>Title: User-Oriented Mobility Model</p>
5    * <p>Description: </p>
6    * <p>Copyright: Copyright (c) 2002</p>
7    * <p>Company: University of Stuttgart</p>
8    * <p>Patches: </p>
9    * <p> <i> Version 1.2 by Marco Fiore (fiore@tlc.polito.it) on 22/05/2006:
10   * <br> &nbsp;&nbsp;&nbsp;&nbsp; Speed attribute moved at Movement class level </i> </p>
11   *
12   * @author 1.0-1.1 Illya Stepanov
13   * @author 1.2 Marco Fiore
14   * @version 1.2
15   */
16  
17  import java.util.StringTokenizer;
18  import java.util.concurrent.ConcurrentHashMap;
19  
20  import CanuMobiSim.de.uni_stuttgart.informatik.canu.mobisim.notifications.VehicleCountReachedNotification;
21  
22  import de.uni_stuttgart.informatik.canu.mobisim.core.Node;
23  import de.uni_stuttgart.informatik.canu.mobisim.core.Position3D;
24  import de.uni_stuttgart.informatik.canu.mobisim.core.Vector3D;
25  import de.uni_stuttgart.informatik.canu.mobisim.extensions.Graph;
26  import de.uni_stuttgart.informatik.canu.mobisim.mobilitymodels.Movement;
27  import de.uni_stuttgart.informatik.canu.mobisim.notifications.DebugNotification;
28  import de.uni_stuttgart.informatik.canu.mobisim.notifications.DestinationReachedNotification;
29  import de.uni_stuttgart.informatik.canu.mobisim.notifications.LoaderNotification;
30  import de.uni_stuttgart.informatik.canu.mobisim.notifications.MovementChangedNotification;
31  import de.uni_stuttgart.informatik.canu.mobisim.notifications.VehiclesInRangeNotification;
32  import de.uni_stuttgart.informatik.canu.senv.core.Edge;
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.core.SpatialModelElement;
36  import de.uni_stuttgart.informatik.canu.spatialmodel.geometry.Point;
37  import de.uni_stuttgart.informatik.canu.tripmodel.core.Trip;
38  import de.uni_stuttgart.informatik.canu.uomm.CommunicationsManager;
39  import examples.bookTrading.Constants;
40  import examples.bookTrading.DataAccess;
41  import examples.bookTrading.NotificationPattern;
42  import examples.bookTrading.QueueManager;
43  
44  /**
45   * This class implements Fluid Traffic Movement Behavior.
46   * 
47   * The implementation is based on I. Seskar, S.V. Marie, J. Holtzman, J. Wasserman,
48   * "Rate of Location Area Updates in Cellular Systems",
49   * Proceedings of IEEE VTC'92, Denver, CO, May 1992. <br>
50   * <br>
51   * @author Illya Stepanov
52   * 
53   * Customised the method to recalculate the traffic speed.to include the functionality of detecting vehicles within 
54   * communication range and forming groups of such vehicles for every oncoming junction. The group of vehicles approaching
55   * a junction is involved in cooperative route allocation.
56   * @author prajakta
57   */
58  public class FluidTrafficMotion extends UserOrientedMovement
59  {
60    /**
61     * Flag to indicate that a random stay duration must be chosen at the beginning of the simulation
62     */
63    protected boolean stayRandom = false;
64    
65    /**
66     * Current stay duration at destination (ms)
67     */
68    protected int stay = 0;           // in ms
69  
70    /**
71     * Current speed (in meters/ms)
72     */
73  //  protected float speed = 0.0f;    // in m/ms
74    
75    /**
76     * Traffic jam density (in 1/meter)
77     */
78    protected float jam_density = 0.0f;
79  
80    /**
81     * Traffic parameters recalculation step (in steps)
82     */
83    protected int recalculation_step = 0; // in steps
84  
85    /**
86     * Destination of previous movement
87     */
88    protected Position3D oldPosition;
89  
90    /**
91     * Destination of current movement
92     */
93    protected Position3D destination;
94  
95    /**
96     * Current movement vector
97     */
98    protected Vector3D movement;
99  
100   /**
101    * Allowed speed (in meters/ms)
102    */
103   protected float allowedSpeed = 0.0f;    // in m/ms, around 300 km/h
104   /**
105    * Current trip
106    */
107   protected Trip trip = new Trip();
108   protected Vertex oldVertex = null;
109     protected boolean oldVertexFlag = false;
110   
111   protected String vehInRng = new String();
112   protected String tempVehInRng = new String();
113   protected String vehInRIQRng = new String();
114   
115 
116 
117 VehiclesInRangeNotification vehRngNotif = new VehiclesInRangeNotification();
118 VehicleCountReachedNotification vehCount = new VehicleCountReachedNotification();
119   
120   protected boolean congestionAlert = false;
121   protected Position3D congStartPoint = null;
122   protected Position3D congEndPoint = null;
123   protected NotificationPattern notifPattern = new NotificationPattern();
124   
125   protected ConcurrentHashMap commFromMnger = new ConcurrentHashMap();
126   
127   protected Position3D uniNodeSrcPos = null;
128   protected Position3D uniNodeDestPos = null;
129   protected String sentCongSegID = null;
130   protected int sentAgentCount=0;
131   protected boolean agentCreated=false;
132   private boolean ownerAdded=false;
133   private  boolean nodeAdded=false;
134 
135 private boolean bothNew;
136 
137   /**
138    * Constructor
139    */
140   public FluidTrafficMotion()
141   {
142       u.addNotificationListener(this);
143   }
144  
145   /**
146    * Returns the module's description. <br>
147    * <br>
148    * @return extension module's description
149    */
150   public String getDescription()
151   {
152     return "Fluid Traffic Movement Behavior";
153   }
154 
155   /**
156    * Performs the module initialization. <br>
157    * <br>
158    * The method is called after finishing the scenario file processing.
159    */
160   public void initialize()
161   {
162     Node node = (Node)owner;
163     commFromMnger = CommunicationsManager.returnCommunicationRecordedVehicles();
164     // set initial position
165     Point pos = initialPositionGenerator.getInitialPosition(node);
166     node.setPosition(pos.getPosition());
167     oldPosition = pos.getPosition();
168     
169     if (stayRandom)
170     {
171       stay = tripGenerator.chooseStayDuration(node);
172     }
173        
174     super.initialize();
175     
176     uniNodeSrcPos  = de.uni_stuttgart.informatik.canu.uomm.CommunicationsManager.storeInitPos(node.getID());
177     uniNodeDestPos  = de.uni_stuttgart.informatik.canu.uomm.CommunicationsManager.storeDestPos(node.getID());
178     
179   }
180 
181   /**
182    * Gets the destination of the previous movement. <br>
183    * <br>
184    * @return the destination of the previous movement
185    */
186   public Position3D getOldPosition()
187   {
188     return oldPosition;
189   }
190   /**
191    * Gets the destination of the current movement. <br>
192    * <br>
193    * @return the destination of the current movement
194    */
195   public Position3D getDestination()
196   {
197     return destination;
198   }
199 
200   /**
201    * Gets the current speed of movement. <br>
202    * <br>
203    * @return the current speed of movement (in meters/ms)
204    */ /*
205   public float getSpeed()
206   {
207     return speed;
208   } */
209 
210   /**
211    * Chooses a new movement path of movement
212    */
213   protected void chooseNewPath()
214   {
215     Node node = (Node)this.owner;
216 
217     trip = tripGenerator.genTrip(node);
218     
219     if(trip!=null){
220     u.sendNotification(new DebugNotification(this, u, "New trip generated:"));
221     for (int i=0; i<trip.getPath().size(); i++)
222     {
223       Point p = (Point)trip.getPath().get(i);
224       if(tripGenerator.getPathPoints()!=null && tripGenerator.getPathPoints().size()==0)
225       u.sendNotification(new DebugNotification(this, u,
226         ""+p.getX()+" "+p.getY()));
227       else
228           u.sendNotification(new DebugNotification(this, u,
229                     "with path points : "+p.getX()+" "+p.getY()));
230     }
231     }
232     
233     // delete the current node position from the path
234     if(trip!=null && trip.getPath()!=null)
235     trip.getPath().remove(0);  
236     
237   }
238 
239   /**
240    * Chooses a new destination and movement speed
241    */
242   protected void chooseNewMovement()
243   {
244     
245       Node owner = (Node)this.owner;
246 
247       if(tripGenerator.getPathPoints()!=null && tripGenerator.getPathPoints().size()==0){
248       if (trip!=null && trip.getPath().size()==0)
249           chooseNewPath();
250       }else{
251           chooseNewPath();
252       }
253       
254         // get the next vertex
255        if(trip!=null) {
256       Point p = (Point)trip.getPath().get(0);
257         trip.getPath().remove(0);
258         
259         destination = p.getPosition();
260 
261         stay = 0;
262 
263         movement = owner.getPosition().getNormalizedDirectionVector(destination);
264         
265         recalculateTrafficSpeed();
266        }
267   }
268 
269   /**
270    * Chooses a time of staying at current position or continues
271    * the movement to destination.
272    */
273   protected void chooseNewStayDuration()
274   {
275     Node owner = (Node)this.owner;
276 
277     oldPosition = owner.getPosition();
278 
279     if (trip==null || trip.getPath().size()==0)
280     {
281       // wait at destination
282       stay = tripGenerator.chooseStayDuration(owner);
283 
284       u.sendNotification(new DestinationReachedNotification(this, u,
285         owner.getPosition(), stay/1000.0f));
286     }
287     else
288     {
289       chooseNewMovement();
290     }
291   }
292 
293   /**
294    * Recalculates the speed of traffic flow
295    */
296   protected synchronized void recalculateTrafficSpeed()
297   {
298   //0-39,60-69 ----120,240
299       //41-59,70-89 ------ 0,340
300       String segID = null;
301       int segThreshold = 0;
302       SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
303       if(spatialModel==null){
304           System.out.println("sptialmodel is null");
305       }
306    Graph graph = spatialModel.getGraph();
307    if(graph==null){
308        System.out.println("graph is null");
309    }
310    String nonEquipped="";//"#0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12,#13,#14,#15,#16,#17,#18,#19,#20,#21,#22,#23,#24,#25,#26,#27,#28,#29,#30,#31,#32,#33,#34,#35,#36,#37,#38,#39";//#30,#31,#32,#33,#34,#35,#36,#37,#38,#39,#40,#41,#42,#43,#44,#45,#46";//#40,#41,#42,#43,#44,#45,#46,#47,#48,#49,#50,#51,#52,#53,#54,#55,#56,#57,#58,#59,#60,#61,#62,#63,#64,#65,#66,#67,#68,#69,#70,#71,#72,#73";/*,#30,#31,#32,#33,#34,#35,336,#37,#38,#39"*/;
311    String ID  = null;
312     String identifier = "";
313     int nodeOnLine=1;
314     int allNodesOnLine=1;
315    int n = 1;
316    double distFromSrcX = 0.0,distFromSrcY=0.0;
317    double distFromDestX=0.0,distFromDestY=0.0;
318    double nodeDistFromSrcX = 0.0,nodeDistFromSrcY=0.0;
319    boolean approvedDistanceForCommunication = false;
320    String congestedSegmentId=null;
321    boolean notifSentFlag = false;
322    boolean ownerEquipped=true,nodeEquipped=true;
323   boolean onSameEdge=false; //checks if the owner and node are indeed on the same edge
324   
325   vehRngNotif = new VehiclesInRangeNotification();
326   Node newNode  = null;
327   
328   
329   ConcurrentHashMap finalMap = u.returnFlagMap();
330   
331    tempVehInRng = "";
332     if (graph==null)
333     {
334       speed = 0001f;
335     }
336     else
337     {
338       float k = 0.0f;
339             
340       Node owner = (Node)this.owner;
341     
342 
343       if(owner.getID().equals("#22") && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
344   //      System.out.println("1---------------");
345       }
346       Vertex vs = graph.getVertex(oldPosition.getX(), oldPosition.getY());
347       Vertex vd = graph.getVertex(destination.getX(), destination.getY());
348       if(oldVertex==null){
349           oldVertex = vs;
350       }
351       
352     
353        
354       Edge currentEdge = null;
355       if ((vs!=null)&&(vd!=null))
356         currentEdge =
357             spatialModel.findEdge(vs, vd);
358       //start--check if point on line
359       
360     
361       
362         double y1=currentEdge.getV1().getY(); double y2=currentEdge.getV2().getY();
363         double x1=currentEdge.getV1().getX(); double x2=currentEdge.getV2().getX();
364         double yDiff = y2-y1;
365         double xDiff=x2-x1;
366     
367         double slope = (y2-y1)/(x2-x1);//yDiff/xDiff;
368         double b = y1-slope*x1;
369         
370     
371         
372     //end--check if point on line
373     
374       if (currentEdge!=null)
375       {
376           SpatialModelElement road_a = spatialModel.mapEdgeToElement(currentEdge);
377 
378         if(road_a != null) {
379               // get speed limit on current road, if any
380               String segmentAttribute = (String)road_a.getAttributes().get("SP");
381               if (segmentAttribute != null) {
382                 allowedSpeed = Float.parseFloat(segmentAttribute)/1000.0f;
383               }
384         }
385         
386         // find other nodes on the same edge
387         java.util.Iterator iter = u.getNodes().iterator();
388        
389         if(u.getNodes()!=null && u.getNodes().size()>0)
390             
391         
392         congestedSegmentId = (String) commFromMnger.get(Double.toString(currentEdge.getV2().getX())+","+Double.toString(currentEdge.getV2().getY()));
393      //   System.out.println("congestedSegmentId : "+congestedSegmentId+" Other nodes on the same edge "+u.getNodes().size()+" starting point x"+currentEdge.getV2().getX()+" starting point y"+currentEdge.getV2().getY());
394         
395         distFromSrcX = currentEdge.getV1().getX()-owner.getPosition().getX();
396         distFromSrcY = currentEdge.getV1().getY()-owner.getPosition().getY();
397     
398         distFromDestX = currentEdge.getV2().getX()-owner.getPosition().getX();
399         distFromDestY = currentEdge.getV2().getY()-owner.getPosition().getY();
400         
401         nodeDistFromSrcX = currentEdge.getV1().getX()-owner.getPosition().getX();
402         nodeDistFromSrcY = currentEdge.getV1().getY()-owner.getPosition().getY();
403     
404         
405         ownerEquipped = checkIfEquipped(owner.getID(),nonEquipped);
406                 
407         boolean setInLoop = false;
408         while (iter.hasNext() &&  ((1*currentEdge.getDistance())/6 < Math.sqrt(distFromSrcX*distFromSrcX+distFromSrcY*distFromSrcY)) &&  ((currentEdge.getDistance()-Math.sqrt(distFromSrcX*distFromSrcX+distFromSrcY*distFromSrcY)>=(currentEdge.getDistance()-6))))
409         {
410           //this flag indicates if the node has already participated in the final route allocation
411             oldVertexFlag = false;
412           nodeAdded = false;
413           ownerAdded = false;
414           approvedDistanceForCommunication = true;
415           Node node = (Node)iter.next();
416           nodeEquipped = checkIfEquipped(node.getID(),nonEquipped);
417                             
418           
419           //my code
420           double ownerXPos = owner.getPosition().getX();  
421           double ownerYPos = owner.getPosition().getY();
422           double nodeXPos = node.getPosition().getX();
423           double nodeYPos = node.getPosition().getY();
424           double distY = nodeYPos-ownerYPos;
425           double distX = nodeXPos-ownerXPos ;
426           double dist = 0.0;
427           onSameEdge=false;
428           if(distY == 0.0)
429               dist = distX;
430           
431           else if(distX == 0.0)
432               dist = distY;
433           else if(distY != 0.0 && distX != 0.0)
434              dist = Math.sqrt((distX*distX)+(distY*distY));
435           //my code1
436           if(dist<0)
437               dist=-dist;
438        
439  
440           if (owner==node)
441             continue;
442                     if (distX == 0 && distY == 0){
443                         onSameEdge = true;
444                         
445                             if(ownerEquipped && nodeEquipped){
446                                     nodeOnLine++;
447                             }else{
448                                     allNodesOnLine++;
449                             }
450                         
451                     }
452                     else if (distX == 0 || distY == 0) {
453                         if((owner.getID().equals("#55") && node.getID().equals("#45")) && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
454 
455                             }
456                         if (distX == 0) { // indicates that the node is yet to
457                                             // reach vs while the owner has
458                                             // already reached
459                             if (node.getPosition().getY() > vs.getY()) {
460                                 //onSameEdge = false;
461                             } else {
462                                 onSameEdge = true;
463                                 if(dist<5){
464                                     if(ownerEquipped && nodeEquipped){
465                                             nodeOnLine++;
466                                     }else{
467                                             allNodesOnLine++;
468                                     }
469                                 }
470                             }
471                         }
472 
473                         else if (distY == 0) { // indicates that the node is yet
474                                                 // to reach vs while the owner
475                                                 // has already reached
476                             if((owner.getID().equals("#55") && node.getID().equals("#45")) && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
477             
478                                 }
479                             if (node.getPosition().getX() < vs.getX()) {
480                                 //onSameEdge = false;
481                             } else {
482                                 onSameEdge = true;
483                                 if(dist<5){
484                                     if(ownerEquipped && nodeEquipped){
485                                         nodeOnLine++;
486                                 }else{
487                                         allNodesOnLine++;
488                                 }
489                                 }
490                             }
491 
492                         }
493                     } else if ((int) (nodeYPos) == (int) (slope * nodeXPos + b)) {
494                         if((owner.getID().equals("#55") && node.getID().equals("#45")) && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
495                     
496                             }
497                         if(dist<5){
498                             if(ownerEquipped && nodeEquipped){
499                                 nodeOnLine++;
500                             }else{
501                                 allNodesOnLine++;
502                             }
503                         }
504                         onSameEdge = true;
505                     }else if (CommunicationsManager.getCommunicationStarted(node.getID(),currentEdge.getV2())){
506                         if((owner.getID().equals("#55") && node.getID().equals("#45")) && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
507                         //  System.out.println("4th : "+"ownerXPos :" + ownerXPos+ "ownerYPos : "+ownerYPos+ " "+" NODE x "+nodeXPos+ " node y "+nodeYPos);
508                             }
509                         if(dist<5){
510                             if(ownerEquipped && nodeEquipped){
511                                 nodeOnLine++;
512                             }else{
513                                 allNodesOnLine++;
514                             }
515                         }
516                         onSameEdge = true;
517                     }
518                     else {
519                                                             
520                         if((owner.getID().equals("#55") && node.getID().equals("#45")) && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
521                         //  System.out.println("fifth : "+"ownerXPos :" + ownerXPos+ "ownerYPos : "+ownerYPos+ " "+" NODE x "+nodeXPos+ " node y "+nodeYPos);
522                             }
523                         CommunicationsManager.removeFromVehInRngList(owner.getID(),node.getID());
524                         onSameEdge = false;
525                         continue;
526                     }
527                     
528   
529                    
530                     if((owner.getID().equals("#55") && node.getID().equals("#45")) && congestedSegmentId!=null && congestedSegmentId.equals("s6")){
531                 //      System.out.println("in while---------------"+owner.getID()+ " and node : "+node.getID()+" cong  "+congestedSegmentId);
532                       }
533         if(congestedSegmentId!=null && congestedSegmentId.equals("s43") && owner.getID().equals("#22")){
534     //        System.out.println("onSameEdge false---------------"+owner.getID()+","+node.getID()+"  "+onSameEdge);
535         }
536           
537           Movement n_m = (Movement)node.getExtension("Movement");
538           if (n_m instanceof FluidTrafficMotion)
539           {
540             FluidTrafficMotion n_mf = (FluidTrafficMotion)n_m;
541             if (n_mf.destination!=null)
542             {
543               Vertex n_vs = graph.getVertex(n_mf.oldPosition.getX(), n_mf.oldPosition.getY());
544               Vertex n_vd = graph.getVertex(n_mf.destination.getX(), n_mf.destination.getY());
545               
546               if ( ((vs==n_vs)&&(vd==n_vd))){
547                   n++;
548                   
549               String vehListStr = (String) CommunicationsManager.checkIfNodesInTheRing(owner.getID());
550             
551               String uniqueNodeID = null;;
552               String uniqueID = null;
553             
554               
555         
556             if(congestedSegmentId!= null && QueueManager.retrieveAndUpdateParticipatingNodes(congestedSegmentId,owner.getID(),"GET"))
557             {
558                 ownerAdded = true;
559             }
560             
561 
562             if(congestedSegmentId!= null && QueueManager.retrieveAndUpdateParticipatingNodes(congestedSegmentId,node.getID(),"GET")==true)
563             {
564                 nodeAdded = true;
565             
566             }
567             
568             if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
569                 if(node!=null && node.getID().equals("#0")){
570             //      System.out.println("#0 nodeAdded"+ nodeAdded+" nodeNew : ");
571                 }
572             }
573             
574             if(ownerAdded && nodeAdded){
575                 bothNew = true;
576                 continue;
577             }else if(!ownerAdded && nodeAdded){
578                 bothNew = false;
579                 //nodeOnLine--;
580             }else if(ownerAdded && !nodeAdded){
581                 bothNew = false;
582                 //nodeOnLine--;
583             }else if(!ownerAdded && !nodeAdded){
584                 bothNew = true;
585             }
586             
587             
588             
589              if(congestedSegmentId!=null && congestedSegmentId.equals("s17")){
590                         if(owner.getID().equals("#18")||owner.getID().equals("#17")||owner.getID().equals("#19")){
591                 //          System.out.println("outside check for s17 owner: "+owner.getID()+" and node: "+node.getID()+ " oldVertexFlag : "+oldVertexFlag+ "currentEdge:  "+ currentEdge.getV2().getX()+","+currentEdge.getV2().getY()+" vehListStr value:  " + vehListStr);
592                         }
593                     }
594             
595             if(oldVertex!=null && !oldVertex.equals(vs)){
596                  if(!CommunicationsManager.getCommunicationStarted(owner.getID(),currentEdge.getV2())){
597                 
598                      
599                      oldVertexFlag = true;
600                      vehRngNotif.setVehicles("");
601                      
602                      if(congestedSegmentId!=null && congestedSegmentId.equals("s17")){
603                     //  if(node.getID().equals("#0") || owner.getID().equals("#0")){
604 
605                     //  }
606                     }
607                      if(congestedSegmentId!=null && congestedSegmentId.equals("s31")){
608                         if(node.getID().equals("#2") || owner.getID().equals("#2")){
609 
610                         }
611                     }
612                     
613                  }else{
614                      oldVertexFlag = false;
615                  }
616             }
617             
618             
619             if(vehListStr == null || oldVertexFlag){
620                 
621                 identifier = (String) CommunicationsManager.checkIfNodesInTheRing(node.getID());
622                  
623                 if(vehListStr!=null && identifier!=null){
624 
625                 }
626                 
627                 
628                 String uniqueOwnerID = null;
629                   if(dist<5 && oldVertexFlag){
630                         
631                       
632                       
633                 //-TEMP   //System.out.println("Owner is "+owner.getID()+" and node is "+node.getID()+ " and oldVertexFlag is: "+oldVertexFlag+ "in the if loop");
634                  if(!CommunicationsManager.getCommunicationStarted(node.getID(),currentEdge.getV2())){
635                  identifier=null;
636                     if(!setInLoop){
637                         vehInRng="";
638                         vehRngNotif.setVehicles("");
639                     }else{
640                         setInLoop=false;
641                     }
642                  }
643                  }
644               if(identifier==null){
645                  
646                 
647                 if(dist > 5){
648                     vehInRng="";
649                     continue;
650                 }else{                  
651                     if(onSameEdge){
652                         if(ownerEquipped && nodeEquipped){
653                         
654                             if(vehListStr==null){
655                             uniqueID = String.valueOf(u.getRandom().nextInt());
656                             vehInRng = vehInRng.concat(node.getID());
657                             vehInRng = vehInRng.concat(",");
658                             vehInRng = vehInRng.concat(owner.getID());
659                             vehRngNotif.setCommunID(uniqueID);
660                             vehRngNotif.setOwner(owner.getID());
661                             vehRngNotif.setVehicles(vehInRng);
662                             setInLoop = true;
663                                         //              System.out.println("vehInRng loop 2 "+vehInRng);
664                             CommunicationsManager.addToCommunicationStarted(owner.getID(),currentEdge.getV2());
665                             CommunicationsManager.addToCommunicationStarted(node.getID(),currentEdge.getV2());
666                             CommunicationsManager.addToCommRing(uniqueID, vehInRng);
667                             DataAccess.addToAgentCntMap(vehInRng);
668 
669                             }else{
670                                 vehInRng = CommunicationsManager.returnVehicleList(vehListStr);
671                                 vehInRng = vehInRng.concat(",");
672                                 vehInRng = vehInRng.concat(node.getID());
673                                 vehRngNotif.setCommunID(vehListStr);
674                                 vehRngNotif.setOwner(owner.getID());
675                                 vehRngNotif.setVehicles(vehInRng);
676                             CommunicationsManager.addToCommunicationStarted(node.getID(),currentEdge.getV2());
677                             CommunicationsManager.addToCommRing(vehListStr, vehInRng);
678                             DataAccess.addToAgentCntMap(vehInRng);
679                         
680     
681                             }
682                         }
683                         
684                         
685                     }else{//nodes not on same edge or do not have common source
686                         if(ownerEquipped){
687                             vehInRng="";
688                             uniqueID  = String.valueOf(u.getRandom().nextInt());
689                             vehInRng = vehInRng.concat(owner.getID());
690                             vehRngNotif.setCommunID(uniqueID);
691                             vehRngNotif.setOwner(owner.getID());
692                             vehRngNotif.setVehicles(vehInRng);
693 
694                             CommunicationsManager.addToCommunicationStarted(owner.getID(),currentEdge.getV2());
695                             CommunicationsManager.addToCommRing(uniqueID, vehInRng);
696                             DataAccess.addToAgentCntMap(vehInRng);
697                         //do nothing about the node
698                         }
699                     }
700                                                 
701                }                                
702                      
703               }else{
704               if(dist > 5){
705                   vehInRng="";
706                   continue;
707               }else{
708                     
709            //         System.out.println("vehListStr is null identifier is not null and dist <= 47 " +dist);
710                 if(onSameEdge)
711                     {
712                         if(ownerEquipped  && bothNew){
713                             vehInRng = CommunicationsManager.returnVehicleList(identifier);
714                             vehInRng = vehInRng.concat(",");
715                             vehInRng = vehInRng.concat(owner.getID());
716                             vehRngNotif.setCommunID(identifier);
717                             vehRngNotif.setOwner(owner.getID());
718                             vehRngNotif.setVehicles(vehInRng);
719                 
720             //-TEMP     System.out.println("vehInRng loop 4 "+vehInRng+" Comm ID "+identifier+ " && node ID is: "+node.getID());
721                             CommunicationsManager.addToCommunicationStarted(owner.getID(),currentEdge.getV2());
722                
723                             CommunicationsManager.addToCommRing(identifier, vehInRng);
724                             DataAccess.addToAgentCntMap(vehInRng);
725                         }
726                     }//on same edge
727                 else{
728                     if(ownerEquipped && !ownerAdded){
729                         vehInRng="";
730                         uniqueID  = String.valueOf(u.getRandom().nextInt());
731                         vehInRng = vehInRng.concat(owner.getID());
732                         vehRngNotif.setCommunID(identifier);
733                         vehRngNotif.setOwner(owner.getID());
734                         vehRngNotif.setVehicles(vehInRng);
735     
736                 //-TEMP     System.out.println("vehInRng loop 4 "+vehInRng+" Comm ID "+identifier+ " && node ID is: "+node.getID());
737                         CommunicationsManager.addToCommunicationStarted(owner.getID(),currentEdge.getV2());
738   
739                         CommunicationsManager.addToCommRing(identifier, vehInRng);
740                         DataAccess.addToAgentCntMap(vehInRng);
741                     }
742                 }
743                     
744                }               
745              }
746                      
747          }else{
748         
749             if(!CommunicationsManager.getCommunicationStarted(node.getID(),currentEdge.getV2())){
750                 
751                 identifier=null;
752             }else{
753                 
754                 identifier = (String) CommunicationsManager.checkIfNodesInTheRing(node.getID());
755                 
756             
757             }
758             if(identifier==null){
759                 
760      //        System.out.println("vehListStr is not null and identifier is null");
761             if(dist > 5){
762                 vehInRng="";
763                 
764                 
765                 continue;
766         
767             }else{
768                 
769 
770                 if(onSameEdge){ //where owner ID is not null
771                     if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
772                             if(node.getID().equals("#0")){
773                             //System.out.println("onSameEdge "+vehInRng+ " "+nodeAdded);
774                             }
775                         }
776                     if(nodeEquipped && bothNew){
777                         vehInRng = CommunicationsManager.returnVehicleList(vehListStr);
778                         vehInRng = vehInRng.concat(",");
779                         vehInRng = vehInRng.concat(node.getID());
780                         vehRngNotif.setCommunID(vehListStr);
781                         vehRngNotif.setOwner(owner.getID());
782                         vehRngNotif.setVehicles(vehInRng);
783             //System.out.println("vehInRng loop 6 "+vehInRng+" Comm ID "+identifier+ " : vehListStr : "+vehListStr);
784                         if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
785                             if(node.getID().equals("#0")){
786                             //System.out.println("vehInRng loop 1 "+vehInRng);
787                             }
788                         }
789                         CommunicationsManager.addToCommunicationStarted(node.getID(),currentEdge.getV2());
790                         CommunicationsManager.addToCommRing(vehListStr, vehInRng);
791                         DataAccess.addToAgentCntMap(vehInRng);
792                     } else if(nodeEquipped && !nodeAdded){
793                         vehInRng="";
794                         uniqueID  = String.valueOf(u.getRandom().nextInt());
795                         vehInRng = vehInRng.concat(node.getID());
796                         vehRngNotif.setCommunID(uniqueID);
797                         vehRngNotif.setOwner(owner.getID());
798                         vehRngNotif.setVehicles(vehInRng);
799                     if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
800                         if(node.getID().equals("#0")){
801                         //System.out.println("vehInRng loop 2 "+vehInRng);
802                         }
803                     }
804                         CommunicationsManager.addToCommunicationStarted(node.getID(),currentEdge.getV2());
805                         CommunicationsManager.addToCommRing(uniqueID, vehInRng);
806                         DataAccess.addToAgentCntMap(vehInRng);
807 
808                         
809                     }
810             }// if on same edge
811                 else{
812                     if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
813                         if(node.getID().equals("#0")){
814                         //System.out.println("ID==NULL && !ONsAMEeDGE");
815                         }
816                     }
817                     
818                     if(!nodeAdded){
819                         vehInRng="";
820                         uniqueID  = String.valueOf(u.getRandom().nextInt());
821                         vehInRng = vehInRng.concat(node.getID());
822                         vehRngNotif.setCommunID(uniqueID);
823                         vehRngNotif.setOwner(owner.getID());
824                         vehRngNotif.setVehicles(vehInRng);
825                     if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
826                         if(node.getID().equals("#0")){
827                         //System.out.println("vehInRng loop 2 "+vehInRng);
828                         }
829                     }
830                         CommunicationsManager.addToCommunicationStarted(node.getID(),currentEdge.getV2());
831                         CommunicationsManager.addToCommRing(uniqueID, vehInRng);
832                         DataAccess.addToAgentCntMap(vehInRng);
833 
834                         
835                     }
836                 }
837             }                               
838          }else{
839             
840             if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
841                 if(owner.getID().equals("#0")){
842                 //System.out.println("(identifier not null & vehListStr not null) -- "+ "vehListStr is :" + vehListStr + " identifier is  : " +identifier+ " bothNew "+bothNew);
843                 }
844             }
845              //identifier not null
846               if(onSameEdge)
847                 {
848                 
849                  if(!ownerAdded){
850                       vehInRng = CommunicationsManager.returnVehicleList(vehListStr);
851                      vehRngNotif.setCommunID(vehListStr);
852                  }else{
853                   vehInRng = CommunicationsManager.returnVehicleList(identifier);
854                  vehRngNotif.setCommunID(identifier);
855                  }
856                     if(vehInRng!=null && vehInRng.indexOf(owner.getID())<=-1 && bothNew){
857                         
858                             vehInRng = vehInRng.concat(",");
859                             vehInRng = vehInRng.concat(owner.getID());
860                             
861                             //vehRngNotif.setCommunID(identifier);
862                             vehRngNotif.setOwner(owner.getID());
863                             vehRngNotif.setVehicles(vehInRng);
864                             
865                         //-TEMP     System.out.println("vehInRng loop 4 "+vehInRng+" Comm ID "+identifier+ " && node ID is: "+node.getID());
866                         //  CommunicationsManager.addToCommunicationStarted(owner.getID(),vs);
867                            
868                             CommunicationsManager.addToCommRing(identifier, vehInRng);
869                     }else if(vehInRng!=null && vehInRng.indexOf(node.getID())<=-1){
870                         
871                             vehInRng = vehInRng.concat(",");
872                             vehInRng = vehInRng.concat(node.getID());
873                             
874                             //vehRngNotif.setCommunID(identifier);
875                             vehRngNotif.setOwner(owner.getID());
876                             vehRngNotif.setVehicles(vehInRng);
877                             
878                         //-TEMP     System.out.println("vehInRng loop 4 "+vehInRng+" Comm ID "+identifier+ " && node ID is: "+node.getID());
879                         //  CommunicationsManager.addToCommunicationStarted(owner.getID(),vs);
880                            
881                             CommunicationsManager.addToCommRing(identifier, vehInRng);
882                     }else{
883                         //vehRngNotif.setCommunID(identifier);
884                         vehRngNotif.setOwner(owner.getID());
885                         vehRngNotif.setVehicles(vehInRng);
886                     }
887             //DataAccess.addToAgentCntMap(vehInRng);
888                 }
889          }
890           
891         }
892             }//vertex comparison
893             
894         }
895      
896         
897       
898       }
899           if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
900               //System.out.println("Communic Id : "+vehRngNotif.getCommunID()+ " "+ vehRngNotif.getVehicles());
901           }
902           
903           vehRngNotif.setRngChng(false);
904             /*  CommunicationsManager.addToCommRing(identifier, vehInRng);
905                DataAccess.addToAgentCntMap(vehInRng);*/
906     //      System.out.println("sending notification..."+vehRngNotif.getVehicles());
907           String vertexX = String.valueOf(currentEdge.getV2().getX());
908           String vertexY = String.valueOf(currentEdge.getV2().getY());
909           
910          
911           vehRngNotif.setYCoord(vertexY);
912           vehRngNotif.setXCoord(vertexX);
913           
914          if(!CommunicationsManager.notifSent(vs,vd,vehRngNotif.getVehicles(),vehRngNotif.getCommunID(),congestedSegmentId,nodeOnLine)){
915             
916        //    if(congestedSegmentId!=null){
917                 
918               u.sendNotification(vehRngNotif);
919         
920          }else{
921             
922               CommunicationsManager.addToCommunicationRecordedVehiclesMap(currentEdge.getV1(),currentEdge.getV2(),vehRngNotif.getVehicles(),vehRngNotif.getCommunID(),congestedSegmentId,nodeOnLine);
923          }
924  
925        } //end of BIG while loop
926       
927         //if((1*currentEdge.getDistance())/3 < Math.sqrt(distFromSrcX*distFromSrcX+distFromSrcY*distFromSrcY)){
928          
929          if(vehRngNotif.getCommunID()!=null){
930         
931                 
932             notifSentFlag = CommunicationsManager.notifSent(vs,vd,vehRngNotif.getVehicles(),vehRngNotif.getCommunID(),congestedSegmentId,nodeOnLine);
933             
934             StringTokenizer st = new StringTokenizer (vehRngNotif.getVehicles(),",");
935             
936             if(ownerEquipped){
937             if (sentAgentCount==0 && sentCongSegID==null && approvedDistanceForCommunication){
938                 
939                 if(congestedSegmentId!=null && congestedSegmentId.equals("s43") && owner.getID().equals("#15")){
940                     //System.out.println("loop 1 : "+ vehRngNotif.getVehicles());
941                 }
942                 
943                 
944                 //nodeOnLine = st.countTokens();
945                 sentAgentCount = nodeOnLine;
946                  sentCongSegID = congestedSegmentId;
947                 
948                  vehCount.setCommID(vehRngNotif.getCommunID());
949                  vehCount.setCongSegID(congestedSegmentId);
950                  vehCount.setAgentCnt(nodeOnLine);
951                  vehCount.setxCoordinatesOfEdge(Double.toString(x2));
952                  vehCount.setyCoordinatesOfEdge(Double.toString(y2));
953         
954                  vehCount.setOwner(owner.getID());
955                 
956                 //ADDED
957                  if(congestedSegmentId!=null && !notifSentFlag){
958                      if(/*onSameEdge &&*/ ownerEquipped){
959                         // if(congestedSegmentId!=null && congestedSegmentId.equals("s1")){
960                             //  if(owner.equals("#5")){
961                                     System.out.println("loop 1-inner "+ vehRngNotif.getVehicles());
962                                 //}
963                             //}
964                          setFlagToFalse(vehRngNotif.getVehicles(),congestedSegmentId,finalMap,vehRngNotif.getCommunID(),currentEdge,owner.getID(),nodeOnLine);
965                      }
966                  }
967             //ADDED
968                  if(owner.getID().equals("10") || owner.getID().equals("15")){
969                 //      System.out.println("sentAgentCount: "+sentAgentCount+" and sentCongSegID: "+sentCongSegID+"by:" +owner.getID());
970                     }
971             //  
972                         if(!notifSentFlag && approvedDistanceForCommunication){
973                     //System.out.println("1 vehiclesL "+vehRngNotif.getVehicles());
974                        //System.out.println("For 1 : " + owner.getID()+ "  "+ congestedSegmentId+ " " +vehCount.getCommID()+ " "+vehCount.getAgentCnt());
975                  u.sendNotification(vehCount);
976                 }
977                  // }
978             }else if(sentCongSegID!=null && congestedSegmentId!=null && approvedDistanceForCommunication){
979                 if(congestedSegmentId!=null && congestedSegmentId.equals("s43")  && owner.getID().equals("#15")){
980                     //System.out.println("loop 2 "+ vehRngNotif.getVehicles());
981                 }
982                 
983                 if(!sentCongSegID.equals(congestedSegmentId) || sentAgentCount!=nodeOnLine){
984             //   if((2*currentEdge.getDistance())/3 > Math.sqrt(distFromSrcX*distFromSrcX+distFromSrcY*distFromSrcY)){
985                     if(sentCongSegID.equals(congestedSegmentId)){
986                         if(sentAgentCount!=nodeOnLine){
987                         //      System.out.println("Will set the flag false here in ELSE LOOP : "+owner.getID()+"congested segment ID: "+ congestedSegmentId+ " nodes:  "+nodeOnLine+"vehRngNotif.getCommunID()"+vehRngNotif.getCommunID()+currentEdge.getV2().getX());
988                             }
989                      }
990                     if(owner.getID().equals("10") || owner.getID().equals("15")){
991             //          System.out.println("sentAgentCount: "+sentAgentCount+" and sentCongSegID: "+sentCongSegID+"by:" +owner.getID());
992                     }
993                         //ADDED
994                     if(congestedSegmentId!=null && !notifSentFlag){
995                         if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
996                         //System.out.println("onSameEdge loop2: "+onSameEdge );
997                         //System.out.println("ownerEquipped loop2: "+ ownerEquipped);
998                         }
999                         if(/*onSameEdge && */ownerEquipped){
1000                            
1001                            
1002                            if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
1003                                if(owner.equals("#29")){
1004                            //      System.out.println("loop 2-inner "+ vehRngNotif.getVehicles());
1005                                }
1006                            }
1007                            setFlagToFalse(vehRngNotif.getVehicles(),congestedSegmentId,finalMap,vehRngNotif.getCommunID(),currentEdge,owner.getID(),nodeOnLine);
1008                        }
1009                    }
1010                    //ADDED
1011                
1012                    //nodeOnLine = st.countTokens();
1013                //  nodeOnLine = getTokenCnt(vehRngNotif.getVehicles());
1014                    
1015                    
1016                    sentAgentCount = nodeOnLine;
1017                     sentCongSegID = congestedSegmentId;
1018                     vehCount.setCommID(vehRngNotif.getCommunID());
1019                     vehCount.setCongSegID(congestedSegmentId);
1020                     //if(congestedSegmentId.equals("s47")){
1021                     vehCount.setAgentCnt(nodeOnLine);
1022                    // }else if(congestedSegmentId.equals("s2")){
1023                    //   vehCount.setAgentCnt(3);
1024                    //   }
1025                     vehCount.setxCoordinatesOfEdge(Double.toString(x2));
1026                     vehCount.setyCoordinatesOfEdge(Double.toString(y2));
1027                     vehCount.setOwner(owner.getID());
1028                    
1029                     
1030                     if(!notifSentFlag && approvedDistanceForCommunication){
1031                    
1032                         u.sendNotification(vehCount);
1033                     }
1034                // }
1035            }
1036                
1037      }else if(congestedSegmentId!=null && approvedDistanceForCommunication){
1038        //  System.out.println(" in else 3 for owner: "+owner.getID()+" "+congestedSegmentId);
1039          if(congestedSegmentId!=null && congestedSegmentId.equals("s43")){
1040                if(owner.equals("#15")){
1041                    //System.out.println("loop 3 "+ vehRngNotif.getVehicles());
1042                }
1043            }
1044          
1045            if(sentAgentCount==1 && sentCongSegID==null){
1046            String num  = String.valueOf(u.getRandom().nextInt());  
1047            vehInRng = " ";
1048            vehInRng = vehInRng.concat(owner.getID());
1049                vehRngNotif.setOwner(owner.getID());
1050                vehRngNotif.setCommunID(num);
1051                vehRngNotif.setVehicles(vehInRng);
1052                if(owner.getID().equals("#10") || owner.getID().equals("#15")){
1053        //          System.out.println("sentAgentCount: "+sentAgentCount+" and sentCongSegID: "+sentCongSegID+"by:" +owner.getID()+" for vehicles : "+vehRngNotif.getVehicles().toString());
1054                }
1055            }else{
1056                
1057            
1058            //ADDED
1059            if(congestedSegmentId!=null && !notifSentFlag){
1060                if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
1061                    //System.out.println("onSameEdge loop3: "+onSameEdge+"  "+ vehRngNotif.getVehicles());
1062                    //System.out.println("ownerEquipped loop3: "+ ownerEquipped+"  "+ vehRngNotif.getVehicles());
1063                    }
1064                if(/*onSameEdge && */ownerEquipped){
1065                    if(congestedSegmentId!=null && congestedSegmentId.equals("s37")){
1066                        if(owner.equals("#29")){
1067                        //  System.out.println("loop 3-inner ");
1068                        }
1069                    }
1070                    setFlagToFalse(vehRngNotif.getVehicles(),congestedSegmentId,finalMap,vehRngNotif.getCommunID(),currentEdge,owner.getID(),nodeOnLine);
1071                }
1072            }
1073            //ADDED     
1074            }
1075            //nodeOnLine = st.countTokens();
1076                 sentAgentCount = nodeOnLine;
1077             sentCongSegID = congestedSegmentId;
1078             vehCount.setCommID(vehRngNotif.getCommunID());
1079             vehCount.setCongSegID(congestedSegmentId);
1080             //if(congestedSegmentId.equals("s47")){
1081             vehCount.setAgentCnt(nodeOnLine);
1082            // }else if(congestedSegmentId.equals("s2")){
1083            //   vehCount.setAgentCnt(3);
1084            //   }
1085             vehCount.setxCoordinatesOfEdge(Double.toString(x2));
1086             vehCount.setyCoordinatesOfEdge(Double.toString(y2));
1087             vehCount.setOwner(owner.getID());
1088            
1089             
1090             
1091             if(!notifSentFlag && approvedDistanceForCommunication){
1092            //  System.out.println("3 vehiclesL "+vehRngNotif.getVehicles());
1093                //System.out.println("For 3" + owner.getID()+ "  "+ congestedSegmentId+ " " +vehCount.getCommID()+ " "+vehCount.getAgentCnt());
1094                 u.sendNotification(vehCount);
1095             }
1096        // }
1097    
1098            
1099}else if(sentAgentCount!=0 && congestedSegmentId==null && sentCongSegID!=null && approvedDistanceForCommunication){
1100
1101    if(congestedSegmentId!=null && congestedSegmentId.equals("s43") && owner.getID().equals("#15")){
1102        //System.out.println("loop 4 : "+vehRngNotif.getVehicles());
1103    }
1104    //  System.out.println(" in else 4 for owner: "+owner.getID()+" "+congestedSegmentId);
1105    //nodeOnLine = st.countTokens();
1106     sentAgentCount = nodeOnLine;
1107     sentCongSegID = congestedSegmentId;
1108     vehCount.setCommID(vehRngNotif.getCommunID());
1109     vehCount.setCongSegID(congestedSegmentId);
1110     //if(congestedSegmentId.equals("s47")){
1111     vehCount.setAgentCnt(nodeOnLine);
1112    // }else if(congestedSegmentId.equals("s2")){
1113    //   vehCount.setAgentCnt(3);
1114    //   }
1115     vehCount.setxCoordinatesOfEdge(Double.toString(x2));
1116     vehCount.setyCoordinatesOfEdge(Double.toString(y2));
1117     vehCount.setOwner(owner.getID());
1118    
1119     
1120     
1121     if(!notifSentFlag && approvedDistanceForCommunication){
1122    //   System.out.println("4 vehiclesL "+vehRngNotif.getVehicles());
1123         //System.out.println("For 4" + owner.getID()+ "  "+ congestedSegmentId+ " " +vehCount.getCommID()+ " "+vehCount.getAgentCnt());
1124         u.sendNotification(vehCount);
1125     }
1126// }
1127
1128
1129}
1130     
1131         }else{
1132             if(congestedSegmentId!=null && congestedSegmentId.equals("s37") && owner.getID().equals("#15")){
1133                    //System.out.println("loop 5 : "+vehRngNotif.getVehicles());
1134                }
1135             
1136             //owner not equipped
1137             //nodeOnLine = st.countTokens();
1138             sentAgentCount = allNodesOnLine;
1139             sentCongSegID = congestedSegmentId;
1140             vehCount.setCommID(owner.getID());
1141             vehCount.setCongSegID(congestedSegmentId);
1142             //if(congestedSegmentId.equals("s47")){
1143             vehCount.setAgentCnt(nodeOnLine);
1144            
1145             vehCount.setxCoordinatesOfEdge(Double.toString(x2));
1146             vehCount.setyCoordinatesOfEdge(Double.toString(y2));
1147             vehCount.setOwner(owner.getID());
1148            
1149             
1150             
1151             if(!notifSentFlag && approvedDistanceForCommunication){
1152      //         System.out.println("5 vehiclesL "+vehRngNotif.getVehicles());
1153                 //System.out.println("For 5" + owner.getID()+ "  "+ congestedSegmentId+ " " +vehCount.getCommID()+ " "+vehCount.getAgentCnt());
1154                 u.sendNotification(vehCount);
1155             }
1156         }
1157             
1158             
1159         }
1160        try{
1161        
1162          if (n>1)
1163          k = (float)(n/currentEdge.getDistance());
1164              
1165      int loop=0;
1166      int inc=0;
1167      
1168        segID = getSegmentID(currentEdge);
1169        segThreshold = getSegmentThreshold(currentEdge);
1170        
1171        
1172        
1173        if(segID!=null){
1174            if(segID.equals("s25") || segID.equals("s26") || segID.equals("s39")|| segID.equals("s40")|| segID.equals("s45")|| segID.equals("s46")|| segID.equals("s49")|| segID.equals("s50")|| segID.equals("s51")){
1175                if(n>segThreshold+inc && n <= segThreshold+6+inc){
1176                    speed = 0.0009f;
1177                }else if(n<=segThreshold+inc){
1178                    speed = 0.0011f;
1179                }else if(n>segThreshold+6+inc && n<=segThreshold+13+inc){
1180                    speed = 0.0003f;
1181                }else if(n>segThreshold+13+inc){
1182                    speed = 0.00008f;
1183                }
1184            }else if(segID.equals("s11") || segID.equals("s14") || segID.equals("s23")|| segID.equals("s24")|| segID.equals("s27")|| segID.equals("s30") || segID.equals("s37") || segID.equals("s38")|| segID.equals("s56")|| segID.equals("s57")){
1185                    
1186                if(n>segThreshold+inc && n <= segThreshold+6+inc){
1187                    speed = 0.0011f;
1188                }else if(n<=segThreshold+inc){
1189                    speed = 0.0013f;
1190                }else if(n>segThreshold+6+inc && n<=segThreshold+13+inc){
1191                    speed = 0.0004f;
1192                }else if(n>segThreshold+13+inc){
1193                    speed = 0.00009f;
1194                }   
1195            }else if(segID.equals("s1") || segID.equals("s2") || segID.equals("s3")|| segID.equals("s4")|| segID.equals("s5")|| segID.equals("s9") || 
1196                    segID.equals("s10") || segID.equals("s12")|| segID.equals("s15")|| segID.equals("s20")|| segID.equals("s21")|| segID.equals("s22")|| segID.equals("s28") ||
1197                segID.equals("s29") || segID.equals("s31")|| segID.equals("s36")|| segID.equals("s41")|| segID.equals("s42")|| segID.equals("s43")|| segID.equals("s44")||
1198                segID.equals("s47")|| segID.equals("s48")|| segID.equals("s53")|| segID.equals("s54")|| segID.equals("s58")){
1199                if(n>segThreshold+inc && n <= segThreshold+6+inc){
1200                    speed = 0.0014f;
1201                }else if(n<=segThreshold+inc){
1202                    speed = 0.0016f;
1203                }else if(n>segThreshold+6+inc && n<=segThreshold+13+inc){
1204                    speed = 0.0005f;
1205                }else if(n>segThreshold+13+inc){
1206                    speed = 0.0001f;
1207                }   
1208                
1209            //  System.out.println("speed for n: "+speed+ " and n "+n+ "segment "+segID);
1210            }else if(segID.equals("s6") ||segID.equals("s7")|| segID.equals("s8")|| segID.equals("s13")|| segID.equals("s16")|| segID.equals("s17") || 
1211                    segID.equals("s18") || segID.equals("s19")|| segID.equals("s32")|| segID.equals("s33") || segID.equals("s34") || segID.equals("s35")|| segID.equals("s52")|| segID.equals("s55")){
1212                if(n>segThreshold+inc && n <= segThreshold+6+inc){
1213                    speed = 0.0016f;
1214                }else if(n<=segThreshold+inc){
1215                    speed = 0.0019f;
1216                }else if(n>segThreshold+6+inc && n<=segThreshold+13+inc){
1217                    speed = 0.0005f;
1218                }else if(n>segThreshold+13+inc){
1219                    speed = 0.0002f;
1220                }   
1221            }
1222        }
1223                
1224            //  System.out.println("speed : "+speed+ "and n:"+n+ " for segment : "+segID);
1225                  
1226}catch(Exception e){
1227    e.printStackTrace();
1228}
1229          //System.out.println("chose the speed, final speed : "+speed+"minspeed:"+minSpeed+"max speed:"+maxSpeed+"by vehicle:"+owner.getID());
1230          
1231          if(speed<=new Float("1.9611111E-4") && n>=7)//0.00019611111000000)
1232          {
1233            
1234              
1235              congestionAlert  = true;
1236              
1237              notifPattern.setReasonForCongestion(1);
1238              Vertex closestVertex = CommunicationsManager.getClosestVertex(oldPosition.getX(), oldPosition.getY());
1239              congStartPoint = new Position3D(closestVertex.getX(),closestVertex.getY(),0.0);
1240        //    System.out.println("chose the speed, final speed : "+speed+"minspeed:"+minSpeed+"max speed:"+maxSpeed+"by vehicle:"+owner.getID()+"for "+n+" vehicles..after "+congStartPoint);
1241              
1242              //notifPattern.setSegmentID(getSegmentFromCoordinates(oldPosition,destination));
1243              notifPattern.setVehicleID("#33");
1244              
1245          }
1246  //    }
1247      if (speed<=0.0f){
1248        speed = minSpeed;
1249      
1250      }
1251    }
1252    
1253//    u.sendNotification(new DebugNotification(this, u, "at "+u.getTimeAsString()+" "+((Node)owner).getID()+" changes speed to "+speed*1000.0f+" m/s"));    
1254  }
1255
1256  }
1257  
1258/**
1259 * This method is used to retrieve segment ID
1260 * @param currentEdge 
1261 * @return segment ID
1262 */
1263
1264private String getSegmentID(Edge currentEdge) {
1265        
1266     String segmentID = null;
1267      if(currentEdge.getV1().getX() == 0.0 && currentEdge.getV1().getY() == 0.0) {
1268            if(currentEdge.getV2().getX() == 12.0 && currentEdge.getV2().getY() == 8.0) {
1269                return "s1";
1270            }
1271        }
1272        
1273        if(currentEdge.getV1().getX() == 12.0 && currentEdge.getV1().getY() == 8.0) {
1274            if(currentEdge.getV2().getX() == 24.5 && currentEdge.getV2().getY() == 21.0) {
1275                return "s2";
1276            }
1277        }
1278        if(currentEdge.getV1().getX() == 6.5 && currentEdge.getV1().getY() == 15.0) {
1279            if(currentEdge.getV2().getX() == 19.5 && currentEdge.getV2().getY() == 22.5) {
1280                return "s3";
1281            }
1282        }
1283        if(currentEdge.getV1().getX() == 24.5 && currentEdge.getV1().getY() == 21.0) {
1284            if(currentEdge.getV2().getX() == 36.5 && currentEdge.getV2().getY() == 31.0) {
1285                return "s4";
1286            }
1287        }
1288        if(currentEdge.getV1().getX() == 36.5 && currentEdge.getV1().getY() == 31.0) {
1289            if(currentEdge.getV2().getX() == 62.5 && currentEdge.getV2().getY() == 24.0) {
1290                return "s5";
1291            }
1292        }
1293        if(currentEdge.getV1().getX() == 62.5 && currentEdge.getV1().getY() == 24.0) {
1294            if(currentEdge.getV2().getX() == 72.0 && currentEdge.getV2().getY() == 52.0) {
1295                return "s6";
1296            }
1297        }
1298        if(currentEdge.getV1().getX() == 72.0 && currentEdge.getV1().getY() == 52.0) {
1299            if(currentEdge.getV2().getX() == 75.5 && currentEdge.getV2().getY() == 81.0) {
1300                return "s7";
1301            }
1302        }
1303        if(currentEdge.getV1().getX() == 75.5 && currentEdge.getV1().getY() == 81.0) {
1304            if(currentEdge.getV2().getX() == 77.5 && currentEdge.getV2().getY() == 95.0) {
1305                return "s8";
1306            }
1307        }
1308        if(currentEdge.getV1().getX() == 77.5 && currentEdge.getV1().getY() == 95.0) {
1309            if(currentEdge.getV2().getX() == 80.0 && currentEdge.getV2().getY() == 107.0) {
1310                return "s9";
1311            }
1312        }
1313        if(currentEdge.getV1().getX() == 80.0 && currentEdge.getV1().getY() == 107.0) {
1314            if(currentEdge.getV2().getX() == 83.0 && currentEdge.getV2().getY() == 129.0) {
1315                return "s10";
1316            }
1317        }
1318        if(currentEdge.getV1().getX() == 83.0 && currentEdge.getV1().getY() == 129.0) {
1319            if(currentEdge.getV2().getX() == 71.0 && currentEdge.getV2().getY() == 131.0) {
1320                return "s11";
1321            }
1322        }
1323        if(currentEdge.getV1().getX() == 83.0 && currentEdge.getV1().getY() == 129.0) {
1324            if(currentEdge.getV2().getX() == 89.0 && currentEdge.getV2().getY() == 176.0) {
1325                return "s12";
1326            }
1327        }
1328        if(currentEdge.getV1().getX() == 89.0 && currentEdge.getV1().getY() == 176.0) {
1329            if(currentEdge.getV2().getX() == 81.0 && currentEdge.getV2().getY() == 177.0) {
1330                return "s13";
1331            }
1332        }
1333        if(currentEdge.getV1().getX() == 83.0 && currentEdge.getV1().getY() == 129.0) {
1334            if(currentEdge.getV2().getX() == 114.0 && currentEdge.getV2().getY() == 125.0) {
1335                return "s14";
1336            }
1337        }
1338        if(currentEdge.getV1().getX() == 114.0 && currentEdge.getV1().getY() == 125.0) {
1339            if(currentEdge.getV2().getX() == 117.5 && currentEdge.getV2().getY() == 171.5) {
1340                return "s15";
1341            }
1342        }
1343        if(currentEdge.getV1().getX() == 117.5 && currentEdge.getV1().getY() == 171.5) {
1344            if(currentEdge.getV2().getX() == 89.0 && currentEdge.getV2().getY() == 176.0) {
1345                return "s16";
1346            }
1347        }
1348        if(currentEdge.getV1().getX() == 81.0 && currentEdge.getV1().getY() == 177.0) {
1349            if(currentEdge.getV2().getX() == 87.5 && currentEdge.getV2().getY() == 219.0) {
1350                return "s17";
1351            }
1352        }
1353        if(currentEdge.getV1().getX() == 87.5 && currentEdge.getV1().getY() == 219.0) {
1354            if(currentEdge.getV2().getX() == 96.5 && currentEdge.getV2().getY() == 232.0) {
1355                return "s18";
1356            }
1357        }
1358        if(currentEdge.getV1().getX() == 96.5 && currentEdge.getV1().getY() == 232.0) {
1359            if(currentEdge.getV2().getX() == 101.5 && currentEdge.getV2().getY() == 272.0) {
1360                return "s19";
1361            }
1362        }
1363        if(currentEdge.getV1().getX() == 96.5 && currentEdge.getV1().getY() == 232.0) {
1364            if(currentEdge.getV2().getX() == 118.5 && currentEdge.getV2().getY() == 269.0) {
1365                return "s20";
1366            }
1367        }
1368        if(currentEdge.getV1().getX() == 118.5 && currentEdge.getV1().getY() == 269.0) {
1369            if(currentEdge.getV2().getX() == 101.5 && currentEdge.getV2().getY() == 272.0) {
1370                return "s21";
1371            }
1372        }
1373        if(currentEdge.getV1().getX() == 101.5 && currentEdge.getV1().getY() == 272.0) {
1374            if(currentEdge.getV2().getX() == 104.0 && currentEdge.getV2().getY() == 288.0) {
1375                return "s22";
1376            }
1377        }
1378        if(currentEdge.getV1().getX() == 24.5 && currentEdge.getV1().getY() == 21.0) {
1379            if(currentEdge.getV2().getX() == 26.0 && currentEdge.getV2().getY() == 34.0) {
1380                return "s23";
1381            }
1382        }
1383        if(currentEdge.getV1().getX() == 26.0 && currentEdge.getV1().getY() == 34.0) {
1384            if(currentEdge.getV2().getX() == 46.5 && currentEdge.getV2().getY() == 41.0) {
1385                return "s24";
1386            }
1387        }
1388        if(currentEdge.getV1().getX() == 46.5 && currentEdge.getV1().getY() == 41.0) {
1389            if(currentEdge.getV2().getX() == 45.0 && currentEdge.getV2().getY() == 45.0) {
1390                return "s25";
1391            }
1392        }
1393        if(currentEdge.getV1().getX() == 45.0 && currentEdge.getV1().getY() == 45.0) {
1394            if(currentEdge.getV2().getX() == 44.0 && currentEdge.getV2().getY() == 51.0) {
1395                return "s26";
1396            }
1397        }
1398        if(currentEdge.getV1().getX() == 44.0 && currentEdge.getV1().getY() == 51.0) {
1399            if(currentEdge.getV2().getX() == 43.0 && currentEdge.getV2().getY() == 84.5) {
1400                return "s27";
1401            }
1402        }
1403        if(currentEdge.getV1().getX() == 43.0 && currentEdge.getV1().getY() == 84.5) {
1404            if(currentEdge.getV2().getX() == 53.5 && currentEdge.getV2().getY() == 84.0) {
1405                return "s28";
1406            }
1407        }
1408        if(currentEdge.getV1().getX() == 53.5 && currentEdge.getV1().getY() == 84.0) {
1409            if(currentEdge.getV2().getX() == 75.5 && currentEdge.getV2().getY() == 81.0) {
1410                return "s29";
1411            }
1412        }
1413        if(currentEdge.getV1().getX() == 43.0 && currentEdge.getV1().getY() == 84.5) {
1414            if(currentEdge.getV2().getX() == 54.5 && currentEdge.getV2().getY() == 133.0) {
1415                return "s30";
1416            }
1417        }
1418        if(currentEdge.getV1().getX() == 54.5 && currentEdge.getV1().getY() == 133.0) {
1419            if(currentEdge.getV2().getX() == 71.0 && currentEdge.getV2().getY() == 131.0) {
1420                return "s31";
1421            }
1422        }
1423        if(currentEdge.getV1().getX() == 71.0 && currentEdge.getV1().getY() == 131.0) {
1424            if(currentEdge.getV2().getX() == 81.0 && currentEdge.getV2().getY() == 177.0) {
1425                return "s32";
1426            }
1427        }
1428        if(currentEdge.getV1().getX() == 53.5 && currentEdge.getV1().getY() == 84.0) {
1429            if(currentEdge.getV2().getX() == 54.5 && currentEdge.getV2().getY() == 94.5) {
1430                return "s33";
1431            }
1432        }
1433        if(currentEdge.getV1().getX() == 54.5 && currentEdge.getV1().getY() == 94.5) {
1434            if(currentEdge.getV2().getX() == 69.0 && currentEdge.getV2().getY() == 115.0) {
1435                return "s34";
1436            }
1437        }
1438        if(currentEdge.getV1().getX() == 69.0 && currentEdge.getV1().getY() == 115.0) {
1439            if(currentEdge.getV2().getX() == 71.0 && currentEdge.getV2().getY() == 131.0) {
1440                return "s35";
1441            }
1442        }
1443        if(currentEdge.getV1().getX() == 53.5 && currentEdge.getV1().getY() == 84.0) {
1444            if(currentEdge.getV2().getX() == 77.5 && currentEdge.getV2().getY() == 95.0) {
1445                return "s36";
1446            }
1447        }
1448        if(currentEdge.getV1().getX() == 19.5 && currentEdge.getV1().getY() == 22.5) {
1449            if(currentEdge.getV2().getX() == 11.0 && currentEdge.getV2().getY() == 47.0) {
1450                return "s37";
1451            }
1452        }
1453        if(currentEdge.getV1().getX() == 11.0 && currentEdge.getV1().getY() == 47.0) {
1454            if(currentEdge.getV2().getX() == 9.0 && currentEdge.getV2().getY() == 51.0) {
1455                return "s38";
1456            }
1457        }
1458        if(currentEdge.getV1().getX() == 9.0 && currentEdge.getV1().getY() == 51.0) {
1459            if(currentEdge.getV2().getX() == 17.0 && currentEdge.getV2().getY() == 55.0) {
1460                return "s39";
1461            }
1462        }
1463        if(currentEdge.getV1().getX() == 17.0 && currentEdge.getV1().getY() == 55.0) {
1464            if(currentEdge.getV2().getX() == 17.0 && currentEdge.getV2().getY() == 59.5) {
1465                return "s40";
1466            }
1467        }
1468        if(currentEdge.getV1().getX() == 17.0 && currentEdge.getV1().getY() == 59.5) {
1469            if(currentEdge.getV2().getX() == 37.0 && currentEdge.getV2().getY() == 52.0) {
1470                return "s41";
1471            }
1472        }
1473        if(currentEdge.getV1().getX() == 37.0 && currentEdge.getV1().getY() == 52.0) {
1474            if(currentEdge.getV2().getX() == 44.0 && currentEdge.getV2().getY() == 51.0) {
1475                return "s42";
1476            }
1477        }
1478        if(currentEdge.getV1().getX() == 17.0 && currentEdge.getV1().getY() == 59.5) {
1479            if(currentEdge.getV2().getX() == 22.5 && currentEdge.getV2().getY() == 90.0) {
1480                return "s43";
1481            }
1482        }
1483        if(currentEdge.getV1().getX() == 22.5 && currentEdge.getV1().getY() == 90.0) {
1484            if(currentEdge.getV2().getX() == 43.0 && currentEdge.getV2().getY() == 84.5) {
1485                return "s44";
1486            }
1487        }
1488        if(currentEdge.getV1().getX() == 12.0 && currentEdge.getV1().getY() == 8.0) {
1489            if(currentEdge.getV2().getX() == 6.5 && currentEdge.getV2().getY() == 15.0) {
1490                return "s45";
1491            }
1492        }
1493        if(currentEdge.getV1().getX() == 11.0 && currentEdge.getV1().getY() == 47.0) {
1494            if(currentEdge.getV2().getX() == 15.0 && currentEdge.getV2().getY() == 47.0) {
1495                return "s46";
1496            }
1497        }
1498        if(currentEdge.getV1().getX() == 24.5 && currentEdge.getV1().getY() == 21.0) {
1499            if(currentEdge.getV2().getX() == 19.0 && currentEdge.getV2().getY() == 35.5) {
1500                return "s47";
1501            }
1502        }
1503        if(currentEdge.getV1().getX() == 19.0 && currentEdge.getV1().getY() == 35.5) {
1504            if(currentEdge.getV2().getX() == 15.0 && currentEdge.getV2().getY() == 47.0) {
1505                return "s48";
1506            }
1507        }
1508        if(currentEdge.getV1().getX() == 19.0 && currentEdge.getV1().getY() == 35.5) {
1509            if(currentEdge.getV2().getX() == 45.0 && currentEdge.getV2().getY() == 45.0) {
1510                return "s49";
1511            }
1512        }
1513        if(currentEdge.getV1().getX() == 15.0 && currentEdge.getV1().getY() == 47.0) {
1514            if(currentEdge.getV2().getX() == 36.0 && currentEdge.getV2().getY() == 55.0) {
1515                return "s50";
1516            }
1517        }
1518        if(currentEdge.getV1().getX() == 36.0 && currentEdge.getV1().getY() == 55.0) {
1519            if(currentEdge.getV2().getX() == 37.0 && currentEdge.getV2().getY() == 52.0) {
1520                return "s51";
1521            }
1522        }
1523        if(currentEdge.getV1().getX() == 70.0 && currentEdge.getV1().getY() == 38.5) {
1524            if(currentEdge.getV2().getX() == 72.0 && currentEdge.getV2().getY() == 52.0) {
1525                return "s52";
1526            }
1527        }
1528        if(currentEdge.getV1().getX() == 46.5 && currentEdge.getV1().getY() == 41.0) {
1529            if(currentEdge.getV2().getX() == 70.0 && currentEdge.getV2().getY() == 38.5) {
1530                return "s53";
1531            }
1532        }
1533        
1534        if(currentEdge.getV1().getX() == 22.5 && currentEdge.getV1().getY() == 90.0) {
1535            if(currentEdge.getV2().getX() == 34.0 && currentEdge.getV2().getY() == 183.5) {
1536                return "s54";
1537            }
1538        }
1539        if(currentEdge.getV1().getX() == 34.0 && currentEdge.getV1().getY() == 183.0) {
1540            if(currentEdge.getV2().getX() == 81.0 && currentEdge.getV2().getY() == 177.0) {
1541                return "s55";
1542            }
1543        }
1544        if(currentEdge.getV1().getX() == 9.0 && currentEdge.getV1().getY() == 51.0) {
1545            if(currentEdge.getV2().getX() == 12.5 && currentEdge.getV2().getY() == 53.5) {
1546                return "s56";
1547            }
1548        }
1549        if(currentEdge.getV1().getX() == 12.5 && currentEdge.getV1().getY() == 53.5) {
1550            if(currentEdge.getV2().getX() == 17.0 && currentEdge.getV2().getY() == 55.0) {
1551                return "s57";
1552            }
1553        }
1554        if(currentEdge.getV1().getX() == 15.0 && currentEdge.getV1().getY() == 47.0) {
1555            if(currentEdge.getV2().getX() == 12.5 && currentEdge.getV2().getY() == 53.0) {
1556                return "s58";
1557            }
1558        }
1559return null;
1560}
1561
1562  
1563  /**
1564   * This method returns the segment threshold
1565   * @param currentEdge 
1566   * @return segment threshold
1567   */
1568  
1569  private int getSegmentThreshold(Edge currentEdge) {
1570        
1571         String segmentID = null;int n=0;
1572          if(currentEdge.getV1().getX() == 0.0 && currentEdge.getV1().getY() == 0.0) {
1573                if(currentEdge.getV2().getX() == 12.0 && currentEdge.getV2().getY() == 8.0) {
1574                    return 9+n;
1575                }
1576            }
1577            
1578            if(currentEdge.getV1().getX() == 12.0 && currentEdge.getV1().getY() == 8.0) {
1579                if(currentEdge.getV2().getX() == 24.5 && currentEdge.getV2().getY() == 21.0) {
1580                    return 9+n;
1581                }
1582            }
1583            if(currentEdge.getV1().getX() == 6.5 && currentEdge.getV1().getY() == 15.0) {
1584                if(currentEdge.getV2().getX() == 19.5 && currentEdge.getV2().getY() == 22.5) {
1585                    return 9+n;
1586                }
1587            }
1588            if(currentEdge.getV1().getX() == 24.5 && currentEdge.getV1().getY() == 21.0) {
1589                if(currentEdge.getV2().getX() == 36.5 && currentEdge.getV2().getY() == 31.0) {
1590                    return 9+n;
1591                }
1592            }
1593            if(currentEdge.getV1().getX() == 36.5 && currentEdge.getV1().getY() == 31.0) {
1594                if(currentEdge.getV2().getX() == 62.5 && currentEdge.getV2().getY() == 24.0) {
1595                    return 12+n;
1596                }
1597            }
1598            if(currentEdge.getV1().getX() == 62.5 && currentEdge.getV1().getY() == 24.0) {
1599                if(currentEdge.getV2().getX() == 72.0 && currentEdge.getV2().getY() == 52.0) {
1600                    return 12+n;
1601                }
1602            }
1603            if(currentEdge.getV1().getX() == 72.0 && currentEdge.getV1().getY() == 52.0) {
1604                if(currentEdge.getV2().getX() == 75.5 && currentEdge.getV2().getY() == 81.0) {
1605                    return 12+n;
1606                }
1607            }
1608            if(currentEdge.getV1().getX() == 75.5 && currentEdge.getV1().getY() == 81.0) {
1609                if(currentEdge.getV2().getX() == 77.5 && currentEdge.getV2().getY() == 95.0) {
1610                    return 8+n;
1611                }
1612            }
1613            if(currentEdge.getV1().getX() == 77.5 && currentEdge.getV1().getY() == 95.0) {
1614                if(currentEdge.getV2().getX() == 80.0 && currentEdge.getV2().getY() == 107.0) {
1615                    return 8+n;
1616                }
1617            }
1618            if(currentEdge.getV1().getX() == 80.0 && currentEdge.getV1().getY() == 107.0) {
1619                if(currentEdge.getV2().getX() == 83.0 && currentEdge.getV2().getY() == 129.0) {
1620                    return 11+n;
1621                }
1622            }
1623            if(currentEdge.getV1().getX() == 83.0 && currentEdge.getV1().getY() == 129.0) {
1624                if(currentEdge.getV2().getX() == 71.0 && currentEdge.getV2().getY() == 131.0) {
1625                    return 8+n;
1626                }
1627            }
1628            if(currentEdge.getV1().getX() == 83.0 && currentEdge.getV1().getY() == 129.0) {
1629                if(currentEdge.getV2().getX() == 89.0 && currentEdge.getV2().getY() == 176.0) {
1630                    return 17+n;
1631                }
1632            }
1633            if(currentEdge.getV1().getX() == 89.0 && currentEdge.getV1().getY() == 176.0) {
1634                if(currentEdge.getV2().getX() == 81.0 && currentEdge.getV2().getY() == 177.0) {
1635                    return 7+n;
1636                }
1637            }
1638            if(currentEdge.getV1().getX() == 83.0 && currentEdge.getV1().getY() == 129.0) {
1639                if(currentEdge.getV2().getX() == 114.0 && currentEdge.getV2().getY() == 125.0) {
1640                    return 13+n;
1641                }
1642            }
1643            if(currentEdge.getV1().getX() == 114.0 && currentEdge.getV1().getY() == 125.0) {
1644                if(currentEdge.getV2().getX() == 117.5 && currentEdge.getV2().getY() == 171.5) {
1645                    return 17+n;
1646                }
1647            }
1648            if(currentEdge.getV1().getX() == 117.5 && currentEdge.getV1().getY() == 171.5) {
1649                if(currentEdge.getV2().getX() == 89.0 && currentEdge.getV2().getY() == 176.0) {
1650                    return 12+n;
1651                }
1652            }
1653            if(currentEdge.getV1().getX() == 81.0 && currentEdge.getV1().getY() == 177.0) {
1654                if(currentEdge.getV2().getX() == 87.5 && currentEdge.getV2().getY() == 219.0) {
1655                    return 16+n;
1656                }
1657            }
1658            if(currentEdge.getV1().getX() == 87.5 && currentEdge.getV1().getY() == 219.0) {
1659                if(currentEdge.getV2().getX() == 96.5 && currentEdge.getV2().getY() == 232.0) {
1660                    return 9+n;
1661                }
1662            }
1663            if(currentEdge.getV1().getX() == 96.5 && currentEdge.getV1().getY() == 232.0) {
1664                if(currentEdge.getV2().getX() == 101.5 && currentEdge.getV2().getY() == 272.0) {
1665                    return 15+n;
1666                }
1667            }
1668            if(currentEdge.getV1().getX() == 96.5 && currentEdge.getV1().getY() == 232.0) {
1669                if(currentEdge.getV2().getX() == 118.5 && currentEdge.getV2().getY() == 269.0) {
1670                    return 16+n;
1671                }
1672            }
1673            if(currentEdge.getV1().getX() == 118.5 && currentEdge.getV1().getY() == 269.0) {
1674                if(currentEdge.getV2().getX() == 101.5 && currentEdge.getV2().getY() == 272.0) {
1675                    return 9+n;
1676                }
1677            }
1678            if(currentEdge.getV1().getX() == 101.5 && currentEdge.getV1().getY() == 272.0) {
1679                if(currentEdge.getV2().getX() == 104.0 && currentEdge.getV2().getY() == 288.0) {
1680                    return 28+n;
1681                }
1682            }
1683            if(currentEdge.getV1().getX() == 24.5 && currentEdge.getV1().getY() == 21.0) {
1684                if(currentEdge.getV2().getX() == 26.0 && currentEdge.getV2().getY() == 34.0) {
1685                    return 8+n;
1686                }
1687            }
1688            if(currentEdge.getV1().getX() == 26.0 && currentEdge.getV1().getY() == 34.0) {
1689                if(currentEdge.getV2().getX() == 46.5 && currentEdge.getV2().getY() == 41.0) {
1690                    return 10+n;
1691                }
1692            }
1693            if(currentEdge.getV1().getX() == 46.5 && currentEdge.getV1().getY() == 41.0) {
1694                if(currentEdge.getV2().getX() == 45.0 && currentEdge.getV2().getY() == 45.0) {
1695                    return 7+n;
1696                }
1697            }
1698            if(currentEdge.getV1().getX() == 45.0 && currentEdge.getV1().getY() == 45.0) {
1699                if(currentEdge.getV2().getX() == 44.0 && currentEdge.getV2().getY() == 51.0) {
1700                    return 7+n;
1701                }
1702            }
1703            if(currentEdge.getV1().getX() == 44.0 && currentEdge.getV1().getY() == 51.0) {
1704                if(currentEdge.getV2().getX() == 43.0 && currentEdge.getV2().getY() == 84.5) {
1705                    return 13+n;
1706                }
1707            }
1708            if(currentEdge.getV1().getX() == 43.0 && currentEdge.getV1().getY() == 84.5) {
1709                if(currentEdge.getV2().getX() == 53.5 && currentEdge.getV2().getY() == 84.0) {
1710                    return 8+n;
1711                }
1712            }
1713            if(currentEdge.getV1().getX() == 53.5 && currentEdge.getV1().getY() == 84.0) {
1714                if(currentEdge.getV2().getX() == 75.5 && currentEdge.getV2().getY() == 81.0) {
1715                    return 11+n;
1716                }
1717            }
1718            if(currentEdge.getV1().getX() == 43.0 && currentEdge.getV1().getY() == 84.5) {
1719                if(currentEdge.getV2().getX() == 54.5 && currentEdge.getV2().getY() == 133.0) {
1720                    return 17+n;
1721                }
1722            }
1723            if(currentEdge.getV1().getX() == 54.5 && currentEdge.getV1().getY() == 133.0) {
1724                if(currentEdge.getV2().getX() == 71.0 && currentEdge.getV2().getY() == 131.0) {
1725                    return 9+n;
1726                }
1727            }
1728            if(currentEdge.getV1().getX() == 71.0 && currentEdge.getV1().getY() == 131.0) {
1729                if(currentEdge.getV2().getX() == 81.0 && currentEdge.getV2().getY() == 177.0) {
1730                    return 17+n;
1731                }
1732            }
1733            if(currentEdge.getV1().getX() == 53.5 && currentEdge.getV1().getY() == 84.0) {
1734                if(currentEdge.getV2().getX() == 54.5 && currentEdge.getV2().getY() == 94.5) {
1735                    return 8+n;
1736                }
1737            }
1738            if(currentEdge.getV1().getX() == 54.5 && currentEdge.getV1().getY() == 94.5) {
1739                if(currentEdge.getV2().getX() == 69.0 && currentEdge.getV2().getY() == 115.0) {
1740                    return 11+n;
1741                }
1742            }
1743            if(currentEdge.getV1().getX() == 69.0 && currentEdge.getV1().getY() == 115.0) {
1744                if(currentEdge.getV2().getX() == 71.0 && currentEdge.getV2().getY() == 131.0) {
1745                    return 9+n;
1746                }
1747            }
1748            if(currentEdge.getV1().getX() == 53.5 && currentEdge.getV1().getY() == 84.0) {
1749                if(currentEdge.getV2().getX() == 77.5 && currentEdge.getV2().getY() == 95.0) {
1750                    return 12+n;
1751                }
1752            }
1753            if(currentEdge.getV1().getX() == 19.5 && currentEdge.getV1().getY() == 22.5) {
1754                if(currentEdge.getV2().getX() == 11.0 && currentEdge.getV2().getY() == 47.0) {
1755                    return 11+n;
1756                }
1757            }
1758            if(currentEdge.getV1().getX() == 11.0 && currentEdge.getV1().getY() == 47.0) {
1759                if(currentEdge.getV2().getX() == 9.0 && currentEdge.getV2().getY() == 51.0) {
1760                    return 7+n;
1761                }
1762            }
1763            if(currentEdge.getV1().getX() == 9.0 && currentEdge.getV1().getY() == 51.0) {
1764                if(currentEdge.getV2().getX() == 17.0 && currentEdge.getV2().getY() == 55.0) {
1765                    return 7+n;
1766                }
1767            }
1768            if(currentEdge.getV1().getX() == 17.0 && currentEdge.getV1().getY() == 55.0) {
1769                if(currentEdge.getV2().getX() == 17.0 && currentEdge.getV2().getY() == 59.5) {
1770                    return 7+n;
1771                }
1772            }
1773            if(currentEdge.getV1().getX() == 17.0 && currentEdge.getV1().getY() == 59.5) {
1774                if(currentEdge.getV2().getX() == 37.0 && currentEdge.getV2().getY() == 52.0) {
1775                    return 10+n;
1776                }
1777            }
1778            if(currentEdge.getV1().getX() == 37.0 && currentEdge.getV1().getY() == 52.0) {
1779                if(currentEdge.getV2().getX() == 44.0 && currentEdge.getV2().getY() == 51.0) {
1780                    return 7+n;
1781                }
1782            }
1783            if(currentEdge.getV1().getX() == 17.0 && currentEdge.getV1().getY() == 59.9) {
1784                if(currentEdge.getV2().getX() == 22.5 && currentEdge.getV2().getY() == 90.0) {
1785                    return 13+n;
1786                }
1787            }
1788            if(currentEdge.getV1().getX() == 22.5 && currentEdge.getV1().getY() == 90.0) {
1789                if(currentEdge.getV2().getX() == 43.0 && currentEdge.getV2().getY() == 84.5) {
1790                    return 10+n;
1791                }
1792            }
1793            if(currentEdge.getV1().getX() == 12.0 && currentEdge.getV1().getY() == 8.0) {
1794                if(currentEdge.getV2().getX() == 6.5 && currentEdge.getV2().getY() == 15.0) {
1795                    return 7+n;
1796                }
1797            }
1798            if(currentEdge.getV1().getX() == 11.0 && currentEdge.getV1().getY() == 47.0) {
1799                if(currentEdge.getV2().getX() == 15.0 && currentEdge.getV2().getY() == 47.0) {
1800                    return 7+n;
1801                }
1802            }
1803            if(currentEdge.getV1().getX() == 24.5 && currentEdge.getV1().getY() == 21.0) {
1804                if(currentEdge.getV2().getX() == 19.0 && currentEdge.getV2().getY() == 35.5) {
1805                    return 9+n;
1806                }
1807            }
1808            if(currentEdge.getV1().getX() == 19.0 && currentEdge.getV1().getY() == 35.5) {
1809                if(currentEdge.getV2().getX() == 15.0 && currentEdge.getV2().getY() == 47.0) {
1810                    return 8+n;
1811                }
1812            }
1813            if(currentEdge.getV1().getX() == 19.0 && currentEdge.getV1().getY() == 35.5) {
1814                if(currentEdge.getV2().getX() == 45.0 && currentEdge.getV2().getY() == 45.0) {
1815                    return 12+n;
1816                }
1817            }
1818            if(currentEdge.getV1().getX() == 15.0 && currentEdge.getV1().getY() == 47.0) {
1819                if(currentEdge.getV2().getX() == 36.0 && currentEdge.getV2().getY() == 55.0) {
1820                    return 11+n;
1821                }
1822            }
1823            if(currentEdge.getV1().getX() == 36.0 && currentEdge.getV1().getY() == 55.0) {
1824                if(currentEdge.getV2().getX() == 37.0 && currentEdge.getV2().getY() == 52.0) {
1825                    return 7+n;
1826                }
1827            }
1828            if(currentEdge.getV1().getX() == 70.0 && currentEdge.getV1().getY() == 38.5) {
1829                if(currentEdge.getV2().getX() == 72.0 && currentEdge.getV2().getY() == 52.0) {
1830                    return 8+n;
1831                }
1832            }
1833            if(currentEdge.getV1().getX() == 46.5 && currentEdge.getV1().getY() == 41.0) {
1834                if(currentEdge.getV2().getX() == 70.0 && currentEdge.getV2().getY() == 38.5) {
1835                    return 11+n;
1836                }
1837            }
1838            if(currentEdge.getV1().getX() == 22.5 && currentEdge.getV1().getY() == 90.0) {
1839                if(currentEdge.getV2().getX() == 34.0 && currentEdge.getV2().getY() == 183.0) {
1840                    return 17+n;
1841                }
1842            }
1843            if(currentEdge.getV1().getX() == 34.0 && currentEdge.getV1().getY() == 183.0) {
1844                if(currentEdge.getV2().getX() == 81.0 && currentEdge.getV2().getY() == 177.0) {
1845                    return 28+n;
1846                }
1847            }
1848            if(currentEdge.getV1().getX() == 9.0 && currentEdge.getV1().getY() == 51.0) {
1849                if(currentEdge.getV2().getX() == 12.5 && currentEdge.getV2().getY() == 53.5) {
1850                    return 7+n;
1851                }
1852            }
1853            if(currentEdge.getV1().getX() == 12.5 && currentEdge.getV1().getY() == 53.5) {
1854                if(currentEdge.getV2().getX() == 17.0 && currentEdge.getV2().getY() == 55.0) {
1855                    return 7+n;
1856                }
1857            }
1858            if(currentEdge.getV1().getX() == 15.0 && currentEdge.getV1().getY() == 47.0) {
1859                if(currentEdge.getV2().getX() == 12.5 && currentEdge.getV2().getY() == 53.0) {
1860                    return 7+n;
1861                }
1862            }
1863    return 0+n;
1864    }
1865
1866    
1867  private boolean checkIfEquipped(String id, String nonEquipped) {
1868    // TODO Auto-generated method stub
1869    String vID=null;
1870      if(nonEquipped!=null && id!=null){
1871        StringTokenizer st = new StringTokenizer(nonEquipped,",");
1872        if(st!=null){
1873            while(st.hasMoreTokens()){
1874                vID=st.nextToken();
1875                if(vID!=null && vID.equals(id)){
1876                    return false;
1877                }
1878            }
1879            
1880        }
1881    }
1882      return true;
1883}
1884
1885private void checkIfOwnerEquipped() {
1886    // TODO Auto-generated method stub
1887 
1888    
1889}
1890
1891/**
1892 * This method is used to set the flag to false
1893 * @param vehicles list of vehicles
1894 * @param congestedSegmentId ID of the congested segment
1895 * @param finalMap map of 
1896 * @param commID communication ID
1897 * @param curEdge current edge
1898 * @param ownerID owner ID
1899 * @param numNodes number of nodes
1900 */
1901private void setFlagToFalse(String vehicles, String congestedSegmentId,ConcurrentHashMap finalMap, String commID, Edge curEdge,String ownerID, int numNodes) {
1902      String listOfVehicles = vehicles;
1903      String segID = congestedSegmentId;
1904      boolean shortestPath=false;
1905      if(vehicles!=null && congestedSegmentId!=null){
1906        if(listOfVehicles!=null && segID!=null){
1907            StringTokenizer st = new StringTokenizer(listOfVehicles,",");
1908            if(st!=null){
1909                while(st.hasMoreTokens()){
1910                    String vID = st.nextToken();
1911                    if(vID!=null && !shortestPath){
1912                        
1913                            finalMap.put(vID, false);
1914                            
1915                                //  if(vID!=null && (vID.equals("#6") ||vID.equals("#49")))
1916                            //  {
1917                            if(congestedSegmentId.equals("s10")){       
1918                            //System.out.println(ownerID+ " setting flaggg to FALSE for : "+ vID+ " for "+congestedSegmentId+" vehicles: "+vehicles+" by: " +commID+" vertices : "+Double.toString(curEdge.getV2().getX())+","+Double.toString(curEdge.getV2().getY())+ " for number of nodes  : "+numNodes);
1919                            }
1920                            //  }
1921                            //}                 
1922                    }
1923                }
1924                u.setReadyFlag(finalMap);
1925            }
1926        }
1927    }
1928    
1929}
1930
1931private boolean checkNumVehInRange(String vehListStr, String vehInRng2) {
1932      int cntO = 0;
1933      int cntN =1;
1934      if(vehListStr!=null){
1935          if(vehListStr.indexOf(",")>-1){
1936          StringTokenizer st = new StringTokenizer(vehListStr,",");
1937          if(st!=null)
1938              cntO= st.countTokens(); 
1939          }else cntO=1;
1940      }
1941      if(vehInRng2!=null){
1942          if(vehInRng2.indexOf(",")>-1){
1943              StringTokenizer st = new StringTokenizer(vehInRng2,",");
1944          
1945          if(st!=null)
1946              cntN= st.countTokens(); }
1947      else cntN=1;
1948      }
1949//    System.out.println("CntN "+cntN+" for "+vehListStr+" and "+"cntO "+cntO+" for "+vehInRng2);
1950      if(cntN == cntO)
1951          return false;
1952      else 
1953          return true;
1954    
1955}
1956
1957/**
1958   * Executes the extension. <br>
1959   * <br>
1960   * The method is called on every simulation timestep.
1961   * @return 0 - the module should be executed on next timesteps,
1962   *        -1 - the module should not be executed on further timesteps and should be removed from the extensions' list
1963   */
1964  public int act()
1965  {
1966    Node owner = (Node)this.owner;
1967    Position3D srcPosition = owner.getPosition();
1968    Graph graph = spatialModel.getGraph();
1969    boolean speedChanged = false;
1970        
1971    if(owner.getID().equals("#0") && u.getTimeInSteps()<98000)
1972    {
1973        owner.setPosition(srcPosition);
1974        return 0;
1975    }
1976    if(owner.getID().equals("#1") && u.getTimeInSteps()<98002)
1977    {
1978        owner.setPosition(srcPosition);
1979        return 0;
1980    }
1981    if(owner.getID().equals("#2") && u.getTimeInSteps()<98004)
1982    {
1983        owner.setPosition(srcPosition);
1984        return 0;
1985    }
1986    if(owner.getID().equals("#3") && u.getTimeInSteps()<98006)
1987    {
1988        owner.setPosition(srcPosition);
1989        return 0;
1990    }
1991    if(owner.getID().equals("#4") && u.getTimeInSteps()<98008)
1992    {
1993        owner.setPosition(srcPosition);
1994        return 0;
1995    }
1996    if(owner.getID().equals("#5") && u.getTimeInSteps()<98010)
1997    {
1998        owner.setPosition(srcPosition);
1999        return 0;
2000    }
2001    if(owner.getID().equals("#6") && u.getTimeInSteps()<98011)
2002    {
2003        owner.setPosition(srcPosition);
2004        return 0;
2005    }
2006    if(owner.getID().equals("#7") && u.getTimeInSteps()<98012)
2007    {
2008        owner.setPosition(srcPosition);
2009        return 0;
2010    }
2011    if(owner.getID().equals("#8") && u.getTimeInSteps()<98013)
2012    {
2013        owner.setPosition(srcPosition);
2014        return 0;
2015    }
2016    if(owner.getID().equals("#9") && u.getTimeInSteps()<98014)
2017    {
2018        owner.setPosition(srcPosition);
2019        return 0;
2020    }
2021    if(owner.getID().equals("#10") && u.getTimeInSteps()<98001)
2022    {
2023        owner.setPosition(srcPosition);
2024        return 0;
2025    }
2026    
2027// }else if(owner.getPosition().getX()==200.0 && owner.getPosition().getY()==300.0){
2028    
2029    if(owner.getID().equals("#11") && u.getTimeInSteps()<98002)
2030    {
2031        owner.setPosition(srcPosition);
2032        return 0;
2033    }
2034    if(owner.getID().equals("#12") && u.getTimeInSteps()<98000)
2035    {
2036        owner.setPosition(srcPosition);
2037        return 0;
2038    }
2039    if(owner.getID().equals("#13") && u.getTimeInSteps()<98003)
2040    {
2041        owner.setPosition(srcPosition);
2042        return 0;
2043    }
2044    if(owner.getID().equals("#14") && u.getTimeInSteps()<98001)
2045    {
2046        owner.setPosition(srcPosition);
2047        return 0;
2048    }if(owner.getID().equals("#15") && u.getTimeInSteps()<98001)
2049    {
2050        owner.setPosition(srcPosition);
2051        return 0;
2052    }if(owner.getID().equals("#16") && u.getTimeInSteps()<98002)
2053    {
2054        owner.setPosition(srcPosition);
2055        return 0;
2056    }if(owner.getID().equals("#17") && u.getTimeInSteps()<98001)
2057    {
2058        owner.setPosition(srcPosition);
2059        return 0;
2060    }if(owner.getID().equals("#18") && u.getTimeInSteps()<98001)
2061    {
2062        owner.setPosition(srcPosition);
2063        return 0;
2064    }
2065    if(owner.getID().equals("#19") && u.getTimeInSteps()<98001)
2066    {
2067        owner.setPosition(srcPosition);
2068        return 0;
2069    }
2070    if(owner.getID().equals("#20") && u.getTimeInSteps()<98002)
2071    {
2072        owner.setPosition(srcPosition);
2073        return 0;
2074    }
2075    if(owner.getID().equals("#21") && u.getTimeInSteps()<98001)
2076    {
2077        owner.setPosition(srcPosition);
2078        return 0;
2079    }
2080    if(owner.getID().equals("#22") && u.getTimeInSteps()<98002)
2081    {
2082        owner.setPosition(srcPosition);
2083        return 0;
2084    }
2085    if(owner.getID().equals("#23") && u.getTimeInSteps()<98002)
2086    {
2087        owner.setPosition(srcPosition);
2088        return 0;
2089    }
2090    if(owner.getID().equals("#24") && u.getTimeInSteps()<98002)
2091    {
2092        owner.setPosition(srcPosition);
2093        return 0;
2094    }if(owner.getID().equals("#25") && u.getTimeInSteps()<98003)
2095    {
2096        owner.setPosition(srcPosition);
2097        return 0;
2098    }if(owner.getID().equals("#26") && u.getTimeInSteps()<98002)
2099    {
2100        owner.setPosition(srcPosition);
2101        return 0;
2102    }if(owner.getID().equals("#27") && u.getTimeInSteps()<98002)
2103    {
2104        owner.setPosition(srcPosition);
2105        return 0;
2106    }if(owner.getID().equals("#28") && u.getTimeInSteps()<98003)
2107    {
2108        owner.setPosition(srcPosition);
2109        return 0;
2110    }
2111    if(owner.getID().equals("#29") && u.getTimeInSteps()<98002)
2112    {
2113        owner.setPosition(srcPosition);
2114        return 0;
2115    }
2116    if(owner.getID().equals("#30") && u.getTimeInSteps()<98000)
2117    {
2118        owner.setPosition(srcPosition);
2119        return 0;
2120    }
2121    if(owner.getID().equals("#31") && u.getTimeInSteps()<98001)
2122    {
2123        owner.setPosition(srcPosition);
2124        return 0;
2125    }
2126    if(owner.getID().equals("#32") && u.getTimeInSteps()<98001)
2127    {
2128        owner.setPosition(srcPosition);
2129        return 0;
2130    }
2131    if(owner.getID().equals("#33") && u.getTimeInSteps()<98001)
2132    {
2133        owner.setPosition(srcPosition);
2134        return 0;
2135    }
2136    if(owner.getID().equals("#34") && u.getTimeInSteps()<98002)
2137    {
2138        owner.setPosition(srcPosition);
2139        return 0;
2140    }
2141    if(owner.getID().equals("#35") && u.getTimeInSteps()<98001)
2142    {
2143        owner.setPosition(srcPosition);
2144        return 0;
2145    }
2146    if(owner.getID().equals("#36") && u.getTimeInSteps()<98001)
2147    {
2148        owner.setPosition(srcPosition);
2149        return 0;
2150    }
2151    if(owner.getID().equals("#37") && u.getTimeInSteps()<98001)
2152    {
2153        owner.setPosition(srcPosition);
2154        return 0;
2155    }
2156    if(owner.getID().equals("#38") && u.getTimeInSteps()<98002)
2157    {
2158        owner.setPosition(srcPosition);
2159        return 0;
2160    }
2161    if(owner.getID().equals("#39") && u.getTimeInSteps()<98001)
2162    {
2163        owner.setPosition(srcPosition);
2164        return 0;
2165    }
2166
2167    if(owner.getID().equals("#40") && u.getTimeInSteps()<98000)
2168    {
2169        owner.setPosition(srcPosition);
2170        return 0;
2171    }
2172    if(owner.getID().equals("#41") && u.getTimeInSteps()<98001)
2173    {
2174        owner.setPosition(srcPosition);
2175        return 0;
2176    }
2177    if(owner.getID().equals("#42") && u.getTimeInSteps()<98001)
2178    {
2179        owner.setPosition(srcPosition);
2180        return 0;
2181    }
2182    if(owner.getID().equals("#43") && u.getTimeInSteps()<98002)
2183    {
2184        owner.setPosition(srcPosition);
2185        return 0;
2186    }
2187    if(owner.getID().equals("#44") && u.getTimeInSteps()<98000)
2188    {
2189        owner.setPosition(srcPosition);
2190        return 0;
2191    }
2192    if(owner.getID().equals("#45") && u.getTimeInSteps()<98000)
2193    {
2194        owner.setPosition(srcPosition);
2195        return 0;
2196    }
2197    if(owner.getID().equals("#46") && u.getTimeInSteps()<98001)
2198    {
2199        owner.setPosition(srcPosition);
2200        return 0;
2201    }
2202    if(owner.getID().equals("#47") && u.getTimeInSteps()<98001)
2203    {
2204        owner.setPosition(srcPosition);
2205        return 0;
2206    }
2207    if(owner.getID().equals("#48") && u.getTimeInSteps()<98000)
2208    {
2209        owner.setPosition(srcPosition);
2210        return 0;
2211    }
2212    if(owner.getID().equals("#49") && u.getTimeInSteps()<98001)
2213    {
2214        owner.setPosition(srcPosition);
2215        return 0;
2216    }
2217    if(owner.getID().equals("#50") && u.getTimeInSteps()<98000)
2218    {
2219        owner.setPosition(srcPosition);
2220        return 0;
2221    }
2222    if(owner.getID().equals("#51") && u.getTimeInSteps()<98001)
2223    {
2224        owner.setPosition(srcPosition);
2225        return 0;
2226    }
2227    if(owner.getID().equals("#52") && u.getTimeInSteps()<98000)
2228    {
2229        owner.setPosition(srcPosition);
2230        return 0;
2231    }
2232    if(owner.getID().equals("#53") && u.getTimeInSteps()<98000)
2233    {
2234        owner.setPosition(srcPosition);
2235        return 0;
2236    }
2237    if(owner.getID().equals("#54") && u.getTimeInSteps()<98001)
2238    {
2239        owner.setPosition(srcPosition);
2240        return 0;
2241    }
2242    if(owner.getID().equals("#55") && u.getTimeInSteps()<98002)
2243    {
2244        owner.setPosition(srcPosition);
2245        return 0;
2246    }
2247    if(owner.getID().equals("#56") && u.getTimeInSteps()<98002)
2248    {
2249        owner.setPosition(srcPosition);
2250        return 0;
2251    }
2252    if(owner.getID().equals("#57") && u.getTimeInSteps()<98003)
2253    {
2254        owner.setPosition(srcPosition);
2255        return 0;
2256    }
2257    if(owner.getID().equals("#58") && u.getTimeInSteps()<98001)
2258    {
2259        owner.setPosition(srcPosition);
2260        return 0;
2261    }
2262    if(owner.getID().equals("#59") && u.getTimeInSteps()<98002)
2263    {
2264        owner.setPosition(srcPosition);
2265        return 0;
2266    }
2267    if(owner.getID().equals("#60") && u.getTimeInSteps()<98003)
2268    {
2269        owner.setPosition(srcPosition);
2270        return 0;
2271    }
2272    if(owner.getID().equals("#61") && u.getTimeInSteps()<98001)
2273    {
2274        owner.setPosition(srcPosition);
2275        return 0;
2276    }
2277    if(owner.getID().equals("#62") && u.getTimeInSteps()<98001)
2278    {
2279        owner.setPosition(srcPosition);
2280        return 0;
2281    }
2282    if(owner.getID().equals("#63") && u.getTimeInSteps()<98003)
2283    {
2284        owner.setPosition(srcPosition);
2285        return 0;
2286    }
2287    if(owner.getID().equals("#64") && u.getTimeInSteps()<98001)
2288    {
2289        owner.setPosition(srcPosition);
2290        return 0;
2291    }
2292    if(owner.getID().equals("#65") && u.getTimeInSteps()<98003)
2293    {
2294        owner.setPosition(srcPosition);
2295        return 0;
2296    }
2297    if(owner.getID().equals("#66") && u.getTimeInSteps()<98002)
2298    {
2299        owner.setPosition(srcPosition);
2300        return 0;
2301    }
2302    if(owner.getID().equals("#67") && u.getTimeInSteps()<98002)
2303    {
2304        owner.setPosition(srcPosition);
2305        return 0;
2306    }
2307    if(owner.getID().equals("#68") && u.getTimeInSteps()<98003)
2308    {
2309        owner.setPosition(srcPosition);
2310        return 0;
2311    }       
2312        if(owner.getID().equals("#69") && u.getTimeInSteps()<98003)
2313        {
2314            owner.setPosition(srcPosition);
2315            return 0;
2316        }
2317    
2318        if(owner.getID().equals("#70") && u.getTimeInSteps()<98000)
2319        {
2320            owner.setPosition(srcPosition);
2321            return 0;
2322        }
2323        if(owner.getID().equals("#71") && u.getTimeInSteps()<98000)
2324        {
2325            owner.setPosition(srcPosition);
2326            return 0;
2327        }
2328        if(owner.getID().equals("#72") && u.getTimeInSteps()<98001)
2329        {
2330            owner.setPosition(srcPosition);
2331            return 0;
2332        }
2333        if(owner.getID().equals("#73") && u.getTimeInSteps()<98001)
2334        {
2335            owner.setPosition(srcPosition);
2336            return 0;
2337        }
2338        if(owner.getID().equals("#74") && u.getTimeInSteps()<98001)
2339        {
2340            owner.setPosition(srcPosition);
2341            return 0;
2342        }
2343        if(owner.getID().equals("#75") && u.getTimeInSteps()<98001)
2344        {
2345            owner.setPosition(srcPosition);
2346            return 0;
2347        }
2348        if(owner.getID().equals("#76") && u.getTimeInSteps()<98000)
2349        {
2350            owner.setPosition(srcPosition);
2351            return 0;
2352        }
2353        if(owner.getID().equals("#77") && u.getTimeInSteps()<98001)
2354        {
2355            owner.setPosition(srcPosition);
2356            return 0;
2357        }
2358        if(owner.getID().equals("#78") && u.getTimeInSteps()<98002)
2359        {
2360            owner.setPosition(srcPosition);
2361            return 0;
2362        }       
2363            if(owner.getID().equals("#79") && u.getTimeInSteps()<98000)
2364            {
2365                owner.setPosition(srcPosition);
2366                return 0;
2367            }
2368            if(owner.getID().equals("#80") && u.getTimeInSteps()<98000)
2369            {
2370                owner.setPosition(srcPosition);
2371                return 0;
2372            }
2373            if(owner.getID().equals("#81") && u.getTimeInSteps()<98001)
2374            {
2375                owner.setPosition(srcPosition);
2376                return 0;
2377            }
2378            if(owner.getID().equals("#82") && u.getTimeInSteps()<98002)
2379            {
2380                owner.setPosition(srcPosition);
2381                return 0;
2382            }
2383            if(owner.getID().equals("#83") && u.getTimeInSteps()<98002)
2384            {
2385                owner.setPosition(srcPosition);
2386                return 0;
2387            }
2388            if(owner.getID().equals("#84") && u.getTimeInSteps()<98000)
2389            {
2390                owner.setPosition(srcPosition);
2391                return 0;
2392            }
2393            if(owner.getID().equals("#85") && u.getTimeInSteps()<98000)
2394            {
2395                owner.setPosition(srcPosition);
2396                return 0;
2397            }
2398            if(owner.getID().equals("#86") && u.getTimeInSteps()<98000)
2399            {
2400                owner.setPosition(srcPosition);
2401                return 0;
2402            }
2403            if(owner.getID().equals("#87") && u.getTimeInSteps()<98004)
2404            {
2405                owner.setPosition(srcPosition);
2406                return 0;
2407            }
2408            if(owner.getID().equals("#88") && u.getTimeInSteps()<98004)
2409            {
2410                owner.setPosition(srcPosition);
2411                return 0;
2412            }       
2413                if(owner.getID().equals("#89") && u.getTimeInSteps()<98003)
2414                {
2415                    owner.setPosition(srcPosition);
2416                    return 0;
2417                }
2418  
2419            
2420    //if node has arrived to destination and stayed enough, a new destination
2421    //choosen
2422    if ((destination==null)||(owner.getPosition().equals(destination)))
2423    { 
2424          //my code addition
2425        if(owner.getPosition().getX() == spatialModel.max_x_clip)   
2426            return 0;
2427        //my code  
2428    if(movement != null)
2429      {
2430        movement = null;
2431        chooseNewStayDuration();
2432        speedChanged = true;        
2433      }
2434      else
2435      if(stay <= 0)
2436      {
2437        chooseNewMovement();
2438        speedChanged = true;
2439      }
2440      else
2441        stay -= u.getStepDuration();
2442    }
2443
2444    if (movement!=null)
2445    {
2446      if ((!speedChanged) && (u.getTimeInSteps()%recalculation_step==0))
2447      {
2448      
2449         // System.out.println("invoking Act  from FDM"+owner.getID());
2450          recalculateTrafficSpeed();
2451          speedChanged = true;
2452      }
2453
2454      //move towards destination
2455      Vector3D m = movement.mult(speed*u.getStepDuration());
2456      if(owner.getPosition().getDistance(destination) >= m.getLength())
2457      {
2458        
2459          if (speedChanged)
2460        {
2461          // check if the next speed change event is about to occur before arriving to destination
2462          double dist = (recalculation_step-u.getTimeInSteps()%recalculation_step)*u.getStepDuration()*(double)speed;
2463          if (owner.getPosition().getDistance(destination) < dist)
2464          {
2465            // move to destination
2466//            u.sendNotification(new MovementChangedNotification(this, u, destination, speed*1000f));
2467          }
2468          else
2469          {
2470            // move until the next speed change event occur
2471  //          u.sendNotification(new MovementChangedNotification(this, u, owner.getPosition().add(movement.mult(dist)), speed*1000f));
2472          }
2473        }
2474        
2475        owner.setPosition(owner.getPosition().add(m));
2476      }
2477      else
2478      {
2479        if(congestionAlert){
2480            
2481            
2482            Vertex closestVertex = CommunicationsManager.getClosestVertex(destination.getX(), destination.getY());
2483            congEndPoint = new Position3D(closestVertex.getX(),closestVertex.getY(),0.0);
2484
2485            notifPattern.setSegmentID(getSegmentFromCoordinates(congStartPoint,congEndPoint));
2486
2487            congestionAlert = false;
2488        }
2489        /*if (speedChanged)
2490          u.sendNotification(new MovementChangedNotification(this, u, destination, speed*1000f));
2491*/     /*   if(destination!=null){
2492        if(destination.getX() == uniNodeDestPos.getX() && destination.getY() == uniNodeDestPos.getY()) {
2493        //  System.out.println("*************************"+owner.getID());
2494            boolean initFlag = true;
2495            String segID = "";
2496            String segList = "";
2497            Position3D p = null;
2498            Position3D initPos = DataAccess.getInitialPosition(owner.getID());
2499            System.out.println("********************getInitialPosition*****"+owner.getID());
2500            ArrayList verList = CommunicationsManager.getActualTraversalVertices(owner.getID());
2501            System.out.println("*****************getActualTraversalVertices********"+owner.getID());
2502            verList.add(new Position3D(uniNodeDestPos.getX(),uniNodeDestPos.getY(),0.0));
2503            Iterator iter = verList.iterator();
2504            System.out.println("*****************verList.iterator********"+owner.getID());
2505            while(iter!=null && iter.hasNext())
2506            {
2507                Position3D pos = (Position3D) iter.next();
2508                if(owner.getID().equals("#5")){
2509                System.out.println("FFFFor vehicle : "+owner.getID()+"vertex is:"+pos.getX()+","+pos.getY());
2510                }
2511                if(initFlag && !pos.equals(initPos)){
2512                    segID = DataAccess.getSegmentID(initPos,pos);
2513                    System.out.println("segID");
2514                    if(segID!=null)
2515                        segList = segList.concat(segID);
2516                    initFlag = false;
2517                }
2518                else if(!pos.equals(p) && !pos.equals(initPos)){
2519                    segID  = DataAccess.getSegmentID(p,pos);
2520                    System.out.println("segIDD");
2521                    if(segID!=null){
2522                    if(!segList.equals(""))
2523                        segList = segList.concat(",");
2524                    segList = segList.concat(segID);
2525                    }
2526                }
2527                p = pos;
2528            }
2529            if(owner.getID().equals("#5")){
2530                System.out.println("SEGMENT - "+segList);
2531            }
2532            DataAccess.addActualVehPath(owner.getID(), segList);
2533            u.sendNotification(new FinalDestinationReachedNotification(this, u, owner.getID()));
2534        }
2535      }*/
2536        owner.setPosition(destination);
2537      }
2538    }
2539    
2540    return 0;
2541  }
2542
2543  private String getSegmentFromCoordinates(Position3D positionSrc,Position3D positionDest) {
2544    return DataAccess.getSegmentID(positionSrc,positionDest);
2545}
2546
2547/**
2548   * Initializes the object from XML tag. <br>
2549   * <br>
2550   * @param element source tag
2551   * @throws Exception Exception if parameters are invalid
2552   */
2553  public void load(org.w3c.dom.Element element) throws Exception
2554  {
2555    u.sendNotification(new LoaderNotification(this, u,
2556      "Loading FluidTrafficMotion extension"));
2557
2558    super.load(element);
2559
2560    org.w3c.dom.Node n;
2561
2562    n = element.getElementsByTagName("minspeed").item(0);
2563    if(n==null)
2564      throw new Exception("<minspeed> is missing!");
2565    minSpeed = Float.parseFloat(n.getFirstChild().getNodeValue())/1000;
2566
2567    n = element.getElementsByTagName("maxspeed").item(0);
2568    if(n==null)
2569      throw new Exception("<maxspeed> is missing!");
2570    maxSpeed = Float.parseFloat(n.getFirstChild().getNodeValue())/1000;
2571
2572    n = element.getElementsByTagName("k_jam").item(0);
2573    if(n==null)
2574      throw new Exception("<k_jam> is missing!");
2575    jam_density = Float.parseFloat(n.getFirstChild().getNodeValue());
2576
2577    n = element.getElementsByTagName("step").item(0);
2578    if(n==null)
2579      throw new Exception("<step> is missing!");
2580    int i = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
2581    if ((i<u.getStepDuration())||(i%u.getStepDuration()!=0))
2582      throw new Exception("Invalid <step> value: "+(float)i/1000.0f);      
2583    recalculation_step = i/u.getStepDuration();
2584
2585    n = element.getElementsByTagName("stay").item(0);
2586    if (n!=null)
2587    {
2588      String randTag = ((org.w3c.dom.Element)n).getAttribute("random");
2589      if ((randTag.length()>0) && Boolean.valueOf(randTag).booleanValue())
2590        stayRandom = true;
2591      else
2592        stay=(int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
2593    }
2594
2595    // check
2596    if (minSpeed<=0)
2597      throw new Exception("Invalid <minspeed> value: "+(float)minSpeed*1000);
2598    if (maxSpeed<minSpeed)
2599      throw new Exception("Invalid <maxspeed> value: "+(float)maxSpeed*1000);
2600    if (jam_density<=0)
2601      throw new Exception("Invalid <k_jam> value: "+jam_density);
2602    if (stay<0)
2603      throw new Exception("Invalid <stay> value: "+(float)stay/1000);
2604
2605    u.sendNotification(new LoaderNotification(this, u,
2606      "Finished loading FluidTrafficMotion extension"));
2607  }
2608}