View Javadoc
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   * Generators based on distribution tags for typed scalars. <br>
31   * <br>
32   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
33   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
34   * source code and binary code of this software is proprietary information of Delft University of Technology.
35   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
36   */
37  public final class Generators
38  {
39      /**
40       * 
41       */
42      private Generators()
43      {
44          // utility class
45      }
46  
47      /**
48       * Parse a Length distribution into a Generator for Lengths
49       * @param streamMap the map with predefined streams
50       * @param lengthDist the tag to parse
51       * @return the generator
52       * @throws XmlParserException on parse error
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                  /** {@inheritDoc} */
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       * Parse a Position distribution into a Generator for Positions
86       * @param streamMap the map with predefined streams
87       * @param positionDist the tag to parse
88       * @return the generator
89       * @throws XmlParserException on parse error
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                 /** {@inheritDoc} */
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      * Parse a Duration distribution into a Generator for Durations
123      * @param streamMap the map with predefined streams
124      * @param durationDist the tag to parse
125      * @return the generator
126      * @throws XmlParserException on parse error
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                 /** {@inheritDoc} */
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      * Parse a Time distribution into a Generator for Times
160      * @param streamMap the map with predefined streams
161      * @param timeDist the tag to parse
162      * @return the generator
163      * @throws XmlParserException on parse error
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                 /** {@inheritDoc} */
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      * Parse a Speed distribution into a Generator for Speeds
197      * @param streamMap the map with predefined streams
198      * @param speedDist the tag to parse
199      * @return the generator
200      * @throws XmlParserException on parse error
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                 /** {@inheritDoc} */
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      * Parse an Acceleration distribution into a Generator for Accelerations.
234      * @param streamMap the map with predefined streams
235      * @param accelerationDist the tag to parse
236      * @return the generator
237      * @throws XmlParserException on parse error
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                 /** {@inheritDoc} */
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      * Parse an Acceleration distribution into a Generator for Decelerations (accceleration with a minus sign).
271      * @param streamMap the map with predefined streams
272      * @param decelerationDist the tag to parse
273      * @return the generator
274      * @throws XmlParserException on parse error
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                 /** {@inheritDoc} */
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 }