View Javadoc
1   package org.opentrafficsim.road.network.lane.object;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.value.vdouble.scalar.Duration;
9   import org.djunits.value.vdouble.scalar.Length;
10  import org.djunits.value.vdouble.scalar.Speed;
11  import org.opentrafficsim.core.gtu.GTUType;
12  import org.opentrafficsim.core.network.LongitudinalDirectionality;
13  import org.opentrafficsim.core.network.NetworkException;
14  import org.opentrafficsim.road.network.animation.SpeedSignAnimation;
15  import org.opentrafficsim.road.network.lane.CrossSectionElement;
16  import org.opentrafficsim.road.network.lane.Lane;
17  
18  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19  
20  /**
21   * Speed sign.
22   * <p>
23   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2017 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   */
31  public class SpeedSign extends AbstractLaneBasedObject
32  {
33  
34      /** */
35      private static final long serialVersionUID = 20170420L;
36  
37      /** End of day. */
38      private static final Duration ENDOFDAY = new Duration(24, DurationUnit.HOUR);
39  
40      /** Speed limit. */
41      private final Speed speed;
42  
43      /** GTU type. */
44      private final GTUType gtuType;
45  
46      /** Start time-of-day. */
47      private final Duration startTimeOfDay;
48  
49      /** End time-of-day. */
50      private final Duration endTimeOfDay;
51  
52      /**
53       * Construct a new SpeedSign.
54       * @param id String; the id of the new SpeedSign
55       * @param lane Lane on/over which the SpeedSign is positioned
56       * @param direction LongitudinalDirectionality; driving direction for which the new SpeedSign applies
57       * @param longitudinalPosition Length; the longitudinal position along the lane of the new SpeedSign
58       * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
59       * @param speed Speed; the speed limit shown by the new SpeedSign
60       * @param gtuType GTU type that should obey the speed sign
61       * @param startTimeOfDay start time-of-day
62       * @param endTimeOfDay end time-of-day
63       * @throws NetworkException when the position on the lane is out of bounds
64       */
65      @SuppressWarnings("checkstyle:parameternumber")
66      public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
67              final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed, final GTUType gtuType,
68              final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
69      {
70          super(id, lane, direction, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition));
71          this.speed = speed;
72          this.gtuType = gtuType;
73          this.startTimeOfDay = startTimeOfDay;
74          this.endTimeOfDay = endTimeOfDay;
75  
76          try
77          {
78              new SpeedSignAnimation(this, simulator);
79          }
80          catch (RemoteException | NamingException exception)
81          {
82              throw new NetworkException(exception);
83          }
84      }
85  
86      /**
87       * Speed sign active all day.
88       * @param id id
89       * @param lane lane
90       * @param direction direction
91       * @param longitudinalPosition longitudinal position
92       * @param simulator simulator
93       * @param speed speed
94       * @param gtuType GTU type
95       * @throws NetworkException when the position on the lane is out of bounds
96       */
97      public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
98              final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed, final GTUType gtuType)
99              throws NetworkException
100     {
101         this(id, lane, direction, longitudinalPosition, simulator, speed, gtuType, Duration.ZERO, ENDOFDAY);
102     }
103 
104     /**
105      * Speed sign for all GTU types.
106      * @param id id
107      * @param lane lane
108      * @param direction direction
109      * @param longitudinalPosition longitudinal position
110      * @param simulator simulator
111      * @param speed speed
112      * @param startTimeOfDay start time-of-day
113      * @param endTimeOfDay end time-of-day
114      * @throws NetworkException when the position on the lane is out of bounds
115      */
116     @SuppressWarnings("checkstyle:parameternumber")
117     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
118             final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed,
119             final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
120     {
121         this(id, lane, direction, longitudinalPosition, simulator, speed, GTUType.VEHICLE, startTimeOfDay, endTimeOfDay);
122     }
123 
124     /**
125      * Speed sign active all day for all GTU types.
126      * @param id id
127      * @param lane lane
128      * @param direction direction
129      * @param longitudinalPosition longitudinal position
130      * @param simulator simulator
131      * @param speed speed
132      * @throws NetworkException when the position on the lane is out of bounds
133      */
134     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
135             final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed)
136             throws NetworkException
137     {
138         this(id, lane, direction, longitudinalPosition, simulator, speed, GTUType.VEHICLE, Duration.ZERO, ENDOFDAY);
139     }
140 
141     /**
142      * Return whether this speed limit is currently active.
143      * @param gtuTypeIn GTU type
144      * @param time current time-of-day
145      * @return whether this speed limit is currently active
146      */
147     public final boolean isActive(final GTUType gtuTypeIn, final Duration time)
148     {
149         return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
150     }
151 
152     /**
153      * Returns the speed.
154      * @return the speed
155      */
156     public final Speed getSpeed()
157     {
158         return this.speed;
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public final AbstractLaneBasedObject clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator,
164             final boolean animation) throws NetworkException
165     {
166         return new SpeedSign(getId(), (Lane) newCSE, getDirection(), getLongitudinalPosition(), newSimulator, this.speed,
167                 this.gtuType, this.startTimeOfDay, this.endTimeOfDay);
168     }
169 
170     /** {@inheritDoc} */
171     @Override
172     public int hashCode()
173     {
174         final int prime = 31;
175         int result = 1;
176         result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
177         result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
178         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
179         result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
180         return result;
181     }
182 
183     /** {@inheritDoc} */
184     @Override
185     public boolean equals(final Object obj)
186     {
187         if (this == obj)
188         {
189             return true;
190         }
191         if (obj == null)
192         {
193             return false;
194         }
195         if (getClass() != obj.getClass())
196         {
197             return false;
198         }
199         SpeedSign other = (SpeedSign) obj;
200         if (this.endTimeOfDay == null)
201         {
202             if (other.endTimeOfDay != null)
203             {
204                 return false;
205             }
206         }
207         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
208         {
209             return false;
210         }
211         if (this.gtuType == null)
212         {
213             if (other.gtuType != null)
214             {
215                 return false;
216             }
217         }
218         else if (!this.gtuType.equals(other.gtuType))
219         {
220             return false;
221         }
222         if (this.speed == null)
223         {
224             if (other.speed != null)
225             {
226                 return false;
227             }
228         }
229         else if (!this.speed.equals(other.speed))
230         {
231             return false;
232         }
233         if (this.startTimeOfDay == null)
234         {
235             if (other.startTimeOfDay != null)
236             {
237                 return false;
238             }
239         }
240         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
241         {
242             return false;
243         }
244         return true;
245     }
246 
247     /** {@inheritDoc} */
248     @Override
249     public final String toString()
250     {
251         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
252                 + ", endTime=" + this.endTimeOfDay + "]";
253     }
254 
255 }