View Javadoc
1   package org.opentrafficsim.kpi.sampling.indicator;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import org.djunits.value.vdouble.scalar.Duration;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djunits.value.vdouble.scalar.Time;
9   import org.opentrafficsim.kpi.interfaces.LinkDataInterface;
10  import org.opentrafficsim.kpi.sampling.Query;
11  import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
12  
13  /**
14   * Sum of (approximate) link lengths divided by mean speed. 
15   * <p>
16   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 okt. 2016 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   */
24  public class MeanTravelTime extends AbstractIndicator<Duration>
25  {
26  
27      /** Mean speed indicator. */
28      private final MeanSpeed meanSpeed;
29  
30      /**
31       * @param meanSpeed mean speed indicator
32       */
33      public MeanTravelTime(final MeanSpeed meanSpeed)
34      {
35          this.meanSpeed = meanSpeed;
36      }
37  
38      /** {@inheritDoc} */
39      @Override
40      public final Duration calculate(final Query query, final Time startTime, final Time endTime)
41      {
42          Length cumulLength = Length.ZERO;
43          Set<LinkDataInterface> links = new HashSet<>();
44          for (TrajectoryGroup trajectoryGroup : query.getTrajectoryGroups(startTime, endTime))
45          {
46              if (!links.contains(trajectoryGroup.getLaneDirection().getLaneData().getLinkData()))
47              {
48                  cumulLength = cumulLength.plus(trajectoryGroup.getLength()); // TODO should be average lane length of link
49                  links.add(trajectoryGroup.getLaneDirection().getLaneData().getLinkData());
50              }
51          }
52          return cumulLength.divideBy(this.meanSpeed.getValue(query, startTime, endTime));
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      @SuppressWarnings("checkstyle:designforextension")
58      public String toString()
59      {
60          return "MeanTravelTime [meanSpeed=" + this.meanSpeed + "]";
61      }
62  
63  }