View Javadoc
1   package org.opentrafficsim.road.gtu;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import javax.naming.NamingException;
7   
8   import org.djunits.unit.DurationUnit;
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.unit.SpeedUnit;
11  import org.djunits.unit.UNITS;
12  import org.djunits.value.vdouble.scalar.Duration;
13  import org.djunits.value.vdouble.scalar.Length;
14  import org.djunits.value.vdouble.scalar.Speed;
15  import org.djunits.value.vdouble.scalar.Time;
16  import org.junit.Test;
17  import org.opentrafficsim.base.parameters.ParameterException;
18  import org.opentrafficsim.core.compatibility.GTUCompatibility;
19  import org.opentrafficsim.core.distributions.Generator;
20  import org.opentrafficsim.core.distributions.ProbabilityException;
21  import org.opentrafficsim.core.dsol.AbstractOTSModel;
22  import org.opentrafficsim.core.dsol.OTSModelInterface;
23  import org.opentrafficsim.core.dsol.OTSSimulator;
24  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25  import org.opentrafficsim.core.gtu.GTUException;
26  import org.opentrafficsim.core.gtu.GTUType;
27  import org.opentrafficsim.core.gtu.TemplateGTUType;
28  import org.opentrafficsim.core.network.LongitudinalDirectionality;
29  import org.opentrafficsim.core.network.Node;
30  import org.opentrafficsim.core.network.route.FixedRouteGenerator;
31  import org.opentrafficsim.core.network.route.Route;
32  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
33  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
34  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedTemplateGTUType;
35  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
36  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
37  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
38  import org.opentrafficsim.road.network.OTSRoadNetwork;
39  import org.opentrafficsim.road.network.lane.LaneType;
40  
41  import nl.tudelft.simulation.dsol.SimRuntimeException;
42  import nl.tudelft.simulation.jstats.distributions.DistConstant;
43  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
44  import nl.tudelft.simulation.jstats.streams.StreamInterface;
45  
46  /**
47   * Test the TemplateGTUType class.
48   * <p>
49   * Copyright (c) 2013-2019 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/license.html">OpenTrafficSim License</a>.
51   * <p>
52   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
53   * initial version 15 jan. 2015 <br>
54   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
55   */
56  public class LaneBasedTemplateGTUTypeTest implements UNITS
57  {
58      /** The random stream. */
59      private StreamInterface stream = new MersenneTwister();
60  
61      /** The network. */
62      private OTSRoadNetwork network = new OTSRoadNetwork("TemplateGTU network", true);
63  
64      /**
65       * Test construction of a TemplateGTUType and prove that each one uses private fields.
66       * @throws Exception when something goes wrong (should not happen)
67       */
68      @Test
69      public final void constructorTest() throws Exception
70      {
71          GTUType pcType = this.network.getGtuType(GTUType.DEFAULTS.CAR);
72          final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcLength =
73                  new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 4), METER);
74          final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcWidth =
75                  new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 1.6), METER);
76          final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> pcMaximumSpeed =
77                  new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 180), KM_PER_HOUR);
78          OTSSimulatorInterface simulator = new OTSSimulator();
79          OTSModelInterface model = new DummyModelForTemplateGTUTest(simulator);
80          simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
81          LaneBasedTemplateGTUType passengerCar = new LaneBasedTemplateGTUType(pcType, new Generator<Length>()
82          {
83              @Override
84              public Length draw()
85              {
86                  return pcLength.draw();
87              }
88          }, new Generator<Length>()
89          {
90              @Override
91              public Length draw()
92              {
93                  return pcWidth.draw();
94              }
95          }, new Generator<Speed>()
96          {
97              @Override
98              public Speed draw()
99              {
100                 return pcMaximumSpeed.draw();
101             }
102         }, new DummyStrategicalPlannerFactory(), new FixedRouteGenerator(null));
103         verifyFields(passengerCar, pcType, pcLength, pcWidth, pcMaximumSpeed);
104         GTUType truckType = this.network.getGtuType(GTUType.DEFAULTS.TRUCK);
105         ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckLength =
106                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 18), METER);
107         ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckWidth =
108                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 2.2), METER);
109         ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> truckMaximumSpeed =
110                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 110), KM_PER_HOUR);
111         LaneBasedTemplateGTUType truck = new LaneBasedTemplateGTUType(truckType, new Generator<Length>()
112         {
113             @Override
114             public Length draw()
115             {
116                 return truckLength.draw();
117             }
118         }, new Generator<Length>()
119         {
120             @Override
121             public Length draw()
122             {
123                 return truckWidth.draw();
124             }
125         }, new Generator<Speed>()
126         {
127             @Override
128             public Speed draw()
129             {
130                 return truckMaximumSpeed.draw();
131             }
132         }, new DummyStrategicalPlannerFactory(), new FixedRouteGenerator(null));
133         verifyFields(truck, truckType, truckLength, truckWidth, truckMaximumSpeed);
134     }
135 
136     /**
137      * Dummy strategical planner factory.
138      * <p>
139      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
140      * <br>
141      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
142      * <p>
143      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Aug 2, 2016 <br>
144      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
145      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
146      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
147      */
148     private class DummyStrategicalPlannerFactory implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner>
149     {
150 
151         /**
152          * 
153          */
154         DummyStrategicalPlannerFactory()
155         {
156         }
157 
158         /** {@inheritDoc} */
159         @Override
160         public LaneBasedStrategicalPlanner create(final LaneBasedGTU gtu, final Route route, final Node origin,
161                 final Node destination) throws GTUException
162         {
163             return null;
164         }
165 
166     }
167 
168     /**
169      * Test the isCompatible method.
170      * @throws Exception when something goes wrong (should not happen)
171      */
172     @Test
173     public final void compatibleLaneTypeTest() throws Exception
174     {
175         // Create some TemplateGTUTypes
176         GTUType pc = this.network.getGtuType(GTUType.DEFAULTS.CAR);
177         ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcLength =
178                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 4), METER);
179         ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcWidth =
180                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 1.6), METER);
181         ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> pcMaximumSpeed =
182                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 180), KM_PER_HOUR);
183         TemplateGTUType passengerCar = new TemplateGTUType(pc, new Generator<Length>()
184         {
185             @Override
186             public Length draw()
187             {
188                 return pcLength.draw();
189             }
190         }, new Generator<Length>()
191         {
192             @Override
193             public Length draw()
194             {
195                 return pcWidth.draw();
196             }
197         }, new Generator<Speed>()
198         {
199             @Override
200             public Speed draw()
201             {
202                 return pcMaximumSpeed.draw();
203             }
204         });
205         GTUType truckType = this.network.getGtuType(GTUType.DEFAULTS.TRUCK);
206         ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckLength =
207                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 18), METER);
208         ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckWidth =
209                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 2.2), METER);
210         ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> truckMaximumSpeed =
211                 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 110), KM_PER_HOUR);
212         TemplateGTUType truck = new TemplateGTUType(truckType, new Generator<Length>()
213         {
214             @Override
215             public Length draw()
216             {
217                 return truckLength.draw();
218             }
219         }, new Generator<Length>()
220         {
221             @Override
222             public Length draw()
223             {
224                 return truckWidth.draw();
225             }
226         }, new Generator<Speed>()
227         {
228             @Override
229             public Speed draw()
230             {
231                 return truckMaximumSpeed.draw();
232             }
233         });
234 
235         // Create some LaneTypes
236         GTUCompatibility<LaneType> noTrucks = new GTUCompatibility<>((LaneType) null);
237         noTrucks.addAllowedGTUType(passengerCar.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
238         LaneType trucksForbidden =
239                 new LaneType("No Trucks", this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), noTrucks, this.network);
240 
241         GTUCompatibility<LaneType> truckOnly = new GTUCompatibility<>((LaneType) null);
242         truckOnly.addAllowedGTUType(truck.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
243         LaneType trucksOnly =
244                 new LaneType("Trucks Only", this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), truckOnly, this.network);
245 
246         GTUCompatibility<LaneType> bicyclesOnly = new GTUCompatibility<>((LaneType) null);
247         LaneType bicycleLane =
248                 new LaneType("Bicycles Only", this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), bicyclesOnly, this.network);
249 
250         GTUCompatibility<LaneType> urban = new GTUCompatibility<>((LaneType) null);
251         urban.addAllowedGTUType(passengerCar.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
252         urban.addAllowedGTUType(truck.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
253         LaneType urbanRoad = new LaneType("Urban road - open to all traffic",
254                 this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), urban, this.network);
255 
256         // Now we test all combinations
257         // TODO assertTrue("Passengers cars are allowed on a no trucks lane", passengerCar.isCompatible(trucksForbidden));
258         // TODO assertFalse("Trucks are not allowed on a no trucks lane", truck.isCompatible(trucksForbidden));
259         // TODO assertFalse("Passenger cars are not allowed on a trucks only lane", passengerCar.isCompatible(trucksOnly));
260         // TODO assertTrue("Trucks are allowed on a trucks only lane", truck.isCompatible(trucksOnly));
261         // TODO assertTrue("Passenger cars are allowed on an urban road", passengerCar.isCompatible(urbanRoad));
262         // TODO assertTrue("Trucks are allowed on an urban road", truck.isCompatible(urbanRoad));
263         // TODO assertFalse("Passenger cars are not allowed on a bicycle path", passengerCar.isCompatible(bicycleLane));
264         // TODO assertFalse("Trucks are not allowed on an urban road", truck.isCompatible(bicycleLane));
265     }
266 
267     /**
268      * Verify all the values in a TemplateGTUType&lt;String&gt;.
269      * @param templateGTUType TemplateGTUType&lt;String&gt;; the TemplateGTUType
270      * @param gtuType String; the expected id
271      * @param length Length; the expected length
272      * @param width Length; the expected width
273      * @param maximumSpeed Speed; the expected maximum speed
274      * @throws ProbabilityException in case of probability drawing exception
275      * @throws ParameterException in case of a parameter problem.
276      * @throws GTUException in case of a GTU exception
277      * @throws NamingException in case of a naming exception
278      */
279     private void verifyFields(final LaneBasedTemplateGTUType templateGTUType, final GTUType gtuType,
280             final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> length,
281             final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> width,
282             final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> maximumSpeed)
283             throws ProbabilityException, ParameterException, NamingException, GTUException
284     {
285         assertTrue("Type should be " + gtuType, gtuType.equals(templateGTUType.getGTUType()));
286         LaneBasedGTUCharacteristics characteristics = templateGTUType.draw();
287         assertEquals("Length should be " + length, length.draw().getSI(), characteristics.getLength().getSI(), 0.0001);
288         assertEquals("Width should be " + width, width.draw().getSI(), characteristics.getWidth().getSI(), 0.0001);
289         assertEquals("Maximum speed should be " + maximumSpeed, maximumSpeed.draw().getSI(),
290                 characteristics.getMaximumSpeed().getSI(), 0.0001);
291     }
292 
293     /**
294      * Dummy OTSModelInterface.
295      * <p>
296      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
297      * <br>
298      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
299      * <p>
300      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
301      * initial version 4 jan. 2015 <br>
302      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
303      */
304     static class DummyModelForTemplateGTUTest extends AbstractOTSModel
305     {
306         /**
307          * @param simulator the simulator to use
308          */
309         public DummyModelForTemplateGTUTest(final OTSSimulatorInterface simulator)
310         {
311             super(simulator);
312         }
313 
314         /** */
315         private static final long serialVersionUID = 20141027L;
316 
317         /** {@inheritDoc} */
318         @Override
319         public final void constructModel() throws SimRuntimeException
320         {
321             //
322         }
323 
324         /** {@inheritDoc} */
325         @Override
326         public final OTSRoadNetwork getNetwork()
327         {
328             return null;
329         }
330     }
331 
332 }