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   
8   import java.rmi.RemoteException;
9   
10  import javax.media.j3d.Bounds;
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.OTSGeometryException;
28  import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
29  import org.opentrafficsim.core.gtu.plan.strategical.StrategicalPlanner;
30  import org.opentrafficsim.core.gtu.plan.tactical.TacticalPlanner;
31  import org.opentrafficsim.core.idgenerator.IdGenerator;
32  import org.opentrafficsim.core.network.Link;
33  import org.opentrafficsim.core.network.LinkDirection;
34  import org.opentrafficsim.core.network.Network;
35  import org.opentrafficsim.core.network.NetworkException;
36  import org.opentrafficsim.core.network.Node;
37  import org.opentrafficsim.core.network.OTSNetwork;
38  import org.opentrafficsim.core.network.route.Route;
39  import org.opentrafficsim.core.perception.PerceivableContext;
40  
41  import nl.tudelft.simulation.dsol.SimRuntimeException;
42  import nl.tudelft.simulation.language.d3.DirectedPoint;
43  
44  /**
45   * Test the AbstractGTU class.
46   * <p>
47   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
48   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
49   * <p>
50   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Nov 11, 2015 <br>
51   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
52   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
53   */
54  public class GTUTest
55  {
56      /** GTU that will be returned when the fake strategical planner is asked for the associated GTU with getGTU. */
57      public GTU gtuOfStrategicalPlanner = null;
58  
59      /** */
60      private static final long serialVersionUID = 20151217L;
61  
62      /**
63       * Test the constructor.
64       * @throws GTUException should not happen uncaught; if it does the test has failed
65       * @throws NetworkException should not happen uncaught; if it does the test has failed
66       * @throws SimRuntimeException should not happen uncaught; if it does the test has failed
67       * @throws NamingException should not happen uncaught; if it does the test has failed
68       * @throws RemoteException should not happen uncaught; if it does the test has failed
69       * @throws OTSGeometryException should not happen uncaught; if it does the test has failed
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          OTSNetwork perceivableContext = new OTSNetwork("network", true);
78          OTSSimulatorInterface simulator = new OTSSimulator();
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             // Ignore expected exception
186         }
187         try
188         {
189             new TestGTU("IdOfGTU", null, simulator, perceivableContext);
190             fail("null gtuType should have thrown a GTUException");
191         }
192         catch (GTUException ge)
193         {
194             // Ignore expected exception
195         }
196         try
197         {
198             new TestGTU("IdOfGTU", gtuType1, null, perceivableContext);
199             fail("null simulator should have thrown a GTUException");
200         }
201         catch (GTUException ge)
202         {
203             // Ignore expected exception
204         }
205         try
206         {
207             new TestGTU("IdOfGTU", gtuType1, simulator, null);
208             fail("null perceivableContext should have thrown a GTUException");
209         }
210         catch (GTUException ge)
211         {
212             // Ignore expected exception
213         }
214         IdGenerator idGenerator = new IdGenerator("baseName");
215         int lastBeforeId = Integer.parseInt(idGenerator.nextId().substring(8));
216         gtu = new TestGTU(idGenerator, gtuType1, simulator, perceivableContext);
217         int firstAfterId = Integer.parseInt(idGenerator.nextId().substring(8));
218         assertEquals("Id generator was called once in the constructor", 1 + 1, firstAfterId - lastBeforeId);
219         try
220         {
221             new TestGTU((IdGenerator) null, gtuType1, simulator, perceivableContext);
222             fail("null idGenerator should have thrown a GTUException");
223         }
224         catch (GTUException ge)
225         {
226             // Ignore expected exception
227         }
228         Speed initialSpeed = new Speed(10, SpeedUnit.KM_PER_HOUR);
229         try
230         {
231             gtu.init(null, initialLocation, initialSpeed);
232             fail("null strategicalPlanner should have thrown a GTUException");
233         }
234         catch (GTUException ge)
235         {
236             // Ignore expected exception
237         }
238         // FIXME should the next one not ge a GTUException?
239         try
240         {
241             gtu.init(strategicalPlanner, null, initialSpeed);
242             fail("null initialLocation should have thrown a NullPointerException");
243         }
244         catch (NullPointerException ne)
245         {
246             // Ignore expected exception
247         }
248         try
249         {
250             gtu.init(strategicalPlanner, new DirectedPoint(Double.NaN, 20, 30), initialSpeed);
251             fail("null initialSpeed should have thrown a GTUException");
252         }
253         catch (GTUException ge)
254         {
255             // Ignore expected exception
256         }
257         try
258         {
259             gtu.init(strategicalPlanner, new DirectedPoint(10, Double.NaN, 30), initialSpeed);
260             fail("null initialSpeed should have thrown a GTUException");
261         }
262         catch (GTUException ge)
263         {
264             // Ignore expected exception
265         }
266         try
267         {
268             gtu.init(strategicalPlanner, new DirectedPoint(10, 20, Double.NaN), initialSpeed);
269             fail("null initialSpeed should have thrown a GTUException");
270         }
271         catch (GTUException ge)
272         {
273             // Ignore expected exception
274         }
275         try
276         {
277             gtu.init(strategicalPlanner, initialLocation, null);
278             fail("null initialSpeed should have thrown a GTUException");
279         }
280         catch (GTUException ge)
281         {
282             // Ignore expected exception
283         }
284         // The null pointer returned by the strategical planner will cause a NullPointerException
285         // FIXME should probably explicitly throw an exception for a misbehaving strategical planner
286         try
287         {
288             gtu.init(strategicalPlanner, initialLocation, initialSpeed);
289             fail("strategicalPlanner that returns a null pointer should have thrown a NullPointerException");
290         }
291         catch (NullPointerException ne)
292         {
293             // Ignore expected exception
294         }
295         this.gtuOfStrategicalPlanner = firstGTU;
296         try
297         {
298             gtu.init(strategicalPlanner, initialLocation, initialSpeed);
299             fail("wrong strategicalPlanner should have thrown a GTUException");
300         }
301         catch (GTUException ge)
302         {
303             // Ignore expected exception
304         }
305         this.gtuOfStrategicalPlanner = gtu;
306         // FIXME should the AbstractGTU not complain more directly about a null returned by
307         // strategicalPlanner.generateTacticalPlanner()?
308         try
309         {
310             gtu.init(strategicalPlanner, initialLocation, initialSpeed);
311             fail("init with fake strategical planner should have caused a NullPointerExeption in move");
312         }
313         catch (NullPointerException ne)
314         {
315             // Ignore expected exception
316         }
317     }
318 
319     /** */
320     class GTUModel extends AbstractOTSModel
321     {
322         /** */
323         private static final long serialVersionUID = 1L;
324 
325         /**
326          * @param simulator the simulator
327          */
328         GTUModel(final OTSSimulatorInterface simulator)
329         {
330             super(simulator);
331         }
332 
333         /** {@inheritDoc} */
334         @Override
335         public void constructModel() throws SimRuntimeException
336         {
337             // Not used
338         }
339 
340         /** {@inheritDoc} */
341         @Override
342         public final OTSNetwork getNetwork()
343         {
344             return null;
345         }
346     }
347 
348     /** */
349     class TestGTU extends AbstractGTU
350     {
351         /** */
352         private static final long serialVersionUID = 20151111L;
353 
354         /**
355          * @param id String; id of the new GTU
356          * @param gtuType GTUType; type of the new GTU
357          * @param simulator OTSSimulatorInterface; simulator that controls the new GTU
358          * @param perceivableContext PerceivableContext; the perceivable context of the new GTU
359          * @throws SimRuntimeException when something goes wrong in the scheduling of the first move event
360          * @throws GTUException when something goes wrong during GTU instantiation
361          */
362         TestGTU(final String id, final GTUType gtuType, final OTSSimulatorInterface simulator,
363                 final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
364         {
365             super(id, gtuType, simulator, perceivableContext);
366         }
367 
368         /**
369          * @param idGenerator IdGenerator; id generator that will generate the id of the new GTU
370          * @param gtuType GTUType; type of the new GTU
371          * @param simulator OTSSimulatorInterface; simulator that controls the new GTU
372          * @param perceivableContext PerceivableContext; the perceivable context of the new GTU
373          * @throws SimRuntimeException when something goes wrong in the scheduling of the first move event
374          * @throws GTUException when something goes wrong during GTU instantiation
375          */
376         TestGTU(final IdGenerator idGenerator, final GTUType gtuType, final OTSSimulatorInterface simulator,
377 
378                 final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
379         {
380             super(idGenerator, gtuType, simulator, perceivableContext);
381         }
382 
383         /** {@inheritDoc} */
384         @Override
385         public Length getLength()
386         {
387             return null;
388         }
389 
390         /** {@inheritDoc} */
391         @Override
392         public Length getWidth()
393         {
394             return null;
395         }
396 
397         /** {@inheritDoc} */
398         @Override
399         public Speed getMaximumSpeed()
400         {
401             return null;
402         }
403 
404         /** {@inheritDoc} */
405         @Override
406         public RelativePosition getFront()
407         {
408             return null;
409         }
410 
411         /** {@inheritDoc} */
412         @Override
413         public RelativePosition getRear()
414         {
415             return null;
416         }
417 
418         /** {@inheritDoc} */
419         @Override
420         public RelativePosition getCenter()
421         {
422             return null;
423         }
424 
425         /** {@inheritDoc} */
426         @Override
427         public ImmutableMap<TYPE, RelativePosition> getRelativePositions()
428         {
429             return null;
430         }
431 
432         /** {@inheritDoc} */
433         @Override
434         public Bounds getBounds()
435         {
436             return null;
437         }
438 
439         /** {@inheritDoc} */
440         @Override
441         public ImmutableSet<RelativePosition> getContourPoints()
442         {
443             return null;
444         }
445     }
446 
447 }