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.sampling.KpiLaneDirection;
12 import org.opentrafficsim.kpi.sampling.Sampler;
13 import org.opentrafficsim.kpi.sampling.SpaceTimeRegion;
14 import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public abstract class AbstractSamplerPlot extends AbstractSpaceTimePlot
31 {
32
33
34 private final Sampler<?> sampler;
35
36
37 private final GraphPath<KpiLaneDirection> path;
38
39
40 private List<Time> lastUpdateTime = new ArrayList<>();
41
42
43 private List<List<TrajectoryGroup<?>>> trajectoriesCache = new ArrayList<>();
44
45
46
47
48
49
50
51
52
53
54 public AbstractSamplerPlot(final String caption, final Duration updateInterval, final OTSSimulatorInterface simulator,
55 final Sampler<?> sampler, final GraphPath<KpiLaneDirection> path, final Duration delay)
56 {
57 super(caption, updateInterval, simulator, delay, DEFAULT_INITIAL_UPPER_TIME_BOUND);
58 this.sampler = sampler;
59 this.path = path;
60 for (Section<KpiLaneDirection> section : path.getSections())
61 {
62 for (KpiLaneDirection kpiLaneDirection : section)
63 {
64 sampler.registerSpaceTimeRegion(new SpaceTimeRegion(kpiLaneDirection, Length.ZERO,
65 kpiLaneDirection.getLaneData().getLength(), Time.ZERO, Time.instantiateSI(Double.MAX_VALUE)));
66 }
67 }
68 for (int i = 0; i < path.getNumberOfSeries(); i++)
69 {
70 this.trajectoriesCache.add(new ArrayList<>());
71 this.lastUpdateTime.add(null);
72 }
73 }
74
75
76
77
78
79
80 protected List<TrajectoryGroup<?>> getTrajectories(final int series)
81 {
82 if (this.lastUpdateTime.get(series) == null || this.lastUpdateTime.get(series).lt(getUpdateTime()))
83 {
84 List<TrajectoryGroup<?>> cache = new ArrayList<>();
85 for (Section<KpiLaneDirection> section : getPath().getSections())
86 {
87 cache.add(this.sampler.getTrajectoryGroup(section.getSource(series)));
88 }
89 this.trajectoriesCache.set(series, cache);
90 this.lastUpdateTime.set(series, getUpdateTime());
91 }
92 return this.trajectoriesCache.get(series);
93 }
94
95
96
97
98
99 public final GraphPath<KpiLaneDirection> getPath()
100 {
101 return this.path;
102 }
103
104
105 @Override
106 protected final Length getEndLocation()
107 {
108 return getPath().getTotalLength();
109 }
110
111
112
113
114
115 protected final Sampler<?> getSampler()
116 {
117 return this.sampler;
118 }
119
120 }