View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.LinkedHashMap;
6   import java.util.Map;
7   
8   import org.djunits.unit.SpeedUnit;
9   import org.djunits.value.vdouble.scalar.Length;
10  import org.djunits.value.vdouble.scalar.Speed;
11  import org.djutils.exceptions.Throw;
12  import org.opentrafficsim.base.HierarchicalType;
13  import org.opentrafficsim.base.parameters.ParameterException;
14  import org.opentrafficsim.core.distributions.ConstantGenerator;
15  import org.opentrafficsim.core.distributions.ProbabilityException;
16  import org.opentrafficsim.core.network.Network;
17  import org.opentrafficsim.core.units.distributions.ContinuousDistSpeed;
18  
19  import nl.tudelft.simulation.jstats.distributions.DistNormal;
20  import nl.tudelft.simulation.jstats.streams.StreamInterface;
21  
22  /**
23   * A GTU type identifies the type of a GTU. <br>
24   * GTU types are used to check whether a particular GTU can travel over a particular part of infrastructure. E.g. a
25   * (LaneBased)GTU with GTUType CAR can travel over lanes that have a LaneType that has the GTUType CAR in the compatibility set.
26   * <p>
27   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * <p>
30   * @version $Revision: 5268 $, $LastChangedDate: 2019-04-17 13:37:13 +0200 (Wed, 17 Apr 2019) $, by $Author: wjschakel $,
31   *          initial version Dec 31, 2014 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   */
35  public final class GTUType extends HierarchicalType<GTUType> implements Serializable
36  {
37      /** */
38      private static final long serialVersionUID = 20141231L;
39  
40      /** Default types with their name. */
41      public enum DEFAULTS
42      {
43          /** Super type for all road users. */
44          ROAD_USER("ROAD_USER"),
45  
46          /** Super type for all water way users. */
47          WATERWAY_USER("WATERWAY_USER"),
48  
49          /** Super type for all rail users. */
50          RAILWAY_USER("RAILWAY_USER"),
51  
52          /** Super type for pedestrians. */
53          PEDESTRIAN("PEDESTRIAN", ROAD_USER),
54  
55          /** Super type for bicycle. */
56          BICYCLE("BICYCLE", ROAD_USER),
57  
58          /** Super type for mopeds. */
59          MOPED("MOPED", BICYCLE),
60  
61          /** Super type for vehicles. */
62          VEHICLE("VEHICLE", ROAD_USER),
63  
64          /** Super type for emergency vehicles. */
65          EMERGENCY_VEHICLE("EMERGENCY_VEHICLE", VEHICLE),
66  
67          /** Super type for ships. */
68          SHIP("SHIP", WATERWAY_USER),
69  
70          /** Super type for trains. */
71          TRAIN("TRAIN", RAILWAY_USER),
72  
73          /** Super type for cars. */
74          CAR("CAR", VEHICLE),
75  
76          /** Super type for vans. */
77          VAN("VAN", VEHICLE),
78  
79          /** Super type for busses. */
80          BUS("BUS", VEHICLE),
81  
82          /** Super type for trucks. */
83          TRUCK("TRUCK", VEHICLE),
84  
85          /** Super type for scheduled busses. */
86          SCHEDULED_BUS("SCHEDULED_BUS", BUS);
87  
88          /** The name. */
89          private final String id;
90  
91          /** The name. */
92          private final DEFAULTS parent;
93  
94          /**
95           * Construct the enum.
96           * @param id String; the id
97           */
98          DEFAULTS(final String id)
99          {
100             this.id = id;
101             this.parent = null;
102         }
103 
104         /**
105          * Construct the enum.
106          * @param id String; the id
107          * @param parent the parent
108          */
109         DEFAULTS(final String id, final DEFAULTS parent)
110         {
111             this.id = id;
112             this.parent = parent;
113         }
114 
115         /** @return the id */
116         public String getId()
117         {
118             return this.id;
119         }
120 
121         /** @return the parent */
122         public DEFAULTS getParent()
123         {
124             return this.parent;
125         }
126     }
127 
128     /** the network to which the GTUType belongs. */
129     private final Network network;
130 
131     /** Templates for GTU characteristics within a network. */
132     private static final Map<Network, Map<DEFAULTS, TemplateGTUType>> DEFAULT_TEMPLATES = new HashMap<>();
133 
134     /**
135      * @param id String; The id of the GTUType to make it identifiable.
136      * @param network Network; The network to which the GTUType belongs
137      * @throws NullPointerException if the id is null
138      */
139     public GTUType(final String id, final Network network) throws NullPointerException
140     {
141         super(id);
142         this.network = network;
143         this.network.addGtuType(this);
144     }
145 
146     /**
147      * @param id String; The id of the GTUType to make it identifiable.
148      * @param parent GTUType; parent GTU type
149      * @throws NullPointerException if the id is null
150      */
151     public GTUType(final String id, final GTUType parent) throws NullPointerException
152     {
153         super(id, parent);
154         this.network = parent.getNetwork();
155         this.network.addGtuType(this);
156     }
157 
158     /**
159      * Whether this, or any of the parent types, equals the given type.
160      * @param type DEFAULTS; type
161      * @return whether this, or any of the parent types, equals the given type
162      */
163     public boolean isOfType(final DEFAULTS type)
164     {
165         if (this.getId().equals(type.getId()))
166         {
167             return true;
168         }
169         if (getParent() != null)
170         {
171             return getParent().isOfType(type);
172         }
173         return false;
174     }
175     
176     /**
177      * Whether this equals the given type. 
178      * @param type DEFAULTS; type
179      * @return whether this equals the given type
180      */
181     public boolean isType(final DEFAULTS type)
182     {
183         return this.getId().equals(type.getId());
184     }
185 
186     /**
187      * Returns default characteristics for given GTUType.
188      * @param gtuType GTUType; GTUType GTU type
189      * @param network Network; the network to use as a key
190      * @param randomStream StreamInterface; stream for random numbers
191      * @return default characteristics for given GTUType
192      * @throws GTUException if there are no default characteristics for the GTU type
193      */
194     public static GTUCharacteristics defaultCharacteristics(final GTUType gtuType, final Network network,
195             final StreamInterface randomStream) throws GTUException
196     {
197         Map<DEFAULTS, TemplateGTUType> map = DEFAULT_TEMPLATES.get(network);
198         if (map == null)
199         {
200             map = new LinkedHashMap<>();
201             DEFAULT_TEMPLATES.put(network, map);
202         }
203         TemplateGTUType template = null;
204         if (map.containsKey(gtuType))
205         {
206             template = map.get(gtuType);
207         }
208         GTUType type = gtuType;
209         DEFAULTS defaultType = null;
210         while (template == null)
211         {
212             if (type.equals(network.getGtuType(DEFAULTS.CAR)))
213             {
214                 // from "Maatgevende normen in de Nederlandse richtlijnen voor wegontwerp", R-2014-38, SWOV
215                 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(4.19)),
216                         new ConstantGenerator<>(Length.createSI(1.7)),
217                         new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
218                 defaultType = DEFAULTS.CAR;
219             }
220             else if (type.equals(network.getGtuType(DEFAULTS.TRUCK)))
221             {
222                 // from "Maatgevende normen in de Nederlandse richtlijnen voor wegontwerp", R-2014-38, SWOV
223                 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(12.0)),
224                         new ConstantGenerator<>(Length.createSI(2.55)),
225                         new ContinuousDistSpeed(new DistNormal(randomStream, 85.0, 2.5), SpeedUnit.KM_PER_HOUR));
226                 defaultType = DEFAULTS.TRUCK;
227             }
228             else if (type.equals(network.getGtuType(DEFAULTS.BUS)))
229             {
230                 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(12.0)),
231                         new ConstantGenerator<>(Length.createSI(2.55)),
232                         new ConstantGenerator<>(new Speed(90, SpeedUnit.KM_PER_HOUR)));
233                 defaultType = DEFAULTS.BUS;
234             }
235             else if (type.equals(network.getGtuType(DEFAULTS.VAN)))
236             {
237                 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(5.0)),
238                         new ConstantGenerator<>(Length.createSI(2.4)),
239                         new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
240                 defaultType = DEFAULTS.VAN;
241             }
242             else if (type.equals(network.getGtuType(DEFAULTS.EMERGENCY_VEHICLE)))
243             {
244                 template = new TemplateGTUType(gtuType, new ConstantGenerator<>(Length.createSI(5.0)),
245                         new ConstantGenerator<>(Length.createSI(2.55)),
246                         new ConstantGenerator<>(new Speed(180, SpeedUnit.KM_PER_HOUR)));
247                 defaultType = DEFAULTS.EMERGENCY_VEHICLE;
248             }
249             else
250             {
251                 type = type.getParent();
252                 Throw.whenNull(type, "GTUType %s is not of any types with default characteristics.", gtuType);
253             }
254             if (template != null)
255             {
256                 map.put(defaultType, template);
257             }
258         }
259         try
260         {
261             return template.draw();
262         }
263         catch (ProbabilityException | ParameterException exception)
264         {
265             throw new GTUException("GTUType draw failed.", exception);
266         }
267     }
268 
269     /**
270      * @return the network to which the GTUType belongs
271      */
272     public Network getNetwork()
273     {
274         return this.network;
275     }
276 
277     /** {@inheritDoc} */
278     @Override
279     public String toString()
280     {
281         return "GTUType: " + this.getId();
282     }
283 
284 }