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.opentrafficsim.core.geometry.OTSLine3D;
8 import org.opentrafficsim.core.network.LinkType;
9 import org.opentrafficsim.core.network.Network;
10 import org.opentrafficsim.core.network.NetworkException;
11 import org.opentrafficsim.core.network.Node;
12 import org.opentrafficsim.core.network.OTSLink;
13 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
14
15 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
16 import nl.tudelft.simulation.event.EventType;
17 import nl.tudelft.simulation.language.Throw;
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 SimulatorInterface.TimeDoubleUnit 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
97 protected CrossSectionLink(final Network newNetwork, final SimulatorInterface.TimeDoubleUnit newSimulator, final boolean animation,
98 final CrossSectionLink link) throws NetworkException
99 {
100 super(newNetwork, newSimulator, animation, link);
101 this.laneKeepingPolicy = link.laneKeepingPolicy;
102 for (CrossSectionElement cse : link.crossSectionElementList)
103 {
104 cse.clone(this, newSimulator, animation);
105
106 }
107 }
108
109
110
111
112
113
114 protected final void addCrossSectionElement(final CrossSectionElement cse)
115 {
116 this.crossSectionElementList.add(cse);
117 if (cse instanceof Lane)
118 {
119 this.lanes.add((Lane) cse);
120 fireEvent(LANE_ADD_EVENT,
121 new Object[] { getNetwork().getId(), getId(), cse.getId(), (Lane) cse, this.lanes.indexOf(cse) });
122 }
123 }
124
125
126
127
128
129 public final List<CrossSectionElement> getCrossSectionElementList()
130 {
131 return this.crossSectionElementList == null ? new ArrayList<>() : new ArrayList<>(this.crossSectionElementList);
132 }
133
134
135
136
137
138 public final LaneKeepingPolicy getLaneKeepingPolicy()
139 {
140 return this.laneKeepingPolicy;
141 }
142
143
144
145
146
147
148 public final CrossSectionElement getCrossSectionElement(final String id)
149 {
150 for (CrossSectionElement cse : this.crossSectionElementList)
151 {
152 if (cse.getId().equals(id))
153 {
154 return cse;
155 }
156 }
157 return null;
158 }
159
160
161
162
163
164 public final List<Lane> getLanes()
165 {
166 return this.lanes == null ? new ArrayList<>() : new ArrayList<>(this.lanes);
167 }
168
169
170
171
172 public final Priority getPriority()
173 {
174 return this.priority;
175 }
176
177
178
179
180 public final void setPriority(final Priority priority)
181 {
182 this.priority = priority;
183 }
184
185
186
187
188
189 public final void setDemandWeight(final double demandWeight)
190 {
191 Throw.when(demandWeight < 0.0, IllegalArgumentException.class, "Demand weight should be positive.");
192 Throw.when(!getLinkType().isConnector(), IllegalArgumentException.class,
193 "Demand weight can only be set on connectors.");
194 this.demandWeight = demandWeight;
195 }
196
197
198
199
200 public final void clearDemandWeight()
201 {
202 this.demandWeight = null;
203 }
204
205
206
207
208
209 public final Double getDemandWeight()
210 {
211 return this.demandWeight;
212 }
213
214
215 @Override
216 public final String toString()
217 {
218 return "CrossSectionLink [crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes
219 + ", laneKeepingPolicy=" + this.laneKeepingPolicy + "]";
220 }
221
222
223 @Override
224 @SuppressWarnings("checkstyle:designforextension")
225 public CrossSectionLink clone(final Network newNetwork, final SimulatorInterface.TimeDoubleUnit newSimulator, final boolean animation)
226 throws NetworkException
227 {
228 return new CrossSectionLink(newNetwork, newSimulator, animation, this);
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243 public enum Priority
244 {
245
246 PRIORITY,
247
248
249 NONE,
250
251
252 TURN_ON_RED,
253
254
255 YIELD,
256
257
258 STOP,
259
260
261 ALL_STOP,
262
263
264 BUS_STOP;
265
266
267
268
269
270 public boolean isPriority()
271 {
272 return this.equals(PRIORITY);
273 }
274
275
276
277
278
279 public boolean isNone()
280 {
281 return this.equals(NONE);
282 }
283
284
285
286
287
288 public boolean isTurnOnRed()
289 {
290 return this.equals(TURN_ON_RED);
291 }
292
293
294
295
296
297 public boolean isYield()
298 {
299 return this.equals(YIELD);
300 }
301
302
303
304
305
306 public boolean isStop()
307 {
308 return this.equals(STOP);
309 }
310
311
312
313
314
315 public boolean isAllStop()
316 {
317 return this.equals(ALL_STOP);
318 }
319
320
321
322
323
324 public boolean isBusStop()
325 {
326 return this.equals(BUS_STOP);
327 }
328
329 }
330
331 }