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
53
54
55
56
57
58
59
60
61 public class LaneBasedTemplateGTUTypeTest implements UNITS
62 {
63
64 private StreamInterface stream = new MersenneTwister();
65
66
67
68
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
107
108
109
110
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
153
154
155
156
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
172
173
174
175
176
177
178
179
180
181
182 private class DummyStrategicalPlannerFactory implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner>
183 {
184
185
186
187
188 public DummyStrategicalPlannerFactory()
189 {
190 }
191
192
193 @Override
194 public LaneBasedStrategicalPlanner create(LaneBasedGTU gtu, Route route) throws GTUException
195 {
196 return null;
197 }
198
199 }
200
201
202
203
204
205 @Test
206 public void compatibleLaneTypeTest() throws Exception
207 {
208
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
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
283
284
285
286
287
288
289
290
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
328
329
330
331
332
333
334
335
336 class DummyModelForTemplateGTUTest implements OTSModelInterface
337 {
338
339 private static final long serialVersionUID = 20150114L;
340
341
342 private SimulatorInterface<Time, Duration, OTSSimTimeDouble> simulator;
343
344
345
346
347
348 public void setSimulator(SimulatorInterface<Time, Duration, OTSSimTimeDouble> simulator)
349 {
350 this.simulator = simulator;
351 }
352
353
354 @Override
355 public void constructModel(SimulatorInterface<Time, Duration, OTSSimTimeDouble> arg0) throws SimRuntimeException
356 {
357
358 }
359
360
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
373 @Override
374 public OTSNetwork getNetwork()
375 {
376 return null;
377 }
378
379 }