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
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
63
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
133
134
135
136
137
138
139
140
141
142
143 private class DummyStrategicalPlannerFactory implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner>
144 {
145
146
147
148
149 DummyStrategicalPlannerFactory()
150 {
151 }
152
153
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
165
166
167 @Test
168 public final void compatibleLaneTypeTest() throws Exception
169 {
170
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
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
248
249
250
251
252
253
254
255
256 }
257
258
259
260
261
262
263
264
265
266
267
268
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
287
288
289
290
291
292
293
294
295 class DummyModelForTemplateGTUTest implements OTSModelInterface
296 {
297
298 private static final long serialVersionUID = 20150114L;
299
300
301 private SimulatorInterface<Time, Duration, SimTimeDoubleUnit> simulator;
302
303
304
305
306
307 public void setSimulator(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> simulator)
308 {
309 this.simulator = simulator;
310 }
311
312
313 @Override
314 public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0) throws SimRuntimeException
315 {
316
317 }
318
319
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
332 @Override
333 public OTSNetwork getNetwork()
334 {
335 return null;
336 }
337
338 }