1 package org.opentrafficsim.core.gtu;
2
3 import java.io.Serializable;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.djunits.unit.SpeedUnit;
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.djunits.value.vdouble.scalar.Speed;
10 import org.djutils.exceptions.Throw;
11 import org.opentrafficsim.base.HierarchicalType;
12 import org.opentrafficsim.base.parameters.ParameterException;
13 import org.opentrafficsim.core.distributions.ConstantGenerator;
14 import org.opentrafficsim.core.distributions.ProbabilityException;
15 import org.opentrafficsim.core.units.distributions.ContinuousDistSpeed;
16
17 import nl.tudelft.simulation.jstats.distributions.DistNormal;
18 import nl.tudelft.simulation.jstats.streams.StreamInterface;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public final class GTUType extends HierarchicalType<GTUType> implements Serializable
34 {
35
36 private static final long serialVersionUID = 20141231L;
37
38
39 public static final GTUType ROAD_USER;
40
41
42 public static final GTUType WATER_WAY_USER;
43
44
45 public static final GTUType RAIL_WAY_USER;
46
47
48 public static final GTUType PEDESTRIAN;
49
50
51 public static final GTUType BICYCLE;
52
53
54 public static final GTUType MOPED;
55
56
57 public static final GTUType VEHICLE;
58
59
60 public static final GTUType EMERGENCY_VEHICLE;
61
62
63 public static final GTUType SHIP;
64
65
66 public static final GTUType TRAIN;
67
68
69 public static final GTUType CAR;
70
71
72 public static final GTUType VAN;
73
74
75 public static final GTUType BUS;
76
77
78 public static final GTUType TRUCK;
79
80
81 public static final GTUType SCHEDULED_BUS;
82
83 static
84 {
85 ROAD_USER = new GTUType("ROAD_USER", null);
86 WATER_WAY_USER = new GTUType("WATER_WAY_USER", null);
87 RAIL_WAY_USER = new GTUType("RAIL_WAY_USER", null);
88
89 SHIP = new GTUType("SHIP", WATER_WAY_USER);
90 TRAIN = new GTUType("TRAIN", RAIL_WAY_USER);
91 PEDESTRIAN = new GTUType("PEDESTRIAN", ROAD_USER);
92 BICYCLE = new GTUType("BICYCLE", ROAD_USER);
93
94 MOPED = new GTUType("MOPED", BICYCLE);
95
96 VEHICLE = new GTUType("VEHICLE", ROAD_USER);
97 EMERGENCY_VEHICLE = new GTUType("EMERGENCY_VEHICLE", VEHICLE);
98 CAR = new GTUType("CAR", VEHICLE);
99 VAN = new GTUType("VAN", VEHICLE);
100 BUS = new GTUType("BUS", VEHICLE);
101 TRUCK = new GTUType("TRUCK", VEHICLE);
102 SCHEDULED_BUS = new GTUType("SCHEDULED BUS", BUS);
103 }
104
105
106 private static final Map<StreamInterface, Map<GTUType, TemplateGTUType>> TEMPLATES = new HashMap<>();
107
108
109
110
111
112
113
114
115 public static GTUCharacteristics defaultCharacteristics(final GTUType gtuType, final StreamInterface randomStream)
116 throws GTUException
117 {
118 Map<GTUType, TemplateGTUType> map = TEMPLATES.get(randomStream);
119 if (map == null)
120 {
121 map = new HashMap<>();
122 TEMPLATES.put(randomStream, map);
123 }
124 GTUType type = gtuType;
125 TemplateGTUType template = map.get(type);
126 while (template == null)
127 {
128 if (type.equals(GTUType.CAR))
129 {
130
131 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(4.19)),
132 new ConstantGenerator<>(Length.createSI(1.7)),
133 new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
134 }
135 else if (type.equals(GTUType.TRUCK))
136 {
137
138 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(12.0)),
139 new ConstantGenerator<>(Length.createSI(2.55)),
140 new ContinuousDistSpeed(new DistNormal(randomStream, 85.0, 2.5), SpeedUnit.KM_PER_HOUR));
141 }
142 else if (type.equals(GTUType.BUS))
143 {
144 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(12.0)),
145 new ConstantGenerator<>(Length.createSI(2.55)),
146 new ConstantGenerator<>(new Speed(90, SpeedUnit.KM_PER_HOUR)));
147 }
148 else if (type.equals(GTUType.VAN))
149 {
150 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(5.0)),
151 new ConstantGenerator<>(Length.createSI(2.4)),
152 new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
153 }
154 else if (type.equals(GTUType.EMERGENCY_VEHICLE))
155 {
156 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(5.0)),
157 new ConstantGenerator<>(Length.createSI(2.55)),
158 new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
159 }
160 else
161 {
162 type = type.getParent();
163 Throw.whenNull(type, "GTUType %s is not of any types with default characteristics.", gtuType);
164 }
165 if (template != null)
166 {
167 map.put(type, template);
168 }
169 }
170 try
171 {
172 return template.draw();
173 }
174 catch (ProbabilityException | ParameterException exception)
175 {
176 throw new GTUException("GTUType draw failed.", exception);
177 }
178 }
179
180
181
182
183
184 private GTUType(final String id) throws NullPointerException
185 {
186 super(id);
187 }
188
189
190
191
192
193
194 public GTUType(final String id, final GTUType parent) throws NullPointerException
195 {
196 super(id, parent);
197 }
198
199
200 @Override
201 public String toString()
202 {
203 return "GTUType: " + this.getId();
204 }
205
206 }