View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Acceleration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.base.parameters.ParameterException;
10  import org.opentrafficsim.core.distributions.ConstantGenerator;
11  import org.opentrafficsim.core.distributions.Generator;
12  import org.opentrafficsim.core.distributions.ProbabilityException;
13  
14  /**
15   * TemplateGTUType stores some of the information that is needed to generate a GTU.
16   * <p>
17   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * <p>
20   * $LastChangedDate: 2019-03-26 15:54:58 +0100 (Tue, 26 Mar 2019) $, @version $Revision: 5190 $, by $Author: wjschakel $,
21   * initial version Jul 8, 2014 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   */
24  public class TemplateGTUType implements Serializable, Generator<GTUCharacteristics>
25  {
26      /** */
27      private static final long serialVersionUID = 20141230L;
28  
29      /** The type of the GTU. */
30      private final GTUType gtuType;
31  
32      /** Generator for the length of the GTU. */
33      private final Generator<Length> lengthGenerator;
34  
35      /** Generator for the width of the GTU. */
36      private final Generator<Length> widthGenerator;
37  
38      /** Generator for the maximum speed of the GTU. */
39      private final Generator<Speed> maximumSpeedGenerator;
40  
41      /** Generator for the maximum acceleration of the GTU. */
42      private final Generator<Acceleration> maxAcceleration;
43  
44      /** Generator for the maximum deceleration of the GTU. */
45      private final Generator<Acceleration> maxDeceleration;
46  
47      /**
48       * @param gtuType GTUType; GTUType, the GTUType to make it identifiable.
49       * @param lengthGenerator Generator&lt;Length&gt;; generator for the length of the GTU type (parallel with driving
50       *            direction).
51       * @param widthGenerator Generator&lt;Length&gt;; generator for the width of the GTU type (perpendicular to driving
52       *            direction).
53       * @param maximumSpeedGenerator Generator&lt;Speed&gt;; generator for the maximum speed of the GTU type (in the driving
54       *            direction).
55       * @throws NullPointerException when one of the arguments is null
56       */
57      public TemplateGTUType(final GTUType gtuType, final Generator<Length> lengthGenerator,
58              final Generator<Length> widthGenerator, final Generator<Speed> maximumSpeedGenerator) throws NullPointerException
59      {
60          this(gtuType, lengthGenerator, widthGenerator, maximumSpeedGenerator,
61                  new ConstantGenerator<>(Acceleration.createSI(3.0)), new ConstantGenerator<>(Acceleration.createSI(-8.0)));
62      }
63  
64      /**
65       * @param gtuType GTUType; GTUType, the GTUType to make it identifiable.
66       * @param lengthGenerator Generator&lt;Length&gt;; generator for the length of the GTU type (parallel with driving
67       *            direction).
68       * @param widthGenerator Generator&lt;Length&gt;; generator for the width of the GTU type (perpendicular to driving
69       *            direction).
70       * @param maximumSpeedGenerator Generator&lt;Speed&gt;; generator for the maximum speed of the GTU type (in the driving
71       *            direction).
72       * @param maximumAccelerationGenerator Generator&lt;Acceleration&gt;; generator for the maximum acceleration of the GTU type
73       * @param maximumDecelerationGenerator Generator&lt;Acceleration&gt;; generator for the maximum deceleration of the GTU type
74       * @throws NullPointerException when one of the arguments is null
75       */
76      public TemplateGTUType(final GTUType gtuType, final Generator<Length> lengthGenerator,
77              final Generator<Length> widthGenerator, final Generator<Speed> maximumSpeedGenerator,
78              final Generator<Acceleration> maximumAccelerationGenerator,
79              final Generator<Acceleration> maximumDecelerationGenerator) throws NullPointerException
80      {
81          Throw.whenNull(gtuType, "gtuType is null");
82          Throw.whenNull(lengthGenerator, "lengthGenerator is null");
83          Throw.whenNull(widthGenerator, "widthGenerator is null");
84          Throw.whenNull(maximumSpeedGenerator, "maximumSpeedGenerator is null");
85          Throw.whenNull(maximumAccelerationGenerator, "maximumAccelerationGenerator is null");
86          Throw.whenNull(maximumDecelerationGenerator, "maximumDecelerationGenerator is null");
87  
88          this.gtuType = gtuType;
89          this.lengthGenerator = lengthGenerator;
90          this.widthGenerator = widthGenerator;
91          this.maximumSpeedGenerator = maximumSpeedGenerator;
92          this.maxAcceleration = maximumAccelerationGenerator;
93          this.maxDeceleration = maximumDecelerationGenerator;
94      }
95  
96      /**
97       * Returns characteristics for the given GTU.
98       * @return characteristics for the given GTU
99       * @throws ProbabilityException in case of probability exception
100      * @throws ParameterException in case of parameter exception
101      */
102     @Override
103     @SuppressWarnings("checkstyle:designforextension")
104     public GTUCharacteristics draw() throws ProbabilityException, ParameterException
105     {
106         Acceleration acceleration = this.maxAcceleration.draw();
107         Acceleration deceleration = this.maxDeceleration.draw();
108         Throw.when(acceleration.si <= 0, IllegalArgumentException.class, "Acceleration should be above 0.");
109         Throw.when(deceleration.si >= 0, IllegalArgumentException.class, "Deceleration should be below 0.");
110         Length length = this.lengthGenerator.draw();
111         return new GTUCharacteristics(this.gtuType, length, this.widthGenerator.draw(), this.maximumSpeedGenerator.draw(),
112                 acceleration, deceleration, length.multiplyBy(0.75));
113     }
114 
115     /**
116      * @return gtuType.
117      */
118     @SuppressWarnings("checkstyle:designforextension")
119     public GTUType getGTUType()
120     {
121         return this.gtuType;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     @SuppressWarnings("checkstyle:designforextension")
127     public String toString()
128     {
129         return String.format("TemplateGTUType [%s, %s, %s, %s]", this.gtuType, this.lengthGenerator, this.widthGenerator,
130                 this.maximumSpeedGenerator);
131     }
132 
133 }