View Javadoc
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   import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
8   
9   import java.rmi.RemoteException;
10  
11  import javax.media.j3d.Bounds;
12  import javax.naming.NamingException;
13  
14  import org.djunits.unit.DurationUnit;
15  import org.djunits.unit.SpeedUnit;
16  import org.djunits.unit.TimeUnit;
17  import org.djunits.value.vdouble.scalar.Duration;
18  import org.djunits.value.vdouble.scalar.Length;
19  import org.djunits.value.vdouble.scalar.Speed;
20  import org.djunits.value.vdouble.scalar.Time;
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.OTSModelInterface;
25  import org.opentrafficsim.core.geometry.OTSGeometryException;
26  import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
27  import org.opentrafficsim.core.gtu.plan.strategical.StrategicalPlanner;
28  import org.opentrafficsim.core.gtu.plan.tactical.TacticalPlanner;
29  import org.opentrafficsim.core.idgenerator.IdGenerator;
30  import org.opentrafficsim.core.network.Link;
31  import org.opentrafficsim.core.network.LinkDirection;
32  import org.opentrafficsim.core.network.NetworkException;
33  import org.opentrafficsim.core.network.Node;
34  import org.opentrafficsim.core.network.OTSNetwork;
35  import org.opentrafficsim.core.network.route.Route;
36  import org.opentrafficsim.core.perception.PerceivableContext;
37  import org.opentrafficsim.simulationengine.SimpleSimulator;
38  
39  import nl.tudelft.simulation.dsol.SimRuntimeException;
40  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
41  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
42  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
43  import nl.tudelft.simulation.immutablecollections.ImmutableMap;
44  import nl.tudelft.simulation.immutablecollections.ImmutableSet;
45  import nl.tudelft.simulation.language.d3.DirectedPoint;
46  
47  /**
48   * Test the AbstractGTU class.
49   * <p>
50   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
51   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
52   * <p>
53   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Nov 11, 2015 <br>
54   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
55   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
56   * @param <S> strategical planner
57   * @param <T> tactical planner
58   */
59  public class GTUTest<S extends StrategicalPlanner, T extends TacticalPlanner> implements OTSModelInterface
60  
61  {
62  
63      /** GTU that will be returned when the fake strategical planner is asked for the associated GTU with getGTU. */
64      public GTU gtuOfStrategicalPlanner = null;
65  
66      /** */
67      private static final long serialVersionUID = 20151217L;
68  
69      /**
70       * Test the constructor.
71       * @throws GTUException should not happen uncaught; if it does the test has failed
72       * @throws NetworkException should not happen uncaught; if it does the test has failed
73       * @throws SimRuntimeException should not happen uncaught; if it does the test has failed
74       * @throws NamingException should not happen uncaught; if it does the test has failed
75       * @throws RemoteException should not happen uncaught; if it does the test has failed
76       * @throws OTSGeometryException should not happen uncaught; if it does the test has failed
77       */
78      @Test
79      public final void testAbstractGTU()
80              throws GTUException, SimRuntimeException, NetworkException, NamingException, RemoteException, OTSGeometryException
81      {
82          TestGTU firstGTU = null;
83          TestGTU lastGTU = null;
84          OTSNetwork perceivableContext = new OTSNetwork("network");
85          DEVSSimulatorInterface.TimeDoubleUnit simulator = new SimpleSimulator(new Time(0, TimeUnit.BASE),
86                  new Duration(0, DurationUnit.SI), new Duration(9999, DurationUnit.SI), this);
87          StrategicalPlanner strategicalPlanner = new StrategicalPlanner()
88          {
89  
90              @Override
91              public Node nextNode(final Node node, final Link previousLink, final GTUType gtuType) throws NetworkException
92              {
93                  return null;
94              }
95  
96              @Override
97              public Node nextNode(final Link link, final GTUDirectionality direction, final GTUType gtuType)
98                      throws NetworkException
99              {
100                 return null;
101             }
102 
103             @Override
104             public LinkDirection nextLinkDirection(final Node node, final Link previousLink, final GTUType gtuType)
105                     throws NetworkException
106             {
107                 return null;
108             }
109 
110             @Override
111             public LinkDirection nextLinkDirection(final Link link, final GTUDirectionality direction, final GTUType gtuType)
112                     throws NetworkException
113             {
114                 return null;
115             }
116 
117             @Override
118             public TacticalPlanner getTacticalPlanner()
119             {
120                 return null;
121             }
122 
123             @Override
124             public TacticalPlanner getTacticalPlanner(Time time)
125             {
126                 return null;
127             }
128 
129             @Override
130             public Route getRoute()
131             {
132                 return null;
133             }
134 
135             @Override
136             public GTU getGtu()
137             {
138                 return GTUTest.this.gtuOfStrategicalPlanner;
139             }
140 
141             @Override
142             public Node getOrigin()
143             {
144                 return null;
145             }
146 
147             @Override
148             public Node getDestination()
149             {
150                 return null;
151             }
152 
153         };
154         Parameters parameters = new ParameterSet();
155         DirectedPoint initialLocation =
156                 new DirectedPoint(10, 20, 30, Math.toRadians(10), Math.toRadians(20), Math.toRadians(30));
157         GTUType gtuType1 = new GTUType("gtu type 1", VEHICLE);
158         GTUType gtuType2 = new GTUType("gtu type 2", VEHICLE);
159         for (String id : new String[] { "id1", "id2" })
160         {
161             for (GTUType gtuType : new GTUType[] { gtuType1, gtuType2 })
162             {
163                 String gtuId = id + " " + gtuType.getId();
164                 TestGTU gtu = new TestGTU(gtuId, gtuType, simulator, perceivableContext);
165                 assertEquals("new GTU has correct id", gtuId, gtu.getId());
166                 assertEquals("new GTU has correct GTUType", gtuType, gtu.getGTUType());
167                 assertEquals("new GTU has correct reference position", RelativePosition.REFERENCE_POSITION, gtu.getReference());
168                 assertEquals("new GTU has correct simulator", simulator, gtu.getSimulator());
169                 assertEquals("new GTU has odometer value 0", 0, gtu.getOdometer().si, 0);
170                 assertTrue("new GTU is stored in the perceivable context", perceivableContext.getGTUs().contains(gtu));
171                 lastGTU = gtu;
172                 if (null == firstGTU)
173                 {
174                     firstGTU = gtu;
175                 }
176             }
177         }
178         assertFalse("first GTU and last GTU have different id", firstGTU.getId().equals(lastGTU.getId()));
179         assertFalse("first GTU and last GTU have different GTUType", firstGTU.getGTUType().equals(lastGTU.getGTUType()));
180         TestGTU gtu = new TestGTU("id3", gtuType1, simulator, perceivableContext);
181         assertEquals("perceivable context now contains 5 GTUs", 5, perceivableContext.getGTUs().size());
182         gtu.destroy();
183         assertFalse("perceivable context no longer contains the destroyed GTU", perceivableContext.containsGTU(gtu));
184         try
185         {
186             new TestGTU((String) null, gtuType1, simulator, perceivableContext);
187             fail("null id should have thrown a GTUException");
188         }
189         catch (GTUException ge)
190         {
191             // Ignore expected exception
192         }
193         try
194         {
195             new TestGTU("IdOfGTU", null, simulator, perceivableContext);
196             fail("null gtuType should have thrown a GTUException");
197         }
198         catch (GTUException ge)
199         {
200             // Ignore expected exception
201         }
202         try
203         {
204             new TestGTU("IdOfGTU", gtuType1, null, perceivableContext);
205             fail("null simulator should have thrown a GTUException");
206         }
207         catch (GTUException ge)
208         {
209             // Ignore expected exception
210         }
211         try
212         {
213             new TestGTU("IdOfGTU", gtuType1, simulator, null);
214             fail("null perceivableContext should have thrown a GTUException");
215         }
216         catch (GTUException ge)
217         {
218             // Ignore expected exception
219         }
220         IdGenerator idGenerator = new IdGenerator("baseName");
221         int lastBeforeId = Integer.parseInt(idGenerator.nextId().substring(8));
222         gtu = new TestGTU(idGenerator, gtuType1, simulator, perceivableContext);
223         int firstAfterId = Integer.parseInt(idGenerator.nextId().substring(8));
224         assertEquals("Id generator was called once in the constructor", 1 + 1, firstAfterId - lastBeforeId);
225         try
226         {
227             new TestGTU((IdGenerator) null, gtuType1, simulator, perceivableContext);
228             fail("null idGenerator should have thrown a GTUException");
229         }
230         catch (GTUException ge)
231         {
232             // Ignore expected exception
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             // Ignore expected exception
243         }
244         // FIXME should the next one not ge a GTUException?
245         try
246         {
247             gtu.init(strategicalPlanner, null, initialSpeed);
248             fail("null initialLocation should have thrown a NullPointerException");
249         }
250         catch (NullPointerException ne)
251         {
252             // Ignore expected exception
253         }
254         try
255         {
256             gtu.init(strategicalPlanner, new DirectedPoint(Double.NaN, 20, 30), initialSpeed);
257             fail("null initialSpeed should have thrown a GTUException");
258         }
259         catch (GTUException ge)
260         {
261             // Ignore expected exception
262         }
263         try
264         {
265             gtu.init(strategicalPlanner, new DirectedPoint(10, Double.NaN, 30), initialSpeed);
266             fail("null initialSpeed should have thrown a GTUException");
267         }
268         catch (GTUException ge)
269         {
270             // Ignore expected exception
271         }
272         try
273         {
274             gtu.init(strategicalPlanner, new DirectedPoint(10, 20, Double.NaN), initialSpeed);
275             fail("null initialSpeed should have thrown a GTUException");
276         }
277         catch (GTUException ge)
278         {
279             // Ignore expected exception
280         }
281         try
282         {
283             gtu.init(strategicalPlanner, initialLocation, null);
284             fail("null initialSpeed should have thrown a GTUException");
285         }
286         catch (GTUException ge)
287         {
288             // Ignore expected exception
289         }
290         // The null pointer returned by the strategical planner will cause a NullPointerException
291         // FIXME should probably explicitly throw an exception for a misbehaving strategical planner
292         try
293         {
294             gtu.init(strategicalPlanner, initialLocation, initialSpeed);
295             fail("strategicalPlanner that returns a null pointer should have thrown a NullPointerException");
296         }
297         catch (NullPointerException ne)
298         {
299             // Ignore expected exception
300         }
301         this.gtuOfStrategicalPlanner = firstGTU;
302         try
303         {
304             gtu.init(strategicalPlanner, initialLocation, initialSpeed);
305             fail("wrong strategicalPlanner should have thrown a GTUException");
306         }
307         catch (GTUException ge)
308         {
309             // Ignore expected exception
310         }
311         this.gtuOfStrategicalPlanner = gtu;
312         // FIXME should the AbstractGTU not complain more directly about a null returned by
313         // strategicalPlanner.generateTacticalPlanner()?
314         try
315         {
316             gtu.init(strategicalPlanner, initialLocation, initialSpeed);
317             fail("init with fake strategical planner should have caused a NullPointerExeption in move");
318         }
319         catch (NullPointerException ne)
320         {
321             // Ignore expected exception
322         }
323     }
324 
325     /** {@inheritDoc} */
326     @Override
327     public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> simulator) throws SimRuntimeException
328     {
329         // Not used
330     }
331 
332     /** {@inheritDoc} */
333     @Override
334     public final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
335     {
336         return null;
337     }
338 
339     /** {@inheritDoc} */
340     @Override
341     public final OTSNetwork getNetwork()
342     {
343         return null;
344     }
345 
346 }
347 
348 /**
349  * ...
350  */
351 class TestGTU extends AbstractGTU
352 {
353     /** */
354     private static final long serialVersionUID = 20151111L;
355 
356     /**
357      * @param id String; id of the new GTU
358      * @param gtuType GTUType; type of the new GTU
359      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; simulator that controls the new GTU
360      * @param perceivableContext PerceivableContext; the perceivable context of the new GTU
361      * @throws SimRuntimeException when something goes wrong in the scheduling of the first move event
362      * @throws GTUException when something goes wrong during GTU instantiation
363      */
364     TestGTU(final String id, final GTUType gtuType, final DEVSSimulatorInterface.TimeDoubleUnit simulator,
365 
366             final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
367     {
368         super(id, gtuType, simulator, perceivableContext);
369     }
370 
371     /**
372      * @param idGenerator IdGenerator; id generator that will generate the id of the new GTU
373      * @param gtuType GTUType; type of the new GTU
374      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; simulator that controls the new GTU
375      * @param perceivableContext PerceivableContext; the perceivable context of the new GTU
376      * @throws SimRuntimeException when something goes wrong in the scheduling of the first move event
377      * @throws GTUException when something goes wrong during GTU instantiation
378      */
379     TestGTU(final IdGenerator idGenerator, final GTUType gtuType, final DEVSSimulatorInterface.TimeDoubleUnit simulator,
380 
381             final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
382     {
383         super(idGenerator, gtuType, simulator, perceivableContext);
384     }
385 
386     /** {@inheritDoc} */
387     @Override
388     public Length getLength()
389     {
390         return null;
391     }
392 
393     /** {@inheritDoc} */
394     @Override
395     public Length getWidth()
396     {
397         return null;
398     }
399 
400     /** {@inheritDoc} */
401     @Override
402     public Speed getMaximumSpeed()
403     {
404         return null;
405     }
406 
407     /** {@inheritDoc} */
408     @Override
409     public RelativePosition getFront()
410     {
411         return null;
412     }
413 
414     /** {@inheritDoc} */
415     @Override
416     public RelativePosition getRear()
417     {
418         return null;
419     }
420 
421     /** {@inheritDoc} */
422     @Override
423     public RelativePosition getCenter()
424     {
425         return null;
426     }
427 
428     /** {@inheritDoc} */
429     @Override
430     public ImmutableMap<TYPE, RelativePosition> getRelativePositions()
431     {
432         return null;
433     }
434 
435     /** {@inheritDoc} */
436     @Override
437     public Bounds getBounds()
438     {
439         return null;
440     }
441 
442     /** {@inheritDoc} */
443     @Override
444     public ImmutableSet<RelativePosition> getContourPoints()
445     {
446         return null;
447     }
448 
449 }