View Javadoc
1   package org.opentrafficsim.road.network.lane.conflict;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.exceptions.Throw;
5   import org.djutils.immutablecollections.ImmutableMap;
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  
16  /**
17   * Conflict rule for conflicts where busses enter the lane after a stop.
18   * <p>
19   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 27 jan. 2017 <br>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
26   */
27  public class BusStopConflictRule implements ConflictRule
28  {
29  
30      /** Simulator. */
31      private final SimulatorInterface.TimeDoubleUnit simulator;
32  
33      /**
34       * Constructor.
35       * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
36       */
37      public BusStopConflictRule(final SimulatorInterface.TimeDoubleUnit simulator)
38      {
39          this.simulator = simulator;
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public ConflictPriority determinePriority(final Conflict conflict)
45      {
46  
47          // determine if we request from bus stop, or not
48          boolean requestingFromBusStop;
49          Conflict busConflict;
50          // conflict builder enforces that only one of the two links has priority BUS_STOP
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          // find bus and determine if it has priority
66          // conflict forces that LongitudinalDirection is DIR_PLUS or DIR_MINUS
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());
77                  if (gtu == null)
78                  {
79                      ImmutableMap<Lane, GTUDirectionality> map =
80                              lane.upstreamLanes(dir, lane.getNetwork().getGtuType(GTUType.DEFAULTS.BUS));
81                      if (map.size() == 1)
82                      {
83                          lane = map.keySet().iterator().next();
84                          // only on bus stop
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 =
107                 gtu != null && gtu.getGTUType().isOfType(GTUType.DEFAULTS.BUS) && gtu.getTurnIndicatorStatus().isLeft();
108 
109         // if bus has priority and bus is asking, PRIORITY
110         // if bus has no priority and bus is not asking (i.e. car is asking), PRIORITY
111         return busHasPriority == requestingFromBusStop ? ConflictPriority.PRIORITY : ConflictPriority.YIELD;
112 
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final ConflictRule clone(final SimulatorInterface.TimeDoubleUnit newSimulator)
118     {
119         return new BusStopConflictRule(newSimulator);
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final String toString()
125     {
126         return "BusStopConflictRule";
127     }
128 
129 }