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.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(KpiLaneDirection)}. Total length is obtained using {@code getEndLocation()}.
20 * <p>
21 * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
23 * <p>
24 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 4 okt. 2018 <br>
25 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
28 */
29 public abstract class AbstractSamplerPlot extends AbstractSpaceTimePlot
30 {
31
32 /** Sampler data. */
33 private final SamplerData<?> samplerData;
34
35 /** KPI lane directions registered in the sampler. */
36 private final GraphPath<KpiLaneDirection> path;
37
38 /** Time when trajectories were last updated per series in the path. */
39 private List<Time> lastUpdateTime = new ArrayList<>();
40
41 /** Cached trajectories per series in the path. */
42 private List<List<TrajectoryGroup<?>>> trajectoriesCache = new ArrayList<>();
43
44 /**
45 * Constructor.
46 * @param caption String; caption
47 * @param updateInterval Duration; regular update interval (simulation time)
48 * @param simulator OTSSimulatorInterface; simulator
49 * @param samplerData SamplerData<?>; sampler data
50 * @param path GraphPath<KpiLaneDirection>; path
51 * @param delay Duration; amount of time that chart runs behind simulation to prevent gaps in the charted data
52 */
53 public AbstractSamplerPlot(final String caption, final Duration updateInterval, final OTSSimulatorInterface simulator,
54 final SamplerData<?> samplerData, final GraphPath<KpiLaneDirection> path, final Duration delay)
55 {
56 super(caption, updateInterval, simulator, delay, DEFAULT_INITIAL_UPPER_TIME_BOUND);
57 this.samplerData = samplerData;
58 this.path = path;
59 for (int i = 0; i < path.getNumberOfSeries(); i++)
60 {
61 this.trajectoriesCache.add(new ArrayList<>());
62 this.lastUpdateTime.add(null);
63 }
64 }
65
66 /**
67 * Returns all trajectories for the series, in order of the path.
68 * @param series int; series number
69 * @return List<TrajectoryGroup>; the trajectories
70 */
71 protected List<TrajectoryGroup<?>> getTrajectories(final int series)
72 {
73 if (this.lastUpdateTime.get(series) == null || this.lastUpdateTime.get(series).lt(getUpdateTime()))
74 {
75 List<TrajectoryGroup<?>> cache = new ArrayList<>();
76 for (Section<KpiLaneDirection> section : getPath().getSections())
77 {
78 cache.add(this.samplerData.getTrajectoryGroup(section.getSource(series)));
79 }
80 this.trajectoriesCache.set(series, cache);
81 this.lastUpdateTime.set(series, getUpdateTime());
82 }
83 return this.trajectoriesCache.get(series);
84 }
85
86 /**
87 * Returns the path.
88 * @return GraphPath<KpiLaneDirection>; the path
89 */
90 public final GraphPath<KpiLaneDirection> getPath()
91 {
92 return this.path;
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 protected final Length getEndLocation()
98 {
99 return getPath().getTotalLength();
100 }
101
102 /**
103 * Returns the sampler data.
104 * @return SamplerData<?>; sampler.
105 */
106 protected final SamplerData<?> getSamplerData()
107 {
108 return this.samplerData;
109 }
110
111 }