1 package org.opentrafficsim.road.gtu.generator;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.djunits.value.vdouble.scalar.Speed;
5 import org.djutils.exceptions.Try;
6 import org.opentrafficsim.core.definitions.Defaults;
7 import org.opentrafficsim.core.geometry.OtsGeometryException;
8 import org.opentrafficsim.core.gtu.GtuCharacteristics;
9 import org.opentrafficsim.core.gtu.GtuErrorHandler;
10 import org.opentrafficsim.core.gtu.GtuException;
11 import org.opentrafficsim.core.gtu.GtuType;
12 import org.opentrafficsim.core.network.NetworkException;
13 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuCharacteristics;
14 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
15 import org.opentrafficsim.road.network.RoadNetwork;
16 import org.opentrafficsim.road.network.lane.LanePosition;
17
18 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
19 import nl.tudelft.simulation.jstats.streams.StreamInterface;
20
21
22
23
24
25
26
27
28
29
30 public class GtuSpawner
31 {
32
33
34 private boolean useDefaultGtuTemplate = true;
35
36
37 private StreamInterface stream = new MersenneTwister(123L);
38
39
40 private Length noLaneChangeDistance = Length.instantiateSI(100.0);
41
42
43 private boolean instantaneousLaneChanges = false;
44
45
46 private GtuErrorHandler errorHandler = GtuErrorHandler.THROW;
47
48
49
50
51
52
53 public GtuSpawner setUseDefaultGtuTemplate(final boolean useDefaultGtuTemplate)
54 {
55 this.useDefaultGtuTemplate = useDefaultGtuTemplate;
56 return this;
57 }
58
59
60
61
62
63
64 public GtuSpawner setStream(final StreamInterface stream)
65 {
66 this.stream = stream;
67 return this;
68 }
69
70
71
72
73
74
75 public GtuSpawner setNoLaneChangeDistance(final Length noLaneChangeDistance)
76 {
77 this.noLaneChangeDistance = noLaneChangeDistance;
78 return this;
79 }
80
81
82
83
84
85
86 public GtuSpawner setInstantaneousLaneChanges(final boolean instantaneousLaneChanges)
87 {
88 this.instantaneousLaneChanges = instantaneousLaneChanges;
89 return this;
90 }
91
92
93
94
95
96
97 public GtuSpawner setErrorHandler(final GtuErrorHandler errorHandler)
98 {
99 this.errorHandler = errorHandler;
100 return this;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114 public void spawnGtu(final String id, final LaneBasedGtuCharacteristics templateGtuType, final RoadNetwork network,
115 final Speed speed, final LanePosition position) throws GtuException, OtsGeometryException, NetworkException
116 {
117
118 if (this.useDefaultGtuTemplate)
119 {
120 GtuType.registerTemplateSupplier(templateGtuType.getGtuType(), Defaults.NL);
121 }
122 GtuCharacteristics defaultCharacteristics =
123 Try.assign(() -> GtuType.defaultCharacteristics(templateGtuType.getGtuType(), network, this.stream),
124 "Failed getting default Characteristics");
125
126 LaneBasedGtu gtu =
127 new LaneBasedGtu(id, templateGtuType.getGtuType(), templateGtuType.getLength(), templateGtuType.getWidth(),
128 defaultCharacteristics.getMaximumSpeed(), templateGtuType.getLength().divide(2), network);
129
130 gtu.setMaximumAcceleration(defaultCharacteristics.getMaximumAcceleration());
131 gtu.setMaximumDeceleration(defaultCharacteristics.getMaximumDeceleration());
132 gtu.setVehicleModel(templateGtuType.getVehicleModel());
133 gtu.setNoLaneChangeDistance(this.noLaneChangeDistance);
134 gtu.setInstantaneousLaneChange(this.instantaneousLaneChanges);
135 gtu.setErrorHandler(this.errorHandler);
136
137 gtu.init(templateGtuType.getStrategicalPlannerFactory().create(gtu, templateGtuType.getRoute(),
138 templateGtuType.getOrigin(), templateGtuType.getDestination()), position, speed);
139 }
140
141 }