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   * Stores some of the information that is needed to generate a GTU.
16   * <p>
17   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * <p>
20   * $LastChangedDate$, @version $Revision$, by $Author$, initial version Jul 8, 2014 <br>
21   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
22   */
23  public class GtuTemplate implements Serializable, Generator<GtuCharacteristics>
24  {
25      /** */
26      private static final long serialVersionUID = 20141230L;
27  
28      /** The type of the GTU. */
29      private final GtuType gtuType;
30  
31      /** Generator for the length of the GTU. */
32      private final Generator<Length> lengthGenerator;
33  
34      /** Generator for the width of the GTU. */
35      private final Generator<Length> widthGenerator;
36  
37      /** Generator for the maximum speed of the GTU. */
38      private final Generator<Speed> maximumSpeedGenerator;
39  
40      /** Generator for the maximum acceleration of the GTU. */
41      private final Generator<Acceleration> maxAcceleration;
42  
43      /** Generator for the maximum deceleration of the GTU. */
44      private final Generator<Acceleration> maxDeceleration;
45  
46      /**
47       * @param gtuType GtuType; GtuType, the GtuType to make it identifiable.
48       * @param lengthGenerator Generator&lt;Length&gt;; generator for the length of the GTU type (parallel with driving
49       *            direction).
50       * @param widthGenerator Generator&lt;Length&gt;; generator for the width of the GTU type (perpendicular to driving
51       *            direction).
52       * @param maximumSpeedGenerator Generator&lt;Speed&gt;; generator for the maximum speed of the GTU type (in the driving
53       *            direction).
54       * @throws NullPointerException when one of the arguments is null
55       */
56      public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator,
57              final Generator<Length> widthGenerator, final Generator<Speed> maximumSpeedGenerator) throws NullPointerException
58      {
59          this(gtuType, lengthGenerator, widthGenerator, maximumSpeedGenerator,
60                  new ConstantGenerator<>(Acceleration.instantiateSI(3.0)),
61                  new ConstantGenerator<>(Acceleration.instantiateSI(-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 GtuTemplate(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 the same Characteristics, but pertaining to a different GTU type. This is useful for when the GTU type is used
98       * for other purposes in simulation, where the {@code GtuCharacteristics} should be the same.
99       * @param newGtuType GtuType; the new GTU type.
100      * @return Copy of this {@code TemplateGTUType} linked to the new GTU type.
101      */
102     public GtuTemplate copyForGtuType(final GtuType newGtuType)
103     {
104         return new GtuTemplate(newGtuType, this.lengthGenerator, this.widthGenerator, this.maximumSpeedGenerator,
105                 this.maxAcceleration, this.maxDeceleration);
106     }
107 
108     /**
109      * Returns characteristics for the given GTU.
110      * @return characteristics for the given GTU
111      * @throws ProbabilityException in case of probability exception
112      * @throws ParameterException in case of parameter exception
113      */
114     @Override
115     @SuppressWarnings("checkstyle:designforextension")
116     public GtuCharacteristics draw() throws ProbabilityException, ParameterException
117     {
118         Acceleration acceleration = this.maxAcceleration.draw();
119         Acceleration deceleration = this.maxDeceleration.draw();
120         Throw.when(acceleration.si <= 0, IllegalArgumentException.class, "Acceleration should be above 0.");
121         Throw.when(deceleration.si >= 0, IllegalArgumentException.class, "Deceleration should be below 0.");
122         Length length = this.lengthGenerator.draw();
123         return new GtuCharacteristics(this.gtuType, length, this.widthGenerator.draw(), this.maximumSpeedGenerator.draw(),
124                 acceleration, deceleration, length.times(0.75));
125     }
126 
127     /**
128      * @return gtuType.
129      */
130     @SuppressWarnings("checkstyle:designforextension")
131     public GtuType getGtuType()
132     {
133         return this.gtuType;
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     @SuppressWarnings("checkstyle:designforextension")
139     public String toString()
140     {
141         return String.format("TemplateGTUType [%s, %s, %s, %s]", this.gtuType, this.lengthGenerator, this.widthGenerator,
142                 this.maximumSpeedGenerator);
143     }
144 
145 }