View Javadoc

1   /*
2    * Created on Dec 18, 2003
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.real;
12  
13  import java.rmi.RemoteException;
14  import javax.media.j3d.Bounds;
15  import javax.vecmath.Point3d;
16  import nl.tudelft.simulation.dsol.SimRuntimeException;
17  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
18  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
19  import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
20  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
21  import nl.tudelft.simulation.event.Event;
22  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
23  import nl.tudelft.simulation.language.d3.BoundingBox;
24  import nl.tudelft.simulation.language.d3.DirectedPoint;
25  import nl.tudelft.simulation.traffic.animation.TrafficLightAnimation;
26  import nl.tudelft.simulation.traffic.track.TrackInterface;
27  import nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface;
28  
29  /***
30   * This class is an implementation of a simple trafficLight. It is based on the
31   * AbstractVisibleContolPoint and the StopSignInterface. The
32   * LocatableInterface is only used for the animation. <br>
33   * <br>
34   * 
35   * @author J.H. Kwakkel & H.W.G. Phaff
36   * 
37   * @see nl.tudelft.simulation.traffic.controlpoint.real.StopSignInterface
38   * @see nl.tudelft.simulation.traffic.controlpoint.real.AbstractVisibleControlPoint
39   */
40  public class TrafficLight extends AbstractVisibleControlPoint
41          implements
42              StopSignInterface,
43              LocatableInterface
44  {
45      /*** the simulator to schedule on */
46      private DEVSSimulatorInterface simulator;
47  
48      /*** the duration of red time */
49      private DistContinuous durationRed;
50  
51      /*** the duration of green time */
52      private DistContinuous durationGreen;
53  
54      /*** current status of the traffic light */
55      private String status;
56  
57      /*** the name */
58      private String name;
59  
60      /*** dx for animation (bounds) */
61      private double dx;
62  
63      /*** dy for animation (bounds) */
64      private double dy;
65  
66      /***
67       * @param name
68       * @param progression
69       * @param visibleDistance
70       * @param track
71       * @param durationGreen
72       * @param durationRed
73       * @param status
74       * @param simulator
75       * @param dx
76       * @param dy
77       */
78      public TrafficLight(final String name, final double progression,
79              final double visibleDistance, final TrackInterface track,
80              final DistContinuous durationGreen,
81              final DistContinuous durationRed, final String status,
82              final DEVSSimulatorInterface simulator, final double dx,
83              final double dy)
84      {
85          super(track, progression, visibleDistance, simulator);
86          this.name = name;
87          System.out.println(this + ", placed on " + track + " progression "
88                  + progression + " (length=" + track.getLength() + ")");
89          this.status = status;
90          this.durationRed = durationRed;
91          this.durationGreen = durationGreen;
92          this.simulator = simulator;
93          this.dx = dx;
94          this.dy = dy;
95          if (simulator instanceof AnimatorInterface)
96          {
97              new TrafficLightAnimation(this, simulator, dx, dy);
98          }
99          double timeToNext = 0;
100         try
101         {
102             if (this.status.equals(StopSignInterface.CONTINUE))
103             {
104                 timeToNext = this.durationGreen.draw();
105             } else
106             {
107                 timeToNext = this.durationRed.draw();
108             }
109             SimEvent simEvent = new SimEvent(simulator.getSimulatorTime()
110                     + timeToNext, this, this, "changeStatus", null);
111             simulator.scheduleEvent(simEvent);
112         } catch (RemoteException e1)
113         {
114             e1.printStackTrace();
115         } catch (SimRuntimeException e1)
116         {
117             e1.printStackTrace();
118         }
119     }
120 
121     /***
122      * @throws Exception
123      */
124     protected void changeStatus() throws Exception
125     {
126         double timeToNext = 0;
127         if (this.status == StopSignInterface.CONTINUE)
128         {
129             this.status = StopSignInterface.STOP;
130             Event event = new Event(Changeable.CHANGE_STATUS_EVENT, this, null);
131             this.fireEvent(event);
132             timeToNext = this.durationRed.draw();
133         } else
134         {
135             this.status = StopSignInterface.CONTINUE;
136             Event event = new Event(Changeable.CHANGE_STATUS_EVENT, this, null);
137             this.fireEvent(event);
138             timeToNext = this.durationGreen.draw();
139         }
140         SimEvent simEvent = new SimEvent(this.simulator.getSimulatorTime()
141                 + timeToNext, this, this, "changeStatus", null);
142         this.simulator.scheduleEvent(simEvent);
143     }
144 
145     /***
146      * @see nl.tudelft.simulation.traffic.controlpoint.ControlPointInterface#pass(nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface)
147      */
148     public void pass(final VehiclePhysicalInterface vehicle)
149     {
150         // cancel the listener of the vehicle
151         this.removeListener(vehicle, Changeable.CHANGE_STATUS_EVENT);
152     }
153 
154     /***
155      * @see nl.tudelft.simulation.traffic.controlpoint.ControlPointInterface#getProgression()
156      */
157     public double getProgression()
158     {
159         return super.getProgression();
160     }
161 
162     /***
163      * @see nl.tudelft.simulation.traffic.controlpoint.real.Changeable#getStatus()
164      */
165     public String getStatus()
166     {
167         return this.status;
168     }
169 
170     /***
171      * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getLocation()
172      */
173     public DirectedPoint getLocation() throws RemoteException
174     {
175         DirectedPoint p = super.getTrack().getLocationOfProgression(
176                 super.getProgression());
177         return new DirectedPoint(p.x, p.y, p.z);
178     }
179 
180     /***
181      * @see nl.tudelft.simulation.traffic.controlpoint.ControlPointInterface#getTrack()
182      */
183     public TrackInterface getTrack()
184     {
185         return super.getTrack();
186     }
187 
188     /***
189      * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
190      */
191     public Bounds getBounds() throws RemoteException
192     {
193         return new BoundingBox(new Point3d(this.dx - 2.0, this.dy, 0),
194                 new Point3d(this.dx + 2.0, this.dy + 8.0, 0));
195     }
196 
197     /***
198      * @see java.lang.Object#toString()
199      */
200     public String toString()
201     {
202         return "TrafficLight " + this.name;
203     }
204 }