1 package org.opentrafficsim.road.gtu.generator.od;
2
3 import java.util.LinkedHashMap;
4 import java.util.LinkedHashSet;
5 import java.util.Map;
6 import java.util.Set;
7
8 import org.djutils.exceptions.Throw;
9 import org.djutils.exceptions.Try;
10 import org.opentrafficsim.core.distributions.Generator;
11 import org.opentrafficsim.core.gtu.GTUCharacteristics;
12 import org.opentrafficsim.core.gtu.GTUException;
13 import org.opentrafficsim.core.gtu.GTUType;
14 import org.opentrafficsim.core.gtu.TemplateGTUType;
15 import org.opentrafficsim.core.network.Node;
16 import org.opentrafficsim.core.network.route.Route;
17 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
18 import org.opentrafficsim.road.gtu.lane.VehicleModel;
19 import org.opentrafficsim.road.gtu.lane.VehicleModelFactory;
20 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
21 import org.opentrafficsim.road.gtu.strategical.od.Categorization;
22 import org.opentrafficsim.road.gtu.strategical.od.Category;
23 import org.opentrafficsim.road.gtu.strategical.route.RouteGeneratorOD;
24
25 import nl.tudelft.simulation.jstats.streams.StreamInterface;
26
27
28
29
30
31
32
33
34
35
36
37
38 public final class DefaultGTUCharacteristicsGeneratorOD implements GTUCharacteristicsGeneratorOD
39 {
40
41 private Generator<GTUType> gtuTypeGenerator = null;
42
43
44 private final Map<GTUType, TemplateGTUType> templates = new LinkedHashMap<>();
45
46
47 private final RouteGeneratorOD routeGenerator;
48
49
50 private final StrategicalPlannerFactorySupplierOD factorySupplier;
51
52
53 private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
54
55
56
57
58 public DefaultGTUCharacteristicsGeneratorOD()
59 {
60 this(null, RouteGeneratorOD.NULL, new LinkedHashSet<>(), StrategicalPlannerFactorySupplierOD.lmrs());
61 }
62
63
64
65
66
67 public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeGenerator)
68 {
69 this(null, routeGenerator, new LinkedHashSet<>(), StrategicalPlannerFactorySupplierOD.lmrs());
70 }
71
72
73
74
75
76
77 public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeSupplier, final Set<TemplateGTUType> templates)
78 {
79 this(null, routeSupplier, templates, StrategicalPlannerFactorySupplierOD.lmrs());
80 }
81
82
83
84
85
86
87 public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeGenerator,
88 final StrategicalPlannerFactorySupplierOD factorySupplier)
89 {
90 this(null, routeGenerator, new LinkedHashSet<>(), factorySupplier);
91 }
92
93
94
95
96
97
98
99
100 public DefaultGTUCharacteristicsGeneratorOD(final Generator<GTUType> gtuTypeGenerator,
101 final RouteGeneratorOD routeGenerator, final Set<TemplateGTUType> templates,
102 final StrategicalPlannerFactorySupplierOD factorySupplier)
103 {
104 Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
105 this.gtuTypeGenerator = gtuTypeGenerator;
106 if (routeGenerator == null)
107 {
108 this.routeGenerator = RouteGeneratorOD.NULL;
109 }
110 else
111 {
112 this.routeGenerator = routeGenerator;
113 }
114 if (templates != null)
115 {
116 for (TemplateGTUType template : templates)
117 {
118 this.templates.put(template.getGTUType(), template);
119 }
120 }
121 this.factorySupplier = factorySupplier;
122 }
123
124
125
126
127
128 public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates)
129 {
130 this(null, RouteGeneratorOD.NULL, templates, StrategicalPlannerFactorySupplierOD.lmrs());
131 }
132
133
134
135
136
137
138 public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates,
139 final StrategicalPlannerFactorySupplierOD factorySupplier)
140 {
141 this(null, RouteGeneratorOD.NULL, templates, factorySupplier);
142 }
143
144
145
146
147
148
149 public DefaultGTUCharacteristicsGeneratorOD(final StrategicalPlannerFactorySupplierOD factorySupplier)
150 {
151 this(null, RouteGeneratorOD.NULL, new LinkedHashSet<>(), factorySupplier);
152 }
153
154
155
156
157
158
159
160
161
162
163
164 private DefaultGTUCharacteristicsGeneratorOD(final Generator<GTUType> gtuTypeGenerator,
165 final RouteGeneratorOD routeSupplier, final Set<TemplateGTUType> templates,
166 final StrategicalPlannerFactorySupplierOD factorySupplier, final VehicleModelFactory vehicleModelFactory)
167 {
168 Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
169 this.gtuTypeGenerator = gtuTypeGenerator;
170 if (routeSupplier == null)
171 {
172 this.routeGenerator = RouteGeneratorOD.NULL;
173 }
174 else
175 {
176 this.routeGenerator = routeSupplier;
177 }
178 if (templates != null)
179 {
180 for (TemplateGTUType template : templates)
181 {
182 this.templates.put(template.getGTUType(), template);
183 }
184 }
185 this.factorySupplier = factorySupplier;
186 if (vehicleModelFactory == null)
187 {
188 this.vehicleModelFactory = VehicleModelFactory.MINMAX;
189 }
190 else
191 {
192 this.vehicleModelFactory = vehicleModelFactory;
193 }
194 }
195
196
197 @Override
198 public LaneBasedGTUCharacteristics draw(final Node origin, final Node destination, final Category category,
199 final StreamInterface randomStream) throws GTUException
200 {
201 Categorization categorization = category.getCategorization();
202
203 GTUType gtuType;
204 if (categorization.entails(GTUType.class))
205 {
206 gtuType = category.get(GTUType.class);
207 }
208 else if (this.gtuTypeGenerator != null)
209 {
210 gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GTUException.class, "Parameter while drawing GTU type.");
211 }
212 else
213 {
214 gtuType = origin.getNetwork().getGtuType(GTUType.DEFAULTS.CAR);
215 }
216 GTUCharacteristics gtuCharacteristics;
217 if (this.templates.containsKey(gtuType))
218 {
219 gtuCharacteristics =
220 Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
221 }
222 else
223 {
224 gtuCharacteristics = Try.assign(() -> GTUType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
225 "Exception while applying default GTU characteristics.");
226 }
227
228 LaneBasedStrategicalPlannerFactory<?> laneBasedStrategicalPlannerFactory =
229 this.factorySupplier.getFactory(origin, destination, category, randomStream);
230
231 Route route;
232 if (categorization.entails(Route.class))
233 {
234 route = category.get(Route.class);
235 }
236 else
237 {
238
239
240 route = this.routeGenerator.getRoute(origin, destination, gtuType);
241 }
242
243 VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
244
245 return new LaneBasedGTUCharacteristics(gtuCharacteristics, laneBasedStrategicalPlannerFactory, route, origin,
246 destination, vehicleModel);
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261 @SuppressWarnings("hiddenfield")
262 public static class Factory
263 {
264
265 private Generator<GTUType> gtuTypeGenerator = null;
266
267
268 private Set<TemplateGTUType> templates = new LinkedHashSet<>();
269
270
271 private RouteGeneratorOD routeGenerator = RouteGeneratorOD.NULL;
272
273
274 private StrategicalPlannerFactorySupplierOD factorySupplier = StrategicalPlannerFactorySupplierOD.lmrs();
275
276
277 private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
278
279
280
281
282
283 public Factory setGtuTypeGenerator(final Generator<GTUType> gtuTypeGenerator)
284 {
285 this.gtuTypeGenerator = gtuTypeGenerator;
286 return this;
287 }
288
289
290
291
292
293 public Factory setTemplates(final Set<TemplateGTUType> templates)
294 {
295 this.templates = templates;
296 return this;
297 }
298
299
300
301
302
303 public Factory setRouteSupplier(final RouteGeneratorOD routeSupplier)
304 {
305 this.routeGenerator = routeSupplier;
306 return this;
307 }
308
309
310
311
312
313 public Factory setFactorySupplier(final StrategicalPlannerFactorySupplierOD factorySupplier)
314 {
315 this.factorySupplier = factorySupplier;
316 return this;
317 }
318
319
320
321
322
323 public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
324 {
325 this.vehicleModelFactory = vehicleModelFactory;
326 return this;
327 }
328
329
330
331
332
333 @SuppressWarnings("synthetic-access")
334 public DefaultGTUCharacteristicsGeneratorOD create()
335 {
336 return new DefaultGTUCharacteristicsGeneratorOD(this.gtuTypeGenerator, this.routeGenerator, this.templates,
337 this.factorySupplier, this.vehicleModelFactory);
338 }
339 }
340
341 }