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   
7   import java.awt.geom.Rectangle2D;
8   import java.rmi.RemoteException;
9   
10  import nl.tudelft.simulation.dsol.SimRuntimeException;
11  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12  import nl.tudelft.simulation.jstats.distributions.DistConstant;
13  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
14  import nl.tudelft.simulation.jstats.streams.StreamInterface;
15  
16  import org.junit.Test;
17  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
18  import org.opentrafficsim.core.dsol.OTSModelInterface;
19  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
20  import org.opentrafficsim.core.gtu.TemplateGTUType;
21  import org.opentrafficsim.core.network.lane.LaneType;
22  import org.opentrafficsim.core.unit.LengthUnit;
23  import org.opentrafficsim.core.unit.SpeedUnit;
24  import org.opentrafficsim.core.unit.TimeUnit;
25  import org.opentrafficsim.core.value.vdouble.scalar.DistContinuousDoubleScalar;
26  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
27  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar.Abs;
28  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar.Rel;
29  import org.opentrafficsim.simulationengine.SimpleSimulator;
30  
31  /**
32   * Test the TemplateGTUType class.
33   * <p>
34   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights
35   * reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
37   * <p>
38   * @version 15 jan. 2015 <br>
39   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
40   */
41  public class TemplateGTUTypeTest
42  {
43      /** the random stream. */
44      private StreamInterface stream = new MersenneTwister();
45  
46      /**
47       * Test construction of a TemplateGTUType and prove that each one uses private fields.
48       * @throws SimRuntimeException
49       * @throws RemoteException
50       */
51      @Test
52      public void constructorTest() throws RemoteException, SimRuntimeException
53      {
54          String pcId = "passenger car";
55          DistContinuousDoubleScalar.Rel<LengthUnit> pcLength =
56                  new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 4), LengthUnit.METER);
57          DistContinuousDoubleScalar.Rel<LengthUnit> pcWidth =
58                  new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 1.6), LengthUnit.METER);
59          DistContinuousDoubleScalar.Abs<SpeedUnit> pcMaximumSpeed =
60                  new DistContinuousDoubleScalar.Abs<SpeedUnit>(new DistConstant(this.stream, 180), SpeedUnit.KM_PER_HOUR);
61          OTSModelInterface model = new DummyModelForTemplateGTUTest();
62          SimpleSimulator simulator =
63                  new SimpleSimulator(new DoubleScalar.Abs<TimeUnit>(0.0, TimeUnit.SECOND),
64                          new DoubleScalar.Rel<TimeUnit>(0.0, TimeUnit.SECOND), new DoubleScalar.Rel<TimeUnit>(3600.0,
65                                  TimeUnit.SECOND), model, new Rectangle2D.Double(-1000, -1000, 2000, 2000));
66          TemplateGTUType<String> passengerCar =
67                  new TemplateGTUType<String>(pcId, pcLength, pcWidth, pcMaximumSpeed,
68                          (OTSDEVSSimulatorInterface) simulator.getSimulator());
69          verifyFields(passengerCar, pcId, pcLength, pcWidth, pcMaximumSpeed,
70                  (OTSDEVSSimulatorInterface) simulator.getSimulator());
71          String truckId = "truck";
72          DistContinuousDoubleScalar.Rel<LengthUnit> truckLength =
73                  new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 18), LengthUnit.METER);
74          DistContinuousDoubleScalar.Rel<LengthUnit> truckWidth =
75                  new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 2.2), LengthUnit.METER);
76          DistContinuousDoubleScalar.Abs<SpeedUnit> truckMaximumSpeed =
77                  new DistContinuousDoubleScalar.Abs<SpeedUnit>(new DistConstant(this.stream, 110), SpeedUnit.KM_PER_HOUR);
78          SimpleSimulator truckSimulator =
79                  new SimpleSimulator(new DoubleScalar.Abs<TimeUnit>(0.0, TimeUnit.SECOND),
80                          new DoubleScalar.Rel<TimeUnit>(0.0, TimeUnit.SECOND), new DoubleScalar.Rel<TimeUnit>(3600.0,
81                                  TimeUnit.SECOND), model, new Rectangle2D.Double(-1000, -1000, 2000, 2000));
82          TemplateGTUType<String> truck =
83                  new TemplateGTUType<String>(truckId, truckLength, truckWidth, truckMaximumSpeed,
84                          (OTSDEVSSimulatorInterface) truckSimulator.getSimulator());
85          verifyFields(truck, truckId, truckLength, truckWidth, truckMaximumSpeed,
86                  (OTSDEVSSimulatorInterface) truckSimulator.getSimulator());
87          verifyFields(passengerCar, pcId, pcLength, pcWidth, pcMaximumSpeed,
88                  (OTSDEVSSimulatorInterface) simulator.getSimulator());
89      }
90  
91      /**
92       * Test the isCompatible method.
93       * @throws SimRuntimeException
94       * @throws RemoteException
95       */
96      @Test
97      public void compatibleLaneTypeTest() throws RemoteException, SimRuntimeException
98      {
99          // Create some TemplateGTUTypes
100         String pcId = "passenger car";
101         DistContinuousDoubleScalar.Rel<LengthUnit> pcLength =
102                 new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 4), LengthUnit.METER);
103         DistContinuousDoubleScalar.Rel<LengthUnit> pcWidth =
104                 new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 1.6), LengthUnit.METER);
105         DistContinuousDoubleScalar.Abs<SpeedUnit> pcMaximumSpeed =
106                 new DistContinuousDoubleScalar.Abs<SpeedUnit>(new DistConstant(this.stream, 180), SpeedUnit.KM_PER_HOUR);
107         OTSModelInterface model = new DummyModelForTemplateGTUTest();
108         SimpleSimulator simulator =
109                 new SimpleSimulator(new DoubleScalar.Abs<TimeUnit>(0.0, TimeUnit.SECOND),
110                         new DoubleScalar.Rel<TimeUnit>(0.0, TimeUnit.SECOND), new DoubleScalar.Rel<TimeUnit>(3600.0,
111                                 TimeUnit.SECOND), model, new Rectangle2D.Double(-1000, -1000, 2000, 2000));
112         TemplateGTUType<String> passengerCar =
113                 new TemplateGTUType<String>(pcId, pcLength, pcWidth, pcMaximumSpeed,
114                         (OTSDEVSSimulatorInterface) simulator.getSimulator());
115         String truckId = "truck";
116         DistContinuousDoubleScalar.Rel<LengthUnit> truckLength =
117                 new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 18), LengthUnit.METER);
118         DistContinuousDoubleScalar.Rel<LengthUnit> truckWidth =
119                 new DistContinuousDoubleScalar.Rel<LengthUnit>(new DistConstant(this.stream, 2.2), LengthUnit.METER);
120         DistContinuousDoubleScalar.Abs<SpeedUnit> truckMaximumSpeed =
121                 new DistContinuousDoubleScalar.Abs<SpeedUnit>(new DistConstant(this.stream, 110), SpeedUnit.KM_PER_HOUR);
122         SimpleSimulator truckSimulator =
123                 new SimpleSimulator(new DoubleScalar.Abs<TimeUnit>(0.0, TimeUnit.SECOND),
124                         new DoubleScalar.Rel<TimeUnit>(0.0, TimeUnit.SECOND), new DoubleScalar.Rel<TimeUnit>(3600.0,
125                                 TimeUnit.SECOND), model, new Rectangle2D.Double(-1000, -1000, 2000, 2000));
126         TemplateGTUType<String> truck =
127                 new TemplateGTUType<String>(truckId, truckLength, truckWidth, truckMaximumSpeed,
128                         (OTSDEVSSimulatorInterface) truckSimulator.getSimulator());
129         // Create some LaneTypes
130         LaneType<String> trucksForbidden = new LaneType<String>("No Trucks");
131         trucksForbidden.addPermeability(passengerCar);
132         LaneType<String> trucksOnly = new LaneType<String>("Trucks Only");
133         trucksOnly.addPermeability(truck);
134         LaneType<String> bicycleLane = new LaneType<String>("Bicycles Only");
135         LaneType<String> urbanRoad = new LaneType<String>("Urban road - open to all traffic");
136         urbanRoad.addPermeability(passengerCar);
137         urbanRoad.addPermeability(truck);
138         // Now we test all combinations
139         assertTrue("Passengers cars are allowed on a no trucks lane", passengerCar.isCompatible(trucksForbidden));
140         assertFalse("Trucks are not allowed on a no trucks lane", truck.isCompatible(trucksForbidden));
141         assertFalse("Passenger cars are not allowed on a trucks only lane", passengerCar.isCompatible(trucksOnly));
142         assertTrue("Trucks are allowed on a trucks only lane", truck.isCompatible(trucksOnly));
143         assertTrue("Passenger cars are allowed on an urban road", passengerCar.isCompatible(urbanRoad));
144         assertTrue("Trucks are allowed on an urban road", truck.isCompatible(urbanRoad));
145         assertFalse("Passenger cars are not allowed on a bicycle path", passengerCar.isCompatible(bicycleLane));
146         assertFalse("Trucks are not allowed on an urban road", truck.isCompatible(bicycleLane));
147     }
148 
149     /**
150      * Verify all the values in a TemplateGTUType&lt;String&gt;.
151      * @param templateGTUType TemplateGTUType&lt;String&gt;; the TemplateGTUType
152      * @param id String; the expected id
153      * @param length DoubleScalar.Rel&lt;LengthUnit&gt;; the expected length
154      * @param width DoubleScalar.Rel&lt;LengthUnit&gt;; the expected width
155      * @param maximumSpeed DoubleScalar.Abs&lt;SpeedUnit&gt;; the expected maximum velocity
156      * @param simulator OTSDEVSSimulatorInterface; the expected simulator
157      */
158     private void verifyFields(final TemplateGTUType<String> templateGTUType, final String id,
159             final DistContinuousDoubleScalar.Rel<LengthUnit> length,
160             final DistContinuousDoubleScalar.Rel<LengthUnit> width,
161             final DistContinuousDoubleScalar.Abs<SpeedUnit> maximumSpeed, final OTSDEVSSimulatorInterface simulator)
162     {
163         assertTrue("Id should be " + id, id.equals(templateGTUType.getId()));
164         assertEquals("Length should be " + length, length.draw().getSI(), templateGTUType.getLength().getSI(), 0.0001);
165         assertEquals("Sidth should be " + width, width.draw().getSI(), templateGTUType.getWidth().getSI(), 0.0001);
166         assertEquals("Maximum speed should be " + maximumSpeed, maximumSpeed.draw().getSI(), templateGTUType
167                 .getMaximumVelocity().getSI(), 0.0001);
168         assertEquals("Simulator", simulator, templateGTUType.getSimulator());
169     }
170 }
171 
172 /**
173  * Dummy OTSModelInterface.
174  * <p>
175  * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights
176  * reserved. <br>
177  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
178  * <p>
179  * @version 14 jan. 2015 <br>
180  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
181  */
182 class DummyModelForTemplateGTUTest implements OTSModelInterface
183 {
184     /** */
185     private static final long serialVersionUID = 20150114L;
186 
187     /** The simulator. */
188     private SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> simulator;
189 
190     /**
191      * Register the simulator.
192      * @param simulator SimulatorInterface&lt;DoubleScalar.Abs&lt;TimeUnit&gt;, DoubleScalar.Rel&lt;TimeUnit&gt;,
193      *            OTSSimTimeDouble&gt;; the simulator
194      */
195     public void setSimulator(
196             SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> simulator)
197     {
198         this.simulator = simulator;
199     }
200 
201     /** {@inheritDoc} */
202     @Override
203     public void constructModel(SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> arg0)
204             throws SimRuntimeException, RemoteException
205     {
206         // Nothing happens here
207     }
208 
209     /** {@inheritDoc} */
210     @Override
211     public SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> getSimulator()
212             throws RemoteException
213     {
214         if (null == this.simulator)
215         {
216             throw new Error("getSimulator called, but simulator field is null");
217         }
218         return this.simulator;
219     }
220 
221 }