View Javadoc

1   /*
2    * @(#)SingleTrackBlockTrafficLight.java May 31, 2004
3    * 
4    * Copyright (c) 2003, 2004 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  
11  package nl.tudelft.simulation.traffic.controlpoint.blocks;
12  
13  import java.rmi.RemoteException;
14  import java.util.List;
15  import javax.media.j3d.Bounds;
16  import javax.vecmath.Point3d;
17  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
18  import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
19  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
20  import nl.tudelft.simulation.event.Event;
21  import nl.tudelft.simulation.language.d3.BoundingBox;
22  import nl.tudelft.simulation.language.d3.DirectedPoint;
23  import nl.tudelft.simulation.logger.Logger;
24  import nl.tudelft.simulation.traffic.animation.SwitchBlockTrafficLightAnimation;
25  import nl.tudelft.simulation.traffic.controlpoint.real.AbstractVisibleControlPoint;
26  import nl.tudelft.simulation.traffic.controlpoint.real.Changeable;
27  import nl.tudelft.simulation.traffic.controlpoint.real.StopSignInterface;
28  import nl.tudelft.simulation.traffic.track.TrackInterface;
29  import nl.tudelft.simulation.traffic.track.util.TrackProgression;
30  import nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface;
31  
32  /***
33   * 
34   * This class implements the BlockTrafficLights that guards the switch
35   * 
36   * <br>
37   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
38   * University of Technology </a>, the Netherlands. <br>
39   * See for project information <a href="http://www.simulation.tudelft.nl">
40   * www.simulation.tudelft.nl </a> <br>
41   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
42   * License (GPL) </a>, no warranty <br>
43   * 
44   * @version May 31, 2004 <br>
45   * @author Elisangela Kanacilo
46   */
47  public class SwitchBlockTrafficLight extends AbstractVisibleControlPoint
48          implements
49              LocatableInterface,
50              StopSignInterface
51  {
52      /*** GREEN state */
53      public static final String STATE_GREEN = "GREEN";
54  
55      /*** RED state */
56      public static final String STATE_RED = "RED";
57  
58      /*** BLACK state */
59      public static final String STATE_BLACK = "BLACK";
60  
61      /*** REQUEST sensor */
62      public static final int SENSOR_REQUEST = 0;
63  
64      /*** RED sensor */
65      public static final int SENSOR_RED = 1;
66  
67      /*** RELEASE sensor */
68      public static final int SENSOR_RELEASE = 2;
69  
70      /*** sensor names */
71      public static final String[] sensorNames = new String[]{"REQUEST", "RED",
72              "RELEASE"};
73  
74      /***
75       * the distance to the sensor BEFORE the traffic light to request access to
76       * the block
77       */
78      private double distanceRequest;
79  
80      /*** the distance to the sensor AFTER the traffic light for RED */
81      private double distanceRed;
82  
83      /*** the distance to the sensor AFTER the traffic light to release the block */
84      private double distanceRelease;
85  
86      /*** the STATE of the current block signal */
87      private String currentState = STATE_RED;
88  
89      /*** the name */
90      private String name;
91  
92      /*** dx for animation (bounds) */
93      private double dx;
94  
95      /*** dy for animation (bounds) */
96      private double dy;
97  
98      /*** the block to which the trafic light belongs */
99      private VirtualBlock virtualBlock;
100 
101     /*** the vehicle requesting access */
102     private VehiclePhysicalInterface requestingVehicle;
103 
104     /***
105      * @param name
106      * @param track
107      * @param progression
108      * @param virtualBlock
109      * @param visibleDistance
110      * @param distanceRequest
111      * @param distanceRed
112      * @param simulator
113      * @param dx
114      * @param dy
115      */
116     public SwitchBlockTrafficLight(final String name,
117             final TrackInterface track, final double progression,
118             final VirtualBlock virtualBlock, final double visibleDistance,
119             final double distanceRequest, final double distanceRed,
120             final DEVSSimulatorInterface simulator, final double dx,
121             final double dy)
122     {
123         super(track, progression, visibleDistance, simulator);
124         this.name = name;
125         System.out.println(this + ", placed on " + track + " progression "
126                 + progression + " (length=" + track.getLength() + ")");
127         this.virtualBlock = virtualBlock;
128         this.distanceRed = distanceRed;
129         this.distanceRequest = distanceRequest;
130         double vcpProg = distanceRequest - progression;
131         this.buildRequestControlPoints(track, vcpProg, simulator);
132         addSensors(super.track, progression + this.distanceRed, SENSOR_RED,
133                 simulator);
134         this.dx = dx;
135         this.dy = dy;
136         if (simulator instanceof AnimatorInterface)
137         {
138             new SwitchBlockTrafficLightAnimation(this, simulator, dx, dy);
139         }
140     }
141 
142     /***
143      * @param track
144      * @param progression
145      * @param sensor
146      * @param simulator
147      */
148     private void addSensors(final TrackInterface track, final double progression,
149             final int sensor, final DEVSSimulatorInterface simulator)
150     {
151         List tpList = track.calculateTrackProgressionListAll(progression);
152         for (int i = 0; i < tpList.size(); i++)
153         {
154             TrackProgression tp = (TrackProgression) tpList.get(i);
155             new SwitchBlockControlPoint(tp.getTrack(), tp.getProgression(),
156                     this, sensor, simulator);
157             System.out.println(this + ", sensor "
158                     + SwitchBlockTrafficLight.sensorNames[sensor]
159                     + " added on track " + tp.getTrack() + ", progression "
160                     + tp.getProgression());
161         }
162     }
163 
164     /***
165      * Add sensors according to the line. It is used only for adding Release
166      * Sensors for the block
167      * 
168      * @param track
169      * @param progression
170      * @param sensor
171      * @param line
172      * @param simulator
173      */
174     public void addSensors(final TrackInterface track, final double progression,
175             final int sensor, final String line, final DEVSSimulatorInterface simulator)
176     {
177         List tpList = track.calculateTrackProgressionListLine(progression, line);
178         for (int i = 0; i < tpList.size(); i++)
179         {
180             TrackProgression tp = (TrackProgression) tpList.get(i);
181             new SwitchBlockControlPoint(tp.getTrack(), tp.getProgression(),
182                     this, sensor, simulator);
183             System.out.println(this + ", sensor "
184                     + SwitchBlockTrafficLight.sensorNames[sensor]
185                     + " added on track " + tp.getTrack() + ", progression "
186                     + tp.getProgression());
187         }
188     }
189 
190     /***
191      * @param track
192      * @param vcpProg
193      * @param simulator
194      */
195     private void buildRequestControlPoints(TrackInterface track,
196             double vcpProg, final DEVSSimulatorInterface simulator)
197     {
198         List tpList = track.calculateTrackProgressionListAll(-vcpProg);
199         for (int i = 0; i < tpList.size(); i++)
200         {
201             TrackProgression tp = (TrackProgression) tpList.get(i);
202             new SwitchBlockControlPoint(tp.getTrack(), tp.getProgression(),
203                     this, SENSOR_REQUEST, simulator);
204             System.out.println(this + ", sensor "
205                     + SwitchBlockTrafficLight.sensorNames[SENSOR_REQUEST]
206                     + " added on track " + tp.getTrack() + ", progression "
207                     + tp.getProgression());
208         }
209     }
210 
211     /***
212      * @param sensor
213      * @param vehicle
214      */
215     public void triggerSensor(final int sensor,
216             final VehiclePhysicalInterface vehicle)
217     {
218         Logger.fine(this, "triggerSensor", this + ", Vehicle "
219                 + vehicle.toString() + " triggered sensor "
220                 + SwitchBlockTrafficLight.sensorNames[sensor]
221                 + ", old state was " + this.currentState);
222         System.out.println(this + ", Vehicle " + vehicle.toString()
223                 + " triggered sensor "
224                 + SwitchBlockTrafficLight.sensorNames[sensor]
225                 + ", old state was " + this.currentState);
226         if (this.currentState == STATE_BLACK)
227         {
228             this.changeState(STATE_BLACK);
229         } else if (this.currentState == STATE_RED)
230         {
231             switch (sensor)
232             {
233                 case SENSOR_REQUEST :
234                     this.requestingVehicle = vehicle;
235                     this.virtualBlock.requestAccess(this);
236                     break;
237                 case SENSOR_RED :
238                     this.changeState(STATE_BLACK);
239                     Logger.severe(this, "triggerSensor", "RED - RED");
240                     break;
241                 case SENSOR_RELEASE :
242                     this.virtualBlock.releaseBlock();
243                     break;
244                 default :
245                     Logger.severe(this, "triggerSensor",
246                             "sensor not RED, REQUEST or RELEASE");
247                     break;
248             }
249         } else if (this.currentState == STATE_GREEN)
250         {
251             switch (sensor)
252             {
253                 case SENSOR_REQUEST :
254                     this.changeState(STATE_BLACK);
255                     Logger.severe(this, "triggerSensor", "GREEN - REQUEST");
256                     break;
257                 case SENSOR_RED :
258                     this.changeState(STATE_RED);
259                     break;
260                 case SENSOR_RELEASE :
261                     this.changeState(STATE_BLACK);
262                     Logger.severe(this, "triggerSensor", "GREEN - RELEASE");
263                     break;
264                 default :
265                     Logger.severe(this, "triggerSensor",
266                             "sensor not RED, REQUEST or RELEASE");
267                     break;
268             }
269         } else
270         {
271             Logger.severe(this, "triggerSensor",
272                     "SwitchBlockTrafficLight not in state BLACK, RED or GREEN");
273         }
274         Logger.fine(this, "triggerSensor", this + ", Vehicle "
275                 + vehicle.toString() + " new state is " + this.currentState);
276         System.out.println(this + ", Vehicle " + vehicle.toString()
277                 + " new state is " + this.currentState);
278     }
279 
280     /***
281      * @param newState
282      */
283     protected void changeState(final String newState)
284     {
285         this.currentState = newState;
286         Event event = new Event(Changeable.CHANGE_STATUS_EVENT, this, null);
287         this.fireEvent(event);
288     }
289 
290     /***
291      * @see nl.tudelft.simulation.traffic.controlpoint.ControlPointInterface#pass(nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface)
292      */
293     public void pass(final VehiclePhysicalInterface vehicle)
294     {
295         // cancel the listener of the vehicle
296         this.removeListener(vehicle, Changeable.CHANGE_STATUS_EVENT);
297     }
298 
299     /***
300      * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getLocation()
301      */
302     public DirectedPoint getLocation() throws RemoteException
303     {
304         DirectedPoint p = super.getTrack().getLocationOfProgression(
305                 super.getProgression());
306         return new DirectedPoint(p.x, p.y, p.z);
307     }
308 
309     /***
310      * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
311      */
312     public Bounds getBounds() throws RemoteException
313     {
314         return new BoundingBox(new Point3d(this.dx - 2.0, this.dy, 0),
315                 new Point3d(this.dx + 2.0, this.dy + 12.0, 0));
316     }
317 
318     /***
319      * @return Returns the currentState.
320      */
321     public String getCurrentState()
322     {
323         return this.currentState;
324     }
325 
326     /***
327      * @param state
328      */
329     public void setCurrentState(String state)
330     {
331         this.currentState = state;
332     }
333 
334     /***
335      * @see nl.tudelft.simulation.traffic.controlpoint.real.Changeable#getStatus()
336      */
337     public String getStatus()
338     {
339         if ((this.currentState == STATE_RED)
340                 || (this.currentState == STATE_BLACK))
341             return StopSignInterface.STOP;
342         else
343             return StopSignInterface.CONTINUE;
344     }
345 
346     /***
347      * @see java.lang.Object#toString()
348      */
349     public String toString()
350     {
351         return "SwitchBlockTrafficLight " + this.name;
352     }
353 
354     /***
355      * @return Returns the virtualBlock.
356      */
357     public VirtualBlock getVirtualBlock()
358     {
359         return this.virtualBlock;
360     }
361 
362     /***
363      * @return Returns the distanceRed.
364      */
365     public double getDistanceRed()
366     {
367         return this.distanceRed;
368     }
369 
370     /***
371      * @return Returns the distanceRelease.
372      */
373     public double getDistanceRelease()
374     {
375         return this.distanceRelease;
376     }
377 
378     /***
379      * sets the distanceRelease. This parameter need to be changed when the
380      * switch changes its direction
381      * 
382      * @param distanceRelease distance after the SwitchBlockTrafficLight that
383      * the block will be released
384      */
385     public void setDistanceRelease(double distanceRelease)
386     {
387         this.distanceRelease = distanceRelease;
388     }
389 
390     /***
391      * @return Returns the distanceRequest.
392      */
393     public double getDistanceRequest()
394     {
395         return this.distanceRequest;
396     }
397 
398     /***
399      * @return Returns the name.
400      */
401     public String getName()
402     {
403         return this.name;
404     }
405 
406     /***
407      * @return Returns the requestingVehicle.
408      */
409     public VehiclePhysicalInterface getRequestingVehicle()
410     {
411         return this.requestingVehicle;
412     }
413 
414     /***
415      * @param requestingVehicle The requestingVehicle to set.
416      */
417     public void setRequestingVehicle(VehiclePhysicalInterface requestingVehicle)
418     {
419         this.requestingVehicle = requestingVehicle;
420     }
421 }