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
22
23
24
25
26
27
28
29
30
31
32 public class CrossSectionLink extends OTSLink implements Serializable
33 {
34
35 private static final long serialVersionUID = 20141015L;
36
37
38 private final List<CrossSectionElement> crossSectionElementList = new ArrayList<>();
39
40
41 private final List<Lane> lanes = new ArrayList<>();
42
43
44 private final LaneKeepingPolicy laneKeepingPolicy;
45
46
47
48 private Priority priority = Priority.NONE;
49
50
51 private Double demandWeight = null;
52
53
54
55
56
57
58 public static final EventType LANE_ADD_EVENT = new EventType("LINK.LANE.ADD");
59
60
61
62
63
64
65 public static final EventType LANE_REMOVE_EVENT = new EventType("LINK.LANE.REMOVE");
66
67
68
69
70
71
72
73
74
75
76
77
78
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
91
92
93
94
95
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
106 }
107 }
108
109
110 @Override
111 public RoadNetwork getNetwork()
112 {
113 return (RoadNetwork) super.getNetwork();
114 }
115
116
117
118
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
133
134
135 public final List<CrossSectionElement> getCrossSectionElementList()
136 {
137 return this.crossSectionElementList == null ? new ArrayList<>() : new ArrayList<>(this.crossSectionElementList);
138 }
139
140
141
142
143
144 public final LaneKeepingPolicy getLaneKeepingPolicy()
145 {
146 return this.laneKeepingPolicy;
147 }
148
149
150
151
152
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
168
169
170 public final List<Lane> getLanes()
171 {
172 return this.lanes == null ? new ArrayList<>() : new ArrayList<>(this.lanes);
173 }
174
175
176
177
178 public final Priority getPriority()
179 {
180 return this.priority;
181 }
182
183
184
185
186 public final void setPriority(final Priority priority)
187 {
188 this.priority = priority;
189 }
190
191
192
193
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
205
206 public final void clearDemandWeight()
207 {
208 this.demandWeight = null;
209 }
210
211
212
213
214
215 public final Double getDemandWeight()
216 {
217 return this.demandWeight;
218 }
219
220
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
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
241
242
243
244
245
246
247
248
249
250
251 public enum Priority
252 {
253
254 PRIORITY,
255
256
257 NONE,
258
259
260 TURN_ON_RED,
261
262
263 YIELD,
264
265
266 STOP,
267
268
269 ALL_STOP,
270
271
272 BUS_STOP;
273
274
275
276
277
278 public boolean isPriority()
279 {
280 return this.equals(PRIORITY);
281 }
282
283
284
285
286
287 public boolean isNone()
288 {
289 return this.equals(NONE);
290 }
291
292
293
294
295
296 public boolean isTurnOnRed()
297 {
298 return this.equals(TURN_ON_RED);
299 }
300
301
302
303
304
305 public boolean isYield()
306 {
307 return this.equals(YIELD);
308 }
309
310
311
312
313
314 public boolean isStop()
315 {
316 return this.equals(STOP);
317 }
318
319
320
321
322
323 public boolean isAllStop()
324 {
325 return this.equals(ALL_STOP);
326 }
327
328
329
330
331
332 public boolean isBusStop()
333 {
334 return this.equals(BUS_STOP);
335 }
336
337 }
338
339 }