Generators.java

  1. package org.opentrafficsim.road.network.factory.xml.utils;

  2. import java.util.Map;

  3. import org.djunits.unit.AccelerationUnit;
  4. import org.djunits.unit.DurationUnit;
  5. import org.djunits.unit.LengthUnit;
  6. import org.djunits.unit.PositionUnit;
  7. import org.djunits.unit.SpeedUnit;
  8. import org.djunits.unit.TimeUnit;
  9. import org.djunits.value.vdouble.scalar.Acceleration;
  10. import org.djunits.value.vdouble.scalar.Duration;
  11. import org.djunits.value.vdouble.scalar.Length;
  12. import org.djunits.value.vdouble.scalar.Position;
  13. import org.djunits.value.vdouble.scalar.Speed;
  14. import org.djunits.value.vdouble.scalar.Time;
  15. import org.opentrafficsim.base.parameters.ParameterException;
  16. import org.opentrafficsim.core.distributions.Generator;
  17. import org.opentrafficsim.core.distributions.ProbabilityException;
  18. import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
  19. import org.opentrafficsim.road.network.factory.xml.XmlParserException;
  20. import org.opentrafficsim.xml.generated.ACCELERATIONDISTTYPE;
  21. import org.opentrafficsim.xml.generated.DURATIONDISTTYPE;
  22. import org.opentrafficsim.xml.generated.LENGTHDISTTYPE;
  23. import org.opentrafficsim.xml.generated.POSITIONDISTTYPE;
  24. import org.opentrafficsim.xml.generated.SPEEDDISTTYPE;
  25. import org.opentrafficsim.xml.generated.TIMEDISTTYPE;

  26. import nl.tudelft.simulation.dsol.experiment.StreamInformation;

  27. /**
  28.  * Generators based on distribution tags for typed scalars. <br>
  29.  * <br>
  30.  * Copyright (c) 2003-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  31.  * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
  32.  * source code and binary code of this software is proprietary information of Delft University of Technology.
  33.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  34.  */
  35. public final class Generators
  36. {
  37.     /**
  38.      *
  39.      */
  40.     private Generators()
  41.     {
  42.         // utility class
  43.     }

  44.     /**
  45.      * Parse a Length distribution into a Generator for Lengths.
  46.      * @param streamMap the map with predefined streams
  47.      * @param lengthDist the tag to parse
  48.      * @return the generator
  49.      * @throws XmlParserException on parse error
  50.      */
  51.     public static Generator<Length> makeLengthGenerator(final StreamInformation streamMap,
  52.             final LENGTHDISTTYPE lengthDist) throws XmlParserException
  53.     {
  54.         try
  55.         {
  56.             final ContinuousDistDoubleScalar.Rel<Length, LengthUnit> dist =
  57.                     ParseDistribution.parseLengthDist(streamMap, lengthDist);
  58.             Generator<Length> generator = new Generator<Length>()
  59.             {
  60.                 @Override
  61.                 public Length draw() throws ProbabilityException, ParameterException
  62.                 {
  63.                     return dist.draw();
  64.                 }

  65.                 /** {@inheritDoc} */
  66.                 @Override
  67.                 public String toString()
  68.                 {
  69.                     return "Generator<Length>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  70.                 }
  71.             };
  72.             return generator;
  73.         }
  74.         catch (Exception exception)
  75.         {
  76.             throw new XmlParserException(exception);
  77.         }
  78.     }

  79.     /**
  80.      * Parse a Position distribution into a Generator for Positions.
  81.      * @param streamMap the map with predefined streams
  82.      * @param positionDist the tag to parse
  83.      * @return the generator
  84.      * @throws XmlParserException on parse error
  85.      */
  86.     public static Generator<Position> makePositionGenerator(final StreamInformation streamMap,
  87.             final POSITIONDISTTYPE positionDist) throws XmlParserException
  88.     {
  89.         try
  90.         {
  91.             final ContinuousDistDoubleScalar.Abs<Position, PositionUnit, LengthUnit> dist =
  92.                     ParseDistribution.parsePositionDist(streamMap, positionDist);
  93.             Generator<Position> generator = new Generator<Position>()
  94.             {
  95.                 @Override
  96.                 public Position draw() throws ProbabilityException, ParameterException
  97.                 {
  98.                     return dist.draw();
  99.                 }

  100.                 /** {@inheritDoc} */
  101.                 @Override
  102.                 public String toString()
  103.                 {
  104.                     return "Generator<Position>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  105.                 }
  106.             };
  107.             return generator;
  108.         }
  109.         catch (Exception exception)
  110.         {
  111.             throw new XmlParserException(exception);
  112.         }
  113.     }

  114.     /**
  115.      * Parse a Duration distribution into a Generator for Durations.
  116.      * @param streamMap the map with predefined streams
  117.      * @param durationDist the tag to parse
  118.      * @return the generator
  119.      * @throws XmlParserException on parse error
  120.      */
  121.     public static Generator<Duration> makeDurationGenerator(final StreamInformation streamMap,
  122.             final DURATIONDISTTYPE durationDist) throws XmlParserException
  123.     {
  124.         try
  125.         {
  126.             final ContinuousDistDoubleScalar.Rel<Duration, DurationUnit> dist =
  127.                     ParseDistribution.parseDurationDist(streamMap, durationDist);
  128.             Generator<Duration> generator = new Generator<Duration>()
  129.             {
  130.                 @Override
  131.                 public Duration draw() throws ProbabilityException, ParameterException
  132.                 {
  133.                     return dist.draw();
  134.                 }

  135.                 /** {@inheritDoc} */
  136.                 @Override
  137.                 public String toString()
  138.                 {
  139.                     return "Generator<Duration>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  140.                 }
  141.             };
  142.             return generator;
  143.         }
  144.         catch (Exception exception)
  145.         {
  146.             throw new XmlParserException(exception);
  147.         }
  148.     }

  149.     /**
  150.      * Parse a Time distribution into a Generator for Times.
  151.      * @param streamMap the map with predefined streams
  152.      * @param timeDist the tag to parse
  153.      * @return the generator
  154.      * @throws XmlParserException on parse error
  155.      */
  156.     public static Generator<Time> makeTimeGenerator(final StreamInformation streamMap, final TIMEDISTTYPE timeDist)
  157.             throws XmlParserException
  158.     {
  159.         try
  160.         {
  161.             final ContinuousDistDoubleScalar.Abs<Time, TimeUnit, DurationUnit> dist =
  162.                     ParseDistribution.parseTimeDist(streamMap, timeDist);
  163.             Generator<Time> generator = new Generator<Time>()
  164.             {
  165.                 @Override
  166.                 public Time draw() throws ProbabilityException, ParameterException
  167.                 {
  168.                     return dist.draw();
  169.                 }

  170.                 /** {@inheritDoc} */
  171.                 @Override
  172.                 public String toString()
  173.                 {
  174.                     return "Generator<Time>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  175.                 }
  176.             };
  177.             return generator;
  178.         }
  179.         catch (Exception exception)
  180.         {
  181.             throw new XmlParserException(exception);
  182.         }
  183.     }

  184.     /**
  185.      * Parse a Speed distribution into a Generator for Speeds.
  186.      * @param streamMap the map with predefined streams
  187.      * @param speedDist the tag to parse
  188.      * @return the generator
  189.      * @throws XmlParserException on parse error
  190.      */
  191.     public static Generator<Speed> makeSpeedGenerator(final StreamInformation streamMap,
  192.             final SPEEDDISTTYPE speedDist) throws XmlParserException
  193.     {
  194.         try
  195.         {
  196.             final ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> dist =
  197.                     ParseDistribution.parseSpeedDist(streamMap, speedDist);
  198.             Generator<Speed> generator = new Generator<Speed>()
  199.             {
  200.                 @Override
  201.                 public Speed draw() throws ProbabilityException, ParameterException
  202.                 {
  203.                     return dist.draw();
  204.                 }

  205.                 /** {@inheritDoc} */
  206.                 @Override
  207.                 public String toString()
  208.                 {
  209.                     return "Generator<Speed>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  210.                 }
  211.             };
  212.             return generator;
  213.         }
  214.         catch (Exception exception)
  215.         {
  216.             throw new XmlParserException(exception);
  217.         }
  218.     }

  219.     /**
  220.      * Parse an Acceleration distribution into a Generator for Accelerations.
  221.      * @param streamMap the map with predefined streams
  222.      * @param accelerationDist the tag to parse
  223.      * @return the generator
  224.      * @throws XmlParserException on parse error
  225.      */
  226.     public static Generator<Acceleration> makeAccelerationGenerator(final StreamInformation streamMap,
  227.             final ACCELERATIONDISTTYPE accelerationDist) throws XmlParserException
  228.     {
  229.         try
  230.         {
  231.             final ContinuousDistDoubleScalar.Rel<Acceleration, AccelerationUnit> dist =
  232.                     ParseDistribution.parseAccelerationDist(streamMap, accelerationDist);
  233.             Generator<Acceleration> generator = new Generator<Acceleration>()
  234.             {
  235.                 @Override
  236.                 public Acceleration draw() throws ProbabilityException, ParameterException
  237.                 {
  238.                     return dist.draw();
  239.                 }

  240.                 /** {@inheritDoc} */
  241.                 @Override
  242.                 public String toString()
  243.                 {
  244.                     return "Generator<Acceleration>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  245.                 }
  246.             };
  247.             return generator;
  248.         }
  249.         catch (Exception exception)
  250.         {
  251.             throw new XmlParserException(exception);
  252.         }
  253.     }

  254.     /**
  255.      * Parse an Acceleration distribution into a Generator for Decelerations (acceleration with a minus sign).
  256.      * @param streamMap the map with predefined streams
  257.      * @param decelerationDist the tag to parse
  258.      * @return the generator
  259.      * @throws XmlParserException on parse error
  260.      */
  261.     public static Generator<Acceleration> makeDecelerationGenerator(final StreamInformation streamMap,
  262.             final ACCELERATIONDISTTYPE decelerationDist) throws XmlParserException
  263.     {
  264.         try
  265.         {
  266.             final ContinuousDistDoubleScalar.Rel<Acceleration, AccelerationUnit> dist =
  267.                     ParseDistribution.parseAccelerationDist(streamMap, decelerationDist);
  268.             Generator<Acceleration> generator = new Generator<Acceleration>()
  269.             {
  270.                 @Override
  271.                 public Acceleration draw() throws ProbabilityException, ParameterException
  272.                 {
  273.                     return dist.draw().times(-1.0);
  274.                 }

  275.                 /** {@inheritDoc} */
  276.                 @Override
  277.                 public String toString()
  278.                 {
  279.                     return "Generator<Deceleration>(" + dist.getDistribution().toString() + " " + dist.getDisplayUnit() + ")";
  280.                 }
  281.             };
  282.             return generator;
  283.         }
  284.         catch (Exception exception)
  285.         {
  286.             throw new XmlParserException(exception);
  287.         }
  288.     }

  289. }