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<CrossSectionElement>; 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<Lane>; 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 [name=" + this.getId() + ", nodes=" + getStartNode().getId() + "-" + getEndNode().getId()
218 + ", crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes + ", laneKeepingPolicy="
219 + this.laneKeepingPolicy + "]";
220 }
221
222 /** {@inheritDoc} */
223 @Override
224 @SuppressWarnings("checkstyle:designforextension")
225 public CrossSectionLink clone(final Network newNetwork, final OTSSimulatorInterface newSimulator) throws NetworkException
226 {
227 return new CrossSectionLink(newNetwork, newSimulator, this);
228 }
229
230 /**
231 * Priority of a link.
232 * <p>
233 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
234 * <br>
235 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
236 * <p>
237 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 12 dec. 2016 <br>
238 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
239 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
240 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
241 */
242 public enum Priority
243 {
244 /** Traffic has priority. */
245 PRIORITY,
246
247 /** No priority. */
248 NONE,
249
250 /** Turn on red. */
251 TURN_ON_RED,
252
253 /** Yield. */
254 YIELD,
255
256 /** Need to stop. */
257 STOP,
258
259 /** Priority according to all-stop rules. */
260 ALL_STOP,
261
262 /** Priority at bus stop, i.e. bus has right of way if it wants to leave the bus stop. */
263 BUS_STOP;
264
265 /**
266 * Returns whether this is priority.
267 * @return whether this is priority
268 */
269 public boolean isPriority()
270 {
271 return this.equals(PRIORITY);
272 }
273
274 /**
275 * Returns whether this is none.
276 * @return whether this is none
277 */
278 public boolean isNone()
279 {
280 return this.equals(NONE);
281 }
282
283 /**
284 * Returns whether this is turn on red.
285 * @return whether this is turn on red
286 */
287 public boolean isTurnOnRed()
288 {
289 return this.equals(TURN_ON_RED);
290 }
291
292 /**
293 * Returns whether this is yield.
294 * @return whether this is yield
295 */
296 public boolean isYield()
297 {
298 return this.equals(YIELD);
299 }
300
301 /**
302 * Returns whether this is stop.
303 * @return whether this is stop
304 */
305 public boolean isStop()
306 {
307 return this.equals(STOP);
308 }
309
310 /**
311 * Returns whether this is all-stop.
312 * @return whether this is all-stop
313 */
314 public boolean isAllStop()
315 {
316 return this.equals(ALL_STOP);
317 }
318
319 /**
320 * Returns whether this is bus stop.
321 * @return whether this is bus stop
322 */
323 public boolean isBusStop()
324 {
325 return this.equals(BUS_STOP);
326 }
327
328 }
329
330 }