1 package org.opentrafficsim.kpi.sampling.indicator;
2
3 import java.util.List;
4
5 import org.djunits.value.vdouble.scalar.Duration;
6 import org.djunits.value.vdouble.scalar.base.DoubleScalar;
7 import org.djutils.exceptions.Throw;
8 import org.opentrafficsim.kpi.interfaces.GtuData;
9 import org.opentrafficsim.kpi.sampling.Query;
10 import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
11
12 /**
13 * Abstract indicator which stores the last calculated value and returns it in {@code getValue()} for an equal query, start time
14 * and end time.
15 * <p>
16 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18 * </p>
19 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
20 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
21 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
22 * @param <T> class of the value
23 */
24 public abstract class AbstractIndicator<T extends DoubleScalar<?, ?>>
25 {
26
27 /** Last query. */
28 private Query<?, ?> lastQuery;
29
30 /** Last start time. */
31 private Duration lastStartTime;
32
33 /** Last end time. */
34 private Duration lastEndTime;
35
36 /** Last value. */
37 private T lastValue;
38
39 /**
40 * Constructor.
41 */
42 public AbstractIndicator()
43 {
44 //
45 }
46
47 /**
48 * Get value for given query over time interval, returning earlier calculated value if possible. This method uses
49 * {@code Time.ZERO} as start time.
50 * @param query query, only used to check whether earlier calculated value can be returned
51 * @param endTime start time of interval to calculate indicator over
52 * @param trajectoryGroups group of trajectories to calculate the indicator for
53 * @param <G> GTU data type
54 * @return value for given query
55 */
56 public final <G extends GtuData> T getValue(final Query<G, ?> query, final Duration endTime,
57 final List<TrajectoryGroup<G>> trajectoryGroups)
58 {
59 return getValue(query, Duration.ZERO, endTime, trajectoryGroups);
60 }
61
62 /**
63 * Get value for given query over time interval, returning earlier calculated value if possible.
64 * @param query query, only used to check whether earlier calculated value can be returned
65 * @param startTime start time of interval to calculate indicator over
66 * @param endTime start time of interval to calculate indicator over
67 * @param trajectoryGroups group of trajectories to calculate the indicator for
68 * @param <G> GTU data type
69 * @return value for given query
70 */
71 public final <G extends GtuData> T getValue(final Query<G, ?> query, final Duration startTime, final Duration endTime,
72 final List<TrajectoryGroup<G>> trajectoryGroups)
73 {
74 Throw.whenNull(query, "Query may not be null.");
75 Throw.whenNull(startTime, "Start time may not be null.");
76 Throw.whenNull(endTime, "End time may not be null.");
77 if (this.lastQuery == null || !this.lastQuery.equals(query) || !this.lastStartTime.equals(startTime)
78 || !this.lastEndTime.equals(endTime))
79 {
80 this.lastQuery = query;
81 this.lastStartTime = startTime;
82 this.lastEndTime = endTime;
83 this.lastValue = calculate(query, startTime, endTime, trajectoryGroups);
84 }
85 return this.lastValue;
86 }
87
88 /**
89 * Calculate value for given trajectory groups.
90 * @param query query
91 * @param startTime start time of interval to calculate indicator over
92 * @param endTime start time of interval to calculate indicator over
93 * @param trajectoryGroups groups of trajectories to calculate the indicator for
94 * @param <G> GTU data type
95 * @return value for given trajectory groups
96 */
97 protected abstract <G extends GtuData> T calculate(Query<G, ?> query, Duration startTime, Duration endTime,
98 List<TrajectoryGroup<G>> trajectoryGroups);
99
100 }