1 package org.opentrafficsim.road.network.lane.conflict;
2
3 import java.util.Set;
4
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.djutils.exceptions.Throw;
7 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
8 import org.opentrafficsim.core.gtu.GtuException;
9 import org.opentrafficsim.core.gtu.GtuType;
10 import org.opentrafficsim.core.gtu.RelativePosition;
11 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
12 import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
13 import org.opentrafficsim.road.network.lane.Lane;
14
15
16
17
18
19
20
21
22
23
24
25 public class BusStopConflictRule implements ConflictRule
26 {
27
28
29 private final OtsSimulatorInterface simulator;
30
31
32 private final GtuType busType;
33
34
35
36
37
38
39 public BusStopConflictRule(final OtsSimulatorInterface simulator, final GtuType busType)
40 {
41 this.simulator = simulator;
42 this.busType = busType;
43 }
44
45 @Override
46 public ConflictPriority determinePriority(final Conflict conflict)
47 {
48
49
50 boolean requestingFromBusStop;
51 Conflict busConflict;
52
53 if (conflict.getLane().getLink().getPriority().equals(Priority.BUS_STOP))
54 {
55 Throw.when(conflict.getOtherConflict().getLane().getLink().getPriority().equals(Priority.BUS_STOP),
56 IllegalArgumentException.class,
57 "BusStopConflictRule does not support a conflict between two links with priority BUS_STOP.");
58 requestingFromBusStop = true;
59 busConflict = conflict;
60 }
61 else
62 {
63 requestingFromBusStop = false;
64 busConflict = conflict.getOtherConflict();
65 }
66
67
68 Lane lane = busConflict.getLane();
69 Length pos = busConflict.getLongitudinalPosition();
70 LaneBasedGtu gtu = null;
71 try
72 {
73 while (gtu == null && lane != null)
74 {
75 gtu = lane.getGtuBehind(pos, RelativePosition.FRONT, this.simulator.getSimulatorAbsTime());
76 if (gtu == null)
77 {
78 Set<Lane> set = lane.prevLanes(this.busType);
79 if (set.size() == 1)
80 {
81 lane = set.iterator().next();
82
83 if (lane.getLink().getPriority().isBusStop())
84 {
85 pos = lane.getLength();
86 }
87 else
88 {
89 lane = null;
90 }
91 }
92 else
93 {
94 lane = null;
95 }
96 }
97 }
98 }
99 catch (GtuException exception)
100 {
101 throw new RuntimeException("Error while looking for GTU upstream of merge at bus stop.", exception);
102 }
103 boolean busHasPriority = gtu != null && gtu.getType().isOfType(this.busType) && gtu.getTurnIndicatorStatus().isLeft();
104
105
106
107 return busHasPriority == requestingFromBusStop ? ConflictPriority.PRIORITY : ConflictPriority.YIELD;
108
109 }
110
111 @Override
112 public final String toString()
113 {
114 return "BusStopConflictRule";
115 }
116
117 }