1 package org.opentrafficsim.core.gtu;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.io.Serializable;
9 import java.rmi.RemoteException;
10
11 import javax.naming.NamingException;
12
13 import org.djunits.unit.DurationUnit;
14 import org.djunits.unit.SpeedUnit;
15 import org.djunits.value.vdouble.scalar.Duration;
16 import org.djunits.value.vdouble.scalar.Length;
17 import org.djunits.value.vdouble.scalar.Speed;
18 import org.djunits.value.vdouble.scalar.Time;
19 import org.djutils.immutablecollections.ImmutableMap;
20 import org.djutils.immutablecollections.ImmutableSet;
21 import org.junit.Test;
22 import org.opentrafficsim.base.parameters.ParameterSet;
23 import org.opentrafficsim.base.parameters.Parameters;
24 import org.opentrafficsim.core.dsol.AbstractOTSModel;
25 import org.opentrafficsim.core.dsol.OTSSimulator;
26 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
27 import org.opentrafficsim.core.geometry.Bounds;
28 import org.opentrafficsim.core.geometry.DirectedPoint;
29 import org.opentrafficsim.core.geometry.OTSGeometryException;
30 import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
31 import org.opentrafficsim.core.gtu.plan.strategical.StrategicalPlanner;
32 import org.opentrafficsim.core.gtu.plan.tactical.TacticalPlanner;
33 import org.opentrafficsim.core.idgenerator.IdGenerator;
34 import org.opentrafficsim.core.network.Link;
35 import org.opentrafficsim.core.network.LinkDirection;
36 import org.opentrafficsim.core.network.NetworkException;
37 import org.opentrafficsim.core.network.Node;
38 import org.opentrafficsim.core.network.OTSNetwork;
39 import org.opentrafficsim.core.network.route.Route;
40 import org.opentrafficsim.core.perception.PerceivableContext;
41
42 import nl.tudelft.simulation.dsol.SimRuntimeException;
43
44
45
46
47
48
49
50
51
52
53
54 public class GTUTest
55 {
56
57 public GTU gtuOfStrategicalPlanner = null;
58
59
60 private static final long serialVersionUID = 20151217L;
61
62
63
64
65
66
67
68
69
70
71 @Test
72 public final void testAbstractGTU()
73 throws GTUException, SimRuntimeException, NetworkException, NamingException, RemoteException, OTSGeometryException
74 {
75 TestGTU firstGTU = null;
76 TestGTU lastGTU = null;
77 OTSSimulatorInterface simulator = new OTSSimulator("testAbstractGTU");
78 OTSNetwork perceivableContext = new OTSNetwork("network", true, simulator);
79 GTUModel model = new GTUModel(simulator);
80 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(9999, DurationUnit.SI), model);
81 StrategicalPlanner strategicalPlanner = new StrategicalPlanner()
82 {
83
84 @Override
85 public Node nextNode(final Node node, final Link previousLink, final GTUType gtuType) throws NetworkException
86 {
87 return null;
88 }
89
90 @Override
91 public Node nextNode(final Link link, final GTUDirectionality direction, final GTUType gtuType)
92 throws NetworkException
93 {
94 return null;
95 }
96
97 @Override
98 public LinkDirection nextLinkDirection(final Node node, final Link previousLink, final GTUType gtuType)
99 throws NetworkException
100 {
101 return null;
102 }
103
104 @Override
105 public LinkDirection nextLinkDirection(final Link link, final GTUDirectionality direction, final GTUType gtuType)
106 throws NetworkException
107 {
108 return null;
109 }
110
111 @Override
112 public TacticalPlanner<?, ?> getTacticalPlanner()
113 {
114 return null;
115 }
116
117 @Override
118 public TacticalPlanner<?, ?> getTacticalPlanner(final Time time)
119 {
120 return null;
121 }
122
123 @Override
124 public Route getRoute()
125 {
126 return null;
127 }
128
129 @Override
130 public GTU getGtu()
131 {
132 return GTUTest.this.gtuOfStrategicalPlanner;
133 }
134
135 @Override
136 public Node getOrigin()
137 {
138 return null;
139 }
140
141 @Override
142 public Node getDestination()
143 {
144 return null;
145 }
146
147 };
148 Parameters parameters = new ParameterSet();
149 DirectedPoint initialLocation =
150 new DirectedPoint(10, 20, 30, Math.toRadians(10), Math.toRadians(20), Math.toRadians(30));
151 GTUType gtuType1 = new GTUType("gtu type 1", perceivableContext.getGtuType(GTUType.DEFAULTS.VEHICLE));
152 GTUType gtuType2 = new GTUType("gtu type 2", perceivableContext.getGtuType(GTUType.DEFAULTS.VEHICLE));
153 for (String id : new String[] {"id1", "id2"})
154 {
155 for (GTUType gtuType : new GTUType[] {gtuType1, gtuType2})
156 {
157 String gtuId = id + " " + gtuType.getId();
158 TestGTU gtu = new TestGTU(gtuId, gtuType, simulator, perceivableContext);
159 assertEquals("new GTU has correct id", gtuId, gtu.getId());
160 assertEquals("new GTU has correct GTUType", gtuType, gtu.getGTUType());
161 assertEquals("new GTU has correct reference position", RelativePosition.REFERENCE_POSITION, gtu.getReference());
162 assertEquals("new GTU has correct simulator", simulator, gtu.getSimulator());
163 assertEquals("new GTU has odometer value 0", 0, gtu.getOdometer().si, 0);
164 assertTrue("new GTU is stored in the perceivable context", perceivableContext.getGTUs().contains(gtu));
165 lastGTU = gtu;
166 if (null == firstGTU)
167 {
168 firstGTU = gtu;
169 }
170 }
171 }
172 assertFalse("first GTU and last GTU have different id", firstGTU.getId().equals(lastGTU.getId()));
173 assertFalse("first GTU and last GTU have different GTUType", firstGTU.getGTUType().equals(lastGTU.getGTUType()));
174 TestGTU gtu = new TestGTU("id3", gtuType1, simulator, perceivableContext);
175 assertEquals("perceivable context now contains 5 GTUs", 5, perceivableContext.getGTUs().size());
176 gtu.destroy();
177 assertFalse("perceivable context no longer contains the destroyed GTU", perceivableContext.containsGTU(gtu));
178 try
179 {
180 new TestGTU((String) null, gtuType1, simulator, perceivableContext);
181 fail("null id should have thrown a GTUException");
182 }
183 catch (GTUException ge)
184 {
185
186 }
187
188 try
189 {
190 new TestGTU("IdOfGTU", null, simulator, perceivableContext);
191 fail("null gtuType should have thrown a GTUException");
192 }
193 catch (GTUException ge)
194 {
195
196 }
197
198 try
199 {
200 new TestGTU("IdOfGTU", gtuType1, null, perceivableContext);
201 fail("null simulator should have thrown a GTUException");
202 }
203 catch (GTUException ge)
204 {
205
206 }
207
208 try
209 {
210 new TestGTU("IdOfGTU", gtuType1, simulator, null);
211 fail("null perceivableContext should have thrown a GTUException");
212 }
213 catch (GTUException ge)
214 {
215
216 }
217
218 IdGenerator idGenerator = new IdGenerator("baseName");
219 assertTrue("The toString method returns something descriptive", idGenerator.toString().startsWith("IdGenerator"));
220 int lastBeforeId = Integer.parseInt(idGenerator.nextId().substring(8));
221 gtu = new TestGTU(idGenerator, gtuType1, simulator, perceivableContext);
222 int firstAfterId = Integer.parseInt(idGenerator.nextId().substring(8));
223 assertEquals("Id generator was called once in the constructor", 1 + 1, firstAfterId - lastBeforeId);
224 try
225 {
226 new TestGTU((IdGenerator) null, gtuType1, simulator, perceivableContext);
227 fail("null idGenerator should have thrown a GTUException");
228 }
229 catch (GTUException ge)
230 {
231
232 }
233
234 Speed initialSpeed = new Speed(10, SpeedUnit.KM_PER_HOUR);
235 try
236 {
237 gtu.init(null, initialLocation, initialSpeed);
238 fail("null strategicalPlanner should have thrown a GTUException");
239 }
240 catch (GTUException ge)
241 {
242
243 }
244
245
246 try
247 {
248 gtu.init(strategicalPlanner, null, initialSpeed);
249 fail("null initialLocation should have thrown a NullPointerException");
250 }
251 catch (NullPointerException ne)
252 {
253
254 }
255
256 try
257 {
258 gtu.init(strategicalPlanner, new DirectedPoint(Double.NaN, 20, 30), initialSpeed);
259 fail("null initialSpeed should have thrown an IllegalArgumentException");
260 }
261 catch (IllegalArgumentException ge)
262 {
263
264 }
265
266 try
267 {
268 gtu.init(strategicalPlanner, new DirectedPoint(10, Double.NaN, 30), initialSpeed);
269 fail("null initialSpeed should have thrown an IllegalArgumentException");
270 }
271 catch (IllegalArgumentException ge)
272 {
273
274 }
275
276 try
277 {
278 gtu.init(strategicalPlanner, new DirectedPoint(10, 20, Double.NaN), initialSpeed);
279 fail("null initialSpeed should have thrown an IllegalArgumentException");
280 }
281 catch (IllegalArgumentException ge)
282 {
283
284 }
285
286 try
287 {
288 gtu.init(strategicalPlanner, initialLocation, null);
289 fail("null initialSpeed should have thrown a GTUException");
290 }
291 catch (GTUException ge)
292 {
293
294 }
295
296
297
298 try
299 {
300 gtu.init(strategicalPlanner, initialLocation, initialSpeed);
301 fail("strategicalPlanner that returns a null pointer should have thrown a NullPointerException");
302 }
303 catch (NullPointerException ne)
304 {
305
306 }
307
308 this.gtuOfStrategicalPlanner = firstGTU;
309 try
310 {
311 gtu.init(strategicalPlanner, initialLocation, initialSpeed);
312 fail("wrong strategicalPlanner should have thrown a GTUException");
313 }
314 catch (GTUException ge)
315 {
316
317 }
318
319 this.gtuOfStrategicalPlanner = gtu;
320
321
322 try
323 {
324 gtu.init(strategicalPlanner, initialLocation, initialSpeed);
325 fail("init with fake strategical planner should have caused a GTUExeption in move");
326 }
327 catch (GTUException ne)
328 {
329
330 }
331 }
332
333
334 class GTUModel extends AbstractOTSModel
335 {
336
337 private static final long serialVersionUID = 1L;
338
339
340
341
342 GTUModel(final OTSSimulatorInterface simulator)
343 {
344 super(simulator);
345 }
346
347
348 @Override
349 public void constructModel() throws SimRuntimeException
350 {
351
352 }
353
354
355 @Override
356 public final OTSNetwork getNetwork()
357 {
358 return null;
359 }
360
361
362 @Override
363 public Serializable getSourceId()
364 {
365 return "GTUModel";
366 }
367 }
368
369
370 class TestGTU extends AbstractGTU
371 {
372
373 private static final long serialVersionUID = 20151111L;
374
375
376
377
378
379
380
381
382
383 TestGTU(final String id, final GTUType gtuType, final OTSSimulatorInterface simulator,
384 final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
385 {
386 super(id, gtuType, simulator, perceivableContext);
387 }
388
389
390
391
392
393
394
395
396
397 TestGTU(final IdGenerator idGenerator, final GTUType gtuType, final OTSSimulatorInterface simulator,
398
399 final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
400 {
401 super(idGenerator, gtuType, simulator, perceivableContext);
402 }
403
404
405 @Override
406 public Length getLength()
407 {
408 return null;
409 }
410
411
412 @Override
413 public Length getWidth()
414 {
415 return null;
416 }
417
418
419 @Override
420 public Speed getMaximumSpeed()
421 {
422 return null;
423 }
424
425
426 @Override
427 public RelativePosition getFront()
428 {
429 return null;
430 }
431
432
433 @Override
434 public RelativePosition getRear()
435 {
436 return null;
437 }
438
439
440 @Override
441 public RelativePosition getCenter()
442 {
443 return null;
444 }
445
446
447 @Override
448 public ImmutableMap<TYPE, RelativePosition> getRelativePositions()
449 {
450 return null;
451 }
452
453
454 @Override
455 public Bounds getBounds()
456 {
457 return null;
458 }
459
460
461 @Override
462 public ImmutableSet<RelativePosition> getContourPoints()
463 {
464 return null;
465 }
466 }
467
468 }