View Javadoc
1   package org.opentrafficsim.road.gtu.generator;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.FileReader;
7   import java.io.IOException;
8   import java.io.Serializable;
9   import java.util.LinkedHashSet;
10  import java.util.Set;
11  
12  import javax.naming.NamingException;
13  
14  import org.djunits.unit.LengthUnit;
15  import org.djunits.unit.SpeedUnit;
16  import org.djunits.unit.TimeUnit;
17  import org.djunits.value.vdouble.scalar.Length;
18  import org.djunits.value.vdouble.scalar.Speed;
19  import org.djunits.value.vdouble.scalar.Time;
20  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
21  import org.opentrafficsim.core.geometry.OTSGeometryException;
22  import org.opentrafficsim.core.gtu.GTUDirectionality;
23  import org.opentrafficsim.core.gtu.GTUException;
24  import org.opentrafficsim.core.gtu.GTUType;
25  import org.opentrafficsim.core.gtu.animation.GTUColorer;
26  import org.opentrafficsim.core.network.NetworkException;
27  import org.opentrafficsim.core.network.OTSNetwork;
28  import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
29  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
30  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
31  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
32  import org.opentrafficsim.road.network.lane.Lane;
33  
34  import nl.tudelft.simulation.dsol.SimRuntimeException;
35  
36  /**
37   * Generate GTUs at times prescribed in a text file.
38   * <p>
39   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
40   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
41   * <p>
42   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
43   *          initial version 7 jul. 2015 <br>
44   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
45   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
46   */
47  public class ListGTUGenerator implements Serializable
48  {
49      /** */
50      private static final long serialVersionUID = 20150000L;
51  
52      /** Name of this ListGTUGenerator. */
53      private final String name;
54  
55      /** Lane on which the generated GTUs are placed. */
56      private final Lane lane;
57  
58      /** The type of GTUs generated. */
59      private final GTUType gtuType;
60  
61      /** Initial speed of the generated GTUs. */
62      private final Speed initialSpeed;
63  
64      /** The GTU colorer that will be linked to each generated GTU. */
65      private final GTUColorer gtuColorer;
66  
67      /** The lane-based strategical planner to use. */
68      private final LaneBasedStrategicalPlanner strategicalPlanner;
69  
70      /** The simulator that controls everything. */
71      private final OTSDEVSSimulatorInterface simulator;
72  
73      /** Reader for the event list. */
74      private BufferedReader reader;
75  
76      /** Number of GTUs created. */
77      private int carsCreated = 0;
78  
79      /** The network to initially register the cars in. */
80      private final OTSNetwork network;
81  
82      /**
83       * Construct a GTU generator that takes the times to generate another GTU from an external source. <br>
84       * Currently the external input is a text file in the local file system. This should be replaced by a more general
85       * mechanism. Currently, the format of the input is one floating point value per line. This may be changed into an XML
86       * format that can also specify the GTUType, etc.
87       * @param name String; name if this generator
88       * @param simulator OTSDEVSSimulatorInterface; the simulator
89       * @param gtuType GTUType&lt;ID&gt;; the GTUType of the generated GTUs
90       * @param initialSpeed Speed; the initial speed of the generated GTUs
91       * @param lane Lane; the lane on which the generated GTUs are placed
92       * @param position Length; the position on the lane where the generated GTUs are placed
93       * @param direction the direction on the lane in which the GTU has to be generated (DIR_PLUS, or DIR_MINUS)
94       * @param gtuColorer GTUColorere; the GTUColorer of the generated GTUs
95       * @param strategicalPlanner the lane-based strategical planner to use
96       * @param network the network to initially register the cars in
97       * @param fileName String; name of file with the times when another GTU is to be generated (XXXX STUB)
98       * @throws SimRuntimeException on
99       * @throws NetworkException on
100      */
101     public ListGTUGenerator(final String name, final OTSDEVSSimulatorInterface simulator, final GTUType gtuType,
102             final Speed initialSpeed, final Lane lane, final Length position, final GTUDirectionality direction,
103             final GTUColorer gtuColorer, final LaneBasedStrategicalPlanner strategicalPlanner, final OTSNetwork network,
104             final String fileName) throws SimRuntimeException, NetworkException
105     {
106         if (null == lane)
107         {
108             throw new NetworkException("lane may not be null");
109         }
110         this.name = name;
111         this.lane = lane;
112         this.gtuType = gtuType;
113         this.initialSpeed = initialSpeed;
114         this.simulator = simulator;
115         this.gtuColorer = gtuColorer;
116         this.strategicalPlanner = strategicalPlanner;
117         this.network = network;
118         try
119         {
120             this.reader = new BufferedReader(new FileReader(new File(fileName)));
121             scheduleNextVehicle();
122         }
123         catch (FileNotFoundException exception)
124         {
125             exception.printStackTrace();
126         }
127     }
128 
129     /**
130      * Schedule generation of the next GTU.
131      */
132     private void scheduleNextVehicle()
133     {
134         try
135         {
136             String line = null;
137             do
138             {
139                 line = this.reader.readLine();
140                 if (null == line)
141                 {
142                     return; // End of input; do not re-schedule
143                 }
144             }
145             while (line.equals("")); // ignore blank lines
146             double when = Double.parseDouble(line);
147             this.simulator.scheduleEventAbs(new Time(when, TimeUnit.BASE), this, this, "generateCar", null);
148         }
149         catch (NumberFormatException exception)
150         {
151             exception.printStackTrace();
152             scheduleNextVehicle();
153         }
154         catch (IOException exception)
155         {
156             exception.printStackTrace();
157         }
158         catch (SimRuntimeException exception)
159         {
160             exception.printStackTrace();
161         }
162     }
163 
164     /**
165      * Generate one car and re-schedule this method.
166      */
167     protected final void generateCar()
168     {
169         // TODO use given position in the constructor?
170         Length initialPosition = new Length(0, LengthUnit.METER);
171         Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>();
172         // TODO use given directionality in the constructor?
173         try
174         {
175             initialPositions.add(new DirectedLanePosition(this.lane, initialPosition, GTUDirectionality.DIR_PLUS));
176             Length vehicleLength = new Length(4, LengthUnit.METER);
177             LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU("" + (++this.carsCreated), this.gtuType, vehicleLength,
178                     new Length(1.8, LengthUnit.METER), new Speed(200, SpeedUnit.KM_PER_HOUR), this.simulator, this.network);
179             gtu.initWithAnimation(this.strategicalPlanner, initialPositions, this.initialSpeed, DefaultCarAnimation.class,
180                     this.gtuColorer);
181             scheduleNextVehicle();
182         }
183         catch (SimRuntimeException | NamingException | NetworkException | GTUException | OTSGeometryException exception)
184         {
185             exception.printStackTrace();
186         }
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public String toString()
192     {
193         return "ListGTUGenerator [name=" + this.name + ", lane=" + this.lane + ", gtuType=" + this.gtuType + ", initialSpeed="
194                 + this.initialSpeed + ", carsCreated=" + this.carsCreated + "]";
195     }
196 
197 }