Skip to main content

cimvalidation/
detect.rs

1use crate::Config;
2use cimmodel::CimDataset;
3
4/// The short code for a CGMES profile IRI, from `CGMES/PROF` via
5/// [`crate::cgmes_profile_index`].
6fn uri_to_short_name(uri: &str) -> Option<&'static str> {
7    crate::cgmes_profile_index()
8        .0
9        .iter()
10        .find(|(iri, _)| *iri == uri)
11        .map(|(_, code)| *code)
12}
13
14fn collect_profiles_from_type(
15    dataset: &CimDataset,
16    type_name: &str,
17    seen: &mut std::collections::HashSet<&'static str>,
18) {
19    for mrid in dataset.by_type.get(type_name).into_iter().flatten() {
20        let entry = match dataset.entries.get(mrid) {
21            Some(e) => e,
22            None => continue,
23        };
24        let Some(model) = crate::sparql::Fields::of_class(entry, type_name) else {
25            continue;
26        };
27        for p in model.texts("Model.profile") {
28            let p = p.trim();
29            if !p.is_empty()
30                && let Some(short) = uri_to_short_name(p)
31            {
32                seen.insert(short);
33            }
34        }
35    }
36}
37
38/// Inspects dataset model headers and returns a Config with profiles, solved, and not_solved populated.
39///
40/// A CGMES profile is declared by an `md:FullModel`/`md:DifferenceModel`'s
41/// `Model.profile`, or by a DCAT header's `dcterms:conformsTo` naming the same
42/// profile IRI — the header NC uses, which CGMES datasets may carry instead.
43/// A `conformsTo` naming a vocabulary (`http://iec.ch/TC57/CIM100#`) declares
44/// no profile.
45pub fn detect_config(dataset: &CimDataset) -> Config {
46    let mut seen: std::collections::HashSet<&'static str> = std::collections::HashSet::new();
47    collect_profiles_from_type(dataset, "FullModel", &mut seen);
48    collect_profiles_from_type(dataset, "DifferenceModel", &mut seen);
49    for iri in conforms_to(dataset) {
50        if let Some(short) = uri_to_short_name(iri.trim()) {
51            seen.insert(short);
52        }
53    }
54
55    let mut profiles: Vec<String> = seen.iter().map(|s| s.to_string()).collect();
56    profiles.sort();
57
58    let is_solved = seen.contains("SV");
59    Config {
60        profiles,
61        solved: is_solved,
62        not_solved: !is_solved,
63        ..Config::default()
64    }
65}
66
67// ── NC / property-bag families ─────────────────────────────────────────────
68
69/// Profile codes an NC dataset declares conformance to.
70///
71/// CGMES announces its profiles in an `md:FullModel` header whose
72/// `Model.profile` values are matched against `CGMES/PROF`'s index above. NC
73/// uses a DCAT header instead: a `dcat:Dataset` with `dcterms:conformsTo`
74/// naming profile IRIs, which `NCP/PROF` maps to short codes.
75///
76/// The field key is the bare `conformsTo`, because the decoder keys fields by
77/// the XML local name and the predicate is written `<dcterms:conformsTo>`.
78pub fn detect_nc_profiles(dataset: &CimDataset) -> Vec<String> {
79    let mut seen: Vec<String> = Vec::new();
80    for iri in conforms_to(dataset) {
81        if let Some(code) = nc_profile_code(iri)
82            && !seen.iter().any(|s| s == code)
83        {
84            seen.push(code.to_string());
85        }
86    }
87    seen.sort();
88    seen
89}
90
91/// Every `dcterms:conformsTo` of the dataset's DCAT headers (`dcat:Dataset`,
92/// `dm:DifferenceSet`, which decode as `nc:` classes in either family).
93/// Written as `rdf:resource` in practice, but a plain literal is legal too, so
94/// both are read.
95fn conforms_to(dataset: &CimDataset) -> impl Iterator<Item = &str> {
96    ["nc:Dataset", "nc:DifferenceSet"]
97        .into_iter()
98        .flat_map(|t| dataset.by_type.get(t).into_iter().flatten())
99        .filter_map(|mrid| dataset.entries.get(mrid))
100        .flat_map(|el| {
101            el.get_refs("conformsTo")
102                .iter()
103                .map(String::as_str)
104                .chain(el.get_str("conformsTo"))
105        })
106}
107
108/// The short code for a profile IRI, if it names one.
109///
110/// A dataset may declare the base IRI or a version IRI; the descriptors list
111/// both. An IRI the descriptors do not know is not an NC profile.
112pub fn nc_profile_code(iri: &str) -> Option<&'static str> {
113    let iri = iri.trim();
114    crate::nc_profile_index()
115        .0
116        .iter()
117        .find(|(known, _)| *known == iri)
118        .map(|(_, code)| *code)
119}