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-2019 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              // TODO: from URI as defined in xsd
43              this.reader = new BufferedReader(new FileReader(new File(fileName)));
44          }
45          catch (FileNotFoundException exception)
46          {
47              throw new RuntimeException(exception);
48          }
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public Duration draw() throws ProbabilityException, ParameterException
54      {
55          try
56          {
57              String line = null;
58              do
59              {
60                  line = this.reader.readLine();
61                  if (null == line)
62                  {
63                      return null; // End of input; do not re-schedule
64                  }
65              }
66              while (line.equals("")); // ignore blank lines
67              double when = Double.parseDouble(line);
68              if (when < this.prev)
69              {
70                  throw new RuntimeException(
71                          "Arrival times from file are not in chronological order (" + when + "<" + this.prev + ").");
72              }
73              Duration headway = Duration.createSI(when - this.prev);
74              this.prev = when;
75              return headway;
76          }
77          catch (NumberFormatException | IOException exception)
78          {
79              throw new RuntimeException("Unable to read arrival time from file.", exception);
80          }
81      }
82  
83  }