1 package org.opentrafficsim.road.network.factory.xml.utils;
2
3 import java.util.Map;
4
5 import org.djunits.unit.AccelerationUnit;
6 import org.djunits.unit.DurationUnit;
7 import org.djunits.unit.LengthUnit;
8 import org.djunits.unit.PositionUnit;
9 import org.djunits.unit.SpeedUnit;
10 import org.djunits.unit.TimeUnit;
11 import org.djunits.value.vdouble.scalar.Acceleration;
12 import org.djunits.value.vdouble.scalar.Duration;
13 import org.djunits.value.vdouble.scalar.Length;
14 import org.djunits.value.vdouble.scalar.Position;
15 import org.djunits.value.vdouble.scalar.Speed;
16 import org.djunits.value.vdouble.scalar.Time;
17 import org.opentrafficsim.base.parameters.ParameterException;
18 import org.opentrafficsim.core.distributions.Generator;
19 import org.opentrafficsim.core.distributions.ProbabilityException;
20 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
21 import org.opentrafficsim.road.network.factory.xml.XmlParserException;
22 import org.opentrafficsim.xml.generated.ACCELERATIONDISTTYPE;
23 import org.opentrafficsim.xml.generated.DURATIONDISTTYPE;
24 import org.opentrafficsim.xml.generated.LENGTHDISTTYPE;
25 import org.opentrafficsim.xml.generated.POSITIONDISTTYPE;
26 import org.opentrafficsim.xml.generated.SPEEDDISTTYPE;
27 import org.opentrafficsim.xml.generated.TIMEDISTTYPE;
28
29
30
31
32
33
34
35
36
37 public final class Generators
38 {
39
40
41
42 private Generators()
43 {
44
45 }
46
47
48
49
50
51
52
53
54 public static Generator<Length> makeLengthGenerator(Map<String, StreamInformation> streamMap,
55 final LENGTHDISTTYPE lengthDist) throws XmlParserException
56 {
57 try
58 {
59 final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> dist =
60 ParseDistribution.parseLengthDist(streamMap, lengthDist);
61 Generator<Length> generator = new Generator<Length>()
62 {
63 @Override
64 public Length draw() throws ProbabilityException, ParameterException
65 {
66 return dist.draw();
67 }
68
69
70 @Override
71 public String toString()
72 {
73 return "Generator<Length>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
74 }
75 };
76 return generator;
77 }
78 catch (Exception exception)
79 {
80 throw new XmlParserException(exception);
81 }
82 }
83
84
85
86
87
88
89
90
91 public static Generator<Position> makePositionGenerator(Map<String, StreamInformation> streamMap,
92 final POSITIONDISTTYPE positionDist) throws XmlParserException
93 {
94 try
95 {
96 final ContinuousDistDoubleScalar.Abs<Position, PositionUnit, LengthUnit> dist =
97 ParseDistribution.parsePositionDist(streamMap, positionDist);
98 Generator<Position> generator = new Generator<Position>()
99 {
100 @Override
101 public Position draw() throws ProbabilityException, ParameterException
102 {
103 return dist.draw();
104 }
105
106
107 @Override
108 public String toString()
109 {
110 return "Generator<Position>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
111 }
112 };
113 return generator;
114 }
115 catch (Exception exception)
116 {
117 throw new XmlParserException(exception);
118 }
119 }
120
121
122
123
124
125
126
127
128 public static Generator<Duration> makeDurationGenerator(Map<String, StreamInformation> streamMap,
129 final DURATIONDISTTYPE durationDist) throws XmlParserException
130 {
131 try
132 {
133 final ContinuousDistDoubleScalar.Rel<Duration, DurationUnit> dist =
134 ParseDistribution.parseDurationDist(streamMap, durationDist);
135 Generator<Duration> generator = new Generator<Duration>()
136 {
137 @Override
138 public Duration draw() throws ProbabilityException, ParameterException
139 {
140 return dist.draw();
141 }
142
143
144 @Override
145 public String toString()
146 {
147 return "Generator<Duration>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
148 }
149 };
150 return generator;
151 }
152 catch (Exception exception)
153 {
154 throw new XmlParserException(exception);
155 }
156 }
157
158
159
160
161
162
163
164
165 public static Generator<Time> makeTimeGenerator(Map<String, StreamInformation> streamMap, final TIMEDISTTYPE timeDist)
166 throws XmlParserException
167 {
168 try
169 {
170 final ContinuousDistDoubleScalar.Abs<Time, TimeUnit, DurationUnit> dist =
171 ParseDistribution.parseTimeDist(streamMap, timeDist);
172 Generator<Time> generator = new Generator<Time>()
173 {
174 @Override
175 public Time draw() throws ProbabilityException, ParameterException
176 {
177 return dist.draw();
178 }
179
180
181 @Override
182 public String toString()
183 {
184 return "Generator<Time>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
185 }
186 };
187 return generator;
188 }
189 catch (Exception exception)
190 {
191 throw new XmlParserException(exception);
192 }
193 }
194
195
196
197
198
199
200
201
202 public static Generator<Speed> makeSpeedGenerator(Map<String, StreamInformation> streamMap, final SPEEDDISTTYPE speedDist)
203 throws XmlParserException
204 {
205 try
206 {
207 final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> dist =
208 ParseDistribution.parseSpeedDist(streamMap, speedDist);
209 Generator<Speed> generator = new Generator<Speed>()
210 {
211 @Override
212 public Speed draw() throws ProbabilityException, ParameterException
213 {
214 return dist.draw();
215 }
216
217
218 @Override
219 public String toString()
220 {
221 return "Generator<Speed>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
222 }
223 };
224 return generator;
225 }
226 catch (Exception exception)
227 {
228 throw new XmlParserException(exception);
229 }
230 }
231
232
233
234
235
236
237
238
239 public static Generator<Acceleration> makeAccelerationGenerator(Map<String, StreamInformation> streamMap,
240 final ACCELERATIONDISTTYPE accelerationDist) throws XmlParserException
241 {
242 try
243 {
244 final ContinuousDistDoubleScalar.Rel<Acceleration, AccelerationUnit> dist =
245 ParseDistribution.parseAccelerationDist(streamMap, accelerationDist);
246 Generator<Acceleration> generator = new Generator<Acceleration>()
247 {
248 @Override
249 public Acceleration draw() throws ProbabilityException, ParameterException
250 {
251 return dist.draw();
252 }
253
254
255 @Override
256 public String toString()
257 {
258 return "Generator<Acceleration>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
259 }
260 };
261 return generator;
262 }
263 catch (Exception exception)
264 {
265 throw new XmlParserException(exception);
266 }
267 }
268
269
270
271
272
273
274
275
276 public static Generator<Acceleration> makeDecelerationGenerator(Map<String, StreamInformation> streamMap,
277 final ACCELERATIONDISTTYPE decelerationDist) throws XmlParserException
278 {
279 try
280 {
281 final ContinuousDistDoubleScalar.Rel<Acceleration, AccelerationUnit> dist =
282 ParseDistribution.parseAccelerationDist(streamMap, decelerationDist);
283 Generator<Acceleration> generator = new Generator<Acceleration>()
284 {
285 @Override
286 public Acceleration draw() throws ProbabilityException, ParameterException
287 {
288 return dist.draw().multiplyBy(-1.0);
289 }
290
291
292 @Override
293 public String toString()
294 {
295 return "Generator<Deceleration>(" + dist.getDistribution().toString() + " " + dist.getUnit() + ")";
296 }
297 };
298 return generator;
299 }
300 catch (Exception exception)
301 {
302 throw new XmlParserException(exception);
303 }
304 }
305
306 }