1 package org.opentrafficsim.imb.transceiver.urbanstrategy;
2
3 import java.rmi.RemoteException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
8 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
9 import org.opentrafficsim.core.geometry.OTSGeometryException;
10 import org.opentrafficsim.core.geometry.OTSPoint3D;
11 import org.opentrafficsim.core.network.Link;
12 import org.opentrafficsim.core.network.Network;
13 import org.opentrafficsim.core.network.OTSNetwork;
14 import org.opentrafficsim.imb.IMBException;
15 import org.opentrafficsim.imb.connector.Connector;
16 import org.opentrafficsim.imb.connector.Connector.IMBEventType;
17 import org.opentrafficsim.imb.transceiver.AbstractTransceiver;
18 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
19 import org.opentrafficsim.road.network.lane.CrossSectionLink;
20 import org.opentrafficsim.road.network.lane.Lane;
21
22 import nl.tudelft.simulation.event.Event;
23 import nl.tudelft.simulation.event.EventInterface;
24 import nl.tudelft.simulation.event.EventType;
25 import nl.tudelft.simulation.event.TimedEvent;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public class LaneGTUTransceiver extends AbstractTransceiver
203 {
204
205 private static final long serialVersionUID = 20160918L;
206
207
208 private final OTSNetwork network;
209
210
211
212
213
214
215
216
217
218 public LaneGTUTransceiver(final Connector connector, final OTSDEVSSimulatorInterface simulator, final OTSNetwork network)
219 throws IMBException
220 {
221 super("Lane_GTU", connector, simulator);
222 this.network = network;
223
224
225 addListeners();
226 }
227
228
229
230
231
232 private void addListeners() throws IMBException
233 {
234
235 this.network.addListener(this, Network.LINK_ADD_EVENT);
236 this.network.addListener(this, Network.LINK_REMOVE_EVENT);
237
238
239 for (Link link : this.network.getLinkMap().values())
240 {
241 try
242 {
243 this.notify(new TimedEvent<OTSSimTimeDouble>(Network.LINK_ADD_EVENT, this.network, link.getId(),
244 getSimulator().getSimulatorTime()));
245 }
246 catch (RemoteException exception)
247 {
248 throw new IMBException(exception);
249 }
250 }
251 }
252
253
254 @Override
255 public void notify(final EventInterface event) throws RemoteException
256 {
257 EventType type = event.getType();
258 if (type.equals(Network.LINK_ADD_EVENT))
259 {
260 Link link = this.network.getLink((String) event.getContent());
261 if (!(link instanceof CrossSectionLink))
262 {
263 System.err.println("LaneGTUTransceiver.notify(LINK_ADD) - Don't know how to handle a non-CrossSectionLink");
264 return;
265 }
266 CrossSectionLink csl = (CrossSectionLink) link;
267
268 csl.addListener(this, CrossSectionLink.LANE_ADD_EVENT);
269 csl.addListener(this, CrossSectionLink.LANE_REMOVE_EVENT);
270
271
272 for (Lane lane : csl.getLanes())
273 {
274 try
275 {
276 this.notify(new Event(CrossSectionLink.LANE_ADD_EVENT, csl, new Object[] { link.getNetwork().getId(),
277 link.getId(), lane.getId(), lane, csl.getLanes().indexOf(lane) }));
278 }
279 catch (RemoteException exception)
280 {
281 System.err.println("LaneGTUTransceiver.notify(LINK_ADD) - RemoteException: " + exception.getMessage());
282 return;
283 }
284 }
285 }
286
287 else if (type.equals(CrossSectionLink.LANE_ADD_EVENT))
288 {
289 Object[] content = (Object[]) event.getContent();
290 Lane lane = (Lane) content[3];
291
292
293 try
294 {
295 getConnector().postIMBMessage("Lane_GTU", IMBEventType.NEW, transformNew(event));
296 }
297 catch (IMBException exception)
298 {
299 System.err.println("LaneGTUTransceiver.notify(LANE_ADD) - IMBException: " + exception.getMessage());
300 return;
301 }
302
303 lane.addListener(this, Lane.GTU_ADD_EVENT);
304 lane.addListener(this, Lane.GTU_REMOVE_EVENT);
305
306
307 int gtuCount = lane.getGtuList().size();
308 for (LaneBasedGTU gtu : lane.getGtuList())
309 {
310 try
311 {
312 this.notify(new TimedEvent<OTSSimTimeDouble>(Lane.GTU_ADD_EVENT, lane,
313 new Object[] { gtu.getId(), gtu, gtuCount }, getSimulator().getSimulatorTime()));
314 }
315 catch (RemoteException exception)
316 {
317 System.err.println("LaneGTUTransceiver.notify(LANE_ADD) - RemoteException: " + exception.getMessage());
318 return;
319 }
320 }
321 }
322
323 else if (type.equals(Network.LINK_REMOVE_EVENT))
324 {
325 Link link = this.network.getLink((String) event.getContent());
326 if (!(link instanceof CrossSectionLink))
327 {
328 System.err.println("LaneGTUTransceiver.notify(LINK_REMOVE) - Don't know how to handle a non-CrossSectionLink");
329 return;
330 }
331 CrossSectionLink csl = (CrossSectionLink) link;
332
333 csl.removeListener(this, CrossSectionLink.LANE_ADD_EVENT);
334 csl.removeListener(this, CrossSectionLink.LANE_REMOVE_EVENT);
335
336
337 for (Lane lane : csl.getLanes())
338 {
339 try
340 {
341 this.notify(new Event(CrossSectionLink.LANE_REMOVE_EVENT, csl,
342 new Object[] { link.getNetwork().getId(), link.getId(), lane.getId() }));
343 }
344 catch (RemoteException exception)
345 {
346 System.err.println("LaneGTUTransceiver.notify(LINK_REMOVE) - RemoteException: " + exception.getMessage());
347 return;
348 }
349 }
350 }
351
352 else if (type.equals(CrossSectionLink.LANE_REMOVE_EVENT))
353 {
354 Object[] content = (Object[]) event.getContent();
355 String laneId = (String) content[2];
356 CrossSectionLink csl = (CrossSectionLink) event.getSource();
357 Lane lane = (Lane) csl.getCrossSectionElement(laneId);
358
359 lane.removeListener(this, Lane.GTU_ADD_EVENT);
360 lane.removeListener(this, Lane.GTU_REMOVE_EVENT);
361
362
363 try
364 {
365 getConnector().postIMBMessage("Lane_GTU", IMBEventType.DELETE, transformDelete(event));
366 }
367 catch (IMBException exception)
368 {
369 System.err.println("LaneGTUTransceiver.notify(LANE_REMOVE) - IMBException: " + exception.getMessage());
370 return;
371 }
372 }
373
374 else if (type.equals(Lane.GTU_ADD_EVENT) || type.equals(Lane.GTU_REMOVE_EVENT))
375 {
376 try
377 {
378 getConnector().postIMBMessage("Lane_GTU", IMBEventType.CHANGE, transformChange(event));
379 }
380 catch (IMBException exception)
381 {
382 System.err.println("LaneGTUTransceiver.notify CHANGE - IMBException: " + exception.getMessage());
383 return;
384 }
385 }
386
387 else
388 {
389 System.err.println("LaneGTUTransceiver.notify - Unhandled event: " + event);
390 }
391 }
392
393
394
395
396
397
398 public Object[] transformNew(final EventInterface event)
399 {
400 if (CrossSectionLink.LANE_ADD_EVENT.equals(event.getType()))
401 {
402 Object[] content = (Object[]) event.getContent();
403 Lane lane = (Lane) content[3];
404 int laneNumber = (Integer) content[4];
405 double timestamp = getSimulator().getSimulatorTime().getTime().si;
406 List<Object> resultList = new ArrayList<>();
407 resultList.add(timestamp);
408 resultList.add(this.network.getId());
409 resultList.add(lane.getParentLink().getId());
410 resultList.add(lane.getId());
411 resultList.add(laneNumber);
412 resultList.add(lane.getCenterLine().size());
413 for (int i = 0; i < lane.getCenterLine().size(); i++)
414 {
415 try
416 {
417 OTSPoint3D p = lane.getCenterLine().get(i);
418 resultList.add(p.x);
419 resultList.add(p.y);
420 resultList.add(p.z);
421 }
422 catch (OTSGeometryException exception)
423 {
424 exception.printStackTrace();
425 resultList.add(0.0d);
426 resultList.add(0.0d);
427 resultList.add(0.0d);
428 }
429 }
430 return resultList.toArray();
431 }
432 System.err.println("LaneGTUTransceiver.transformNew: Don't know how to transform event " + event);
433 return new Object[] {};
434 }
435
436
437
438
439
440
441 public Object[] transformChange(final EventInterface event)
442 {
443 Object[] gtuInfo = (Object[]) event.getContent();
444 String gtuId = (String) gtuInfo[0];
445 int countAfterEvent = (Integer) gtuInfo[2];
446 Lane lane = (Lane) event.getSource();
447 double timestamp = getSimulator().getSimulatorTime().getTime().si;
448 if (Lane.GTU_ADD_EVENT.equals(event.getType()))
449 {
450 return new Object[] { timestamp, this.network.getId(), lane.getParentLink().getId(), lane.getId(), true, gtuId,
451 countAfterEvent };
452 }
453 else if (Lane.GTU_REMOVE_EVENT.equals(event.getType()))
454 {
455 return new Object[] { timestamp, this.network.getId(), lane.getParentLink().getId(), lane.getId(), false, gtuId,
456 countAfterEvent };
457 }
458 System.err.println("LaneGTUTransceiver.transformChange: Don't know how to transform event " + event);
459 return new Object[] {};
460 }
461
462
463
464
465
466
467 public Object[] transformDelete(final EventInterface event)
468 {
469 if (CrossSectionLink.LANE_REMOVE_EVENT.equals(event.getType()))
470 {
471 Object[] content = (Object[]) event.getContent();
472 String networkId = (String) content[0];
473 String linkId = (String) content[1];
474 String laneId = (String) content[2];
475 double timestamp = getSimulator().getSimulatorTime().getTime().si;
476 return new Object[] { timestamp, networkId, linkId, laneId };
477 }
478 System.err.println("LaneGTUTransceiver.transformDelete: Don't know how to transform event " + event);
479 return new Object[] {};
480 }
481
482 }