-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbuffer_stops.rs
294 lines (270 loc) · 11.2 KB
/
buffer_stops.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use super::GlobalErrorGenerator;
use super::NoContext;
use crate::generated_data::error::ObjectErrorGenerator;
use crate::infra_cache::Graph;
use crate::infra_cache::InfraCache;
use crate::infra_cache::ObjectCache;
use crate::schema::InfraError;
use editoast_schemas::infra::Endpoint;
use editoast_schemas::infra::TrackEndpoint;
use editoast_schemas::primitives::ObjectRef;
use editoast_schemas::primitives::ObjectType;
// TODO: Use a macro instead to force order and priority continuity
// Example: `static_priority_array![[check_invalid_ref], [check_out_of_range]]`
pub const OBJECT_GENERATORS: [ObjectErrorGenerator<NoContext>; 3] = [
ObjectErrorGenerator::new(1, check_invalid_ref),
ObjectErrorGenerator::new(2, check_out_of_range),
ObjectErrorGenerator::new(3, check_odd_location),
];
pub const GLOBAL_GENERATORS: [GlobalErrorGenerator<NoContext>; 1] =
[GlobalErrorGenerator::new(check_missing)];
/// Retrieve invalid refs errors for buffer stops
fn check_invalid_ref(
buffer_stop: &ObjectCache,
infra_cache: &InfraCache,
_: &Graph,
) -> Vec<InfraError> {
let buffer_stop = buffer_stop.unwrap_buffer_stop();
if !infra_cache
.track_sections()
.contains_key(&buffer_stop.track)
{
let obj_ref = ObjectRef::new(ObjectType::TrackSection, buffer_stop.track.clone());
vec![InfraError::new_invalid_reference(
buffer_stop,
"track",
obj_ref,
)]
} else {
vec![]
}
}
/// Retrieve out of range position errors for buffer stops
fn check_out_of_range(
buffer_stop: &ObjectCache,
infra_cache: &InfraCache,
_: &Graph,
) -> Vec<InfraError> {
let buffer_stop = buffer_stop.unwrap_buffer_stop();
let track_cache = infra_cache
.track_sections()
.get(&buffer_stop.track)
.unwrap()
.unwrap_track_section();
if !(0.0..=track_cache.length).contains(&buffer_stop.position) {
vec![InfraError::new_out_of_range(
buffer_stop,
"position",
buffer_stop.position,
[0.0, track_cache.length],
)]
} else {
vec![]
}
}
/// Check the location of the buffer stop
/// A buffer stop must protect the end of the infrastructures.
/// We trigger a warning when:
/// - It is not placed on track at the end of the infrastructure, it is an error.
/// - Another buffer stop protect the track endpoint (the buffer stop is useless and should be removed).
fn check_odd_location(
buffer_stop: &ObjectCache,
infra_cache: &InfraCache,
graph: &Graph,
) -> Vec<InfraError> {
let buffer_stop = buffer_stop.unwrap_buffer_stop();
let track_id = &buffer_stop.track;
let is_linked_start = graph.has_neighbour(&TrackEndpoint::new(track_id, Endpoint::Begin));
let is_linked_end = graph.has_neighbour(&TrackEndpoint::new(track_id, Endpoint::End));
// The track is not at the end of the infra
if is_linked_end && is_linked_start {
return vec![InfraError::new_odd_buffer_stop_location(buffer_stop)];
}
let buffer_stops = infra_cache.get_track_refs_type(track_id, ObjectType::BufferStop);
// The number of buffer stops matches the number of ends of the track
if buffer_stops.len() == 1 || (buffer_stops.len() == 2 && !is_linked_end && !is_linked_start) {
return vec![];
}
// Retrieve the first and last buffer stops on the track
let mut buffer_stops: Vec<_> = buffer_stops
.iter()
.map(|bs| {
infra_cache
.buffer_stops()
.get(&bs.obj_id)
.unwrap()
.unwrap_buffer_stop()
})
.collect();
buffer_stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
let first_bs = buffer_stops[0];
let last_bs = buffer_stops[buffer_stops.len() - 1];
// Check if the buffer stop is not protecting the end of the infra
if (is_linked_start || first_bs.obj_id != buffer_stop.obj_id)
&& (is_linked_end || last_bs.obj_id != buffer_stop.obj_id)
{
return vec![InfraError::new_odd_buffer_stop_location(buffer_stop)];
}
vec![]
}
/// Check if buffer stops are missing in the track section
fn check_missing(infra_cache: &InfraCache, graph: &Graph) -> Vec<InfraError> {
let mut infra_errors = vec![];
for track in infra_cache.track_sections().values() {
let track = track.unwrap_track_section();
let is_linked_start = graph.has_neighbour(&track.get_begin());
let is_linked_end = graph.has_neighbour(&track.get_end());
if is_linked_start && is_linked_end {
continue;
}
let buffer_stops = infra_cache.get_track_refs_type(&track.obj_id, ObjectType::BufferStop);
if buffer_stops.len() >= 2 {
continue;
} else if buffer_stops.is_empty() {
// Missing buffer stops where the track is not linked
if !is_linked_end {
infra_errors.push(InfraError::new_missing_buffer_stop(track, Endpoint::End));
}
if !is_linked_start {
infra_errors.push(InfraError::new_missing_buffer_stop(track, Endpoint::Begin));
}
continue;
} else if is_linked_end || is_linked_start {
// Only one buffer stop and the track is linked to another track
continue;
}
// Only one buffer stop and the track is not linked to another track
// We have to determine if the missing buffer stop should be at the begin or the end
let bs_id = &buffer_stops[0].obj_id;
let buffer_stop = infra_cache.buffer_stops().get(bs_id).unwrap();
let buffer_stop = buffer_stop.unwrap_buffer_stop();
if buffer_stop.position < track.length / 2. {
infra_errors.push(InfraError::new_missing_buffer_stop(track, Endpoint::End));
} else {
infra_errors.push(InfraError::new_missing_buffer_stop(track, Endpoint::Begin));
}
}
infra_errors
}
#[cfg(test)]
pub mod tests {
use rstest::rstest;
use super::check_invalid_ref;
use super::check_out_of_range;
use super::InfraError;
use crate::generated_data::error::buffer_stops::check_missing;
use crate::generated_data::error::buffer_stops::check_odd_location;
use crate::infra_cache::tests::create_buffer_stop_cache;
use crate::infra_cache::tests::create_small_infra_cache;
use crate::infra_cache::tests::create_track_section_cache;
use crate::infra_cache::Graph;
use editoast_schemas::infra::Endpoint;
use editoast_schemas::primitives::ObjectRef;
use editoast_schemas::primitives::ObjectType;
#[test]
fn invalid_ref() {
let mut infra_cache = create_small_infra_cache();
let bf = create_buffer_stop_cache("BF_error", "E", 250.);
infra_cache.add(bf.clone()).unwrap();
let errors =
check_invalid_ref(&bf.clone().into(), &infra_cache, &Graph::load(&infra_cache));
assert_eq!(1, errors.len());
let obj_ref = ObjectRef::new(ObjectType::TrackSection, "E");
let infra_error = InfraError::new_invalid_reference(&bf, "track", obj_ref);
assert_eq!(infra_error, errors[0]);
}
#[test]
fn out_of_range() {
let mut infra_cache = create_small_infra_cache();
let bf = create_buffer_stop_cache("BF_error", "A", 530.);
infra_cache.add(bf.clone()).unwrap();
let errors =
check_out_of_range(&bf.clone().into(), &infra_cache, &Graph::load(&infra_cache));
assert_eq!(1, errors.len());
let infra_error = InfraError::new_out_of_range(&bf, "position", 530., [0.0, 500.]);
assert_eq!(infra_error, errors[0]);
}
#[test]
fn simple_missing_buffer_stop() {
let mut infra_cache = create_small_infra_cache();
let obj_ref = ObjectRef::new(ObjectType::BufferStop, "BF1");
infra_cache.apply_delete(&obj_ref).unwrap();
let graph = Graph::load(&infra_cache);
let errors = check_missing(&infra_cache, &graph);
assert_eq!(1, errors.len());
let infra_error = InfraError::new_missing_buffer_stop(
infra_cache.track_sections().get("A").unwrap(),
Endpoint::Begin,
);
assert_eq!(infra_error, errors[0]);
}
#[rstest]
#[case(25.)]
#[case(175.)]
fn complex_missing_buffer_stop(#[case] pos: f64) {
let mut infra_cache = create_small_infra_cache();
let track = create_track_section_cache("test", 200.);
infra_cache.add(track.clone()).unwrap();
let bs = create_buffer_stop_cache("bs_test", &track.obj_id, pos);
infra_cache.add(bs).unwrap();
let graph = Graph::load(&infra_cache);
let errors = check_missing(&infra_cache, &graph);
assert_eq!(1, errors.len());
let missing_endpoint = if pos < 100. {
Endpoint::End
} else {
Endpoint::Begin
};
let error = InfraError::new_missing_buffer_stop(&track, missing_endpoint);
assert_eq!(error, errors[0]);
}
#[test]
fn missing_two_buffer_stop() {
let mut infra_cache = create_small_infra_cache();
let track = create_track_section_cache("test", 200.);
infra_cache.add(track.clone()).unwrap();
let graph = Graph::load(&infra_cache);
let errors = check_missing(&infra_cache, &graph);
assert_eq!(2, errors.len());
let error = InfraError::new_missing_buffer_stop(&track, Endpoint::End);
assert_eq!(error, errors[0]);
let error = InfraError::new_missing_buffer_stop(&track, Endpoint::Begin);
assert_eq!(error, errors[1]);
}
#[rstest]
#[case("B", 50.)]
#[case("A", 30.)]
#[case("C", 450.)]
fn odd_location(#[case] track: &str, #[case] pos: f64) {
let mut infra_cache = create_small_infra_cache();
let bs = create_buffer_stop_cache("bs_test", track, pos);
infra_cache.add(bs.clone()).unwrap();
let graph = Graph::load(&infra_cache);
let errors = check_odd_location(&bs.clone().into(), &infra_cache, &graph);
assert_eq!(1, errors.len());
let error = InfraError::new_odd_buffer_stop_location(&bs);
assert_eq!(error, errors[0]);
}
#[test]
fn odd_location_complex() {
let mut infra_cache = create_small_infra_cache();
let track = create_track_section_cache("track_test", 200.);
infra_cache.add(track).unwrap();
let valid_begin = create_buffer_stop_cache("valid_begin", "track_test", 10.);
infra_cache.add(valid_begin.clone()).unwrap();
let valid_end = create_buffer_stop_cache("valid_end", "track_test", 190.);
infra_cache.add(valid_end.clone()).unwrap();
let bs = create_buffer_stop_cache("invalid", "track_test", 100.);
infra_cache.add(bs.clone()).unwrap();
let graph = Graph::load(&infra_cache);
// Verify that the valid buffer stops doesn't generate any errors
for valid_bs in [valid_begin, valid_end] {
let errors = check_odd_location(&valid_bs.into(), &infra_cache, &graph);
assert_eq!(0, errors.len());
}
let errors = check_odd_location(&bs.clone().into(), &infra_cache, &graph);
assert_eq!(1, errors.len());
let error = InfraError::new_odd_buffer_stop_location(&bs);
assert_eq!(error, errors[0]);
}
}