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