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.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   * Super class for plots that use sampler data. Sub classes may obtain trajectories using {@code getTrajectories()}, or
18   * alternatively maintain some other -possibly more efficient- connection to the sampler. This class also connects the plot to a
19   * path, consisting of a list of lanes. Start distance along the path for each lane is provided to sub classes using
20   * {@code getStartDistance(KpiLaneDirection)}. Total length is obtained using {@code getEndLocation()}.
21   * <p>
22   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 4 okt. 2018 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
29   */
30  public abstract class AbstractSamplerPlot extends AbstractSpaceTimePlot
31  {
32  
33      /** */
34      private static final long serialVersionUID = 20181004L;
35  
36      /** Sampler. */
37      private final Sampler<?> sampler;
38  
39      /** KPI lane directions registered in the sampler. */
40      private final GraphPath<KpiLaneDirection> path;
41  
42      /** Time when trajectories were last updated per series in the path. */
43      private List<Time> lastUpdateTime = new ArrayList<>();
44  
45      /** Cached trajectories per series in the path. */
46      private List<List<TrajectoryGroup<?>>> trajectoriesCache = new ArrayList<>();
47  
48      /**
49       * Constructor.
50       * @param caption String; caption
51       * @param updateInterval Duration; regular update interval (simulation time)
52       * @param simulator OTSSimulatorInterface; simulator
53       * @param sampler Sampler&lt;?&gt;; road sampler
54       * @param path GraphPath&lt;KpiLaneDirection&gt;; path
55       * @param delay Duration; delay so critical future events have occurred, e.g. GTU's next move's to extend trajectories
56       */
57      public AbstractSamplerPlot(final String caption, final Duration updateInterval, final OTSSimulatorInterface simulator,
58              final Sampler<?> sampler, final GraphPath<KpiLaneDirection> path, final Duration delay)
59      {
60          super(caption, updateInterval, simulator, delay, DEFAULT_INITIAL_UPPER_TIME_BOUND);
61          this.sampler = sampler;
62          this.path = path;
63          for (Section<KpiLaneDirection> section : path.getSections())
64          {
65              for (KpiLaneDirection kpiLaneDirection : section)
66              {
67                  sampler.registerSpaceTimeRegion(new SpaceTimeRegion(kpiLaneDirection, Length.ZERO,
68                          kpiLaneDirection.getLaneData().getLength(), Time.ZERO, Time.createSI(Double.MAX_VALUE)));
69              }
70          }
71          for (int i = 0; i < path.getNumberOfSeries(); i++)
72          {
73              this.trajectoriesCache.add(new ArrayList<>());
74              this.lastUpdateTime.add(null);
75          }
76      }
77  
78      /**
79       * Returns all trajectories for the series, in order of the path.
80       * @param series int; series number
81       * @return List&lt;TrajectoryGroup&gt;; the trajectories
82       */
83      protected List<TrajectoryGroup<?>> getTrajectories(final int series)
84      {
85          if (this.lastUpdateTime.get(series) == null || this.lastUpdateTime.get(series).lt(getUpdateTime()))
86          {
87              List<TrajectoryGroup<?>> cache = new ArrayList<>();
88              for (Section<KpiLaneDirection> section : getPath().getSections())
89              {
90                  cache.add(this.sampler.getTrajectoryGroup(section.getSource(series)));
91              }
92              this.trajectoriesCache.set(series, cache);
93              this.lastUpdateTime.set(series, getUpdateTime());
94          }
95          return this.trajectoriesCache.get(series);
96      }
97  
98      /**
99       * Returns the path.
100      * @return GraphPath&lt;KpiLaneDirection&gt;; the path
101      */
102     protected final GraphPath<KpiLaneDirection> getPath()
103     {
104         return this.path;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     protected final Length getEndLocation()
110     {
111         return getPath().getTotalLength();
112     }
113 
114     /**
115      * Returns the sampler.
116      * @return Sampler&lt;?&gt;; sampler.
117      */
118     protected final Sampler<?> getSampler()
119     {
120         return this.sampler;
121     }
122 
123 }