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.gtu.GTU;
12 import org.opentrafficsim.core.network.Link;
13 import org.opentrafficsim.core.network.Network;
14 import org.opentrafficsim.core.network.OTSNetwork;
15 import org.opentrafficsim.imb.IMBException;
16 import org.opentrafficsim.imb.connector.Connector;
17 import org.opentrafficsim.imb.connector.Connector.IMBEventType;
18 import org.opentrafficsim.imb.transceiver.AbstractTransceiver;
19 import org.opentrafficsim.road.network.lane.CrossSectionLink;
20
21 import nl.tudelft.simulation.event.EventInterface;
22 import nl.tudelft.simulation.event.EventType;
23 import nl.tudelft.simulation.event.TimedEvent;
24
25
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 public class LinkGTUTransceiver extends AbstractTransceiver
191 {
192
193 private static final long serialVersionUID = 20160913L;
194
195
196 private final OTSNetwork network;
197
198
199
200
201
202
203
204
205
206 public LinkGTUTransceiver(final Connector connector, final OTSDEVSSimulatorInterface simulator, final OTSNetwork network)
207 throws IMBException
208 {
209 super("Link_GTU", connector, simulator);
210 this.network = network;
211
212
213 addListeners();
214 }
215
216
217
218
219
220 private void addListeners() throws IMBException
221 {
222
223 this.network.addListener(this, Network.LINK_ADD_EVENT);
224 this.network.addListener(this, Network.LINK_REMOVE_EVENT);
225
226
227 for (Link link : this.network.getLinkMap().values())
228 {
229 try
230 {
231 this.notify(new TimedEvent<OTSSimTimeDouble>(Network.LINK_ADD_EVENT, this.network, link.getId(),
232 getSimulator().getSimulatorTime()));
233 }
234 catch (RemoteException exception)
235 {
236 throw new IMBException(exception);
237 }
238 }
239 }
240
241
242 @Override
243 public void notify(final EventInterface event) throws RemoteException
244 {
245 EventType type = event.getType();
246 if (type.equals(Network.LINK_ADD_EVENT))
247 {
248 Link link = this.network.getLink((String) event.getContent());
249 if (!(link instanceof CrossSectionLink))
250 {
251 System.err.println("LinkGTUTransceiver.notify NEW - Don't know how to handle a non-CrossSectionLink");
252 return;
253 }
254 CrossSectionLink csl = (CrossSectionLink) link;
255
256
257 try
258 {
259 getConnector().postIMBMessage("Link_GTU", IMBEventType.NEW, transformNew(event));
260 }
261 catch (IMBException exception)
262 {
263 System.err.println("LinkGTUTransceiver.notify NEW - IMBException: " + exception.getMessage());
264 return;
265 }
266
267 csl.addListener(this, Link.GTU_ADD_EVENT);
268 csl.addListener(this, Link.GTU_REMOVE_EVENT);
269
270
271 int gtuCount = link.getGTUs().size();
272 for (GTU gtu : link.getGTUs())
273 {
274 try
275 {
276 this.notify(new TimedEvent<OTSSimTimeDouble>(Link.GTU_ADD_EVENT, link,
277 new Object[] { gtu.getId(), gtu, gtuCount }, getSimulator().getSimulatorTime()));
278 }
279 catch (RemoteException exception)
280 {
281 System.err.println("LinkGTUTransceiver.notify NEW - RemoteException: " + exception.getMessage());
282 return;
283 }
284 }
285 }
286
287 else if (type.equals(Network.LINK_REMOVE_EVENT))
288 {
289 Link link = this.network.getLink((String) event.getContent());
290 if (!(link instanceof CrossSectionLink))
291 {
292 System.err.println("LinkGTUTransceiver.notify DELETE - Don't know how to handle a non-CrossSectionLink");
293 return;
294 }
295 CrossSectionLink csl = (CrossSectionLink) link;
296 csl.removeListener(this, Link.GTU_ADD_EVENT);
297 csl.removeListener(this, Link.GTU_REMOVE_EVENT);
298
299
300 try
301 {
302 getConnector().postIMBMessage("Link_GTU", IMBEventType.DELETE, transformDelete(event));
303 }
304 catch (IMBException exception)
305 {
306 System.err.println("LinkGTUTransceiver.notify DELETE - IMBException: " + exception.getMessage());
307 return;
308 }
309 }
310
311 else if (type.equals(Link.GTU_ADD_EVENT) || type.equals(Link.GTU_REMOVE_EVENT))
312 {
313 try
314 {
315 getConnector().postIMBMessage("Link_GTU", IMBEventType.CHANGE, transformChange(event));
316 }
317 catch (IMBException exception)
318 {
319 System.err.println("LinkGTUTransceiver.notify CHANGE - IMBException: " + exception.getMessage());
320 return;
321 }
322 }
323
324 else
325 {
326 System.err.println("LinkGTUTransceiver.notify - Unhandled event: " + event);
327 }
328 }
329
330
331
332
333
334
335 public Object[] transformNew(final EventInterface event)
336 {
337 if (Network.LINK_ADD_EVENT.equals(event.getType()))
338 {
339 String linkId = (String) event.getContent();
340 Link link = this.network.getLink(linkId);
341 double timestamp = getSimulator().getSimulatorTime().getTime().si;
342 List<Object> resultList = new ArrayList<>();
343 resultList.add(timestamp);
344 resultList.add(this.network.getId());
345 resultList.add(linkId);
346 resultList.add(link.getStartNode().getId());
347 resultList.add(link.getEndNode().getId());
348 resultList.add(link.getDesignLine().size());
349 for (int i = 0; i < link.getDesignLine().size(); i++)
350 {
351 try
352 {
353 OTSPoint3D p = link.getDesignLine().get(i);
354 resultList.add(p.x);
355 resultList.add(p.y);
356 resultList.add(p.z);
357 }
358 catch (OTSGeometryException exception)
359 {
360 exception.printStackTrace();
361 resultList.add(0.0d);
362 resultList.add(0.0d);
363 resultList.add(0.0d);
364 }
365 }
366 return resultList.toArray();
367 }
368 System.err.println("LinkGTUTransceiver.transformNew: Don't know how to transform event " + event);
369 return new Object[] {};
370 }
371
372
373
374
375
376
377 public Object[] transformChange(final EventInterface event)
378 {
379 Object[] gtuInfo = (Object[]) event.getContent();
380 String gtuId = (String) gtuInfo[0];
381 int countAfterEvent = (Integer) gtuInfo[2];
382 Link link = (Link) event.getSource();
383 double timestamp = getSimulator().getSimulatorTime().getTime().si;
384 if (Link.GTU_ADD_EVENT.equals(event.getType()))
385 {
386 return new Object[] { timestamp, link.getNetwork().getId(), link.getId(), true, gtuId, countAfterEvent };
387 }
388 else if (Link.GTU_REMOVE_EVENT.equals(event.getType()))
389 {
390 return new Object[] { timestamp, link.getNetwork().getId(), link.getId(), false, gtuId, countAfterEvent };
391 }
392 System.err.println("LinkGTUTransceiver.transformChange: Don't know how to transform event " + event);
393 return new Object[] {};
394 }
395
396
397
398
399
400
401 public Object[] transformDelete(final EventInterface event)
402 {
403 if (Network.LINK_REMOVE_EVENT.equals(event.getType()))
404 {
405 String linkId = (String) event.getContent();
406 double timestamp = getSimulator().getSimulatorTime().getTime().si;
407 return new Object[] { timestamp, this.network.getId(), linkId };
408 }
409 System.err.println("LinkGTUTransceiver.transformDelete: Don't know how to transform event " + event);
410 return new Object[] {};
411 }
412
413 }