1 package org.opentrafficsim.kpi.sampling.indicator;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.djunits.unit.DurationUnit;
8 import org.djunits.value.vdouble.scalar.Duration;
9 import org.djunits.value.vfloat.vector.FloatSpeedVector;
10 import org.djutils.exceptions.Throw;
11 import org.opentrafficsim.base.OtsRuntimeException;
12 import org.opentrafficsim.kpi.interfaces.GtuData;
13 import org.opentrafficsim.kpi.sampling.Query;
14 import org.opentrafficsim.kpi.sampling.SamplingException;
15 import org.opentrafficsim.kpi.sampling.Trajectory;
16 import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
17 import org.opentrafficsim.kpi.sampling.data.ReferenceSpeed;
18
19
20
21
22
23
24
25
26
27
28
29 public class TotalDelayReference extends AbstractIndicator<Duration>
30 {
31
32
33 private static final ReferenceSpeed REF_SPEED_TYPE = ReferenceSpeed.INSTANCE;
34
35
36
37
38 public TotalDelayReference()
39 {
40
41 }
42
43 @Override
44 protected final <G extends GtuData> Duration calculate(final Query<G, ?> query, final Duration startTime,
45 final Duration endTime, final List<TrajectoryGroup<G>> trajectoryGroups)
46 {
47 Map<String, Duration> gtuTimes = new LinkedHashMap<>();
48 Map<String, Duration> gtuRefTimes = new LinkedHashMap<>();
49 for (TrajectoryGroup<? extends GtuData> trajectoryGroup : trajectoryGroups)
50 {
51 try
52 {
53 for (Trajectory<?> trajectory : trajectoryGroup.getTrajectories())
54 {
55 Duration sumTime;
56 Duration sumRefTime;
57 if (gtuTimes.containsKey(trajectory.getGtuId()))
58 {
59 sumTime = gtuTimes.get(trajectory.getGtuId());
60 sumRefTime = gtuRefTimes.get(trajectory.getGtuId());
61 }
62 else
63 {
64 sumTime = Duration.ZERO;
65 sumRefTime = Duration.ZERO;
66 }
67 Throw.when(!trajectory.contains(REF_SPEED_TYPE), UnsupportedOperationException.class,
68 "TotalDelayReference can only work with trajectories that have %s extended data.",
69 REF_SPEED_TYPE.getId());
70 FloatSpeedVector refSpeed = trajectory.getExtendedData(REF_SPEED_TYPE);
71 float[] x = trajectory.getX();
72 for (int i = 1; i < refSpeed.size(); i++)
73 {
74 double refV = refSpeed.get(i - 1).si;
75 double dx = x[i] - x[i - 1];
76 sumRefTime = sumRefTime.plus(new Duration(dx / refV, DurationUnit.SI));
77 }
78 gtuTimes.put(trajectory.getGtuId(), sumTime.plus(trajectory.getTotalDuration()));
79 gtuRefTimes.put(trajectory.getGtuId(), sumRefTime);
80 }
81 }
82 catch (SamplingException exception)
83 {
84 throw new OtsRuntimeException("Exception while trying to determine delay in trajectory.", exception);
85 }
86 }
87 Duration delaySum = Duration.ZERO;
88 for (String id : gtuTimes.keySet())
89 {
90 Duration gtuTime = gtuTimes.get(id);
91 Duration gtuRefTime = gtuRefTimes.get(id);
92 if (gtuTime.gt(gtuRefTime))
93 {
94 delaySum = delaySum.plus(gtuTime.minus(gtuRefTime));
95 }
96 }
97 return delaySum;
98 }
99
100 @Override
101 @SuppressWarnings("checkstyle:designforextension")
102 public String toString()
103 {
104 return "TotalDelayReference";
105 }
106
107 }