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