1 package org.opentrafficsim.road.gtu.generator.od;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.djunits.value.vdouble.scalar.Frequency;
7 import org.djunits.value.vdouble.scalar.Length;
8 import org.opentrafficsim.core.gtu.GTUType;
9 import org.opentrafficsim.core.gtu.animation.GTUColorer;
10 import org.opentrafficsim.core.idgenerator.IdGenerator;
11 import org.opentrafficsim.core.network.LinkType;
12 import org.opentrafficsim.core.network.Node;
13 import org.opentrafficsim.road.gtu.animation.DefaultSwitchableGTUColorer;
14 import org.opentrafficsim.road.gtu.generator.CFBARoomChecker;
15 import org.opentrafficsim.road.gtu.generator.GeneratorPositions.LaneBias;
16 import org.opentrafficsim.road.gtu.generator.GeneratorPositions.LaneBiases;
17 import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.RoomChecker;
18 import org.opentrafficsim.road.gtu.generator.MarkovCorrelation;
19 import org.opentrafficsim.road.gtu.generator.headway.ArrivalsHeadwayGenerator.HeadwayDistribution;
20 import org.opentrafficsim.road.network.lane.Lane;
21
22 import nl.tudelft.simulation.language.Throw;
23
24
25
26
27
28
29
30
31
32
33
34
35 public class ODOptions
36 {
37
38
39 public static final Option<HeadwayDistribution> HEADWAY_DIST =
40 new Option<>("headway distribution", HeadwayDistribution.EXPONENTIAL);
41
42
43 public static final Option<IdGenerator> GTU_ID = new Option<>("gtu id", new IdGenerator(""));
44
45
46 public static final Option<GTUColorer> GTU_COLORER = new Option<>("gtu colorer", new DefaultSwitchableGTUColorer());
47
48
49 public static final Option<GTUCharacteristicsGeneratorOD> GTU_TYPE =
50 new Option<>("gtu type", new DefaultGTUCharacteristicsGeneratorOD());
51
52
53 public static final Option<RoomChecker> ROOM_CHECKER = new Option<>("room checker", new CFBARoomChecker());
54
55
56 public static final Option<MarkovCorrelation<GTUType, Frequency>> MARKOV = new Option<>("markov", null);
57
58
59 public static final Option<LaneBiases> LANE_BIAS = new Option<>("lane bias",
60 new LaneBiases().addBias(GTUType.TRUCK, LaneBias.TRUCK_RIGHT).addBias(GTUType.VEHICLE, LaneBias.WEAK_LEFT));
61
62
63 public static final Option<Length> NO_LC_DIST = new Option<>("no lc distance", null);
64
65
66 public static final Option<Boolean> ANIMATION = new Option<>("show generator animation", false);
67
68
69 private OptionSet<Void> options = new OptionSet<>();
70
71
72 private OptionSet<Lane> laneOptions = new OptionSet<>();
73
74
75 private OptionSet<Node> nodeOptions = new OptionSet<>();
76
77
78 private OptionSet<LinkType> linkTypeOptions = new OptionSet<>();
79
80
81
82
83
84
85
86
87 public final <K> ODOptions set(final Option<K> option, final K value)
88 {
89 this.options.set(null, option, value);
90 return this;
91 }
92
93
94
95
96
97
98
99
100
101 public final <K> ODOptions set(final Lane lane, final Option<K> option, final K value)
102 {
103 this.laneOptions.set(lane, option, value);
104 return this;
105 }
106
107
108
109
110
111
112
113
114
115 public final <K> ODOptions set(final Node node, final Option<K> option, final K value)
116 {
117 this.nodeOptions.set(node, option, value);
118 return this;
119 }
120
121
122
123
124
125
126
127
128
129 public final <K> ODOptions set(final LinkType linkType, final Option<K> option, final K value)
130 {
131 this.linkTypeOptions.set(linkType, option, value);
132 return this;
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public final <K> K get(final Option<K> option, final Lane lane, final Node node, final LinkType linkType)
152 {
153 Throw.whenNull(option, "Option may not be null.");
154 K value = this.laneOptions.get(lane, option);
155 if (value != null)
156 {
157 return value;
158 }
159 value = this.nodeOptions.get(node, option);
160 if (value != null)
161 {
162 return value;
163 }
164 value = this.linkTypeOptions.get(linkType, option);
165 if (value != null)
166 {
167 return value;
168 }
169 value = this.options.get(null, option);
170 if (value != null)
171 {
172 return value;
173 }
174 return option.getDefaultValue();
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 private static final class Option<K>
191 {
192
193
194 private final String id;
195
196
197 private final K defaultValue;
198
199
200
201
202
203
204 Option(final String id, final K defaultValue)
205 {
206 this.id = id;
207 this.defaultValue = defaultValue;
208 }
209
210
211
212
213
214 public K getDefaultValue()
215 {
216 return this.defaultValue;
217 }
218
219
220 @Override
221 public int hashCode()
222 {
223 final int prime = 31;
224 int result = 1;
225 result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
226 return result;
227 }
228
229
230 @Override
231 public boolean equals(final Object obj)
232 {
233 if (this == obj)
234 {
235 return true;
236 }
237 if (obj == null)
238 {
239 return false;
240 }
241 if (getClass() != obj.getClass())
242 {
243 return false;
244 }
245 Option<?> other = (Option<?>) obj;
246 if (this.id == null)
247 {
248 if (other.id != null)
249 {
250 return false;
251 }
252 }
253 else if (!this.id.equals(other.id))
254 {
255 return false;
256 }
257 return true;
258 }
259
260
261 @Override
262 public String toString()
263 {
264 return "Option [id=" + this.id + "]";
265 }
266
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 private class OptionSet<C>
283 {
284
285
286 private Map<C, Map<Option<?>, Object>> optionsSet = new HashMap<>();
287
288
289
290
291 OptionSet()
292 {
293
294 }
295
296
297
298
299
300
301
302
303 public <K> void set(final C category, final Option<K> option, final K value)
304 {
305 Map<Option<?>, Object> map = this.optionsSet.get(category);
306 if (map == null)
307 {
308 map = new HashMap<>();
309 this.optionsSet.put(category, map);
310 }
311 map.put(option, value);
312 }
313
314
315
316
317
318
319
320
321 @SuppressWarnings("unchecked")
322 public <K> K get(final C category, final Option<K> option)
323 {
324 if (!this.optionsSet.containsKey(category))
325 {
326 return null;
327 }
328 return (K) this.optionsSet.get(category).get(option);
329 }
330
331 }
332
333 }