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
15
16
17
18
19
20
21
22
23
24
25
26 public abstract class AbstractSamplerPlot extends AbstractSpaceTimePlot
27 {
28
29
30 private final SamplerData<?> samplerData;
31
32
33 private final GraphPath<? extends LaneData<?>> path;
34
35
36 private List<Duration> lastUpdateTime = new ArrayList<>();
37
38
39 private List<List<TrajectoryGroup<?>>> trajectoriesCache = new ArrayList<>();
40
41
42
43
44
45
46
47
48
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
65
66
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
89
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
104
105
106 protected final SamplerData<?> getSamplerData()
107 {
108 return this.samplerData;
109 }
110
111 }