1 package org.opentrafficsim.road.gtu.generator.characteristics;
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.definitions.DefaultsNl;
11 import org.opentrafficsim.core.distributions.Generator;
12 import org.opentrafficsim.core.gtu.GtuCharacteristics;
13 import org.opentrafficsim.core.gtu.GtuException;
14 import org.opentrafficsim.core.gtu.GtuTemplate;
15 import org.opentrafficsim.core.gtu.GtuType;
16 import org.opentrafficsim.core.network.Node;
17 import org.opentrafficsim.core.network.route.Route;
18 import org.opentrafficsim.road.gtu.lane.VehicleModel;
19 import org.opentrafficsim.road.gtu.lane.VehicleModelFactory;
20 import org.opentrafficsim.road.gtu.lane.tactical.following.IdmPlusFactory;
21 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLmrsPerceptionFactory;
22 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
23 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
24 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlannerFactory;
25 import org.opentrafficsim.road.od.Categorization;
26 import org.opentrafficsim.road.od.Category;
27
28 import nl.tudelft.simulation.jstats.streams.StreamInterface;
29
30
31
32
33
34
35
36
37
38
39
40 public final class DefaultLaneBasedGtuCharacteristicsGeneratorOd implements LaneBasedGtuCharacteristicsGeneratorOd
41 {
42
43 private Generator<GtuType> gtuTypeGenerator = null;
44
45
46 private final Map<GtuType, GtuTemplate> templates = new LinkedHashMap<>();
47
48
49 private final LaneBasedStrategicalPlannerFactory<?> factory;
50
51
52 private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
53
54
55
56
57
58
59
60
61 private DefaultLaneBasedGtuCharacteristicsGeneratorOd(final Generator<GtuType> gtuTypeGenerator,
62 final Set<GtuTemplate> templates, final LaneBasedStrategicalPlannerFactory<?> factory,
63 final VehicleModelFactory vehicleModelFactory)
64 {
65 Throw.whenNull(factory, "Strategical planner factory may not be null.");
66 this.gtuTypeGenerator = gtuTypeGenerator;
67 if (templates != null)
68 {
69 for (GtuTemplate template : templates)
70 {
71 this.templates.put(template.getGtuType(), template);
72 }
73 }
74 this.factory = factory;
75 if (vehicleModelFactory == null)
76 {
77 this.vehicleModelFactory = VehicleModelFactory.MINMAX;
78 }
79 else
80 {
81 this.vehicleModelFactory = vehicleModelFactory;
82 }
83 }
84
85
86 @Override
87 public LaneBasedGtuCharacteristics draw(final Node origin, final Node destination, final Category category,
88 final StreamInterface randomStream) throws GtuException
89 {
90 Categorization categorization = category.getCategorization();
91
92 GtuType gtuType;
93 if (categorization.entails(GtuType.class))
94 {
95 gtuType = category.get(GtuType.class);
96 }
97 else if (this.gtuTypeGenerator != null)
98 {
99 gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GtuException.class, "Parameter while drawing GTU type.");
100 }
101 else
102 {
103 gtuType = DefaultsNl.CAR;
104 }
105 GtuCharacteristics gtuCharacteristics;
106 if (this.templates.containsKey(gtuType))
107 {
108 gtuCharacteristics =
109 Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
110 }
111 else
112 {
113 gtuCharacteristics = Try.assign(() -> GtuType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
114 "Exception while applying default GTU characteristics.");
115 }
116 Route route = categorization.entails(Route.class) ? category.get(Route.class) : null;
117 VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
118
119 return new LaneBasedGtuCharacteristics(gtuCharacteristics, this.factory, route, origin, destination, vehicleModel);
120 }
121
122
123
124
125
126
127 public static LaneBasedStrategicalRoutePlannerFactory defaultLmrs(final StreamInterface stream)
128 {
129 return new LaneBasedStrategicalRoutePlannerFactory(
130 new LmrsFactory(new IdmPlusFactory(stream), new DefaultLmrsPerceptionFactory()));
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 @SuppressWarnings("hiddenfield")
145 public static class Factory
146 {
147
148 private Generator<GtuType> gtuTypeGenerator = null;
149
150
151 private Set<GtuTemplate> templates = new LinkedHashSet<>();
152
153
154 private final LaneBasedStrategicalPlannerFactory<?> factory;
155
156
157 private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
158
159
160
161
162
163 public Factory(final LaneBasedStrategicalPlannerFactory<?> factory)
164 {
165 this.factory = factory;
166 }
167
168
169
170
171
172 public Factory setGtuTypeGenerator(final Generator<GtuType> gtuTypeGenerator)
173 {
174 this.gtuTypeGenerator = gtuTypeGenerator;
175 return this;
176 }
177
178
179
180
181
182 public Factory setTemplates(final Set<GtuTemplate> templates)
183 {
184 this.templates = templates;
185 return this;
186 }
187
188
189
190
191
192 public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
193 {
194 this.vehicleModelFactory = vehicleModelFactory;
195 return this;
196 }
197
198
199
200
201
202 @SuppressWarnings("synthetic-access")
203 public DefaultLaneBasedGtuCharacteristicsGeneratorOd create()
204 {
205 return new DefaultLaneBasedGtuCharacteristicsGeneratorOd(this.gtuTypeGenerator, this.templates, this.factory,
206 this.vehicleModelFactory);
207 }
208 }
209
210 }