1 package org.opentrafficsim.road.gtu;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import javax.naming.NamingException;
7
8 import org.djunits.unit.DurationUnit;
9 import org.djunits.unit.LengthUnit;
10 import org.djunits.unit.SpeedUnit;
11 import org.djunits.unit.UNITS;
12 import org.djunits.value.vdouble.scalar.Duration;
13 import org.djunits.value.vdouble.scalar.Length;
14 import org.djunits.value.vdouble.scalar.Speed;
15 import org.djunits.value.vdouble.scalar.Time;
16 import org.junit.Test;
17 import org.opentrafficsim.base.parameters.ParameterException;
18 import org.opentrafficsim.core.compatibility.GTUCompatibility;
19 import org.opentrafficsim.core.distributions.Generator;
20 import org.opentrafficsim.core.distributions.ProbabilityException;
21 import org.opentrafficsim.core.dsol.AbstractOTSModel;
22 import org.opentrafficsim.core.dsol.OTSModelInterface;
23 import org.opentrafficsim.core.dsol.OTSSimulator;
24 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25 import org.opentrafficsim.core.gtu.GTUException;
26 import org.opentrafficsim.core.gtu.GTUType;
27 import org.opentrafficsim.core.gtu.TemplateGTUType;
28 import org.opentrafficsim.core.network.LongitudinalDirectionality;
29 import org.opentrafficsim.core.network.Node;
30 import org.opentrafficsim.core.network.route.FixedRouteGenerator;
31 import org.opentrafficsim.core.network.route.Route;
32 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
33 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
34 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedTemplateGTUType;
35 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
36 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
37 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
38 import org.opentrafficsim.road.network.OTSRoadNetwork;
39 import org.opentrafficsim.road.network.lane.LaneType;
40
41 import nl.tudelft.simulation.dsol.SimRuntimeException;
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
48
49
50
51
52
53
54
55
56 public class LaneBasedTemplateGTUTypeTest implements UNITS
57 {
58
59 private StreamInterface stream = new MersenneTwister();
60
61
62 private OTSRoadNetwork network = new OTSRoadNetwork("TemplateGTU network", true);
63
64
65
66
67
68 @Test
69 public final void constructorTest() throws Exception
70 {
71 GTUType pcType = this.network.getGtuType(GTUType.DEFAULTS.CAR);
72 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcLength =
73 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 4), METER);
74 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcWidth =
75 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 1.6), METER);
76 final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> pcMaximumSpeed =
77 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 180), KM_PER_HOUR);
78 OTSSimulatorInterface simulator = new OTSSimulator();
79 OTSModelInterface model = new DummyModelForTemplateGTUTest(simulator);
80 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
81 LaneBasedTemplateGTUType passengerCar = new LaneBasedTemplateGTUType(pcType, new Generator<Length>()
82 {
83 @Override
84 public Length draw()
85 {
86 return pcLength.draw();
87 }
88 }, new Generator<Length>()
89 {
90 @Override
91 public Length draw()
92 {
93 return pcWidth.draw();
94 }
95 }, new Generator<Speed>()
96 {
97 @Override
98 public Speed draw()
99 {
100 return pcMaximumSpeed.draw();
101 }
102 }, new DummyStrategicalPlannerFactory(), new FixedRouteGenerator(null));
103 verifyFields(passengerCar, pcType, pcLength, pcWidth, pcMaximumSpeed);
104 GTUType truckType = this.network.getGtuType(GTUType.DEFAULTS.TRUCK);
105 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckLength =
106 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 18), METER);
107 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckWidth =
108 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 2.2), METER);
109 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> truckMaximumSpeed =
110 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 110), KM_PER_HOUR);
111 LaneBasedTemplateGTUType truck = new LaneBasedTemplateGTUType(truckType, new Generator<Length>()
112 {
113 @Override
114 public Length draw()
115 {
116 return truckLength.draw();
117 }
118 }, new Generator<Length>()
119 {
120 @Override
121 public Length draw()
122 {
123 return truckWidth.draw();
124 }
125 }, new Generator<Speed>()
126 {
127 @Override
128 public Speed draw()
129 {
130 return truckMaximumSpeed.draw();
131 }
132 }, new DummyStrategicalPlannerFactory(), new FixedRouteGenerator(null));
133 verifyFields(truck, truckType, truckLength, truckWidth, truckMaximumSpeed);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148 private class DummyStrategicalPlannerFactory implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner>
149 {
150
151
152
153
154 DummyStrategicalPlannerFactory()
155 {
156 }
157
158
159 @Override
160 public LaneBasedStrategicalPlanner create(final LaneBasedGTU gtu, final Route route, final Node origin,
161 final Node destination) throws GTUException
162 {
163 return null;
164 }
165
166 }
167
168
169
170
171
172 @Test
173 public final void compatibleLaneTypeTest() throws Exception
174 {
175
176 GTUType pc = this.network.getGtuType(GTUType.DEFAULTS.CAR);
177 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcLength =
178 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 4), METER);
179 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> pcWidth =
180 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 1.6), METER);
181 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> pcMaximumSpeed =
182 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 180), KM_PER_HOUR);
183 TemplateGTUType passengerCar = new TemplateGTUType(pc, new Generator<Length>()
184 {
185 @Override
186 public Length draw()
187 {
188 return pcLength.draw();
189 }
190 }, new Generator<Length>()
191 {
192 @Override
193 public Length draw()
194 {
195 return pcWidth.draw();
196 }
197 }, new Generator<Speed>()
198 {
199 @Override
200 public Speed draw()
201 {
202 return pcMaximumSpeed.draw();
203 }
204 });
205 GTUType truckType = this.network.getGtuType(GTUType.DEFAULTS.TRUCK);
206 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckLength =
207 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 18), METER);
208 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> truckWidth =
209 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 2.2), METER);
210 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> truckMaximumSpeed =
211 new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 110), KM_PER_HOUR);
212 TemplateGTUType truck = new TemplateGTUType(truckType, new Generator<Length>()
213 {
214 @Override
215 public Length draw()
216 {
217 return truckLength.draw();
218 }
219 }, new Generator<Length>()
220 {
221 @Override
222 public Length draw()
223 {
224 return truckWidth.draw();
225 }
226 }, new Generator<Speed>()
227 {
228 @Override
229 public Speed draw()
230 {
231 return truckMaximumSpeed.draw();
232 }
233 });
234
235
236 GTUCompatibility<LaneType> noTrucks = new GTUCompatibility<>((LaneType) null);
237 noTrucks.addAllowedGTUType(passengerCar.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
238 LaneType trucksForbidden =
239 new LaneType("No Trucks", this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), noTrucks, this.network);
240
241 GTUCompatibility<LaneType> truckOnly = new GTUCompatibility<>((LaneType) null);
242 truckOnly.addAllowedGTUType(truck.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
243 LaneType trucksOnly =
244 new LaneType("Trucks Only", this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), truckOnly, this.network);
245
246 GTUCompatibility<LaneType> bicyclesOnly = new GTUCompatibility<>((LaneType) null);
247 LaneType bicycleLane =
248 new LaneType("Bicycles Only", this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), bicyclesOnly, this.network);
249
250 GTUCompatibility<LaneType> urban = new GTUCompatibility<>((LaneType) null);
251 urban.addAllowedGTUType(passengerCar.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
252 urban.addAllowedGTUType(truck.getGTUType(), LongitudinalDirectionality.DIR_BOTH);
253 LaneType urbanRoad = new LaneType("Urban road - open to all traffic",
254 this.network.getLaneType(LaneType.DEFAULTS.FREEWAY), urban, this.network);
255
256
257
258
259
260
261
262
263
264
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279 private void verifyFields(final LaneBasedTemplateGTUType templateGTUType, final GTUType gtuType,
280 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> length,
281 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> width,
282 final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> maximumSpeed)
283 throws ProbabilityException, ParameterException, NamingException, GTUException
284 {
285 assertTrue("Type should be " + gtuType, gtuType.equals(templateGTUType.getGTUType()));
286 LaneBasedGTUCharacteristics characteristics = templateGTUType.draw();
287 assertEquals("Length should be " + length, length.draw().getSI(), characteristics.getLength().getSI(), 0.0001);
288 assertEquals("Width should be " + width, width.draw().getSI(), characteristics.getWidth().getSI(), 0.0001);
289 assertEquals("Maximum speed should be " + maximumSpeed, maximumSpeed.draw().getSI(),
290 characteristics.getMaximumSpeed().getSI(), 0.0001);
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304 static class DummyModelForTemplateGTUTest extends AbstractOTSModel
305 {
306
307
308
309 public DummyModelForTemplateGTUTest(final OTSSimulatorInterface simulator)
310 {
311 super(simulator);
312 }
313
314
315 private static final long serialVersionUID = 20141027L;
316
317
318 @Override
319 public final void constructModel() throws SimRuntimeException
320 {
321
322 }
323
324
325 @Override
326 public final OTSRoadNetwork getNetwork()
327 {
328 return null;
329 }
330 }
331
332 }