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