1 package org.opentrafficsim.kpi.sampling.indicator;
2
3 import java.util.List;
4 import java.util.function.BiFunction;
5
6 import org.djunits.value.vdouble.scalar.Duration;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.opentrafficsim.kpi.interfaces.GtuData;
9 import org.opentrafficsim.kpi.interfaces.LaneData;
10 import org.opentrafficsim.kpi.sampling.Query;
11 import org.opentrafficsim.kpi.sampling.Trajectory;
12 import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
13
14
15
16
17
18
19
20
21
22
23
24 public class TotalDelay extends AbstractIndicator<Duration>
25 {
26
27
28 private final BiFunction<LaneData<?>, String, Speed> referenceSpeedProvider;
29
30
31
32
33
34 public TotalDelay(final Speed referenceSpeed)
35 {
36 this((lane, gtuTypeId) -> referenceSpeed);
37 }
38
39
40
41
42
43 public TotalDelay(final BiFunction<LaneData<?>, String, Speed> referenceSpeedProvider)
44 {
45 this.referenceSpeedProvider = referenceSpeedProvider;
46 }
47
48 @Override
49 protected <G extends GtuData> Duration calculate(final Query<G, ?> query, final Duration startTime, final Duration endTime,
50 final List<TrajectoryGroup<G>> trajectoryGroups)
51 {
52 double delay = 0.0;
53 for (TrajectoryGroup<?> trajectoryGroup : trajectoryGroups)
54 {
55 for (Trajectory<?> trajectory : trajectoryGroup.getTrajectories())
56 {
57 Speed referenceSpeedOnLane =
58 this.referenceSpeedProvider.apply(trajectoryGroup.getLane(), trajectory.getGtuTypeId());
59 double d = trajectory.getTotalDuration().si - trajectory.getTotalLength().si / referenceSpeedOnLane.si;
60 if (d > 0.0)
61 {
62 delay += d;
63 }
64 }
65 }
66 return Duration.ofSI(delay);
67 }
68
69 @Override
70 public String toString()
71 {
72 return "TotalDelay [referenceSpeed=" + this.referenceSpeedProvider + "]";
73 }
74
75 }