View Javadoc
1   package org.opentrafficsim.road.gtu.generator.od;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.opentrafficsim.core.gtu.GTUCharacteristics;
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.core.gtu.GTUType;
11  import org.opentrafficsim.core.gtu.TemplateGTUType;
12  import org.opentrafficsim.core.gtu.Try;
13  import org.opentrafficsim.core.network.Node;
14  import org.opentrafficsim.core.network.route.Route;
15  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
16  import org.opentrafficsim.road.gtu.lane.VehicleModel;
17  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
18  import org.opentrafficsim.road.gtu.strategical.od.Categorization;
19  import org.opentrafficsim.road.gtu.strategical.od.Category;
20  import org.opentrafficsim.road.gtu.strategical.route.RouteSupplier;
21  
22  import nl.tudelft.simulation.jstats.streams.StreamInterface;
23  import nl.tudelft.simulation.language.Throw;
24  
25  /**
26   * Default generator for {@code LaneBasedGTUCharacteristics}.
27   * <p>
28   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
30   * <p>
31   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 dec. 2017 <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   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
35   */
36  public final class DefaultGTUCharacteristicsGeneratorOD implements GTUCharacteristicsGeneratorOD
37  {
38      /** Templates. */
39      private final Map<GTUType, TemplateGTUType> templates = new HashMap<>();
40  
41      /** Route supplier. */
42      private final RouteSupplier routeSupplier;
43  
44      /** Supplies a strategical factory. */
45      private final StrategicalPlannerFactorySupplierOD factorySupplier;
46  
47      /**
48       * Constructor using null-routes, default GTU characteristics and LMRS.
49       */
50      public DefaultGTUCharacteristicsGeneratorOD()
51      {
52          this(RouteSupplier.NULL, new HashSet<>(), StrategicalPlannerFactorySupplierOD.LMRS);
53      }
54  
55      /**
56       * Constructor using route supplier, default GTU characteristics and LMRS.
57       * @param routeSupplier RouteSupplier; route supplier
58       */
59      public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier)
60      {
61          this(routeSupplier, new HashSet<>(), StrategicalPlannerFactorySupplierOD.LMRS);
62      }
63  
64      /**
65       * Constructor using route supplier, provided GTU templates and LMRS.
66       * @param routeSupplier RouteSupplier; route supplier
67       * @param templates Set&lt;TemplateGTUType&gt;; templates
68       */
69      public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier, final Set<TemplateGTUType> templates)
70      {
71          this(routeSupplier, templates, StrategicalPlannerFactorySupplierOD.LMRS);
72      }
73  
74      /**
75       * Constructor using route supplier, default GTU characteristics and provided strategical planner factory supplier.
76       * @param routeSupplier RouteSupplier; route supplier
77       * @param factorySupplier StrategicalPlannerFactorySupplier; strategical factory supplier
78       */
79      public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier,
80              final StrategicalPlannerFactorySupplierOD factorySupplier)
81      {
82          this(routeSupplier, new HashSet<>(), factorySupplier);
83      }
84  
85      /**
86       * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
87       * @param routeSupplier RouteSupplier; route supplier
88       * @param templates Set&lt;TemplateGTUType&gt;; templates
89       * @param factorySupplier StrategicalPlannerFactorySupplier; strategical factory supplier
90       */
91      public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier, final Set<TemplateGTUType> templates,
92              final StrategicalPlannerFactorySupplierOD factorySupplier)
93      {
94          Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
95          if (routeSupplier == null)
96          {
97              this.routeSupplier = RouteSupplier.NULL;
98          }
99          else
100         {
101             this.routeSupplier = routeSupplier;
102         }
103         if (templates != null)
104         {
105             for (TemplateGTUType template : templates)
106             {
107                 this.templates.put(template.getGTUType(), template);
108             }
109         }
110         this.factorySupplier = factorySupplier;
111     }
112 
113     /**
114      * Constructor using null-routes, provided GTU templates and LMRS.
115      * @param templates Set&lt;TemplateGTUType&gt;; templates
116      */
117     public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates)
118     {
119         this(RouteSupplier.NULL, templates, StrategicalPlannerFactorySupplierOD.LMRS);
120     }
121 
122     /**
123      * Constructor using null-routes, provided GTU templates and provided strategical planner factory supplier.
124      * @param templates Set&lt;TemplateGTUType&gt;; templates
125      * @param factorySupplier StrategicalPlannerFactorySupplier; strategical factory supplier
126      */
127     public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates,
128             final StrategicalPlannerFactorySupplierOD factorySupplier)
129     {
130         this(RouteSupplier.NULL, templates, factorySupplier);
131     }
132 
133     /**
134      * Constructor using using null-routes, default GTU characteristics and provided GTU templates and provided strategical
135      * planner factory supplier.
136      * @param factorySupplier StrategicalPlannerFactorySupplier; strategical factory supplier
137      */
138     public DefaultGTUCharacteristicsGeneratorOD(final StrategicalPlannerFactorySupplierOD factorySupplier)
139     {
140         this(RouteSupplier.NULL, new HashSet<>(), factorySupplier);
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public LaneBasedGTUCharacteristics draw(final Node origin, final Node destination, final Category category,
146             final StreamInterface randomStream) throws GTUException
147     {
148         Categorization categorization = category.getCategorization();
149         // GTU characteristics
150         GTUType gtuType;
151         if (categorization.entails(GTUType.class))
152         {
153             gtuType = category.get(GTUType.class);
154         }
155         else
156         {
157             gtuType = GTUType.CAR;
158         }
159         GTUCharacteristics gtuCharacteristics;
160         if (this.templates.containsKey(gtuType))
161         {
162             gtuCharacteristics =
163                     Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
164         }
165         else
166         {
167             gtuCharacteristics = Try.assign(() -> GTUType.defaultCharacteristics(gtuType, randomStream),
168                     "Exception while applying default GTU characteristics.");
169         }
170         // strategical factory
171         LaneBasedStrategicalPlannerFactory<?> laneBasedStrategicalPlannerFactory =
172                 this.factorySupplier.getFactory(origin, destination, category, randomStream);
173         // route
174         Route route;
175         if (categorization.entails(Route.class))
176         {
177             route = category.get(Route.class);
178         }
179         else
180         {
181             // get route from supplier
182             route = this.routeSupplier.getRoute(origin, destination, gtuType);
183         }
184         return new LaneBasedGTUCharacteristics(gtuCharacteristics, laneBasedStrategicalPlannerFactory, route, origin,
185                 destination, VehicleModel.MINMAX);
186     }
187 }