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
33
34
35
36
37
38
39
40
41 public class TemplateGTUTypeTest
42 {
43
44 private StreamInterface stream = new MersenneTwister();
45
46
47
48
49
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
93
94
95
96 @Test
97 public void compatibleLaneTypeTest() throws RemoteException, SimRuntimeException
98 {
99
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
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
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
151
152
153
154
155
156
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
174
175
176
177
178
179
180
181
182 class DummyModelForTemplateGTUTest implements OTSModelInterface
183 {
184
185 private static final long serialVersionUID = 20150114L;
186
187
188 private SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> simulator;
189
190
191
192
193
194
195 public void setSimulator(
196 SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> simulator)
197 {
198 this.simulator = simulator;
199 }
200
201
202 @Override
203 public void constructModel(SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> arg0)
204 throws SimRuntimeException, RemoteException
205 {
206
207 }
208
209
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 }