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