1pub mod violation;
2pub use violation::Violation;
3
4pub mod detect;
5pub mod sparql;
6pub use detect::{detect_config, detect_nc_profiles};
7
8use std::collections::HashSet;
9
10#[derive(Debug, Default, Clone)]
11pub struct Config {
12 pub profiles: Vec<String>,
14 pub solved: bool,
16 pub not_solved: bool,
18 pub common: bool,
20 pub quality: bool,
22 pub silenced_rules: Vec<String>,
24 pub eqbd_base_voltage_ids: Option<HashSet<String>>,
26}
27
28pub mod helpers;
29mod par;
30
31pub mod bag;
36pub mod shape_source;
37pub mod shapes;
38#[cfg(feature = "sparql-shapes")]
39pub mod sparql_shapes;
40#[rustfmt::skip] pub mod nc_shapes;
42#[rustfmt::skip] pub mod nc_profiles;
44
45pub fn nc_shapes() -> &'static [shapes::ShapeDef] {
48 static R: std::sync::OnceLock<&'static [shapes::ShapeDef]> = std::sync::OnceLock::new();
49 R.get_or_init(|| shape_source::resolve("nc", nc_shapes::SHAPES))
50}
51
52pub type ProfileIndex = (
54 &'static [(&'static str, &'static str)],
55 &'static [&'static str],
56);
57
58pub fn nc_profile_index() -> ProfileIndex {
61 static R: std::sync::OnceLock<ProfileIndex> = std::sync::OnceLock::new();
62 *R.get_or_init(|| {
63 shape_source::resolve_profiles("nc", nc_profiles::PROFILE_IRIS, nc_profiles::PROFILES)
64 })
65}
66
67pub fn validate_nc_profile(
73 dataset: &cimmodel::CimDataset,
74 profile: &str,
75 cfg: &Config,
76) -> Vec<Violation> {
77 validate_nc_profile_in(dataset, &[], profile, cfg)
78}
79
80pub fn validate_nc_profile_in(
82 dataset: &cimmodel::CimDataset,
83 set: &[cimmodel::CimDataset],
84 profile: &str,
85 cfg: &Config,
86) -> Vec<Violation> {
87 let mut v = bag::validate_profile_in(dataset, set, profile, nc_shapes(), cfg);
88 if nc_profile_index().1.contains(&profile) {
91 v.extend(sparql::nc::validate_local(dataset));
92 }
93 v
94}
95
96pub fn validate_nc_merged(dataset: &cimmodel::CimDataset, cfg: &Config) -> Vec<Violation> {
100 let mut v = bag::validate_profile(dataset, MERGED_PROFILE, nc_shapes(), cfg);
101 v.extend(sparql::nc::validate_merged(dataset));
102 v
103}
104
105pub const MERGED_PROFILE: &str = "ALL";
110
111#[rustfmt::skip] pub mod cgmes_shapes;
120#[rustfmt::skip] pub mod cgmes_profiles;
122
123pub fn cgmes_shapes() -> &'static [shapes::ShapeDef] {
126 static R: std::sync::OnceLock<&'static [shapes::ShapeDef]> = std::sync::OnceLock::new();
127 R.get_or_init(|| shape_source::resolve("cgmes", cgmes_shapes::SHAPES))
128}
129
130fn cgmes_tagged(tag: &str) -> &'static [&'static shapes::ShapeDef] {
132 type Index = std::collections::HashMap<&'static str, Vec<&'static shapes::ShapeDef>>;
133 static IDX: std::sync::OnceLock<Index> = std::sync::OnceLock::new();
134 let idx = IDX.get_or_init(|| {
135 let mut idx = Index::new();
136 for shape in cgmes_shapes() {
137 for tag in shape.profiles {
138 idx.entry(*tag).or_default().push(shape);
139 }
140 }
141 idx
142 });
143 idx.get(tag).map_or(&[], Vec::as_slice)
144}
145
146fn run_cgmes(dataset: &cimmodel::CimDataset, active: &[&shapes::ShapeDef]) -> Vec<Violation> {
147 bag::validate_shapes(dataset, bag::Source::Cgmes, active)
148}
149
150fn run_cgmes_in(
151 dataset: &cimmodel::CimDataset,
152 set: &[cimmodel::CimDataset],
153 active: &[&shapes::ShapeDef],
154) -> Vec<Violation> {
155 bag::validate_shapes_in(dataset, set, bag::Source::Cgmes, active)
156}
157
158pub fn validate_header(dataset: &cimmodel::CimDataset, _cfg: &Config) -> Vec<Violation> {
162 run_cgmes(dataset, cgmes_tagged("HDR"))
163}
164
165pub fn validate_profile_shacl(
169 dataset: &cimmodel::CimDataset,
170 profile: &str,
171 _cfg: &Config,
172) -> Vec<Violation> {
173 run_cgmes(dataset, cgmes_tagged(profile))
174}
175
176pub fn validate_crossprofile_shacl(dataset: &cimmodel::CimDataset, cfg: &Config) -> Vec<Violation> {
182 let has = |p: &str| cfg.profiles.is_empty() || cfg.profiles.iter().any(|x| x == p);
183 let mut active: Vec<&shapes::ShapeDef> = cimschema_cross_profiles()
184 .iter()
185 .filter(|p| has(p))
186 .flat_map(|p| cgmes_tagged(&format!("X:{p}")).iter().copied())
187 .collect();
188 if cfg.not_solved {
189 for p in cgmes_profile_index().1.iter().filter(|p| has(p)) {
190 active.extend_from_slice(cgmes_tagged(&format!("{p}!NS")));
191 }
192 }
193 if cfg.common {
194 active.extend_from_slice(cgmes_tagged("COMMON"));
195 }
196 run_cgmes(dataset, &active)
197}
198
199pub fn cgmes_profile_index() -> ProfileIndex {
203 static R: std::sync::OnceLock<ProfileIndex> = std::sync::OnceLock::new();
204 *R.get_or_init(|| {
205 shape_source::resolve_profiles(
206 "cgmes",
207 cgmes_profiles::PROFILE_IRIS,
208 cgmes_profiles::PROFILES,
209 )
210 })
211}
212
213fn cimschema_cross_profiles() -> &'static [&'static str] {
217 &["DL", "DY", "GL", "OP", "SV", "TP"]
218}
219
220pub fn validate_profile_local(
228 dataset: &cimmodel::CimDataset,
229 profile: &str,
230 cfg: &Config,
231) -> Vec<Violation> {
232 validate_profile_local_in(dataset, &[], profile, cfg)
233}
234
235pub fn validate_profile_local_in(
244 dataset: &cimmodel::CimDataset,
245 set: &[cimmodel::CimDataset],
246 profile: &str,
247 cfg: &Config,
248) -> Vec<Violation> {
249 if !cfg.profiles.is_empty() && !cfg.profiles.iter().any(|p| p == profile) {
250 return Vec::new();
251 }
252 if !cgmes_profile_index().1.contains(&profile) && nc_profile_index().1.contains(&profile) {
257 return validate_nc_profile_in(dataset, set, profile, cfg);
258 }
259
260 let mut v = run_cgmes_in(dataset, set, cgmes_tagged(profile));
261 v.extend(sparql::validate_profile_local(dataset, profile, cfg));
262 v
263}
264
265pub fn validate_crossprofile(dataset: &cimmodel::CimDataset, cfg: &Config) -> Vec<Violation> {
270 let (sparql, mut v) = par::par_two(
271 || sparql::validate_crossprofile(dataset, cfg),
272 || validate_crossprofile_shacl(dataset, cfg),
273 );
274 v.extend(sparql);
275 let cgmes = cgmes_profile_index().1;
278 if cfg
279 .profiles
280 .iter()
281 .any(|p| !cgmes.contains(&p.as_str()) && nc_profile_index().1.contains(&p.as_str()))
282 {
283 v.extend(validate_nc_merged(dataset, cfg));
284 }
285 v
286}
287
288pub fn combined_config(
291 per_file: &[cimmodel::CimDataset],
292 profiles: Option<Vec<String>>,
293 solved: Option<bool>,
294 common: bool,
295 quality: bool,
296 silenced_rules: Vec<String>,
297) -> Config {
298 let mut cfg = Config::default();
299 for ds in per_file {
300 let c = detect_config(ds);
301 for p in c.profiles.into_iter().chain(detect_nc_profiles(ds)) {
302 if !cfg.profiles.contains(&p) {
303 cfg.profiles.push(p);
304 }
305 }
306 cfg.solved |= c.solved;
307 }
308 cfg.not_solved = !cfg.solved;
309 if let Some(p) = profiles {
310 cfg.profiles = p;
311 }
312 if let Some(s) = solved {
313 cfg.solved = s;
314 cfg.not_solved = !s;
315 }
316 cfg.common = common;
317 cfg.quality = quality;
318 cfg.silenced_rules = silenced_rules;
319 cfg
320}
321
322pub fn validate_files(per_file: Vec<cimmodel::CimDataset>, cfg: &Config) -> Vec<Violation> {
327 validate_files_traced(per_file, cfg)
328 .into_iter()
329 .map(|(_, v)| v)
330 .collect()
331}
332
333pub fn validate_files_traced(
338 per_file: Vec<cimmodel::CimDataset>,
339 cfg: &Config,
340) -> Vec<(Option<usize>, Violation)> {
341 let set = per_file.as_slice();
342 let mut violations: Vec<(Option<usize>, Violation)> = par::par_each(set, |ds| {
343 let mut v = validate_header(ds, cfg);
344 let mut profiles = detect_config(ds).profiles;
345 profiles.extend(detect_nc_profiles(ds));
346 for profile in &profiles {
347 v.extend(validate_profile_local_in(ds, set, profile, cfg));
348 }
349 v
350 })
351 .into_iter()
352 .enumerate()
353 .flat_map(|(i, found)| found.into_iter().map(move |v| (Some(i), v)))
354 .collect();
355
356 let mut merged = cimmodel::CimDataset::new();
357 for ds in per_file {
358 merged.merge(ds);
359 }
360 violations.extend(
361 validate_crossprofile(&merged, cfg)
362 .into_iter()
363 .map(|v| (None, v)),
364 );
365
366 if !cfg.silenced_rules.is_empty() {
367 let silenced: HashSet<&str> = cfg.silenced_rules.iter().map(String::as_str).collect();
368 violations.retain(|(_, v)| !silenced.contains(v.rule_id.as_str()));
369 }
370 violations
371}