View Javadoc
1   package org.opentrafficsim.road.network.lane.object;
2   
3   import java.util.LinkedHashSet;
4   import java.util.List;
5   import java.util.Set;
6   
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djutils.immutablecollections.Immutable;
9   import org.djutils.immutablecollections.ImmutableHashSet;
10  import org.djutils.immutablecollections.ImmutableMap;
11  import org.djutils.immutablecollections.ImmutableSet;
12  import org.opentrafficsim.core.gtu.GTUDirectionality;
13  import org.opentrafficsim.core.gtu.GTUType;
14  import org.opentrafficsim.core.network.LongitudinalDirectionality;
15  import org.opentrafficsim.core.network.NetworkException;
16  import org.opentrafficsim.road.network.lane.CrossSectionElement;
17  import org.opentrafficsim.road.network.lane.Lane;
18  import org.opentrafficsim.road.network.lane.conflict.BusStopConflictRule;
19  import org.opentrafficsim.road.network.lane.conflict.Conflict;
20  
21  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22  
23  /**
24   * A bus stop is a location on a lane. The stop has a name, and a set of lines. At a single stop in reality, there may be
25   * different locations where busses stop for different lines. A {@code BusStop} pertains to only one such location. The bus stop
26   * in reality is represented by a shared name over a few {@code BusStop}'s, with different lines. As lines may also be set
27   * dynamically, the name and lines are insufficient to identify a specific {@code BusStop}. Hence there is a fixed unique id per
28   * {@code BusStop}.
29   * <p>
30   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
32   * <p>
33   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 jan. 2017 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
37   */
38  public class BusStop extends AbstractLaneBasedObject
39  {
40  
41      /** */
42      private static final long serialVersionUID = 20170124L;
43  
44      /** Line numbers. */
45      private final Set<String> lines = new LinkedHashSet<>();
46  
47      /** Stop name. */
48      private final String name;
49  
50      /** Stored conflicts downstream. */
51      private Set<Conflict> conflicts = null;
52  
53      /**
54       * @param id String; id
55       * @param lane Lane; lane
56       * @param longitudinalPosition Length; position
57       * @param name String; name of stop
58       * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator to schedule on
59       * @throws NetworkException when the position on the lane is out of bounds
60       */
61      public BusStop(final String id, final Lane lane, final Length longitudinalPosition, final String name,
62              final SimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
63      {
64          super(id, lane, LongitudinalDirectionality.DIR_PLUS, longitudinalPosition,
65                  LaneBasedObject.makeGeometry(lane, longitudinalPosition), Length.ZERO);
66          this.name = name;
67      }
68  
69      /**
70       * Sets the lines.
71       * @param lines Set&lt;String&gt;; lines that stop at this location
72       */
73      public final void setLines(final Set<String> lines)
74      {
75          this.lines.clear();
76          this.lines.addAll(lines);
77      }
78  
79      /**
80       * Returns the lines set.
81       * @return whether the lines belongs to this stop
82       */
83      public final ImmutableSet<String> getLines()
84      {
85          return new ImmutableHashSet<>(this.lines, Immutable.COPY);
86      }
87  
88      /**
89       * Returns the downstream conflicts of the bus stop. Search is only performed over links with BUS_STOP priority.
90       * @return downstream conflicts of the given conflict
91       */
92      public final Set<Conflict> getConflicts()
93      {
94          if (this.conflicts == null)
95          {
96              this.conflicts = new LinkedHashSet<>();
97              Lane lane = getLane();
98              // conflict forces only plus or minus as direction
99              GTUDirectionality dir = getDirection().isForward() ? GTUDirectionality.DIR_PLUS : GTUDirectionality.DIR_MINUS;
100             Length position = getLongitudinalPosition();
101             while (lane != null)
102             {
103                 List<LaneBasedObject> objects = lane.getObjectAhead(position, dir);
104                 while (objects != null)
105                 {
106                     for (LaneBasedObject object : objects)
107                     {
108                         if (object instanceof Conflict)
109                         {
110                             Conflict./../../../../org/opentrafficsim/road/network/lane/conflict/Conflict.html#Conflict">Conflict conflict = (Conflict) object;
111                             if (conflict.getConflictRule() instanceof BusStopConflictRule)
112                             {
113                                 this.conflicts.add(conflict);
114                             }
115                         }
116                     }
117                     objects = lane.getObjectAhead(objects.get(0).getLongitudinalPosition(), dir);
118                 }
119                 ImmutableMap<Lane, GTUDirectionality> downstreamLanes =
120                         lane.downstreamLanes(dir, lane.getNetwork().getGtuType(GTUType.DEFAULTS.BUS));
121                 int numLanes = 0;
122                 for (Lane nextLane : downstreamLanes.keySet())
123                 {
124                     if (nextLane.getParentLink().getPriority().isBusStop())
125                     {
126                         numLanes++;
127                         lane = nextLane;
128                         dir = downstreamLanes.get(lane);
129                         position = dir.isPlus() ? Length.ZERO : lane.getLength();
130                     }
131                 }
132                 if (numLanes != 1)
133                 {
134                     lane = null;
135                 }
136             }
137         }
138         return this.conflicts;
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public final int hashCode()
144     {
145         final int prime = 31;
146         int result = 1;
147         result = prime * result + this.getId().hashCode();
148         return result;
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final boolean equals(final Object obj)
154     {
155         if (this == obj)
156         {
157             return true;
158         }
159         if (obj == null)
160         {
161             return false;
162         }
163         if (getClass() != obj.getClass())
164         {
165             return false;
166         }
167         BusStop../../../../../../org/opentrafficsim/road/network/lane/object/BusStop.html#BusStop">BusStop other = (BusStop) obj;
168         if (!this.getId().equals(other.getId()))
169         {
170             return false;
171         }
172         return true;
173     }
174 
175     /** {@inheritDoc} */
176     @Override
177     public final String toString()
178     {
179         String out = "BusStop [id=" + getId() + ", lines=";
180         String delim = "";
181         for (String line : this.lines)
182         {
183             out = out + delim + line;
184             delim = "/";
185         }
186         return out + "]";
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public final AbstractLaneBasedObject clone(final CrossSectionElement newCSE,
192             final SimulatorInterface.TimeDoubleUnit newSimulator) throws NetworkException
193     {
194         BusStopnetwork/lane/object/BusStop.html#BusStop">BusStop busStop = new BusStop(getId(), (Lane) newCSE, getLongitudinalPosition(), this.name, newSimulator);
195         busStop.setLines(this.lines);
196         return busStop;
197     }
198 
199 }