View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.headway;
2   
3   import java.util.SortedSet;
4   import java.util.TreeSet;
5   
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.opentrafficsim.core.gtu.GTUException;
9   import org.opentrafficsim.road.network.lane.CrossSectionLink;
10  import org.opentrafficsim.road.network.lane.conflict.ConflictPriority;
11  import org.opentrafficsim.road.network.lane.conflict.ConflictRule;
12  import org.opentrafficsim.road.network.lane.conflict.ConflictType;
13  
14  import nl.tudelft.simulation.language.Throw;
15  
16  /**
17   * <p>
18   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jun 2, 2016 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class HeadwayConflict extends AbstractHeadwayCopy
27  {
28  
29      /** */
30      private static final long serialVersionUID = 20160602L;
31  
32      /** Conflict type. */
33      private final ConflictType conflictType;
34  
35      /** Conflict priority. */
36      private final ConflictPriority conflictPriority;
37  
38      /** Length of the conflict in the conflicting directions. */
39      private final Length conflictingLength;
40  
41      /**
42       * Set of conflicting GTU's <i>completely</i> upstream of the <i>start</i> of the conflict ordered close to far from the
43       * start of the conflict. Distance and overlap info concerns the conflict.
44       */
45      private final SortedSet<HeadwayGTU> upstreamConflictingGTUs;
46  
47      /**
48       * Set of conflicting GTU's (partially) downstream of the <i>start</i> of the conflict ordered close to far from the start
49       * of conflict. Distance and overlap info concerns the conflict.
50       */
51      private final SortedSet<HeadwayGTU> downstreamConflictingGTUs;
52  
53      /** Visibility on the conflicting lane within which conflicting vehicles are visible. */
54      private final Length conflictingVisibility;
55  
56      /** Speed limit on the conflicting lane. */
57      private final Speed conflictingSpeedLimit;
58  
59      /** Link of conflicting conflict. */
60      private final CrossSectionLink conflictingLink;
61  
62      /** Stop line on the own lane. */
63      private final HeadwayStopLine stopLine;
64  
65      /** Stop line on the conflicting lane. */
66      private final HeadwayStopLine conflictingStopLine;
67  
68      /** Type of conflict rule. */
69      private final Class<? extends ConflictRule> conflictRuleType;
70  
71      /** Distance of traffic light upstream on conflicting lane. */
72      private Length conflictingTrafficLightDistance = null;
73      
74      /** Whether the conflict is permitted by the traffic light. */
75      private boolean permitted = false;
76      
77      /**
78       * Constructor.
79       * @param conflictType conflict type
80       * @param conflictPriority conflict priority
81       * @param conflictRuleType conflict rule type
82       * @param id id
83       * @param distance distance
84       * @param length length of the conflict
85       * @param conflictingLength length of the conflict on the conflicting lane
86       * @param upstreamConflictingGTUs conflicting GTU's upstream of the <i>start</i> of the conflict
87       * @param downstreamConflictingGTUs conflicting GTU's downstream of the <i>start</i> of the conflict
88       * @param conflictingVisibility visibility on the conflicting lane within which conflicting vehicles are visible
89       * @param conflictingSpeedLimit speed limit on the conflicting lane
90       * @param conflictingLink conflicting link
91       * @param stopLine stop line on the own lane
92       * @param conflictingStopLine stop line on the conflicting lane
93       * @throws GTUException when id is null, or parameters are inconsistent
94       */
95      @SuppressWarnings("checkstyle:parameternumber")
96      public HeadwayConflict(final ConflictType conflictType, final ConflictPriority conflictPriority,
97              final Class<? extends ConflictRule> conflictRuleType, final String id, final Length distance, final Length length,
98              final Length conflictingLength, final SortedSet<HeadwayGTU> upstreamConflictingGTUs,
99              final SortedSet<HeadwayGTU> downstreamConflictingGTUs, final Length conflictingVisibility,
100             final Speed conflictingSpeedLimit, final CrossSectionLink conflictingLink, final HeadwayStopLine stopLine,
101             final HeadwayStopLine conflictingStopLine) throws GTUException
102     {
103         super(ObjectType.CONFLICT, id, distance, length);
104         Throw.whenNull(conflictType, "Conflict type may not be null.");
105         Throw.whenNull(conflictPriority, "Conflict priority may not be null.");
106         Throw.whenNull(conflictRuleType, "Conflict rule type may not be null.");
107         Throw.whenNull(id, "Conflict id may not be null.");
108         Throw.whenNull(distance, "Conflict distance may not be null.");
109         Throw.whenNull(conflictingLength, "Conflict length may not be null.");
110         Throw.whenNull(upstreamConflictingGTUs, "Upstreaem conflicting GTU's may not be null.");
111         Throw.whenNull(downstreamConflictingGTUs, "Downstream conflicting GTU's may not be null.");
112         Throw.whenNull(conflictingVisibility, "Conflict visibility may not be null.");
113         Throw.whenNull(conflictingSpeedLimit, "Conflict speed limit may not be null.");
114         this.conflictType = conflictType;
115         this.conflictPriority = conflictPriority;
116         this.conflictRuleType = conflictRuleType;
117         this.conflictingLength = conflictingLength;
118         this.upstreamConflictingGTUs = upstreamConflictingGTUs;
119         this.downstreamConflictingGTUs = downstreamConflictingGTUs;
120         this.conflictingVisibility = conflictingVisibility;
121         this.conflictingSpeedLimit = conflictingSpeedLimit;
122         this.conflictingLink = conflictingLink;
123         this.stopLine = stopLine;
124         this.conflictingStopLine = conflictingStopLine;
125     }
126 
127     /**
128      * Constructor without stop lines.
129      * @param conflictType conflict type
130      * @param conflictPriority conflict priority
131      * @param conflictRuleType conflict rule type
132      * @param id id
133      * @param distance distance
134      * @param length length of the conflict
135      * @param conflictingLength length of the conflict on the conflicting lane
136      * @param upstreamConflictingGTUs conflicting GTU's upstream of the <i>start</i> of the conflict
137      * @param downstreamConflictingGTUs conflicting GTU's downstream of the <i>start</i> of the conflict
138      * @param conflictingVisibility visibility on the conflicting lane within which conflicting vehicles are visible
139      * @param conflictingSpeedLimit speed limit on the conflicting lane
140      * @param conflictingLink conflicting link
141      * @throws GTUException when id is null, or parameters are inconsistent
142      */
143     @SuppressWarnings("checkstyle:parameternumber")
144     public HeadwayConflict(final ConflictType conflictType, final ConflictPriority conflictPriority, 
145             final Class<? extends ConflictRule> conflictRuleType, final String id,
146             final Length distance, final Length length, final Length conflictingLength,
147             final SortedSet<HeadwayGTU> upstreamConflictingGTUs,
148             final SortedSet<HeadwayGTU> downstreamConflictingGTUs, final Length conflictingVisibility,
149             final Speed conflictingSpeedLimit, final CrossSectionLink conflictingLink) throws GTUException
150     {
151         this(conflictType, conflictPriority, conflictRuleType, id, distance, length, conflictingLength, upstreamConflictingGTUs,
152                 downstreamConflictingGTUs, conflictingVisibility, conflictingSpeedLimit, conflictingLink, null, null);
153     }
154 
155     /**
156      * Returns the conflict type.
157      * @return conflict type
158      */
159     public final ConflictType getConflictType()
160     {
161         return this.conflictType;
162     }
163 
164     /**
165      * Returns whether this is a crossing conflict.
166      * @return whether this is a crossing conflict
167      */
168     public final boolean isCrossing()
169     {
170         return this.conflictType.equals(ConflictType.CROSSING);
171     }
172 
173     /**
174      * Returns whether this is a merge conflict.
175      * @return whether this is a merge conflict
176      */
177     public final boolean isMerge()
178     {
179         return this.conflictType.equals(ConflictType.MERGE);
180     }
181 
182     /**
183      * Returns whether this is a split conflict.
184      * @return whether this is a split conflict
185      */
186     public final boolean isSplit()
187     {
188         return this.conflictType.equals(ConflictType.SPLIT);
189     }
190 
191     /**
192      * Returns the conflict priority.
193      * @return conflict priority
194      */
195     public final ConflictPriority getConflictPriority()
196     {
197         return this.conflictPriority;
198     }
199 
200     /**
201      * Returns the length of the conflict on the conflicting lane.
202      * @return length of the conflict on the conflicting lane
203      */
204     public final Length getConflictingLength()
205     {
206         return this.conflictingLength;
207     }
208 
209     /**
210      * Returns a set of conflicting GTU's upstream of the <i>start</i> of the conflict ordered close to far from the conflict.
211      * @return set of conflicting GTU's upstream of the <i>start</i> of the conflict ordered close to far from the conflict
212      */
213     public final SortedSet<HeadwayGTU> getUpstreamConflictingGTUs()
214     {
215         return new TreeSet<>(this.upstreamConflictingGTUs);
216     }
217 
218     /**
219      * Returns a set of conflicting GTU's downstream of the <i>start</i> of the conflict ordered close to far from the conflict.
220      * Distance is given relative to the <i>end</i> of the conflict, or null for conflicting vehicles on the conflict. In the
221      * latter case the overlap is used.
222      * @return set of conflicting GTU's downstream of the <i>start</i> of the conflict ordered close to far from the conflict
223      */
224     public final SortedSet<HeadwayGTU> getDownstreamConflictingGTUs()
225     {
226         return new TreeSet<>(this.downstreamConflictingGTUs);
227     }
228 
229     /**
230      * Returns the visibility on the conflicting lane within which conflicting vehicles are visible. All upstream conflicting
231      * GTUs have a distance smaller than the visibility. Depending on a limited visibility, a certain (lower) speed may be
232      * required while approaching the conflict.
233      * @return visibility on the conflicting lane within which conflicting vehicles are visible
234      */
235     public final Length getConflictingVisibility()
236     {
237         return this.conflictingVisibility;
238     }
239 
240     /**
241      * Returns the speed limit on the conflicting lane.
242      * @return speed limit on the conflicting lane
243      */
244     public final Speed getConflictingSpeedLimit()
245     {
246         return this.conflictingSpeedLimit;
247     }
248 
249     /**
250      * Returns the conflicting link.
251      * @return the conflicting link
252      */
253     public final CrossSectionLink getConflictingLink()
254     {
255         return this.conflictingLink;
256     }
257 
258     /**
259      * Returns the stop line.
260      * @return stop line
261      */
262     public final HeadwayStopLine getStopLine()
263     {
264         return this.stopLine;
265     }
266 
267     /**
268      * Returns the stop line on the conflicting lane.
269      * @return stop line
270      */
271     public final HeadwayStopLine getConflictingStopLine()
272     {
273         return this.conflictingStopLine;
274     }
275     
276     /**
277      * Returns the conflict rule type.
278      * @return conflict rule type
279      */
280     public Class<? extends ConflictRule> getConflictRuleType()
281     {
282         return this.conflictRuleType;
283     }
284     
285     /**
286      * Returns the distance of a traffic light upstream on the conflicting lane.
287      * @return distance of a traffic light upstream on the conflicting lane.
288      */
289     public Length getConflictingTrafficLightDistance()
290     {
291         return this.conflictingTrafficLightDistance;
292     }
293     
294     /**
295      * Whether the conflict is permitted by the traffic light.
296      * @return whether the conflict is permitted by the traffic light
297      */
298     public boolean isPermitted()
299     {
300         return this.permitted;
301     }
302 
303     /**
304      * Set the distance of a traffic light upstream on the conflicting lane.
305      * @param trafficLightDistance distance of a traffic light upstream on the conflicting lane.
306      * @param permittedConflict whether the conflict is permitted by the traffic light
307      */
308     public void setConflictingTrafficLight(final Length trafficLightDistance, final boolean permittedConflict)
309     {
310         this.conflictingTrafficLightDistance = trafficLightDistance;
311         this.permitted = permittedConflict;
312     }
313 
314     /** {@inheritDoc} */
315     public final String toString()
316     {
317         return String.format("Headway %s to object %s of type %s", getDistance(), getId(), getObjectType());
318     }
319 
320 }