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