1 package org.opentrafficsim.road.gtu.generator.characteristics;
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.definitions.DefaultsNl;
11 import org.opentrafficsim.core.distributions.Generator;
12 import org.opentrafficsim.core.gtu.GtuCharacteristics;
13 import org.opentrafficsim.core.gtu.GtuException;
14 import org.opentrafficsim.core.gtu.GtuTemplate;
15 import org.opentrafficsim.core.gtu.GtuType;
16 import org.opentrafficsim.core.network.Node;
17 import org.opentrafficsim.core.network.route.Route;
18 import org.opentrafficsim.road.gtu.lane.VehicleModel;
19 import org.opentrafficsim.road.gtu.lane.VehicleModelFactory;
20 import org.opentrafficsim.road.gtu.lane.tactical.following.IdmPlusFactory;
21 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLmrsPerceptionFactory;
22 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
23 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
24 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlannerFactory;
25 import org.opentrafficsim.road.od.Categorization;
26 import org.opentrafficsim.road.od.Category;
27
28 import nl.tudelft.simulation.jstats.streams.StreamInterface;
29
30
31
32
33
34
35
36
37
38
39
40 public final class DefaultLaneBasedGtuCharacteristicsGeneratorOd implements LaneBasedGtuCharacteristicsGeneratorOd
41 {
42
43 private Generator<GtuType> gtuTypeGenerator = null;
44
45
46 private final Map<GtuType, GtuTemplate> templates = new LinkedHashMap<>();
47
48
49 private final LaneBasedStrategicalPlannerFactory<?> factory;
50
51
52 private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
53
54
55
56
57
58
59
60
61 private DefaultLaneBasedGtuCharacteristicsGeneratorOd(final Generator<GtuType> gtuTypeGenerator,
62 final Set<GtuTemplate> templates, final LaneBasedStrategicalPlannerFactory<?> factory,
63 final VehicleModelFactory vehicleModelFactory)
64 {
65 Throw.whenNull(factory, "Strategical planner factory may not be null.");
66 this.gtuTypeGenerator = gtuTypeGenerator;
67 if (templates != null)
68 {
69 for (GtuTemplate template : templates)
70 {
71 this.templates.put(template.getGtuType(), template);
72 }
73 }
74 this.factory = factory;
75 if (vehicleModelFactory == null)
76 {
77 this.vehicleModelFactory = VehicleModelFactory.MINMAX;
78 }
79 else
80 {
81 this.vehicleModelFactory = vehicleModelFactory;
82 }
83 }
84
85 @Override
86 public LaneBasedGtuCharacteristics draw(final Node origin, final Node destination, final Category category,
87 final StreamInterface randomStream) throws GtuException
88 {
89 Categorization categorization = category.getCategorization();
90
91 GtuType gtuType;
92 if (categorization.entails(GtuType.class))
93 {
94 gtuType = category.get(GtuType.class);
95 }
96 else if (this.gtuTypeGenerator != null)
97 {
98 gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GtuException.class, "Parameter while drawing GTU type.");
99 }
100 else
101 {
102 gtuType = DefaultsNl.CAR;
103 }
104 GtuCharacteristics gtuCharacteristics;
105 if (this.templates.containsKey(gtuType))
106 {
107 gtuCharacteristics =
108 Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
109 }
110 else
111 {
112 gtuCharacteristics = Try.assign(() -> GtuType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
113 "Exception while applying default GTU characteristics.");
114 }
115 Route route = categorization.entails(Route.class) ? category.get(Route.class) : null;
116 VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
117
118 return new LaneBasedGtuCharacteristics(gtuCharacteristics, this.factory, route, origin, destination, vehicleModel);
119 }
120
121
122
123
124
125
126 public static LaneBasedStrategicalRoutePlannerFactory defaultLmrs(final StreamInterface stream)
127 {
128 return new LaneBasedStrategicalRoutePlannerFactory(
129 new LmrsFactory(new IdmPlusFactory(stream), new DefaultLmrsPerceptionFactory()));
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143 @SuppressWarnings("hiddenfield")
144 public static class Factory
145 {
146
147 private Generator<GtuType> gtuTypeGenerator = null;
148
149
150 private Set<GtuTemplate> templates = new LinkedHashSet<>();
151
152
153 private final LaneBasedStrategicalPlannerFactory<?> factory;
154
155
156 private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
157
158
159
160
161
162 public Factory(final LaneBasedStrategicalPlannerFactory<?> factory)
163 {
164 this.factory = factory;
165 }
166
167
168
169
170
171 public Factory setGtuTypeGenerator(final Generator<GtuType> gtuTypeGenerator)
172 {
173 this.gtuTypeGenerator = gtuTypeGenerator;
174 return this;
175 }
176
177
178
179
180
181 public Factory setTemplates(final Set<GtuTemplate> templates)
182 {
183 this.templates = templates;
184 return this;
185 }
186
187
188
189
190
191 public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
192 {
193 this.vehicleModelFactory = vehicleModelFactory;
194 return this;
195 }
196
197
198
199
200
201 @SuppressWarnings("synthetic-access")
202 public DefaultLaneBasedGtuCharacteristicsGeneratorOd create()
203 {
204 return new DefaultLaneBasedGtuCharacteristicsGeneratorOd(this.gtuTypeGenerator, this.templates, this.factory,
205 this.vehicleModelFactory);
206 }
207 }
208
209 }