View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.djutils.exceptions.Throw;
8   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
9   import org.opentrafficsim.core.geometry.OTSLine3D;
10  import org.opentrafficsim.core.network.LinkType;
11  import org.opentrafficsim.core.network.Network;
12  import org.opentrafficsim.core.network.NetworkException;
13  import org.opentrafficsim.core.network.Node;
14  import org.opentrafficsim.core.network.OTSLink;
15  import org.opentrafficsim.road.network.RoadNetwork;
16  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
17  
18  import nl.tudelft.simulation.event.EventType;
19  
20  /**
21   * A CrossSectionLink is a link with lanes where GTUs can possibly switch between lanes.
22   * <p>
23   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * <p>
26   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
27   * initial version Aug 19, 2014 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
31   */
32  public class CrossSectionLink extends OTSLink implements Serializable
33  {
34      /** */
35      private static final long serialVersionUID = 20141015L;
36  
37      /** List of cross-section elements. */
38      private final List<CrossSectionElement> crossSectionElementList = new ArrayList<>();
39  
40      /** List of lanes. */
41      private final List<Lane> lanes = new ArrayList<>();
42  
43      /** The policy to generally keep left, keep right, or keep lane. */
44      private final LaneKeepingPolicy laneKeepingPolicy;
45  
46      /** Priority. */
47      // TODO per GTUDirectionality / LongitudinalDirectionality?
48      private Priority priority = Priority.NONE;
49  
50      /** Fraction in range 0...1 to divide origin or destination flow over connectors. */
51      private Double demandWeight = null;
52  
53      /**
54       * The (regular, not timed) event type for pub/sub indicating the addition of a Lane to a CrossSectionLink. <br>
55       * Payload: Object[] { String networkId, String linkId, String LaneId, Lane lane, int laneNumber } <br>
56       * TODO work in a different way with lane numbers to align to standard lane numbering.
57       */
58      public static final EventType LANE_ADD_EVENT = new EventType("LINK.LANE.ADD");
59  
60      /**
61       * The (regular, not timed) event type for pub/sub indicating the removal of a Lane from a CrossSectionLink. <br>
62       * Payload: Object[] { String networkId, String linkId, String LaneId } <br>
63       * TODO allow for the removal of a Lane; currently this is not possible.
64       */
65      public static final EventType LANE_REMOVE_EVENT = new EventType("LINK.LANE.REMOVE");
66  
67      /**
68       * Construction of a cross section link.
69       * @param network RoadNetwork; the network
70       * @param id String; the link id.
71       * @param startNode Node; the start node (directional).
72       * @param endNode Node; the end node (directional).
73       * @param linkType LinkType; the link type
74       * @param designLine OTSLine3D; the design line of the Link
75       * @param simulator OTSSimulatorInterface; the simulator on which events can be scheduled
76       * @param laneKeepingPolicy LaneKeepingPolicy; the policy to generally keep left, keep right, or keep lane
77       * @throws NetworkException if link already exists in the network, if name of the link is not unique, or if the start node
78       *             or the end node of the link are not registered in the network.
79       */
80      @SuppressWarnings("checkstyle:parameternumber")
81      public CrossSectionLink(final RoadNetwork network, final String id, final Node startNode, final Node endNode,
82              final LinkType linkType, final OTSLine3D designLine, final OTSSimulatorInterface simulator,
83              final LaneKeepingPolicy laneKeepingPolicy) throws NetworkException
84      {
85          super(network, id, startNode, endNode, linkType, designLine, simulator);
86          this.laneKeepingPolicy = laneKeepingPolicy;
87      }
88  
89      /**
90       * Clone a CrossSectionLink for a new network.
91       * @param newNetwork Network; the new network to which the clone belongs
92       * @param newSimulator OTSSimulatorInterface; the new simulator for this network
93       * @param link CrossSectionLink; the link to clone from
94       * @throws NetworkException if link already exists in the network, if name of the link is not unique, or if the start node
95       *             or the end node of the link are not registered in the network.
96       */
97      protected CrossSectionLink(final RoadNetwork newNetwork, final OTSSimulatorInterface newSimulator,
98              final CrossSectionLink link) throws NetworkException
99      {
100         super(newNetwork, newSimulator, link);
101         this.laneKeepingPolicy = link.laneKeepingPolicy;
102         for (CrossSectionElement cse : link.crossSectionElementList)
103         {
104             cse.clone(this, newSimulator);
105             // the CrossSectionElement will add itself to the Link (OTS-237)
106         }
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public RoadNetwork getNetwork()
112     {
113         return (RoadNetwork) super.getNetwork();
114     }
115     /**
116      * Add a cross section element at the end of the list. <br>
117      * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction.
118      * @param cse CrossSectionElement; the cross section element to add.
119      */
120     protected final void addCrossSectionElement(final CrossSectionElement cse)
121     {
122         this.crossSectionElementList.add(cse);
123         if (cse instanceof Lane)
124         {
125             this.lanes.add((Lane) cse);
126             fireEvent(LANE_ADD_EVENT,
127                     new Object[] {getNetwork().getId(), getId(), cse.getId(), (Lane) cse, this.lanes.indexOf(cse)});
128         }
129     }
130 
131     /**
132      * Retrieve a safe copy of the cross section element list.
133      * @return List&lt;CrossSectionElement&gt;; the cross section element list.
134      */
135     public final List<CrossSectionElement> getCrossSectionElementList()
136     {
137         return this.crossSectionElementList == null ? new ArrayList<>() : new ArrayList<>(this.crossSectionElementList);
138     }
139 
140     /**
141      * Retrieve the lane keeping policy.
142      * @return LaneKeepingPolicy; the lane keeping policy on this CrossSectionLink
143      */
144     public final LaneKeepingPolicy getLaneKeepingPolicy()
145     {
146         return this.laneKeepingPolicy;
147     }
148 
149     /**
150      * Find a cross section element with a specified id.
151      * @param id String; the id to search for
152      * @return CrossSectionElement; the cross section element with the given id, or null if not found
153      */
154     public final CrossSectionElement getCrossSectionElement(final String id)
155     {
156         for (CrossSectionElement cse : this.crossSectionElementList)
157         {
158             if (cse.getId().equals(id))
159             {
160                 return cse;
161             }
162         }
163         return null;
164     }
165 
166     /**
167      * Return a safe copy of the list of lanes of this CrossSectionLink.
168      * @return List&lt;Lane&gt;; the list of lanes.
169      */
170     public final List<Lane> getLanes()
171     {
172         return this.lanes == null ? new ArrayList<>() : new ArrayList<>(this.lanes);
173     }
174 
175     /**
176      * @return priority.
177      */
178     public final Priority getPriority()
179     {
180         return this.priority;
181     }
182 
183     /**
184      * @param priority Priority; set priority.
185      */
186     public final void setPriority(final Priority priority)
187     {
188         this.priority = priority;
189     }
190 
191     /**
192      * Sets the demand weight. This is only applicable to links of type CONNECTOR.
193      * @param demandWeight double; demand weight, which is any positive value
194      */
195     public final void setDemandWeight(final double demandWeight)
196     {
197         Throw.when(demandWeight < 0.0, IllegalArgumentException.class, "Demand weight should be positive.");
198         Throw.when(!getLinkType().isConnector(), IllegalArgumentException.class,
199                 "Demand weight can only be set on connectors.");
200         this.demandWeight = demandWeight;
201     }
202 
203     /**
204      * Clears the demand weight. This is only applicable to links of type CONNECTOR.
205      */
206     public final void clearDemandWeight()
207     {
208         this.demandWeight = null;
209     }
210 
211     /**
212      * Returns the demand weight. This is only applicable to links of type CONNECTOR.
213      * @return Double; demand weight, any positive value, or {@code null}
214      */
215     public final Double getDemandWeight()
216     {
217         return this.demandWeight;
218     }
219 
220     /** {@inheritDoc} */
221     @Override
222     public final String toString()
223     {
224         return "CrossSectionLink [name=" + this.getId() + ", nodes=" + getStartNode().getId() + "-" + getEndNode().getId()
225                 + ", crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes + ", laneKeepingPolicy="
226                 + this.laneKeepingPolicy + "]";
227     }
228 
229     /** {@inheritDoc} */
230     @Override
231     @SuppressWarnings("checkstyle:designforextension")
232     public CrossSectionLink clone(final Network newNetwork, final OTSSimulatorInterface newSimulator) throws NetworkException
233     {
234         Throw.when(!(newNetwork instanceof RoadNetwork), NetworkException.class,
235                 "CrossSectionLink.clone. newNetwork not of the type Roadnetwork");
236         return new CrossSectionLink((RoadNetwork) newNetwork, newSimulator, this);
237     }
238 
239     /**
240      * Priority of a link.
241      * <p>
242      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
243      * <br>
244      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
245      * <p>
246      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 12 dec. 2016 <br>
247      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
248      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
249      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
250      */
251     public enum Priority
252     {
253         /** Traffic has priority. */
254         PRIORITY,
255 
256         /** No priority. */
257         NONE,
258 
259         /** Turn on red. */
260         TURN_ON_RED,
261 
262         /** Yield. */
263         YIELD,
264 
265         /** Need to stop. */
266         STOP,
267 
268         /** Priority according to all-stop rules. */
269         ALL_STOP,
270 
271         /** Priority at bus stop, i.e. bus has right of way if it wants to leave the bus stop. */
272         BUS_STOP;
273 
274         /**
275          * Returns whether this is priority.
276          * @return whether this is priority
277          */
278         public boolean isPriority()
279         {
280             return this.equals(PRIORITY);
281         }
282 
283         /**
284          * Returns whether this is none.
285          * @return whether this is none
286          */
287         public boolean isNone()
288         {
289             return this.equals(NONE);
290         }
291 
292         /**
293          * Returns whether this is turn on red.
294          * @return whether this is turn on red
295          */
296         public boolean isTurnOnRed()
297         {
298             return this.equals(TURN_ON_RED);
299         }
300 
301         /**
302          * Returns whether this is yield.
303          * @return whether this is yield
304          */
305         public boolean isYield()
306         {
307             return this.equals(YIELD);
308         }
309 
310         /**
311          * Returns whether this is stop.
312          * @return whether this is stop
313          */
314         public boolean isStop()
315         {
316             return this.equals(STOP);
317         }
318 
319         /**
320          * Returns whether this is all-stop.
321          * @return whether this is all-stop
322          */
323         public boolean isAllStop()
324         {
325             return this.equals(ALL_STOP);
326         }
327 
328         /**
329          * Returns whether this is bus stop.
330          * @return whether this is bus stop
331          */
332         public boolean isBusStop()
333         {
334             return this.equals(BUS_STOP);
335         }
336 
337     }
338 
339 }