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#[rustfmt::skip] pub mod nc_shapes;
40#[rustfmt::skip] pub mod nc_profiles;
42
43pub fn nc_shapes() -> &'static [shapes::ShapeDef] {
46 static R: std::sync::OnceLock<&'static [shapes::ShapeDef]> = std::sync::OnceLock::new();
47 R.get_or_init(|| shape_source::resolve("nc", nc_shapes::SHAPES))
48}
49
50pub type ProfileIndex = (
52 &'static [(&'static str, &'static str)],
53 &'static [&'static str],
54);
55
56pub fn nc_profile_index() -> ProfileIndex {
59 static R: std::sync::OnceLock<ProfileIndex> = std::sync::OnceLock::new();
60 *R.get_or_init(|| {
61 shape_source::resolve_profiles("nc", nc_profiles::PROFILE_IRIS, nc_profiles::PROFILES)
62 })
63}
64
65pub fn validate_nc_profile(
71 dataset: &cimmodel::CimDataset,
72 profile: &str,
73 cfg: &Config,
74) -> Vec<Violation> {
75 validate_nc_profile_in(dataset, &[], profile, cfg)
76}
77
78pub fn validate_nc_profile_in(
80 dataset: &cimmodel::CimDataset,
81 set: &[cimmodel::CimDataset],
82 profile: &str,
83 cfg: &Config,
84) -> Vec<Violation> {
85 let mut v = bag::validate_profile_in(dataset, set, profile, nc_shapes(), cfg);
86 if nc_profile_index().1.contains(&profile) {
89 v.extend(sparql::nc::validate_local(dataset));
90 }
91 v
92}
93
94pub fn validate_nc_merged(dataset: &cimmodel::CimDataset, cfg: &Config) -> Vec<Violation> {
98 let mut v = bag::validate_profile(dataset, MERGED_PROFILE, nc_shapes(), cfg);
99 v.extend(sparql::nc::validate_merged(dataset));
100 v
101}
102
103pub const MERGED_PROFILE: &str = "ALL";
108
109#[rustfmt::skip] pub mod cgmes_shapes;
118#[rustfmt::skip] pub mod cgmes_profiles;
120
121pub fn cgmes_shapes() -> &'static [shapes::ShapeDef] {
124 static R: std::sync::OnceLock<&'static [shapes::ShapeDef]> = std::sync::OnceLock::new();
125 R.get_or_init(|| shape_source::resolve("cgmes", cgmes_shapes::SHAPES))
126}
127
128fn cgmes_tagged(tag: &str) -> &'static [&'static shapes::ShapeDef] {
130 type Index = std::collections::HashMap<&'static str, Vec<&'static shapes::ShapeDef>>;
131 static IDX: std::sync::OnceLock<Index> = std::sync::OnceLock::new();
132 let idx = IDX.get_or_init(|| {
133 let mut idx = Index::new();
134 for shape in cgmes_shapes() {
135 for tag in shape.profiles {
136 idx.entry(*tag).or_default().push(shape);
137 }
138 }
139 idx
140 });
141 idx.get(tag).map_or(&[], Vec::as_slice)
142}
143
144fn run_cgmes(dataset: &cimmodel::CimDataset, active: &[&shapes::ShapeDef]) -> Vec<Violation> {
145 bag::validate_shapes(dataset, bag::Source::Cgmes, active)
146}
147
148fn run_cgmes_in(
149 dataset: &cimmodel::CimDataset,
150 set: &[cimmodel::CimDataset],
151 active: &[&shapes::ShapeDef],
152) -> Vec<Violation> {
153 bag::validate_shapes_in(dataset, set, bag::Source::Cgmes, active)
154}
155
156pub fn validate_header(dataset: &cimmodel::CimDataset, _cfg: &Config) -> Vec<Violation> {
160 run_cgmes(dataset, cgmes_tagged("HDR"))
161}
162
163pub fn validate_profile_shacl(
167 dataset: &cimmodel::CimDataset,
168 profile: &str,
169 _cfg: &Config,
170) -> Vec<Violation> {
171 run_cgmes(dataset, cgmes_tagged(profile))
172}
173
174pub fn validate_crossprofile_shacl(dataset: &cimmodel::CimDataset, cfg: &Config) -> Vec<Violation> {
180 let has = |p: &str| cfg.profiles.is_empty() || cfg.profiles.iter().any(|x| x == p);
181 let mut active: Vec<&shapes::ShapeDef> = cimschema_cross_profiles()
182 .iter()
183 .filter(|p| has(p))
184 .flat_map(|p| cgmes_tagged(&format!("X:{p}")).iter().copied())
185 .collect();
186 if cfg.not_solved {
187 for p in cgmes_profile_index().1.iter().filter(|p| has(p)) {
188 active.extend_from_slice(cgmes_tagged(&format!("{p}!NS")));
189 }
190 }
191 if cfg.common {
192 active.extend_from_slice(cgmes_tagged("COMMON"));
193 }
194 run_cgmes(dataset, &active)
195}
196
197pub fn cgmes_profile_index() -> ProfileIndex {
201 static R: std::sync::OnceLock<ProfileIndex> = std::sync::OnceLock::new();
202 *R.get_or_init(|| {
203 shape_source::resolve_profiles(
204 "cgmes",
205 cgmes_profiles::PROFILE_IRIS,
206 cgmes_profiles::PROFILES,
207 )
208 })
209}
210
211fn cimschema_cross_profiles() -> &'static [&'static str] {
215 &["DL", "DY", "GL", "OP", "SV", "TP"]
216}
217
218pub fn validate_profile_local(
226 dataset: &cimmodel::CimDataset,
227 profile: &str,
228 cfg: &Config,
229) -> Vec<Violation> {
230 validate_profile_local_in(dataset, &[], profile, cfg)
231}
232
233pub fn validate_profile_local_in(
242 dataset: &cimmodel::CimDataset,
243 set: &[cimmodel::CimDataset],
244 profile: &str,
245 cfg: &Config,
246) -> Vec<Violation> {
247 if !cfg.profiles.is_empty() && !cfg.profiles.iter().any(|p| p == profile) {
248 return Vec::new();
249 }
250 if !cgmes_profile_index().1.contains(&profile) && nc_profile_index().1.contains(&profile) {
255 return validate_nc_profile_in(dataset, set, profile, cfg);
256 }
257
258 let mut v = run_cgmes_in(dataset, set, cgmes_tagged(profile));
259 v.extend(sparql::validate_profile_local(dataset, profile, cfg));
260 v
261}
262
263pub fn validate_crossprofile(dataset: &cimmodel::CimDataset, cfg: &Config) -> Vec<Violation> {
268 let (sparql, mut v) = par::par_two(
269 || sparql::validate_crossprofile(dataset, cfg),
270 || validate_crossprofile_shacl(dataset, cfg),
271 );
272 v.extend(sparql);
273 let cgmes = cgmes_profile_index().1;
276 if cfg
277 .profiles
278 .iter()
279 .any(|p| !cgmes.contains(&p.as_str()) && nc_profile_index().1.contains(&p.as_str()))
280 {
281 v.extend(validate_nc_merged(dataset, cfg));
282 }
283 v
284}
285
286pub fn combined_config(
289 per_file: &[cimmodel::CimDataset],
290 profiles: Option<Vec<String>>,
291 solved: Option<bool>,
292 common: bool,
293 quality: bool,
294 silenced_rules: Vec<String>,
295) -> Config {
296 let mut cfg = Config::default();
297 for ds in per_file {
298 let c = detect_config(ds);
299 for p in c.profiles.into_iter().chain(detect_nc_profiles(ds)) {
300 if !cfg.profiles.contains(&p) {
301 cfg.profiles.push(p);
302 }
303 }
304 cfg.solved |= c.solved;
305 }
306 cfg.not_solved = !cfg.solved;
307 if let Some(p) = profiles {
308 cfg.profiles = p;
309 }
310 if let Some(s) = solved {
311 cfg.solved = s;
312 cfg.not_solved = !s;
313 }
314 cfg.common = common;
315 cfg.quality = quality;
316 cfg.silenced_rules = silenced_rules;
317 cfg
318}
319
320pub fn validate_files(per_file: Vec<cimmodel::CimDataset>, cfg: &Config) -> Vec<Violation> {
325 validate_files_traced(per_file, cfg)
326 .into_iter()
327 .map(|(_, v)| v)
328 .collect()
329}
330
331pub fn validate_files_traced(
336 per_file: Vec<cimmodel::CimDataset>,
337 cfg: &Config,
338) -> Vec<(Option<usize>, Violation)> {
339 let set = per_file.as_slice();
340 let mut violations: Vec<(Option<usize>, Violation)> = par::par_each(set, |ds| {
341 let mut v = validate_header(ds, cfg);
342 let mut profiles = detect_config(ds).profiles;
343 profiles.extend(detect_nc_profiles(ds));
344 for profile in &profiles {
345 v.extend(validate_profile_local_in(ds, set, profile, cfg));
346 }
347 v
348 })
349 .into_iter()
350 .enumerate()
351 .flat_map(|(i, found)| found.into_iter().map(move |v| (Some(i), v)))
352 .collect();
353
354 let mut merged = cimmodel::CimDataset::new();
355 for ds in per_file {
356 merged.merge(ds);
357 }
358 violations.extend(
359 validate_crossprofile(&merged, cfg)
360 .into_iter()
361 .map(|v| (None, v)),
362 );
363
364 if !cfg.silenced_rules.is_empty() {
365 let silenced: HashSet<&str> = cfg.silenced_rules.iter().map(String::as_str).collect();
366 violations.retain(|(_, v)| !silenced.contains(v.rule_id.as_str()));
367 }
368 violations
369}