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
21
22
23
24
25
26
27
28
29
30
31 public class CrossSectionLink extends OTSLink implements Serializable
32 {
33
34 private static final long serialVersionUID = 20141015L;
35
36
37 private final List<CrossSectionElement> crossSectionElementList = new ArrayList<>();
38
39
40 private final List<Lane> lanes = new ArrayList<>();
41
42
43 private final LaneKeepingPolicy laneKeepingPolicy;
44
45
46
47 private Priority priority = Priority.NONE;
48
49
50 private Double demandWeight = null;
51
52
53
54
55
56
57 public static final EventType LANE_ADD_EVENT = new EventType("LINK.LANE.ADD");
58
59
60
61
62
63
64 public static final EventType LANE_REMOVE_EVENT = new EventType("LINK.LANE.REMOVE");
65
66
67
68
69
70
71
72
73
74
75
76
77
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
90
91
92
93
94
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
105 }
106 }
107
108
109
110
111
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
126
127
128 public final List<CrossSectionElement> getCrossSectionElementList()
129 {
130 return this.crossSectionElementList == null ? new ArrayList<>() : new ArrayList<>(this.crossSectionElementList);
131 }
132
133
134
135
136
137 public final LaneKeepingPolicy getLaneKeepingPolicy()
138 {
139 return this.laneKeepingPolicy;
140 }
141
142
143
144
145
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
161
162
163 public final List<Lane> getLanes()
164 {
165 return this.lanes == null ? new ArrayList<>() : new ArrayList<>(this.lanes);
166 }
167
168
169
170
171 public final Priority getPriority()
172 {
173 return this.priority;
174 }
175
176
177
178
179 public final void setPriority(final Priority priority)
180 {
181 this.priority = priority;
182 }
183
184
185
186
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
198
199 public final void clearDemandWeight()
200 {
201 this.demandWeight = null;
202 }
203
204
205
206
207
208 public final Double getDemandWeight()
209 {
210 return this.demandWeight;
211 }
212
213
214 @Override
215 public final String toString()
216 {
217 return "CrossSectionLink [crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes
218 + ", laneKeepingPolicy=" + this.laneKeepingPolicy + "]";
219 }
220
221
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
231
232
233
234
235
236
237
238
239
240
241 public enum Priority
242 {
243
244 PRIORITY,
245
246
247 NONE,
248
249
250 TURN_ON_RED,
251
252
253 YIELD,
254
255
256 STOP,
257
258
259 ALL_STOP,
260
261
262 BUS_STOP;
263
264
265
266
267
268 public boolean isPriority()
269 {
270 return this.equals(PRIORITY);
271 }
272
273
274
275
276
277 public boolean isNone()
278 {
279 return this.equals(NONE);
280 }
281
282
283
284
285
286 public boolean isTurnOnRed()
287 {
288 return this.equals(TURN_ON_RED);
289 }
290
291
292
293
294
295 public boolean isYield()
296 {
297 return this.equals(YIELD);
298 }
299
300
301
302
303
304 public boolean isStop()
305 {
306 return this.equals(STOP);
307 }
308
309
310
311
312
313 public boolean isAllStop()
314 {
315 return this.equals(ALL_STOP);
316 }
317
318
319
320
321
322 public boolean isBusStop()
323 {
324 return this.equals(BUS_STOP);
325 }
326
327 }
328
329 }