1 package org.opentrafficsim.draw.graphs;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.NoSuchElementException;
7
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.djunits.value.vdouble.scalar.Speed;
10 import org.djunits.value.vdouble.scalar.Time;
11 import org.djutils.exceptions.Throw;
12 import org.djutils.immutablecollections.Immutable;
13 import org.djutils.immutablecollections.ImmutableArrayList;
14 import org.djutils.immutablecollections.ImmutableList;
15 import org.opentrafficsim.base.WeightedMeanAndSum;
16 import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
17 import org.opentrafficsim.kpi.sampling.Sampler;
18 import org.opentrafficsim.kpi.sampling.SpaceTimeRegion;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class GraphPath<S> extends AbstractGraphSpace<S>
35 {
36
37
38 private final List<Section<S>> sections;
39
40
41 private final List<Length> startDistances = new ArrayList<>();
42
43
44 private final Length totalLength;
45
46
47 private final Speed speedLimit;
48
49
50
51
52
53
54 public GraphPath(final String name, final List<Section<S>> sections)
55 {
56 this(new ArrayList<String>()
57 {
58
59 private static final long serialVersionUID = 20181020L;
60 {
61 add(name);
62 }
63 }, sections);
64 }
65
66
67
68
69
70
71 public GraphPath(final List<String> seriesNames, final List<Section<S>> sections)
72 {
73 super(seriesNames);
74 this.sections = sections;
75 Length cumulativeLength = Length.ZERO;
76 for (Section<S> section : sections)
77 {
78 this.startDistances.add(cumulativeLength);
79 cumulativeLength = cumulativeLength.plus(section.getLength());
80 }
81 this.totalLength = cumulativeLength;
82 WeightedMeanAndSum<Double, Double> mean = new WeightedMeanAndSum<>();
83 for (Section<S> section : sections)
84 {
85 mean.add(section.getSpeedLimit().si, section.getLength().si);
86 }
87 this.speedLimit = Speed.instantiateSI(mean.getMean());
88 }
89
90
91
92
93
94
95 public final Length getStartDistance(final Section<S> section)
96 {
97 int index = this.sections.indexOf(section);
98 Throw.when(index == -1, IllegalArgumentException.class, "Section is not part of the path.");
99 return this.startDistances.get(index);
100 }
101
102
103
104
105
106 public final Length getTotalLength()
107 {
108 return this.totalLength;
109 }
110
111
112
113
114
115 public final Speed getSpeedLimit()
116 {
117 return this.speedLimit;
118 }
119
120
121
122
123
124
125 public Section<S> get(final int index)
126 {
127 return this.sections.get(index);
128 }
129
130
131 @Override
132 public Iterator<S> iterator()
133 {
134 return new Iterator<S>()
135 {
136
137
138 private Iterator<Section<S>> sectionIterator = getSections().iterator();
139
140
141 private Iterator<S> sourceIterator = this.sectionIterator.hasNext() ? this.sectionIterator.next().iterator() : null;
142
143
144 @Override
145 public boolean hasNext()
146 {
147 if (this.sourceIterator != null && this.sourceIterator.hasNext())
148 {
149 return true;
150 }
151 while (this.sectionIterator.hasNext())
152 {
153 Iterator<S> it = this.sectionIterator.next().iterator();
154 if (it.hasNext())
155 {
156 this.sourceIterator = it;
157 return true;
158 }
159 }
160 this.sourceIterator = null;
161 return false;
162 }
163
164
165 @Override
166 public S next()
167 {
168 Throw.when(!hasNext(), NoSuchElementException.class, "No more element left.");
169 return this.sourceIterator.next();
170 }
171 };
172 }
173
174
175 @Override
176 public Iterator<S> iterator(final int series)
177 {
178 List<S> list = new ArrayList<>();
179 for (Section<S> section : this.sections)
180 {
181 list.add(section.getSource(series));
182 }
183 return new ImmutableArrayList<>(list, Immutable.WRAP).iterator();
184 }
185
186
187
188
189
190 public ImmutableList<Section<S>> getSections()
191 {
192 return new ImmutableArrayList<>(this.sections, Immutable.WRAP);
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public static interface Section<S> extends Iterable<S>
209 {
210
211
212
213
214 Length getLength();
215
216
217
218
219
220 Speed getSpeedLimit();
221
222
223
224
225
226
227 S getSource(int series);
228 }
229
230
231 @Override
232 public String toString()
233 {
234 return "GraphPath [sections=" + this.sections + ", startDistances=" + this.startDistances + ", totalLength="
235 + this.totalLength + ", speedLimit=" + this.speedLimit + "]";
236 }
237
238
239
240
241
242
243 public static void initRecording(final Sampler<?> sampler, final GraphPath<KpiLaneDirection> path)
244 {
245 for (Section<KpiLaneDirection> section : path.getSections())
246 {
247 for (KpiLaneDirection kpiLaneDirection : section)
248 {
249 sampler.registerSpaceTimeRegion(new SpaceTimeRegion(kpiLaneDirection, Length.ZERO,
250 kpiLaneDirection.getLaneData().getLength(), Time.ZERO, Time.instantiateSI(Double.MAX_VALUE)));
251 }
252 }
253 }
254
255 }