View Javadoc
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   * Simple class to spawn GTUs.
26   * <p>
27   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * </p>
30   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
31   * @author Christoph Thees
32   */
33  public class GtuSpawner
34  {
35  
36      /** Default GTU templates. */
37      private BiFunction<GtuType, StreamInterface, Optional<GtuTemplate>> defaultGtuTemplate = Defaults.NL;
38  
39      /** Random stream. */
40      private StreamInterface stream = new MersenneTwister(123L);
41  
42      /** Length over which no lane changes will happen. */
43      private Length noLaneChangeDistance = Length.ofSI(100.0);
44  
45      /** Lane bookkeeping. */
46      private LaneBookkeeping bookkeeping = LaneBookkeeping.EDGE;
47  
48      /** Error handler. */
49      private GtuErrorHandler errorHandler = GtuErrorHandler.THROW;
50  
51      /**
52       * Constructor.
53       */
54      public GtuSpawner()
55      {
56          //
57      }
58  
59      /**
60       * Sets the default GTU templates.
61       * @param defaultGtuTemplate default GTU templates.
62       * @return for method chaining.
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       * Sets random stream.
73       * @param stream random stream.
74       * @return for method chaining.
75       */
76      public GtuSpawner setStream(final StreamInterface stream)
77      {
78          this.stream = stream;
79          return this;
80      }
81  
82      /**
83       * Sets no lane change distance.
84       * @param noLaneChangeDistance no lane change distance.
85       * @return for method chaining.
86       */
87      public GtuSpawner setNoLaneChangeDistance(final Length noLaneChangeDistance)
88      {
89          this.noLaneChangeDistance = noLaneChangeDistance;
90          return this;
91      }
92  
93      /**
94       * Sets how lane bookkeeping at lane changes is done.
95       * @param bookkeeping how lane bookkeeping at lane changes is done
96       * @return for method chaining
97       */
98      public GtuSpawner setBookkeeping(final LaneBookkeeping bookkeeping)
99      {
100         this.bookkeeping = bookkeeping;
101         return this;
102     }
103 
104     /**
105      * Sets the error handler.
106      * @param errorHandler error handler.
107      * @return for method chaining.
108      */
109     public GtuSpawner setErrorHandler(final GtuErrorHandler errorHandler)
110     {
111         this.errorHandler = errorHandler;
112         return this;
113     }
114 
115     /**
116      * Create a single GTU.
117      * @param id id.
118      * @param templateGtuType characteristics.
119      * @param network network.
120      * @param speed speed.
121      * @param position position.
122      * @throws GtuException when initial GTU values are not correct
123      * @throws NetworkException when the GTU cannot be placed on the given lane
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 }