View Javadoc

1   /*
2    * @(#)VirtualSwitchBlock.java Jun 20, 2004
3    * 
4    * Copyright (c) 2004 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * 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.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
17  import nl.tudelft.simulation.jstats.distributions.DistConstant;
18  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
19  import nl.tudelft.simulation.jstats.streams.Java2Random;
20  import nl.tudelft.simulation.traffic.track.TrackInterface;
21  import nl.tudelft.simulation.traffic.track.TrackLinkInterface;
22  import nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface;
23  
24  /***
25   * The VirtualSwitchBlock defines the block to be guarded including the turning
26   * of a switch, depending on the 'line' attribute of the vehicleControl.
27   * <p>
28   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
29   * University of Technology </a>, the Netherlands. <br>
30   * See for project information <a href="http://www.simulation.tudelft.nl">
31   * www.simulation.tudelft.nl </a> <br>
32   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
33   * License (GPL) </a>, no warranty <br>
34   * 
35   * @version Jun 20, 2004 <br>
36   * @author <a
37   * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
38   * Verbraeck </a>
39   */
40  public class VirtualSwitchBlock extends VirtualBlock
41  {
42      /*** the directions contained in the virtual block * */
43      private Map directions = new HashMap();
44  
45      /*** the simulator */
46      private DEVSSimulatorInterface simulator;
47  
48      /*** the time to trn the switch */
49      private DistContinuous switchTime;
50  
51      /***
52       * constucts a new VirtualSwitchBlock
53       * 
54       * @param name the name of the VirtualSwitchBlock
55       * @param simulator
56       * @param switchTime
57       */
58      public VirtualSwitchBlock(final String name,
59              final DEVSSimulatorInterface simulator,
60              final DistContinuous switchTime)
61      {
62          super(name);
63          this.simulator = simulator;
64          this.switchTime = switchTime;
65          if (switchTime == null)
66              this.switchTime = new DistConstant(new Java2Random(), 2.0);
67      }
68  
69      /***
70       * adds a list of direction to the block for each line
71       * 
72       * @param trafficLight guarding the block
73       * @param lineList list of lines that will use the direction to be set
74       * @param distanceRelease distance after the traffic light that the block
75       * will be released for other vehicles
76       * @param fromTrack start track to set the direction of the switch
77       * @param toTrack end track to set the direction of the switch
78       */
79      public void addSwitchDirection(final SwitchBlockTrafficLight trafficLight,
80              final List lineList, final double distanceRelease,
81              final TrackInterface fromTrack, final TrackInterface toTrack)
82      {
83          for (int i = 0; i < lineList.size(); i++)
84          {
85              Direction direction = new Direction(trafficLight, (String) lineList
86                      .get(i), distanceRelease, fromTrack, toTrack);
87              String key = makeDirectionKey(trafficLight, (String) lineList
88                      .get(i));
89              System.out.println("Added directionKey = " + key);
90              this.directions.put(key, direction);
91              // the release distance for each traffic light is set here
92              trafficLight.setDistanceRelease(distanceRelease);
93              System.out.println("addSwitchDirection distanceRelease: "
94                      + direction.getDistanceRelease()
95                      + ", trafficlight.releaseDistance: "
96                      + trafficLight.getDistanceRelease());
97              //last argument 2 = SENSOR_RELEASE
98              trafficLight.addSensors(trafficLight.getTrack(), trafficLight
99                      .getProgression()
100                     + trafficLight.getDistanceRelease(),
101                     SwitchBlockTrafficLight.SENSOR_RELEASE, (String) lineList
102                             .get(i), this.simulator);
103             System.out.println(trafficLight.getName() + ", release distance: "
104                     + direction.getDistanceRelease() + ", distance Red: "
105                     + trafficLight.getDistanceRed() + ", distance request: "
106                     + trafficLight.getDistanceRequest());
107         }
108     }
109 
110     /***
111      * @param trafficLight
112      * @param line
113      * @return
114      */
115     private String makeDirectionKey(final SwitchBlockTrafficLight trafficLight,
116             final String line)
117     {
118         return trafficLight.toString() + "@" + line;
119     }
120 
121     /***
122      * requestAcess process the request to access a SwitchBlock
123      * 
124      * @param switchBlockTrafficLight correspondent switchBlockTrafficLight that
125      * request access to the block
126      */
127     public void requestAccess(
128             final SwitchBlockTrafficLight switchBlockTrafficLight)
129     {
130         VehiclePhysicalInterface vehicle = switchBlockTrafficLight
131                 .getRequestingVehicle();
132         System.out.println("VirtualSwitchBlock - request access line "
133                 + vehicle.getVehicleControl().getLine() + " at "
134                 + switchBlockTrafficLight.getName());
135         if (super.busy == 0)
136         {
137             super.busy++;
138             System.out.println("variable busy:" + super.busy);
139             changeSwitchDirection(switchBlockTrafficLight, vehicle
140                     .getVehicleControl().getLine());
141         } else
142         {
143             this.requestList.add(switchBlockTrafficLight);
144         }
145     }
146 
147     /***
148      * changeSwitchDirection changes the switch to the position of the track
149      * when the position of the switch is changed, a new release distance for
150      * the traffic light is set
151      * 
152      * @param switchBlockTrafficLight
153      * @param line
154      */
155     private void changeSwitchDirection(
156             final SwitchBlockTrafficLight switchBlockTrafficLight,
157             final String line)
158     {
159         System.out.println(makeDirectionKey(switchBlockTrafficLight, line));
160         Direction direction = (Direction) this.directions.get(makeDirectionKey(
161                 switchBlockTrafficLight, line));
162         System.out
163                 .println("changing switch direction. Release distance to be activated: "
164                         + direction.getDistanceRelease());
165         switchBlockTrafficLight.setDistanceRelease(direction
166                 .getDistanceRelease());
167         TrackInterface fromTrack = direction.getFromTrack();
168         TrackInterface toTrack = direction.getToTrack();
169         TrackLinkInterface startLink = toTrack.getStartLink();
170         if (startLink.getActiveSuccessor(fromTrack).equals(toTrack))
171         {
172             System.out.println(startLink.toString()
173                     + " Switch already on the right position");
174         } else if (!startLink.getActiveSuccessor(fromTrack).equals(toTrack))
175         {
176             try
177             {
178                 this.simulator.scheduleEvent(this.switchTime.draw(), this,
179                         this, "turnSwitch", new Object[]{startLink, fromTrack,
180                                 toTrack, switchBlockTrafficLight});
181             } catch (Exception e)
182             {
183                 e.printStackTrace();
184             }
185         } else
186         {
187             switchBlockTrafficLight
188                     .changeState(SwitchBlockTrafficLight.STATE_GREEN);
189         }
190     }
191 
192     /***
193      * turnSwitch turns the switch to the position from fromTrack to toTrack
194      * 
195      * @param startLink start link of the switch
196      * @param fromTrack track before the switch
197      * @param toTrack track to where the switch needs to be positioned
198      * @param switchBlockTrafficLight
199      */
200     protected void turnSwitch(final TrackLinkInterface startLink,
201             final TrackInterface fromTrack, final TrackInterface toTrack,
202             SwitchBlockTrafficLight switchBlockTrafficLight)
203     {
204         startLink.setActiveSuccessor(fromTrack, toTrack);
205         switchBlockTrafficLight
206                 .changeState(SwitchBlockTrafficLight.STATE_GREEN);
207     }
208 
209     /***
210      * An innerclass containing the record.
211      */
212     private static class Direction
213     {
214         /*** the traficLight * */
215         private SwitchBlockTrafficLight trafficLight = null;
216 
217         /*** the line */
218         private String line;
219 
220         /*** distance to relase the block */
221         private double distanceRelease = 0;
222 
223         /*** the track */
224         private TrackInterface fromTrack = null;
225 
226         /*** the track */
227         private TrackInterface toTrack = null;
228 
229         /***
230          * construct a new Direction
231          * 
232          * @param trafficLight the trafficLight
233          * @param line line that follows this direction
234          * @param distanceRelease distance after the switchBlockTrafficLight
235          * that the block will be released
236          * @param fromTrack the predecessor 'from' track
237          * @param toTrack the successor 'to' track
238          */
239         public Direction(final SwitchBlockTrafficLight trafficLight,
240                 final String line, final double distanceRelease,
241                 final TrackInterface fromTrack, final TrackInterface toTrack)
242         {
243             super();
244             this.trafficLight = trafficLight;
245             this.distanceRelease = distanceRelease;
246             this.line = line;
247             this.fromTrack = fromTrack;
248             this.toTrack = toTrack;
249             System.out.println("Direction added: " + this.trafficLight + ", "
250                     + this.distanceRelease + ", " + this.line + ", "
251                     + this.fromTrack.toString() + ", "
252                     + this.toTrack.toString());
253         }
254 
255         /***
256          * @return Returns the line.
257          */
258         public String getLine()
259         {
260             return this.line;
261         }
262 
263         /***
264          * @return Returns the fromTrack.
265          */
266         public TrackInterface getFromTrack()
267         {
268             return this.fromTrack;
269         }
270 
271         /***
272          * @return Returns the toTrack.
273          */
274         public TrackInterface getToTrack()
275         {
276             return this.toTrack;
277         }
278 
279         /***
280          * @return Returns the trafficLight.
281          */
282         public SwitchBlockTrafficLight getTrafficLight()
283         {
284             return this.trafficLight;
285         }
286 
287         /***
288          * @return distanceRelease
289          */
290         public double getDistanceRelease()
291         {
292             return this.distanceRelease;
293         }
294     }
295 }