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