1 package org.opentrafficsim.road.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.IdSupplier;
12 import org.opentrafficsim.core.network.LinkType;
13 import org.opentrafficsim.core.network.Node;
14 import org.opentrafficsim.road.gtu.LaneBookkeeping;
15 import org.opentrafficsim.road.gtu.generator.CfBaRoomChecker;
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.characteristics.LaneBasedGtuCharacteristicsGeneratorOd;
20 import org.opentrafficsim.road.gtu.generator.headway.ArrivalsHeadwayGenerator.HeadwayDistribution;
21 import org.opentrafficsim.road.network.Lane;
22
23
24
25
26
27
28
29
30
31
32
33 public class OdOptions
34 {
35
36 public static final Option<HeadwayDistribution> HEADWAY_DIST =
37 new Option<>("headway distribution", HeadwayDistribution.EXPONENTIAL);
38
39
40 public static final Option<IdSupplier> GTU_ID = new Option<>("gtu id", new IdSupplier(""));
41
42
43 public static final Option<LaneBasedGtuCharacteristicsGeneratorOd> GTU_TYPE = new Option<>("gtu type", null);
44
45
46 public static final Option<RoomChecker> ROOM_CHECKER = new Option<>("room checker", new CfBaRoomChecker());
47
48
49 public static final Option<MarkovCorrelation<GtuType, Frequency>> MARKOV = new Option<>("markov", null);
50
51
52 public static final Option<Length> NO_LC_DIST = new Option<>("no lc distance", Length.ofSI(50.0));
53
54
55 public static final Option<LaneBookkeeping> BOOKKEEPING = new Option<>("bookkeeping", LaneBookkeeping.START);
56
57
58 public static final Option<GtuErrorHandler> ERROR_HANDLER = new Option<>("error handler", GtuErrorHandler.THROW);
59
60
61 public static final Option<LaneBiases> LANE_BIAS = new Option<>("lane bias", new LaneBiases());
62
63
64 private OptionSet<Void> options = new OptionSet<>();
65
66
67 private OptionSet<Lane> laneOptions = new OptionSet<>();
68
69
70 private OptionSet<Node> nodeOptions = new OptionSet<>();
71
72
73 private OptionSet<LinkType> linkTypeOptions = new OptionSet<>();
74
75
76
77
78 public OdOptions()
79 {
80
81 }
82
83
84
85
86
87
88
89
90 public final <K> OdOptions set(final Option<K> option, final K value)
91 {
92 this.options.set(null, option, value);
93 return this;
94 }
95
96
97
98
99
100
101
102
103
104 public final <K> OdOptions set(final Lane lane, final Option<K> option, final K value)
105 {
106 this.laneOptions.set(lane, option, value);
107 return this;
108 }
109
110
111
112
113
114
115
116
117
118 public final <K> OdOptions set(final Node node, final Option<K> option, final K value)
119 {
120 this.nodeOptions.set(node, option, value);
121 return this;
122 }
123
124
125
126
127
128
129
130
131
132 public final <K> OdOptions set(final LinkType linkType, final Option<K> option, final K value)
133 {
134 this.linkTypeOptions.set(linkType, option, value);
135 return this;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public final <K> K get(final Option<K> option, final Lane lane, final Node node, final LinkType linkType)
155 {
156 Throw.whenNull(option, "Option may not be null.");
157 K value = this.laneOptions.get(lane, option);
158 if (value != null)
159 {
160 return value;
161 }
162 value = this.nodeOptions.get(node, option);
163 if (value != null)
164 {
165 return value;
166 }
167 value = this.linkTypeOptions.get(linkType, option);
168 if (value != null)
169 {
170 return value;
171 }
172 value = this.options.get(null, option);
173 if (value != null)
174 {
175 return value;
176 }
177 return option.getDefaultValue();
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public static final class Option<K>
193 {
194
195
196 private final String id;
197
198
199 private final K defaultValue;
200
201
202
203
204
205
206 Option(final String id, final K defaultValue)
207 {
208 this.id = id;
209 this.defaultValue = defaultValue;
210 }
211
212
213
214
215
216 public K getDefaultValue()
217 {
218 return this.defaultValue;
219 }
220
221 @Override
222 public int hashCode()
223 {
224 final int prime = 31;
225 int result = 1;
226 result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
227 return result;
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 @Override
261 public String toString()
262 {
263 return "Option [id=" + this.id + "]";
264 }
265
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280 private class OptionSet<C>
281 {
282
283
284 private Map<C, Map<Option<?>, Object>> optionsSet = new LinkedHashMap<>();
285
286
287
288
289 OptionSet()
290 {
291
292 }
293
294
295
296
297
298
299
300
301 public <K> void set(final C category, final Option<K> option, final K value)
302 {
303 Map<Option<?>, Object> map = this.optionsSet.get(category);
304 if (map == null)
305 {
306 map = new LinkedHashMap<>();
307 this.optionsSet.put(category, map);
308 }
309 map.put(option, value);
310 }
311
312
313
314
315
316
317
318
319 @SuppressWarnings("unchecked")
320 public <K> K get(final C category, final Option<K> option)
321 {
322 if (!this.optionsSet.containsKey(category))
323 {
324 return null;
325 }
326 return (K) this.optionsSet.get(category).get(option);
327 }
328
329 }
330
331 }