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