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
27
28
29
30
31
32
33
34
35
36 public final class DefaultGTUCharacteristicsGeneratorOD implements GTUCharacteristicsGeneratorOD
37 {
38
39 private final Map<GTUType, TemplateGTUType> templates = new HashMap<>();
40
41
42 private final RouteSupplier routeSupplier;
43
44
45 private final StrategicalPlannerFactorySupplierOD factorySupplier;
46
47
48
49
50 public DefaultGTUCharacteristicsGeneratorOD()
51 {
52 this(RouteSupplier.NULL, new HashSet<>(), StrategicalPlannerFactorySupplierOD.LMRS);
53 }
54
55
56
57
58
59 public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier)
60 {
61 this(routeSupplier, new HashSet<>(), StrategicalPlannerFactorySupplierOD.LMRS);
62 }
63
64
65
66
67
68
69 public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier, final Set<TemplateGTUType> templates)
70 {
71 this(routeSupplier, templates, StrategicalPlannerFactorySupplierOD.LMRS);
72 }
73
74
75
76
77
78
79 public DefaultGTUCharacteristicsGeneratorOD(final RouteSupplier routeSupplier,
80 final StrategicalPlannerFactorySupplierOD factorySupplier)
81 {
82 this(routeSupplier, new HashSet<>(), factorySupplier);
83 }
84
85
86
87
88
89
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
115
116
117 public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates)
118 {
119 this(RouteSupplier.NULL, templates, StrategicalPlannerFactorySupplierOD.LMRS);
120 }
121
122
123
124
125
126
127 public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates,
128 final StrategicalPlannerFactorySupplierOD factorySupplier)
129 {
130 this(RouteSupplier.NULL, templates, factorySupplier);
131 }
132
133
134
135
136
137
138 public DefaultGTUCharacteristicsGeneratorOD(final StrategicalPlannerFactorySupplierOD factorySupplier)
139 {
140 this(RouteSupplier.NULL, new HashSet<>(), factorySupplier);
141 }
142
143
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
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
171 LaneBasedStrategicalPlannerFactory<?> laneBasedStrategicalPlannerFactory =
172 this.factorySupplier.getFactory(origin, destination, category, randomStream);
173
174 Route route;
175 if (categorization.entails(Route.class))
176 {
177 route = category.get(Route.class);
178 }
179 else
180 {
181
182 route = this.routeSupplier.getRoute(origin, destination, gtuType);
183 }
184 return new LaneBasedGTUCharacteristics(gtuCharacteristics, laneBasedStrategicalPlannerFactory, route, origin,
185 destination, VehicleModel.MINMAX);
186 }
187 }