View Javadoc
1   package org.opentrafficsim.road.gtu.generator.od;
2   
3   import java.util.LinkedHashMap;
4   import java.util.LinkedHashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.djutils.exceptions.Throw;
9   import org.djutils.exceptions.Try;
10  import org.opentrafficsim.core.distributions.Generator;
11  import org.opentrafficsim.core.gtu.GTUCharacteristics;
12  import org.opentrafficsim.core.gtu.GTUException;
13  import org.opentrafficsim.core.gtu.GTUType;
14  import org.opentrafficsim.core.gtu.TemplateGTUType;
15  import org.opentrafficsim.core.network.Node;
16  import org.opentrafficsim.core.network.route.Route;
17  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
18  import org.opentrafficsim.road.gtu.lane.VehicleModel;
19  import org.opentrafficsim.road.gtu.lane.VehicleModelFactory;
20  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
21  import org.opentrafficsim.road.gtu.strategical.od.Categorization;
22  import org.opentrafficsim.road.gtu.strategical.od.Category;
23  import org.opentrafficsim.road.gtu.strategical.route.RouteGeneratorOD;
24  
25  import nl.tudelft.simulation.jstats.streams.StreamInterface;
26  
27  /**
28   * Default generator for {@code LaneBasedGTUCharacteristics}.
29   * <p>
30   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
32   * <p>
33   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 dec. 2017 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
37   */
38  public final class DefaultGTUCharacteristicsGeneratorOD implements GTUCharacteristicsGeneratorOD
39  {
40      /** GTU type generator. */
41      private Generator<GTUType> gtuTypeGenerator = null;
42  
43      /** Templates. */
44      private final Map<GTUType, TemplateGTUType> templates = new LinkedHashMap<>();
45  
46      /** Route generator. */
47      private final RouteGeneratorOD routeGenerator;
48  
49      /** Supplies a strategical factory. */
50      private final StrategicalPlannerFactorySupplierOD factorySupplier;
51  
52      /** Vehicle factory. */
53      private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
54  
55      /**
56       * Constructor using null-routes, default GTU characteristics and LMRS.
57       */
58      public DefaultGTUCharacteristicsGeneratorOD()
59      {
60          this(null, RouteGeneratorOD.NULL, new LinkedHashSet<>(), StrategicalPlannerFactorySupplierOD.lmrs());
61      }
62  
63      /**
64       * Constructor using route generator, default GTU characteristics and LMRS.
65       * @param routeGenerator RouteGeneratorOD; route generator
66       */
67      public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeGenerator)
68      {
69          this(null, routeGenerator, new LinkedHashSet<>(), StrategicalPlannerFactorySupplierOD.lmrs());
70      }
71  
72      /**
73       * Constructor using route supplier, provided GTU templates and LMRS.
74       * @param routeSupplier RouteGeneratorOD; route supplier
75       * @param templates Set&lt;TemplateGTUType&gt;; templates
76       */
77      public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeSupplier, final Set<TemplateGTUType> templates)
78      {
79          this(null, routeSupplier, templates, StrategicalPlannerFactorySupplierOD.lmrs());
80      }
81  
82      /**
83       * Constructor using route supplier, default GTU characteristics and provided strategical planner factory supplier.
84       * @param routeGenerator RouteGeneratorOD; route generator
85       * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
86       */
87      public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeGenerator,
88              final StrategicalPlannerFactorySupplierOD factorySupplier)
89      {
90          this(null, routeGenerator, new LinkedHashSet<>(), factorySupplier);
91      }
92  
93      /**
94       * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
95       * @param gtuTypeGenerator Generator&lt;GTUType&gt;; GTU type generator
96       * @param routeGenerator RouteGeneratorOD; route generator
97       * @param templates Set&lt;TemplateGTUType&gt;; templates
98       * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
99       */
100     public DefaultGTUCharacteristicsGeneratorOD(final Generator<GTUType> gtuTypeGenerator,
101             final RouteGeneratorOD routeGenerator, final Set<TemplateGTUType> templates,
102             final StrategicalPlannerFactorySupplierOD factorySupplier)
103     {
104         Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
105         this.gtuTypeGenerator = gtuTypeGenerator;
106         if (routeGenerator == null)
107         {
108             this.routeGenerator = RouteGeneratorOD.NULL;
109         }
110         else
111         {
112             this.routeGenerator = routeGenerator;
113         }
114         if (templates != null)
115         {
116             for (TemplateGTUType template : templates)
117             {
118                 this.templates.put(template.getGTUType(), template);
119             }
120         }
121         this.factorySupplier = factorySupplier;
122     }
123 
124     /**
125      * Constructor using null-routes, provided GTU templates and LMRS.
126      * @param templates Set&lt;TemplateGTUType&gt;; templates
127      */
128     public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates)
129     {
130         this(null, RouteGeneratorOD.NULL, templates, StrategicalPlannerFactorySupplierOD.lmrs());
131     }
132 
133     /**
134      * Constructor using null-routes, provided GTU templates and provided strategical planner factory supplier.
135      * @param templates Set&lt;TemplateGTUType&gt;; templates
136      * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
137      */
138     public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates,
139             final StrategicalPlannerFactorySupplierOD factorySupplier)
140     {
141         this(null, RouteGeneratorOD.NULL, templates, factorySupplier);
142     }
143 
144     /**
145      * Constructor using using null-routes, default GTU characteristics and provided GTU templates and provided strategical
146      * planner factory supplier.
147      * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
148      */
149     public DefaultGTUCharacteristicsGeneratorOD(final StrategicalPlannerFactorySupplierOD factorySupplier)
150     {
151         this(null, RouteGeneratorOD.NULL, new LinkedHashSet<>(), factorySupplier);
152     }
153 
154     // TODO: remove above constructors and use factory always
155 
156     /**
157      * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
158      * @param gtuTypeGenerator Generator&lt;GTUType&gt;; GTU type generator
159      * @param routeSupplier RouteGeneratorOD; route supplier
160      * @param templates Set&lt;TemplateGTUType&gt;; templates
161      * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
162      * @param vehicleModelFactory VehicleModelFactory; vehicle model factory
163      */
164     private DefaultGTUCharacteristicsGeneratorOD(final Generator<GTUType> gtuTypeGenerator,
165             final RouteGeneratorOD routeSupplier, final Set<TemplateGTUType> templates,
166             final StrategicalPlannerFactorySupplierOD factorySupplier, final VehicleModelFactory vehicleModelFactory)
167     {
168         Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
169         this.gtuTypeGenerator = gtuTypeGenerator;
170         if (routeSupplier == null)
171         {
172             this.routeGenerator = RouteGeneratorOD.NULL;
173         }
174         else
175         {
176             this.routeGenerator = routeSupplier;
177         }
178         if (templates != null)
179         {
180             for (TemplateGTUType template : templates)
181             {
182                 this.templates.put(template.getGTUType(), template);
183             }
184         }
185         this.factorySupplier = factorySupplier;
186         if (vehicleModelFactory == null)
187         {
188             this.vehicleModelFactory = VehicleModelFactory.MINMAX;
189         }
190         else
191         {
192             this.vehicleModelFactory = vehicleModelFactory;
193         }
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public LaneBasedGTUCharacteristics draw(final Node origin, final Node destination, final Category category,
199             final StreamInterface randomStream) throws GTUException
200     {
201         Categorization categorization = category.getCategorization();
202         // GTU characteristics
203         GTUType gtuType;
204         if (categorization.entails(GTUType.class))
205         {
206             gtuType = category.get(GTUType.class);
207         }
208         else if (this.gtuTypeGenerator != null)
209         {
210             gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GTUException.class, "Parameter while drawing GTU type.");
211         }
212         else
213         {
214             gtuType = origin.getNetwork().getGtuType(GTUType.DEFAULTS.CAR);
215         }
216         GTUCharacteristics gtuCharacteristics;
217         if (this.templates.containsKey(gtuType))
218         {
219             gtuCharacteristics =
220                     Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
221         }
222         else
223         {
224             gtuCharacteristics = Try.assign(() -> GTUType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
225                     "Exception while applying default GTU characteristics.");
226         }
227         // strategical factory
228         LaneBasedStrategicalPlannerFactory<?> laneBasedStrategicalPlannerFactory =
229                 this.factorySupplier.getFactory(origin, destination, category, randomStream);
230         // route
231         Route route;
232         if (categorization.entails(Route.class))
233         {
234             route = category.get(Route.class);
235         }
236         else
237         {
238             // get route from supplier
239             route = this.routeGenerator.getRoute(origin, destination, gtuType);
240         }
241         // vehicle model
242         VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
243 
244         return new LaneBasedGTUCharacteristics(gtuCharacteristics, laneBasedStrategicalPlannerFactory, route, origin,
245                 destination, vehicleModel);
246     }
247 
248     /**
249      * Factory for {@code DefaultGTUCharacteristicsGeneratorOD}.
250      * <p>
251      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
252      * <br>
253      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
254      * <p>
255      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 8 jan. 2019 <br>
256      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
257      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
258      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
259      */
260     @SuppressWarnings("hiddenfield")
261     public static class Factory
262     {
263         /** GTU type. */
264         private Generator<GTUType> gtuTypeGenerator = null;
265 
266         /** Templates. */
267         private Set<TemplateGTUType> templates = new LinkedHashSet<>();
268 
269         /** Route supplier. */
270         private RouteGeneratorOD routeGenerator = RouteGeneratorOD.NULL;
271 
272         /** Supplies a strategical factory. */
273         private StrategicalPlannerFactorySupplierOD factorySupplier = StrategicalPlannerFactorySupplierOD.lmrs();
274 
275         /** Vehicle factory. */
276         private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
277 
278         /**
279          * @param gtuTypeGenerator Generator&lt;GTUType&gt;; set gtuTypeGenerator.
280          * @return Factory; this factory for method chaining
281          */
282         public Factory setGtuTypeGenerator(final Generator<GTUType> gtuTypeGenerator)
283         {
284             this.gtuTypeGenerator = gtuTypeGenerator;
285             return this;
286         }
287 
288         /**
289          * @param templates Set&lt;TemplateGTUType&gt;; set templates.
290          * @return Factory; this factory for method chaining
291          */
292         public Factory setTemplates(final Set<TemplateGTUType> templates)
293         {
294             this.templates = templates;
295             return this;
296         }
297 
298         /**
299          * @param routeSupplier RouteGeneratorOD; set routeSupplier.
300          * @return Factory; this factory for method chaining
301          */
302         public Factory setRouteSupplier(final RouteGeneratorOD routeSupplier)
303         {
304             this.routeGenerator = routeSupplier;
305             return this;
306         }
307 
308         /**
309          * @param factorySupplier StrategicalPlannerFactorySupplierOD; set factorySupplier.
310          * @return Factory; this factory for method chaining
311          */
312         public Factory setFactorySupplier(final StrategicalPlannerFactorySupplierOD factorySupplier)
313         {
314             this.factorySupplier = factorySupplier;
315             return this;
316         }
317 
318         /**
319          * @param vehicleModelFactory VehicleModelFactory; set vehicleModelFactory.
320          * @return Factory; this factory for method chaining
321          */
322         public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
323         {
324             this.vehicleModelFactory = vehicleModelFactory;
325             return this;
326         }
327 
328         /**
329          * Creates the default GTU characteristics generator based on OD information.
330          * @return default GTU characteristics generator based on OD information
331          */
332         @SuppressWarnings("synthetic-access")
333         public DefaultGTUCharacteristicsGeneratorOD create()
334         {
335             return new DefaultGTUCharacteristicsGeneratorOD(this.gtuTypeGenerator, this.routeGenerator, this.templates,
336                     this.factorySupplier, this.vehicleModelFactory);
337         }
338     }
339 
340 }