-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathspeed_sections.rs
249 lines (233 loc) · 10.4 KB
/
speed_sections.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
use std::collections::HashMap;
use std::collections::HashSet;
use rangemap::RangeMap;
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::ApplicableDirections;
use editoast_schemas::infra::Direction;
use editoast_schemas::primitives::OSRDIdentified;
use editoast_schemas::primitives::ObjectRef;
use editoast_schemas::primitives::ObjectType;
pub const OBJECT_GENERATORS: [ObjectErrorGenerator<NoContext>; 2] = [
ObjectErrorGenerator::new(1, check_empty),
ObjectErrorGenerator::new(2, check_speed_section_track_ranges),
];
pub const GLOBAL_GENERATORS: [GlobalErrorGenerator<NoContext>; 1] =
[GlobalErrorGenerator::new(check_overlapping)];
/// Check if a track section has empty speed section
pub fn check_empty(speed_section: &ObjectCache, _: &InfraCache, _: &Graph) -> Vec<InfraError> {
let speed_section = speed_section.unwrap_speed_section();
if speed_section.track_ranges.is_empty() {
vec![InfraError::new_empty_object(speed_section, "track_ranges")]
} else {
vec![]
}
}
/// Retrieve invalid refs and out of range errors for speed sections
pub fn check_speed_section_track_ranges(
speed_section: &ObjectCache,
infra_cache: &InfraCache,
_: &Graph,
) -> Vec<InfraError> {
let mut infra_errors = vec![];
let speed_section = speed_section.unwrap_speed_section();
for (index, track_range) in speed_section.track_ranges.iter().enumerate() {
let track_id = &track_range.track;
if !infra_cache
.track_sections()
.contains_key::<String>(track_id)
{
let obj_ref = ObjectRef::new::<&String>(ObjectType::TrackSection, track_id);
infra_errors.push(InfraError::new_invalid_reference(
speed_section,
format!("track_ranges.{index}"),
obj_ref,
));
continue;
}
let track_cache = infra_cache
.track_sections()
.get::<String>(track_id)
.unwrap()
.unwrap_track_section();
for (pos, field) in [(track_range.begin, "begin"), (track_range.end, "end")] {
if !(0.0..=track_cache.length).contains(&pos) {
infra_errors.push(InfraError::new_out_of_range(
speed_section,
format!("track_ranges.{index}.{field}"),
pos,
[0.0, track_cache.length],
));
}
}
}
infra_errors
}
fn get_directions(directions: ApplicableDirections) -> Vec<Direction> {
match directions {
ApplicableDirections::Both => vec![Direction::StartToStop, Direction::StopToStart],
ApplicableDirections::StartToStop => vec![Direction::StartToStop],
ApplicableDirections::StopToStart => vec![Direction::StopToStart],
}
}
/// Checks that speed sections overlapping
pub fn check_overlapping(infra_cache: &InfraCache, _: &Graph) -> Vec<InfraError> {
let mut overlapping_sc = HashSet::new();
// Key: (Track, Tag, Direction)
// - Tag is None for the default speed limit and Some for the tagged ones
// Value: RangeMap<Range, SpeedSectionId>
let mut range_maps: HashMap<(String, Option<String>, Direction), RangeMap<u64, String>> =
Default::default();
// Iterate over all the speed sections insure we don't report duplicated errors
for speed_section in infra_cache.speed_sections().values() {
let speed_section = speed_section.unwrap_speed_section();
// Ignore PSL (they can overlap)
if speed_section.extensions.psl_sncf.is_some() {
continue;
}
for track_range in speed_section.track_ranges.iter() {
let range = (track_range.begin * 100.) as u64..(track_range.end * 100.) as u64;
let track_id = &track_range.track.0;
for direction in get_directions(track_range.applicable_directions) {
// Handle the default speed limit if it exists
if speed_section.speed_limit.is_some() {
let range_map = range_maps
.entry((track_id.clone(), None, direction))
.or_default();
for (_, overlap) in range_map.overlapping(&range) {
if speed_section.get_id() == overlap {
// Avoid reporting overlap with itself
continue;
}
overlapping_sc.insert((speed_section.get_id().clone(), overlap.clone()));
}
range_map.insert(range.clone(), speed_section.get_id().to_string());
}
// Handle all the tags
for tag in speed_section.speed_limit_by_tag.keys() {
let range_map = range_maps
.entry((track_id.clone(), Some(tag.0.clone()), direction))
.or_default();
for (_, overlap) in range_map.overlapping(&range) {
if speed_section.get_id() == overlap {
// Avoid reporting overlap with itself
continue;
}
overlapping_sc.insert((speed_section.get_id().clone(), overlap.clone()));
}
range_map.insert(range.clone(), speed_section.get_id().to_string());
}
}
}
}
// Map the overlapping speed sections to infra errors
overlapping_sc
.into_iter()
.map(|(sc1, sc2)| InfraError::new_overlapping_speed_sections(sc1, sc2))
.collect()
}
#[cfg(test)]
mod tests {
use super::check_speed_section_track_ranges;
use super::InfraError;
use crate::generated_data::error::speed_sections::check_overlapping;
use crate::infra_cache::tests::create_small_infra_cache;
use crate::infra_cache::tests::create_speed_section_cache;
use crate::infra_cache::Graph;
use crate::schema::Speed;
use editoast_schemas::primitives::ObjectRef;
use editoast_schemas::primitives::ObjectType;
#[test]
fn invalid_ref() {
let mut infra_cache = create_small_infra_cache();
let track_ranges_error = vec![("A", 20., 500.), ("E", 0., 500.), ("B", 0., 250.)];
let speed_section = create_speed_section_cache("SP_error", track_ranges_error);
infra_cache.add(speed_section.clone()).unwrap();
let errors = check_speed_section_track_ranges(
&speed_section.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(&speed_section, "track_ranges.1", obj_ref);
assert_eq!(infra_error, errors[0]);
}
#[test]
fn out_of_range() {
let mut infra_cache = create_small_infra_cache();
let track_ranges_error = vec![("A", 20., 530.), ("B", 0., 250.)];
let speed_section = create_speed_section_cache("SP_error", track_ranges_error);
infra_cache.add(speed_section.clone()).unwrap();
let errors = check_speed_section_track_ranges(
&speed_section.clone().into(),
&infra_cache,
&Graph::load(&infra_cache),
);
assert_eq!(1, errors.len());
let infra_error =
InfraError::new_out_of_range(&speed_section, "track_ranges.0.end", 530., [0.0, 500.]);
assert_eq!(infra_error, errors[0]);
}
#[test]
fn overlapping_default() {
let mut infra_cache = create_small_infra_cache();
let track_ranges_1 = vec![("A", 20., 220.)];
let mut speed_section_1 = create_speed_section_cache("SP_error_1", track_ranges_1);
speed_section_1.speed_limit = Some(Speed(42.));
infra_cache.add(speed_section_1).unwrap();
let track_ranges_2 = vec![("A", 100., 150.), ("A", 200., 480.)];
let mut speed_section_2 = create_speed_section_cache("SP_error_2", track_ranges_2);
speed_section_2.speed_limit = Some(Speed(100.));
infra_cache.add(speed_section_2).unwrap();
let errors = check_overlapping(&infra_cache, &Graph::load(&infra_cache));
assert_eq!(1, errors.len());
let infra_error = InfraError::new_overlapping_speed_sections("SP_error_1", "SP_error_2");
assert_eq!(infra_error, errors[0]);
}
#[test]
fn overlapping_tags() {
let mut infra_cache = create_small_infra_cache();
let track_ranges_1 = vec![("A", 20., 220.)];
let mut speed_section_1 = create_speed_section_cache("SP_error_1", track_ranges_1);
speed_section_1
.speed_limit_by_tag
.insert("my_tag".into(), Speed(42.));
infra_cache.add(speed_section_1).unwrap();
let track_ranges_2 = vec![("A", 100., 150.), ("A", 200., 480.)];
let mut speed_section_2 = create_speed_section_cache("SP_error_2", track_ranges_2);
speed_section_2
.speed_limit_by_tag
.insert("my_tag".into(), Speed(100.));
infra_cache.add(speed_section_2).unwrap();
let errors = check_overlapping(&infra_cache, &Graph::load(&infra_cache));
assert_eq!(1, errors.len());
let infra_error = InfraError::new_overlapping_speed_sections("SP_error_1", "SP_error_2");
assert_eq!(infra_error, errors[0]);
}
#[test]
fn overlapping_pass() {
let mut infra_cache = create_small_infra_cache();
let track_ranges_1 = vec![("A", 20., 220.)];
let mut speed_section_1 = create_speed_section_cache("SP_error_1", track_ranges_1);
speed_section_1.speed_limit = Some(Speed(42.));
speed_section_1
.speed_limit_by_tag
.insert("my_tag_2".into(), Speed(42.));
infra_cache.add(speed_section_1).unwrap();
let track_ranges_2 = vec![("A", 100., 150.), ("A", 200., 480.)];
let mut speed_section_2 = create_speed_section_cache("SP_error_2", track_ranges_2);
speed_section_2
.speed_limit_by_tag
.insert("my_tag".into(), Speed(100.));
infra_cache.add(speed_section_2).unwrap();
let errors = check_overlapping(&infra_cache, &Graph::load(&infra_cache));
assert_eq!(0, errors.len());
}
}