View Javadoc
1   package org.opentrafficsim.core.gtu.generator;
2   
3   import java.lang.reflect.Method;
4   import java.rmi.RemoteException;
5   
6   import nl.tudelft.simulation.dsol.SimRuntimeException;
7   import nl.tudelft.simulation.language.reflection.ClassUtil;
8   
9   import org.opentrafficsim.core.car.LaneBasedIndividualCar;
10  import org.opentrafficsim.core.car.LaneBasedIndividualCar.LaneBasedIndividualCarBuilder;
11  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
12  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
13  import org.opentrafficsim.core.gtu.GTU;
14  import org.opentrafficsim.core.gtu.GTUException;
15  import org.opentrafficsim.core.gtu.GTUType;
16  import org.opentrafficsim.core.gtu.following.GTUFollowingModel;
17  import org.opentrafficsim.core.network.lane.Lane;
18  import org.opentrafficsim.core.unit.LengthUnit;
19  import org.opentrafficsim.core.unit.SpeedUnit;
20  import org.opentrafficsim.core.unit.TimeUnit;
21  import org.opentrafficsim.core.value.vdouble.scalar.DistContinuousDoubleScalar;
22  
23  /**
24   * <p>
25   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
27   * <p>
28   * @version Feb 2, 2015 <br>
29   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31   * @param <ID> the ID type of the GTU (e.g., String or Integer)
32   */
33  public abstract class AbstractGTUGenerator<ID>
34  {
35      /** the generator name. Will be used for generated GTUs as Name:# where # is the id of the gtu when ID is a String. */
36      private final String name;
37  
38      /** the type of GTU to generate. */
39      private final GTUType<ID> gtuType;
40  
41      /** the gtu class to instantiate. */
42      private final Class<GTU<ID>> gtuClass;
43  
44      /** the GTU following model to use. */
45      private final GTUFollowingModel gtuFollowingModel;
46  
47      /** distribution of the initial speed of the GTU. */
48      private final DistContinuousDoubleScalar.Abs<SpeedUnit> initialSpeedDist;
49  
50      /** distribution of the interarrival time. */
51      private final DistContinuousDoubleScalar.Rel<TimeUnit> interarrivelTimeDist;
52  
53      /** maximum number of GTUs to generate. */
54      private final long maxGTUs;
55  
56      /** start time of generation (delayed start). */
57      private final OTSSimTimeDouble startTime;
58  
59      /** end time of generation. */
60      private final OTSSimTimeDouble endTime;
61  
62      /** lane to generate the GTU on -- at the end for now. */
63      private final Lane lane;
64  
65      /** number of generated GTUs. */
66      @SuppressWarnings("checkstyle:visibilitymodifier")
67      protected long numberGTUs = 0;
68  
69      /**
70       * @param name the name of the generator.
71       * @param simulator the simulator to schedule the start of the generation.
72       * @param gtuType the type of GTU to generate.
73       * @param gtuClass the gtu class to instantiate.
74       * @param gtuFollowingModel the GTU following model to use.
75       * @param initialSpeedDist distribution of the initial speed of the GTU.
76       * @param interarrivelTimeDist distribution of the interarrival time.
77       * @param maxGTUs maximum number of GTUs to generate.
78       * @param startTime start time of generation (delayed start).
79       * @param endTime end time of generation.
80       * @param lane the lane to generate the GTU on -- at the end for now.
81       * @throws SimRuntimeException when simulation scheduling fails
82       * @throws RemoteException when remote simulator cannot be reached
83       */
84      @SuppressWarnings("checkstyle:parameternumber")
85      public AbstractGTUGenerator(final String name, final OTSDEVSSimulatorInterface simulator, final GTUType<ID> gtuType,
86          final Class<GTU<ID>> gtuClass, final GTUFollowingModel gtuFollowingModel,
87          final DistContinuousDoubleScalar.Abs<SpeedUnit> initialSpeedDist,
88          final DistContinuousDoubleScalar.Rel<TimeUnit> interarrivelTimeDist, final long maxGTUs,
89          final OTSSimTimeDouble startTime, final OTSSimTimeDouble endTime, final Lane lane) throws RemoteException,
90          SimRuntimeException
91      {
92          super();
93          this.name = name;
94          this.gtuType = gtuType;
95          this.gtuClass = gtuClass;
96          this.gtuFollowingModel = gtuFollowingModel;
97          this.initialSpeedDist = initialSpeedDist;
98          this.interarrivelTimeDist = interarrivelTimeDist;
99          this.maxGTUs = maxGTUs;
100         this.startTime = startTime;
101         this.endTime = endTime;
102         this.lane = lane;
103 
104         simulator.scheduleEventAbs(startTime, this, this, "generate", null);
105     }
106 
107     /**
108      * Generate a GTU.
109      * @throws Exception 
110      */
111     @SuppressWarnings("unchecked")
112     protected final void generate() throws Exception
113     {
114         // get the return type of the getID() method of the GTU class
115         Class<?> getidtype = String.class;
116         try
117         {
118             Method getid = ClassUtil.resolveMethod(this.gtuClass, "getId", new Class<?>[] {});
119             getidtype = getid.getReturnType();
120         }
121         catch (NoSuchMethodException exception)
122         {
123             throw new GTUException("GTU class " + this.gtuClass.getName() + " does not have getId() method.", exception);
124         }
125 
126         // create a unique id
127         ID id = null;
128         this.numberGTUs++;
129         if (String.class.isAssignableFrom(getidtype))
130         {
131             id = (ID) new String(this.name + ":" + this.numberGTUs);
132         }
133         else if (int.class.isAssignableFrom(getidtype))
134         {
135             id = (ID) new Integer((int) this.numberGTUs);
136         }
137         else if (long.class.isAssignableFrom(getidtype))
138         {
139             id = (ID) new Long(this.numberGTUs);
140         }
141         else
142         {
143             throw new GTUException("GTU ID class " + getidtype.getName() + ": cannot instantiate.");
144         }
145 
146         // create the GTU
147         if (LaneBasedIndividualCar.class.isAssignableFrom(getGtuClass()))
148         {
149             LaneBasedIndividualCarBuilder<ID> carBuilder = new LaneBasedIndividualCarBuilder<ID>();
150             carBuilder.setId(id);
151             carBuilder.setGtuType(getGtuType());
152             carBuilder.setLength(getLengthDist().draw());
153             carBuilder.setWidth(getWidthDist().draw());
154             carBuilder.setMaximumVelocity(getMaximumSpeedDist().draw());
155             carBuilder.setInitialSpeed(getInitialSpeedDist().draw());
156             carBuilder.setSimulator(getSimulator());
157             // TODO carBuilder.setInitialLongitudinalPositions(initialLongitudinalPositions);
158             LaneBasedIndividualCar<ID> car = carBuilder.build();
159         }
160         else
161         {
162             throw new GTUException("GTU class " + getGtuClass().getName() + ": cannot instantiate, no builder.");
163         }
164 
165         // reschedule next arrival
166         OTSSimTimeDouble nextTime = getSimulator().getSimulatorTime().plus(this.interarrivelTimeDist.draw());
167         if (nextTime.le(this.endTime))
168         {
169             getSimulator().scheduleEventAbs(nextTime, this, this, "generate", null);
170         }
171     }
172 
173     /** @return simulator. */
174     public abstract OTSDEVSSimulatorInterface getSimulator();
175 
176     /** @return lengthDist. */
177     public abstract DistContinuousDoubleScalar.Rel<LengthUnit> getLengthDist();
178 
179     /** @return widthDist. */
180     public abstract DistContinuousDoubleScalar.Rel<LengthUnit> getWidthDist();
181 
182     /** @return maximumSpeedDist. */
183     public abstract DistContinuousDoubleScalar.Abs<SpeedUnit> getMaximumSpeedDist();
184 
185     /**
186      * @return name.
187      */
188     public final String getName()
189     {
190         return this.name;
191     }
192 
193     /**
194      * @return gtuType.
195      */
196     public final GTUType<ID> getGtuType()
197     {
198         return this.gtuType;
199     }
200 
201     /**
202      * @return gtuClass.
203      */
204     public final Class<GTU<ID>> getGtuClass()
205     {
206         return this.gtuClass;
207     }
208 
209     /**
210      * @return gtuFollowingModel.
211      */
212     public final GTUFollowingModel getGtuFollowingModel()
213     {
214         return this.gtuFollowingModel;
215     }
216 
217     /**
218      * @return initialSpeedDist.
219      */
220     public final DistContinuousDoubleScalar.Abs<SpeedUnit> getInitialSpeedDist()
221     {
222         return this.initialSpeedDist;
223     }
224 
225     /**
226      * @return interarrivelTimeDist.
227      */
228     public final DistContinuousDoubleScalar.Rel<TimeUnit> getInterarrivelTimeDist()
229     {
230         return this.interarrivelTimeDist;
231     }
232 
233     /**
234      * @return maxGTUs.
235      */
236     public final long getMaxGTUs()
237     {
238         return this.maxGTUs;
239     }
240 
241     /**
242      * @return startTime.
243      */
244     public final OTSSimTimeDouble getStartTime()
245     {
246         return this.startTime;
247     }
248 
249     /**
250      * @return endTime.
251      */
252     public final OTSSimTimeDouble getEndTime()
253     {
254         return this.endTime;
255     }
256 
257 }