View Javadoc
1   package org.opentrafficsim.draw.graphs;
2   
3   import java.util.ArrayList;
4   import java.util.List;
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.core.dsol.OtsSimulatorInterface;
10  import org.opentrafficsim.draw.graphs.GraphPath.Section;
11  import org.opentrafficsim.kpi.interfaces.LaneData;
12  import org.opentrafficsim.kpi.sampling.SamplerData;
13  import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
14  
15  /**
16   * Super class for plots that use sampler data. Sub classes may obtain trajectories using {@code getTrajectories()}, or
17   * alternatively maintain some other -possibly more efficient- connection to the sampler. This class also connects the plot to a
18   * path, consisting of a list of lanes. Start distance along the path for each lane is provided to sub classes using
19   * {@code getStartDistance(LaneData)}. Total length is obtained using {@code getEndLocation()}.
20   * <p>
21   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * </p>
24   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
26   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
27   */
28  public abstract class AbstractSamplerPlot extends AbstractSpaceTimePlot
29  {
30  
31      /** Sampler data. */
32      private final SamplerData<?> samplerData;
33  
34      /** KPI lane directions registered in the sampler. */
35      private final GraphPath<? extends LaneData> path;
36  
37      /** Time when trajectories were last updated per series in the path. */
38      private List<Time> lastUpdateTime = new ArrayList<>();
39  
40      /** Cached trajectories per series in the path. */
41      private List<List<TrajectoryGroup<?>>> trajectoriesCache = new ArrayList<>();
42  
43      /**
44       * Constructor.
45       * @param caption String; caption
46       * @param updateInterval Duration; regular update interval (simulation time)
47       * @param simulator OtsSimulatorInterface; simulator
48       * @param samplerData SamplerData&lt;?&gt;; sampler data
49       * @param path GraphPath&lt;? extends LaneData&gt;; path
50       * @param delay Duration; amount of time that chart runs behind simulation to prevent gaps in the charted data
51       */
52      public AbstractSamplerPlot(final String caption, final Duration updateInterval, final OtsSimulatorInterface simulator,
53              final SamplerData<?> samplerData, final GraphPath<? extends LaneData> path, final Duration delay)
54      {
55          super(caption, updateInterval, simulator, delay, DEFAULT_INITIAL_UPPER_TIME_BOUND);
56          this.samplerData = samplerData;
57          this.path = path;
58          for (int i = 0; i < path.getNumberOfSeries(); i++)
59          {
60              this.trajectoriesCache.add(new ArrayList<>());
61              this.lastUpdateTime.add(null);
62          }
63      }
64  
65      /**
66       * Returns all trajectories for the series, in order of the path.
67       * @param series int; series number
68       * @return List&lt;TrajectoryGroup&gt;; the trajectories
69       */
70      protected List<TrajectoryGroup<?>> getTrajectories(final int series)
71      {
72          if (this.lastUpdateTime.get(series) == null || this.lastUpdateTime.get(series).lt(getUpdateTime()))
73          {
74              List<TrajectoryGroup<?>> cache = new ArrayList<>();
75              for (Section<? extends LaneData> section : getPath().getSections())
76              {
77                  cache.add(this.samplerData.getTrajectoryGroup(section.getSource(series)));
78              }
79              this.trajectoriesCache.set(series, cache);
80              this.lastUpdateTime.set(series, getUpdateTime());
81          }
82          return this.trajectoriesCache.get(series);
83      }
84  
85      /**
86       * Returns the path.
87       * @return GraphPath&lt;? extends LaneData&gt;; the path
88       */
89      public final GraphPath<? extends LaneData> getPath()
90      {
91          return this.path;
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      protected final Length getEndLocation()
97      {
98          return getPath().getTotalLength();
99      }
100 
101     /**
102      * Returns the sampler data.
103      * @return SamplerData&lt;?&gt;; sampler.
104      */
105     protected final SamplerData<?> getSamplerData()
106     {
107         return this.samplerData;
108     }
109 
110 }