1 package org.opentrafficsim.road.car;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7
8 import javax.naming.NamingException;
9
10 import nl.tudelft.simulation.dsol.SimRuntimeException;
11 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
12 import nl.tudelft.simulation.language.reflection.ClassUtil;
13
14 import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
15 import org.opentrafficsim.core.gtu.GTUException;
16 import org.opentrafficsim.core.gtu.RelativePosition;
17 import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
18 import org.opentrafficsim.core.gtu.TemplateGTUType;
19 import org.opentrafficsim.core.network.NetworkException;
20 import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
21 import org.opentrafficsim.road.gtu.following.GTUFollowingModel;
22 import org.opentrafficsim.road.gtu.lane.AbstractLaneBasedTemplateGTU;
23 import org.opentrafficsim.road.network.lane.Lane;
24 import org.opentrafficsim.road.network.route.CompleteLaneBasedRouteNavigator;
25
26
27
28
29
30
31
32
33
34
35
36 public class LaneBasedTemplateCar extends AbstractLaneBasedTemplateGTU
37 {
38
39 private static final long serialVersionUID = 20141025L;
40
41
42 private Renderable2D animation;
43
44
45 private final Map<RelativePosition.TYPE, RelativePosition> relativePositions = new LinkedHashMap<>();
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public LaneBasedTemplateCar(final String id, final TemplateGTUType templateGtuType,
61 final GTUFollowingModel gtuFollowingModel, final Map<Lane, Length.Rel> initialLongitudinalPositions,
62 final Speed.Abs initialSpeed, final CompleteLaneBasedRouteNavigator routeNavigator) throws NamingException,
63 NetworkException, SimRuntimeException, GTUException
64 {
65 this(id, templateGtuType, gtuFollowingModel, initialLongitudinalPositions, initialSpeed, routeNavigator,
66 DefaultCarAnimation.class);
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public LaneBasedTemplateCar(final String id, final TemplateGTUType templateGtuType,
84 final GTUFollowingModel gtuFollowingModel, final Map<Lane, Length.Rel> initialLongitudinalPositions,
85 final Speed.Abs initialSpeed, final CompleteLaneBasedRouteNavigator routeNavigator,
86 final Class<? extends Renderable2D> animationClass) throws NamingException, NetworkException,
87 SimRuntimeException, GTUException
88 {
89 super(id, templateGtuType, gtuFollowingModel, initialLongitudinalPositions, initialSpeed, routeNavigator);
90
91
92
93
94 Length.Rel zero = new Length.Rel(0.0d, METER);
95 Length.Rel dx = new Length.Rel(getLength().getSI(), METER);
96 this.relativePositions
97 .put(RelativePosition.FRONT, new RelativePosition(dx, zero, zero, RelativePosition.FRONT));
98 this.relativePositions
99 .put(RelativePosition.REAR, new RelativePosition(zero, zero, zero, RelativePosition.REAR));
100 this.relativePositions.put(RelativePosition.REFERENCE, RelativePosition.REFERENCE_POSITION);
101
102
103 if (getSimulator() instanceof OTSAnimatorInterface && animationClass != null)
104 {
105 try
106 {
107 Constructor<?> constructor =
108 ClassUtil.resolveConstructor(animationClass, new Object[]{this, getSimulator()});
109 this.animation = (Renderable2D) constructor.newInstance(this, getSimulator());
110 }
111 catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
112 | IllegalArgumentException | InvocationTargetException exception)
113 {
114 throw new NetworkException("Could not instantiate car animation of type " + animationClass.getName(),
115 exception);
116 }
117 }
118 }
119
120
121 @Override
122 @SuppressWarnings("checkstyle:designforextension")
123 public RelativePosition getFront()
124 {
125 return this.relativePositions.get(RelativePosition.FRONT);
126 }
127
128
129 @Override
130 @SuppressWarnings("checkstyle:designforextension")
131 public RelativePosition getRear()
132 {
133 return this.relativePositions.get(RelativePosition.REAR);
134 }
135
136
137 @Override
138 public final Map<TYPE, RelativePosition> getRelativePositions()
139 {
140 return this.relativePositions;
141 }
142
143
144 @Override
145 public final void destroy()
146 {
147 if (this.animation != null)
148 {
149 try
150 {
151 this.animation.destroy();
152 }
153 catch (Exception e)
154 {
155 System.err.println("Car: " + this.getId());
156 e.printStackTrace();
157 }
158 }
159 super.destroy();
160 }
161
162
163 public final String toString()
164 {
165 try
166 {
167 Map<Lane, Length.Rel> frontPositions = positions(getFront());
168 Lane frontLane = frontPositions.keySet().iterator().next();
169 return String.format("Car %s front:%s[%s]", getId(), frontLane, frontPositions.get(frontLane));
170 }
171 catch (NetworkException exception)
172 {
173 exception.printStackTrace();
174 }
175 return "Caught exception in toString";
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 @SuppressWarnings("checkstyle:hiddenfield")
204 public static class LaneBasedTemplateCarBuilder
205 {
206
207 private String id = null;
208
209
210 private TemplateGTUType templateGtuType = null;
211
212
213 private Map<Lane, Length.Rel> initialLongitudinalPositions = null;;
214
215
216 private Speed.Abs initialSpeed = null;
217
218
219 private GTUFollowingModel gtuFollowingModel = null;
220
221
222 private CompleteLaneBasedRouteNavigator routeNavigator = null;
223
224
225 private Class<? extends Renderable2D> animationClass = null;
226
227
228
229
230
231 public final LaneBasedTemplateCarBuilder setId(final String id)
232 {
233 this.id = id;
234 return this;
235 }
236
237
238
239
240
241 public final LaneBasedTemplateCarBuilder setTemplateGtuType(final TemplateGTUType templateGtuType)
242 {
243 this.templateGtuType = templateGtuType;
244 return this;
245 }
246
247
248
249
250
251 public final LaneBasedTemplateCarBuilder setInitialLongitudinalPositions(
252 final Map<Lane, Length.Rel> initialLongitudinalPositions)
253 {
254 this.initialLongitudinalPositions = initialLongitudinalPositions;
255 return this;
256 }
257
258
259
260
261
262 public final LaneBasedTemplateCarBuilder setInitialSpeed(final Speed.Abs initialSpeed)
263 {
264 this.initialSpeed = initialSpeed;
265 return this;
266 }
267
268
269
270
271
272 public final LaneBasedTemplateCarBuilder
273 setRouteNavigator(final CompleteLaneBasedRouteNavigator routeNavigator)
274 {
275 this.routeNavigator = routeNavigator;
276 return this;
277 }
278
279
280
281
282
283 public final LaneBasedTemplateCarBuilder setAnimationClass(final Class<? extends Renderable2D> animationClass)
284 {
285 this.animationClass = animationClass;
286 return this;
287 }
288
289
290
291
292
293
294
295
296 public final LaneBasedTemplateCar build() throws NamingException, NetworkException, SimRuntimeException,
297 GTUException
298 {
299
300
301 return new LaneBasedTemplateCar(this.id, this.templateGtuType, this.gtuFollowingModel,
302 this.initialLongitudinalPositions, this.initialSpeed, this.routeNavigator, this.animationClass);
303 }
304 }
305
306 }