View Javadoc
1   package org.opentrafficsim.road.gtu.generator.headway;
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   
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.opentrafficsim.base.parameters.ParameterException;
11  import org.opentrafficsim.core.distributions.Generator;
12  import org.opentrafficsim.core.distributions.ProbabilityException;
13  
14  /**
15   * Headway generator that takes it's input from a file.
16   * <p>
17   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 jan. 2018 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class ListHeadways implements Generator<Duration>
26  {
27  
28      /** Reader for the event list. */
29      private final BufferedReader reader;
30  
31      /** Time of previous GTU. */
32      private double prev = 0.0;
33  
34      /**
35       * Constructor using file.
36       * @param fileName String; file with arrival times
37       */
38      public ListHeadways(final String fileName)
39      {
40          try
41          {
42              this.reader = new BufferedReader(new FileReader(new File(fileName)));
43          }
44          catch (FileNotFoundException exception)
45          {
46              throw new RuntimeException(exception);
47          }
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public Duration draw() throws ProbabilityException, ParameterException
53      {
54          try
55          {
56              String line = null;
57              do
58              {
59                  line = this.reader.readLine();
60                  if (null == line)
61                  {
62                      return null; // End of input; do not re-schedule
63                  }
64              }
65              while (line.equals("")); // ignore blank lines
66              double when = Double.parseDouble(line);
67              if (when < this.prev)
68              {
69                  throw new RuntimeException(
70                          "Arrival times from file are not in chronological order (" + when + "<" + this.prev + ").");
71              }
72              Duration headway = Duration.createSI(when - this.prev);
73              this.prev = when;
74              return headway;
75          }
76          catch (NumberFormatException | IOException exception)
77          {
78              throw new RuntimeException("Unable to read arrival time from file.", exception);
79          }
80      }
81  
82  }