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
46 @Override
47 public ConflictPriority determinePriority(final Conflict conflict)
48 {
49
50
51 boolean requestingFromBusStop;
52 Conflict busConflict;
53
54 if (conflict.getLane().getLink().getPriority().equals(Priority.BUS_STOP))
55 {
56 Throw.when(conflict.getOtherConflict().getLane().getLink().getPriority().equals(Priority.BUS_STOP),
57 IllegalArgumentException.class,
58 "BusStopConflictRule does not support a conflict between two links with priority BUS_STOP.");
59 requestingFromBusStop = true;
60 busConflict = conflict;
61 }
62 else
63 {
64 requestingFromBusStop = false;
65 busConflict = conflict.getOtherConflict();
66 }
67
68
69 Lane lane = busConflict.getLane();
70 Length pos = busConflict.getLongitudinalPosition();
71 LaneBasedGtu gtu = null;
72 try
73 {
74 while (gtu == null && lane != null)
75 {
76 gtu = lane.getGtuBehind(pos, RelativePosition.FRONT, this.simulator.getSimulatorAbsTime());
77 if (gtu == null)
78 {
79 Set<Lane> set = lane.prevLanes(this.busType);
80 if (set.size() == 1)
81 {
82 lane = set.iterator().next();
83
84 if (lane.getLink().getPriority().isBusStop())
85 {
86 pos = lane.getLength();
87 }
88 else
89 {
90 lane = null;
91 }
92 }
93 else
94 {
95 lane = null;
96 }
97 }
98 }
99 }
100 catch (GtuException exception)
101 {
102 throw new RuntimeException("Error while looking for GTU upstream of merge at bus stop.", exception);
103 }
104 boolean busHasPriority = gtu != null && gtu.getType().isOfType(this.busType) && gtu.getTurnIndicatorStatus().isLeft();
105
106
107
108 return busHasPriority == requestingFromBusStop ? ConflictPriority.PRIORITY : ConflictPriority.YIELD;
109
110 }
111
112
113 @Override
114 public final String toString()
115 {
116 return "BusStopConflictRule";
117 }
118
119 }