View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.djunits.unit.SpeedUnit;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Speed;
10  import org.djutils.exceptions.Throw;
11  import org.opentrafficsim.base.HierarchicalType;
12  import org.opentrafficsim.base.parameters.ParameterException;
13  import org.opentrafficsim.core.distributions.ConstantGenerator;
14  import org.opentrafficsim.core.distributions.ProbabilityException;
15  import org.opentrafficsim.core.units.distributions.ContinuousDistSpeed;
16  
17  import nl.tudelft.simulation.jstats.distributions.DistNormal;
18  import nl.tudelft.simulation.jstats.streams.StreamInterface;
19  
20  /**
21   * A GTU type identifies the type of a GTU. <br>
22   * GTU types are used to check whether a particular GTU can travel over a particular part of infrastructure. E.g. a
23   * (LaneBased)GTU with GTUType CAR can travel over lanes that have a LaneType that has the GTUType CAR in the compatibility set.
24   * <p>
25   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * <p>
28   * @version $Revision: 4831 $, $LastChangedDate: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, by $Author: averbraeck $,
29   *          initial version Dec 31, 2014 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   */
33  public final class GTUType extends HierarchicalType<GTUType> implements Serializable
34  {
35      /** */
36      private static final long serialVersionUID = 20141231L;
37  
38      /** Super type for all road users. */
39      public static final GTUType ROAD_USER;
40  
41      /** Super type for all water way users. */
42      public static final GTUType WATER_WAY_USER;
43  
44      /** Super type for all rail users. */
45      public static final GTUType RAIL_WAY_USER;
46  
47      /** Super type for pedestrians. */
48      public static final GTUType PEDESTRIAN;
49  
50      /** Super type for bicycle. */
51      public static final GTUType BICYCLE;
52  
53      /** Super type for mopeds. */
54      public static final GTUType MOPED;
55  
56      /** Super type for vehicles. */
57      public static final GTUType VEHICLE;
58  
59      /** Super type for emergency vehicles. */
60      public static final GTUType EMERGENCY_VEHICLE;
61  
62      /** Super type for ships. */
63      public static final GTUType SHIP;
64  
65      /** Super type for trains. */
66      public static final GTUType TRAIN;
67  
68      /** Super type for cars. */
69      public static final GTUType CAR;
70  
71      /** Super type for vans. */
72      public static final GTUType VAN;
73  
74      /** Super type for busses. */
75      public static final GTUType BUS;
76  
77      /** Super type for trucks. */
78      public static final GTUType TRUCK;
79  
80      /** Super type for scheduled busses. */
81      public static final GTUType SCHEDULED_BUS;
82  
83      static
84      {
85          ROAD_USER = new GTUType("ROAD_USER", null);
86          WATER_WAY_USER = new GTUType("WATER_WAY_USER", null);
87          RAIL_WAY_USER = new GTUType("RAIL_WAY_USER", null);
88  
89          SHIP = new GTUType("SHIP", WATER_WAY_USER);
90          TRAIN = new GTUType("TRAIN", RAIL_WAY_USER);
91          PEDESTRIAN = new GTUType("PEDESTRIAN", ROAD_USER);
92          BICYCLE = new GTUType("BICYCLE", ROAD_USER);
93  
94          MOPED = new GTUType("MOPED", BICYCLE);
95  
96          VEHICLE = new GTUType("VEHICLE", ROAD_USER);
97          EMERGENCY_VEHICLE = new GTUType("EMERGENCY_VEHICLE", VEHICLE);
98          CAR = new GTUType("CAR", VEHICLE);
99          VAN = new GTUType("VAN", VEHICLE);
100         BUS = new GTUType("BUS", VEHICLE);
101         TRUCK = new GTUType("TRUCK", VEHICLE);
102         SCHEDULED_BUS = new GTUType("SCHEDULED BUS", BUS);
103     }
104 
105     /** Templates for GTU characteristics. */
106     private static final Map<StreamInterface, Map<GTUType, TemplateGTUType>> TEMPLATES = new HashMap<>();
107 
108     /**
109      * Returns default characteristics for given GTUType.
110      * @param gtuType GTUType; GTUType GTU type
111      * @param randomStream StreamInterface; stream for random numbers
112      * @return default characteristics for given GTUType
113      * @throws GTUException if there are no default characteristics for the GTU type
114      */
115     public static GTUCharacteristics defaultCharacteristics(final GTUType gtuType, final StreamInterface randomStream)
116             throws GTUException
117     {
118         Map<GTUType, TemplateGTUType> map = TEMPLATES.get(randomStream);
119         if (map == null)
120         {
121             map = new HashMap<>();
122             TEMPLATES.put(randomStream, map);
123         }
124         GTUType type = gtuType;
125         TemplateGTUType template = map.get(type);
126         while (template == null)
127         {
128             if (type.equals(GTUType.CAR))
129             {
130                 // from "Maatgevende normen in de Nederlandse richtlijnen voor wegontwerp", R-2014-38, SWOV
131                 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(4.19)),
132                         new ConstantGenerator<>(Length.createSI(1.7)),
133                         new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
134             }
135             else if (type.equals(GTUType.TRUCK))
136             {
137                 // from "Maatgevende normen in de Nederlandse richtlijnen voor wegontwerp", R-2014-38, SWOV
138                 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(12.0)),
139                         new ConstantGenerator<>(Length.createSI(2.55)),
140                         new ContinuousDistSpeed(new DistNormal(randomStream, 85.0, 2.5), SpeedUnit.KM_PER_HOUR));
141             }
142             else if (type.equals(GTUType.BUS))
143             {
144                 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(12.0)),
145                         new ConstantGenerator<>(Length.createSI(2.55)),
146                         new ConstantGenerator<>(new Speed(90, SpeedUnit.KM_PER_HOUR)));
147             }
148             else if (type.equals(GTUType.VAN))
149             {
150                 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(5.0)),
151                         new ConstantGenerator<>(Length.createSI(2.4)),
152                         new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
153             }
154             else if (type.equals(GTUType.EMERGENCY_VEHICLE))
155             {
156                 template = new TemplateGTUType(type, new ConstantGenerator<>(Length.createSI(5.0)),
157                         new ConstantGenerator<>(Length.createSI(2.55)),
158                         new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
159             }
160             else
161             {
162                 type = type.getParent();
163                 Throw.whenNull(type, "GTUType %s is not of any types with default characteristics.", gtuType);
164             }
165             if (template != null)
166             {
167                 map.put(type, template);
168             }
169         }
170         try
171         {
172             return template.draw();
173         }
174         catch (ProbabilityException | ParameterException exception)
175         {
176             throw new GTUException("GTUType draw failed.", exception);
177         }
178     }
179 
180     /**
181      * @param id String; The id of the GTUType to make it identifiable.
182      * @throws NullPointerException if the id is null
183      */
184     private GTUType(final String id) throws NullPointerException
185     {
186         super(id);
187     }
188 
189     /**
190      * @param id String; The id of the GTUType to make it identifiable.
191      * @param parent GTUType; parent GTU type
192      * @throws NullPointerException if the id is null
193      */
194     public GTUType(final String id, final GTUType parent) throws NullPointerException
195     {
196         super(id, parent);
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public String toString()
202     {
203         return "GTUType: " + this.getId();
204     }
205 
206 }