1 package org.opentrafficsim.core.gtu;
2
3 import java.io.Serializable;
4 import java.util.HashMap;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7
8 import org.djunits.unit.SpeedUnit;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.Speed;
11 import org.djutils.exceptions.Throw;
12 import org.opentrafficsim.base.HierarchicalType;
13 import org.opentrafficsim.base.parameters.ParameterException;
14 import org.opentrafficsim.core.distributions.ConstantGenerator;
15 import org.opentrafficsim.core.distributions.ProbabilityException;
16 import org.opentrafficsim.core.network.Network;
17 import org.opentrafficsim.core.units.distributions.ContinuousDistSpeed;
18
19 import nl.tudelft.simulation.jstats.distributions.DistNormal;
20 import nl.tudelft.simulation.jstats.streams.StreamInterface;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public final class GTUType extends HierarchicalType<GTUType> implements Serializable
36 {
37
38 private static final long serialVersionUID = 20141231L;
39
40
41 public enum DEFAULTS
42 {
43
44 ROAD_USER("ROAD_USER"),
45
46
47 WATERWAY_USER("WATERWAY_USER"),
48
49
50 RAILWAY_USER("RAILWAY_USER"),
51
52
53 PEDESTRIAN("PEDESTRIAN", ROAD_USER),
54
55
56 BICYCLE("BICYCLE", ROAD_USER),
57
58
59 MOPED("MOPED", BICYCLE),
60
61
62 VEHICLE("VEHICLE", ROAD_USER),
63
64
65 EMERGENCY_VEHICLE("EMERGENCY_VEHICLE", VEHICLE),
66
67
68 SHIP("SHIP", WATERWAY_USER),
69
70
71 TRAIN("TRAIN", RAILWAY_USER),
72
73
74 CAR("CAR", VEHICLE),
75
76
77 VAN("VAN", VEHICLE),
78
79
80 BUS("BUS", VEHICLE),
81
82
83 TRUCK("TRUCK", VEHICLE),
84
85
86 SCHEDULED_BUS("SCHEDULED_BUS", BUS);
87
88
89 private final String id;
90
91
92 private final DEFAULTS parent;
93
94
95
96
97
98 DEFAULTS(final String id)
99 {
100 this.id = id;
101 this.parent = null;
102 }
103
104
105
106
107
108
109 DEFAULTS(final String id, final DEFAULTS parent)
110 {
111 this.id = id;
112 this.parent = parent;
113 }
114
115
116 public String getId()
117 {
118 return this.id;
119 }
120
121
122 public DEFAULTS getParent()
123 {
124 return this.parent;
125 }
126 }
127
128
129 private final Network network;
130
131
132 private static final Map<Network, Map<DEFAULTS, TemplateGTUType>> DEFAULT_TEMPLATES = new HashMap<>();
133
134
135
136
137
138
139 public GTUType(final String id, final Network network) throws NullPointerException
140 {
141 super(id);
142 this.network = network;
143 this.network.addGtuType(this);
144 }
145
146
147
148
149
150
151 public GTUType(final String id, final GTUType parent) throws NullPointerException
152 {
153 super(id, parent);
154 this.network = parent.getNetwork();
155 this.network.addGtuType(this);
156 }
157
158
159
160
161
162
163 public boolean isOfType(final DEFAULTS type)
164 {
165 if (this.getId().equals(type.getId()))
166 {
167 return true;
168 }
169 if (getParent() != null)
170 {
171 return getParent().isOfType(type);
172 }
173 return false;
174 }
175
176
177
178
179
180
181 public boolean isType(final DEFAULTS type)
182 {
183 return this.getId().equals(type.getId());
184 }
185
186
187
188
189
190
191
192
193
194 public static GTUCharacteristics defaultCharacteristics(final GTUType gtuType, final Network network,
195 final StreamInterface randomStream) throws GTUException
196 {
197 Map<DEFAULTS, TemplateGTUType> map = DEFAULT_TEMPLATES.get(network);
198 if (map == null)
199 {
200 map = new LinkedHashMap<>();
201 DEFAULT_TEMPLATES.put(network, map);
202 }
203 TemplateGTUType template = null;
204 if (map.containsKey(gtuType))
205 {
206 template = map.get(gtuType);
207 }
208 GTUType type = gtuType;
209 DEFAULTS defaultType = null;
210 while (template == null)
211 {
212 if (type.equals(network.getGtuType(DEFAULTS.CAR)))
213 {
214
215 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(4.19)),
216 new ConstantGenerator<>(Length.createSI(1.7)),
217 new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
218 defaultType = DEFAULTS.CAR;
219 }
220 else if (type.equals(network.getGtuType(DEFAULTS.TRUCK)))
221 {
222
223 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(12.0)),
224 new ConstantGenerator<>(Length.createSI(2.55)),
225 new ContinuousDistSpeed(new DistNormal(randomStream, 85.0, 2.5), SpeedUnit.KM_PER_HOUR));
226 defaultType = DEFAULTS.TRUCK;
227 }
228 else if (type.equals(network.getGtuType(DEFAULTS.BUS)))
229 {
230 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(12.0)),
231 new ConstantGenerator<>(Length.createSI(2.55)),
232 new ConstantGenerator<>(new Speed(90, SpeedUnit.KM_PER_HOUR)));
233 defaultType = DEFAULTS.BUS;
234 }
235 else if (type.equals(network.getGtuType(DEFAULTS.VAN)))
236 {
237 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(5.0)),
238 new ConstantGenerator<>(Length.createSI(2.4)),
239 new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
240 defaultType = DEFAULTS.VAN;
241 }
242 else if (type.equals(network.getGtuType(DEFAULTS.EMERGENCY_VEHICLE)))
243 {
244 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(5.0)),
245 new ConstantGenerator<>(Length.createSI(2.55)),
246 new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
247 defaultType = DEFAULTS.EMERGENCY_VEHICLE;
248 }
249 else
250 {
251 type = type.getParent();
252 Throw.whenNull(type, "GTUType %s is not of any types with default characteristics.", gtuType);
253 }
254 if (template != null)
255 {
256 map.put(defaultType, template);
257 }
258 }
259 try
260 {
261 return template.draw();
262 }
263 catch (ProbabilityException | ParameterException exception)
264 {
265 throw new GTUException("GTUType draw failed.", exception);
266 }
267 }
268
269
270
271
272 public Network getNetwork()
273 {
274 return this.network;
275 }
276
277
278 @Override
279 public String toString()
280 {
281 return "GTUType: " + this.getId();
282 }
283
284 }