1 package org.opentrafficsim.core.gtu.generator;
2
3 import java.lang.reflect.Method;
4 import java.rmi.RemoteException;
5
6 import nl.tudelft.simulation.dsol.SimRuntimeException;
7 import nl.tudelft.simulation.language.reflection.ClassUtil;
8
9 import org.opentrafficsim.core.car.LaneBasedIndividualCar;
10 import org.opentrafficsim.core.car.LaneBasedIndividualCar.LaneBasedIndividualCarBuilder;
11 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
12 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
13 import org.opentrafficsim.core.gtu.GTU;
14 import org.opentrafficsim.core.gtu.GTUException;
15 import org.opentrafficsim.core.gtu.GTUType;
16 import org.opentrafficsim.core.gtu.following.GTUFollowingModel;
17 import org.opentrafficsim.core.network.lane.Lane;
18 import org.opentrafficsim.core.unit.LengthUnit;
19 import org.opentrafficsim.core.unit.SpeedUnit;
20 import org.opentrafficsim.core.unit.TimeUnit;
21 import org.opentrafficsim.core.value.vdouble.scalar.DistContinuousDoubleScalar;
22
23
24
25
26
27
28
29
30
31
32
33 public abstract class AbstractGTUGenerator<ID>
34 {
35
36 private final String name;
37
38
39 private final GTUType<ID> gtuType;
40
41
42 private final Class<GTU<ID>> gtuClass;
43
44
45 private final GTUFollowingModel gtuFollowingModel;
46
47
48 private final DistContinuousDoubleScalar.Abs<SpeedUnit> initialSpeedDist;
49
50
51 private final DistContinuousDoubleScalar.Rel<TimeUnit> interarrivelTimeDist;
52
53
54 private final long maxGTUs;
55
56
57 private final OTSSimTimeDouble startTime;
58
59
60 private final OTSSimTimeDouble endTime;
61
62
63 private final Lane lane;
64
65
66 @SuppressWarnings("checkstyle:visibilitymodifier")
67 protected long numberGTUs = 0;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 @SuppressWarnings("checkstyle:parameternumber")
85 public AbstractGTUGenerator(final String name, final OTSDEVSSimulatorInterface simulator, final GTUType<ID> gtuType,
86 final Class<GTU<ID>> gtuClass, final GTUFollowingModel gtuFollowingModel,
87 final DistContinuousDoubleScalar.Abs<SpeedUnit> initialSpeedDist,
88 final DistContinuousDoubleScalar.Rel<TimeUnit> interarrivelTimeDist, final long maxGTUs,
89 final OTSSimTimeDouble startTime, final OTSSimTimeDouble endTime, final Lane lane) throws RemoteException,
90 SimRuntimeException
91 {
92 super();
93 this.name = name;
94 this.gtuType = gtuType;
95 this.gtuClass = gtuClass;
96 this.gtuFollowingModel = gtuFollowingModel;
97 this.initialSpeedDist = initialSpeedDist;
98 this.interarrivelTimeDist = interarrivelTimeDist;
99 this.maxGTUs = maxGTUs;
100 this.startTime = startTime;
101 this.endTime = endTime;
102 this.lane = lane;
103
104 simulator.scheduleEventAbs(startTime, this, this, "generate", null);
105 }
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 protected final void generate() throws Exception
113 {
114
115 Class<?> getidtype = String.class;
116 try
117 {
118 Method getid = ClassUtil.resolveMethod(this.gtuClass, "getId", new Class<?>[] {});
119 getidtype = getid.getReturnType();
120 }
121 catch (NoSuchMethodException exception)
122 {
123 throw new GTUException("GTU class " + this.gtuClass.getName() + " does not have getId() method.", exception);
124 }
125
126
127 ID id = null;
128 this.numberGTUs++;
129 if (String.class.isAssignableFrom(getidtype))
130 {
131 id = (ID) new String(this.name + ":" + this.numberGTUs);
132 }
133 else if (int.class.isAssignableFrom(getidtype))
134 {
135 id = (ID) new Integer((int) this.numberGTUs);
136 }
137 else if (long.class.isAssignableFrom(getidtype))
138 {
139 id = (ID) new Long(this.numberGTUs);
140 }
141 else
142 {
143 throw new GTUException("GTU ID class " + getidtype.getName() + ": cannot instantiate.");
144 }
145
146
147 if (LaneBasedIndividualCar.class.isAssignableFrom(getGtuClass()))
148 {
149 LaneBasedIndividualCarBuilder<ID> carBuilder = new LaneBasedIndividualCarBuilder<ID>();
150 carBuilder.setId(id);
151 carBuilder.setGtuType(getGtuType());
152 carBuilder.setLength(getLengthDist().draw());
153 carBuilder.setWidth(getWidthDist().draw());
154 carBuilder.setMaximumVelocity(getMaximumSpeedDist().draw());
155 carBuilder.setInitialSpeed(getInitialSpeedDist().draw());
156 carBuilder.setSimulator(getSimulator());
157
158 LaneBasedIndividualCar<ID> car = carBuilder.build();
159 }
160 else
161 {
162 throw new GTUException("GTU class " + getGtuClass().getName() + ": cannot instantiate, no builder.");
163 }
164
165
166 OTSSimTimeDouble nextTime = getSimulator().getSimulatorTime().plus(this.interarrivelTimeDist.draw());
167 if (nextTime.le(this.endTime))
168 {
169 getSimulator().scheduleEventAbs(nextTime, this, this, "generate", null);
170 }
171 }
172
173
174 public abstract OTSDEVSSimulatorInterface getSimulator();
175
176
177 public abstract DistContinuousDoubleScalar.Rel<LengthUnit> getLengthDist();
178
179
180 public abstract DistContinuousDoubleScalar.Rel<LengthUnit> getWidthDist();
181
182
183 public abstract DistContinuousDoubleScalar.Abs<SpeedUnit> getMaximumSpeedDist();
184
185
186
187
188 public final String getName()
189 {
190 return this.name;
191 }
192
193
194
195
196 public final GTUType<ID> getGtuType()
197 {
198 return this.gtuType;
199 }
200
201
202
203
204 public final Class<GTU<ID>> getGtuClass()
205 {
206 return this.gtuClass;
207 }
208
209
210
211
212 public final GTUFollowingModel getGtuFollowingModel()
213 {
214 return this.gtuFollowingModel;
215 }
216
217
218
219
220 public final DistContinuousDoubleScalar.Abs<SpeedUnit> getInitialSpeedDist()
221 {
222 return this.initialSpeedDist;
223 }
224
225
226
227
228 public final DistContinuousDoubleScalar.Rel<TimeUnit> getInterarrivelTimeDist()
229 {
230 return this.interarrivelTimeDist;
231 }
232
233
234
235
236 public final long getMaxGTUs()
237 {
238 return this.maxGTUs;
239 }
240
241
242
243
244 public final OTSSimTimeDouble getStartTime()
245 {
246 return this.startTime;
247 }
248
249
250
251
252 public final OTSSimTimeDouble getEndTime()
253 {
254 return this.endTime;
255 }
256
257 }