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