View Javadoc
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   * <p>
19   * Copyright (c) 2013-2017 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 OTSSimulatorInterface simulator;
32  
33      /**
34       * Constructor.
35       * @param simulator simulator
36       */
37      public BusStopConflictRule(final OTSSimulatorInterface 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().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                          // only on bus stop
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         // if bus has priority and bus is asking, PRIORITY
109         // if bus has no priority and bus is not asking (i.e. car is asking), PRIORITY
110         return busHasPriority == requestingFromBusStop ? ConflictPriority.PRIORITY : ConflictPriority.GIVE_WAY;
111 
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public ConflictRule clone(final OTSSimulatorInterface newSimulator)
117     {
118         return new BusStopConflictRule(newSimulator);
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public String toString()
124     {
125         return "BusStopConflictRule";
126     }
127 
128 }