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