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