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