View Javadoc
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   * Simple class to spawn GTUs.
23   * <p>
24   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
28   * @author Christoph Thees
29   */
30  public class GtuSpawner
31  {
32  
33      /** Use default NL GTU templates. */
34      private boolean useDefaultGtuTemplate = true;
35  
36      /** Random stream. */
37      private StreamInterface stream = new MersenneTwister(123L);
38  
39      /** Length over which no lane changes will happen. */
40      private Length noLaneChangeDistance = Length.instantiateSI(100.0);
41  
42      /** Instantaneous lane changes. */
43      private boolean instantaneousLaneChanges = false;
44  
45      /** Error handler. */
46      private GtuErrorHandler errorHandler = GtuErrorHandler.THROW;
47  
48      /**
49       * Sets the use of default NL GTU templates.
50       * @param useDefaultGtuTemplate boolean; use of default NL GTU templates.
51       * @return this; for method chaining.
52       */
53      public GtuSpawner setUseDefaultGtuTemplate(final boolean useDefaultGtuTemplate)
54      {
55          this.useDefaultGtuTemplate = useDefaultGtuTemplate;
56          return this;
57      }
58  
59      /**
60       * Sets random stream.
61       * @param stream StreamInterface; random stream.
62       * @return this; for method chaining.
63       */
64      public GtuSpawner setStream(final StreamInterface stream)
65      {
66          this.stream = stream;
67          return this;
68      }
69  
70      /**
71       * Sets no lane change distance.
72       * @param noLaneChangeDistance Length; no lane change distance.
73       * @return this; for method chaining.
74       */
75      public GtuSpawner setNoLaneChangeDistance(final Length noLaneChangeDistance)
76      {
77          this.noLaneChangeDistance = noLaneChangeDistance;
78          return this;
79      }
80  
81      /**
82       * Sets instantaneous lane changes.
83       * @param instantaneousLaneChanges boolean; instantaneous lane changes.
84       * @return this; for method chaining.
85       */
86      public GtuSpawner setInstantaneousLaneChanges(final boolean instantaneousLaneChanges)
87      {
88          this.instantaneousLaneChanges = instantaneousLaneChanges;
89          return this;
90      }
91  
92      /**
93       * Sets the error handler.
94       * @param errorHandler GtuErrorHandler; error handler.
95       * @return this; for method chaining.
96       */
97      public GtuSpawner setErrorHandler(final GtuErrorHandler errorHandler)
98      {
99          this.errorHandler = errorHandler;
100         return this;
101     }
102 
103     /**
104      * Create a single GTU.
105      * @param id String; id.
106      * @param templateGtuType LaneBasedGtuCharacteristics; characteristics.
107      * @param network RoadNetwork; network.
108      * @param speed Speed; speed.
109      * @param position LanePosition; position.
110      * @throws GtuException when initial GTU values are not correct
111      * @throws OtsGeometryException when the initial path is wrong
112      * @throws NetworkException when the GTU cannot be placed on the given lane
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 }