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