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
38
39
40
41
42
43
44
45
46
47 public class ListGTUGenerator implements Serializable
48 {
49
50 private static final long serialVersionUID = 20150000L;
51
52
53 private final String name;
54
55
56 private final Lane lane;
57
58
59 private final GTUType gtuType;
60
61
62 private final Speed initialSpeed;
63
64
65 private final GTUColorer gtuColorer;
66
67
68 private final LaneBasedStrategicalPlanner strategicalPlanner;
69
70
71 private final OTSDEVSSimulatorInterface simulator;
72
73
74 private BufferedReader reader;
75
76
77 private int carsCreated = 0;
78
79
80 private final OTSNetwork network;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
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;
143 }
144 }
145 while (line.equals(""));
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
166
167 protected final void generateCar()
168 {
169
170 Length initialPosition = new Length(0, LengthUnit.METER);
171 Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>();
172
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
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 }