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