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