cim-kit 0.6.0

IEC CIM/CGMES for power systems: CIM/XML I/O that reproduces the file set, profile-aware validation, typed RDF export
Documentation
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! ENTSO-E's quality-check corpus: real exports, deliberately broken.
//!
//! The conformity models are correct by construction — they are what a *conforming*
//! producer emits, and every reader defect this crate has had lived in the complement of
//! that set. QoCDC is the complement: 100 model sets built for ENTSO-E's Quality of CGMES
//! Datasets Check, from real TSO exports (ELIA, 50Hertz, TenneT), each with a conformant
//! variant and non-conformant ones designed to make a checker say something.
//!
//! It was fetched by `cargo xtask fetch-specs` and read by nothing until this file existed,
//! which is the shape of a gate that looks like coverage. Pointing the reader at it found
//! two defects immediately, both about *sets* rather than documents: a single unreadable
//! file aborted the whole load with a message that did not name it, and vintage detection
//! gave up on the first file instead of asking the next one.
//!
//! What is asserted is deliberately about robustness rather than about findings. The
//! validation rules these models exercise are ENTSO-E's, not this crate's — running them is
//! a SHACL engine's job — so what matters here is that every set is *read*, that the two
//! deliberately unreadable files are named rather than fatal, and that nothing panics.

#![cfg(all(feature = "cgmes2", feature = "cgmes3", feature = "zip"))]

mod common;

use std::path::{Path, PathBuf};

use cim_kit::prelude::*;
use cim_kit::{Rule, Severity};

/// The outer archives `fetch-specs` leaves in place, smallest first.
///
/// TC1 and TC2 are 40 MiB together, cover every test case type, and run in about eight
/// seconds — a gate that takes a minute is a gate people turn off. TC3 and TC4 are 113 MiB
/// and 463 MiB of more of the same, which is worth running somewhere slower than every
/// `cargo test`: set `CIM_QOCDC_ALL=1` for all four, which CONTRIBUTING.md lists as a by-hand check.
const QUICK: [&str; 2] = ["TC1.zip", "TC2.zip"];
const ALL: [&str; 4] = ["TC1.zip", "TC2.zip", "TC3.zip", "TC4.zip"];

fn archives() -> &'static [&'static str] {
    if std::env::var_os("CIM_QOCDC_ALL").is_some() {
        &ALL
    } else {
        &QUICK
    }
}

fn corpus() -> Option<PathBuf> {
    let dir = common::references()?
        .join("test-models/qocdc-3.2.1")
        .join("QoCDC v3.2.1 test models");
    dir.is_dir().then_some(dir)
}

/// Every directory that directly holds instance files or per-file archives.
fn model_sets(root: &Path) -> Vec<PathBuf> {
    fn walk(d: &Path, out: &mut Vec<PathBuf>) {
        let Ok(rd) = std::fs::read_dir(d) else { return };
        let mut has_input = false;
        for e in rd.flatten() {
            let p = e.path();
            if p.is_dir() {
                walk(&p, out);
            } else if p
                .extension()
                .is_some_and(|x| x.eq_ignore_ascii_case("xml") || x.eq_ignore_ascii_case("zip"))
            {
                has_input = true;
            }
        }
        if has_input {
            out.push(d.to_path_buf());
        }
    }
    let mut out = Vec::new();
    walk(root, &mut out);
    out.sort();
    out
}

#[test]
fn every_quality_check_model_set_is_read_and_none_of_them_is_fatal() {
    let Some(corpus) = corpus() else {
        eprintln!("skipping: QoCDC corpus not present (cargo xtask fetch-specs)");
        return;
    };

    let work = std::env::temp_dir().join(format!("cim-qocdc-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&work);
    std::fs::create_dir_all(&work).unwrap();
    let archives = archives();
    for name in archives {
        let path = corpus.join(name);
        let file = std::fs::File::open(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
        zip::ZipArchive::new(std::io::BufReader::new(file))
            .unwrap()
            .extract(&work)
            .unwrap();
    }

    let sets = model_sets(&work);
    assert!(sets.len() > 80, "only {} model sets extracted", sets.len());

    let mut undetected = Vec::new();
    let mut unreadable: Vec<String> = Vec::new();
    let mut repeats: Vec<String> = Vec::new();
    let mut duplicates: Vec<String> = Vec::new();
    let mut objects = 0usize;

    for dir in &sets {
        let files = cim_kit::instance_files(dir);
        // Detection over the whole set, not its first file: one of these sets begins with
        // an archive that is deliberately not XML.
        let Some(schema) = cim_kit::load::detect_vintage(&files) else {
            undetected.push(dir.strip_prefix(&work).unwrap().display().to_string());
            continue;
        };
        let mut ds = Dataset::new(schema);
        let report = ds
            .load_files(&files, &ReadOptions::lenient())
            .unwrap_or_else(|e| panic!("{} was fatal: {e}", dir.display()));
        objects += ds.len();

        let set = dir.strip_prefix(&work).unwrap().display().to_string();
        // The set QoCDC breaks identifiers in on purpose: one `rdf:ID` defined twice, and
        // one shared by unrelated classes. Its value findings follow from that.
        let broken_ids = set.ends_with("NonConform_ID");
        for d in report.report.iter() {
            if broken_ids && d.rule == Rule::DuplicateMrid {
                duplicates.push(d.object.as_ref().map(|m| m.canonical()).unwrap_or_default());
            }
            if !broken_ids && matches!(d.rule, Rule::ConflictingValue | Rule::CardinalityExceeded) {
                repeats.push(format!(
                    "{}: {d}",
                    dir.strip_prefix(&work).unwrap().display()
                ));
            }
            if d.rule == Rule::UnreadableFile {
                assert_eq!(d.severity, Severity::Error, "{d}");
                let file = d.source.clone().unwrap_or_default();
                assert!(!file.is_empty(), "an unreadable file was not named: {d}");
                unreadable.push(format!(
                    "{}/{file}",
                    dir.strip_prefix(&work).unwrap().display()
                ));
            }
        }

        // Whatever it read, it must still be able to say what it holds and write it back.
        let _ = ds.validate();
    }
    std::fs::remove_dir_all(&work).ok();

    assert!(
        undetected.is_empty(),
        "no vintage detected for: {undetected:?}"
    );
    // ~41,900 across the two archives; a floor rather than the figure, since the
    // corpus is versioned and this is a "did it actually read anything" check.
    assert!(objects > 35_000, "only {objects} objects read");

    // Outside the broken-identifier set, no quality-check file gives one object a value
    // twice; the reader finding one would be a false positive or a new defect.
    assert!(repeats.is_empty(), "{repeats:#?}");
    if archives.contains(&"TC3.zip") {
        // Once per copy: the set holds the same file loose and zipped.
        duplicates.sort();
        duplicates.dedup();
        assert_eq!(
            duplicates,
            [
                "1bab071e-27a7-4db3-009f-caebc0c872e5",
                "946e6c6f-5e80-713d-00d8-7378a66f3a80"
            ],
            "the duplicated identifiers of TC3_T1_NonConform_ID"
        );
    }

    // Pinned by name, not by count: the files QoCDC breaks on purpose — an archive holding
    // the text `nonxmlfile`, a bare `&` in a header (loose and zipped), and an unclosed
    // `<cim:ConformLoadGroup>`. Archive entries are named by entry. A new one is a
    // regression; one disappearing means the check stopped looking.
    unreadable.sort();
    if archives.len() > QUICK.len() {
        // The larger archives bring their own broken files; what is pinned is the set the
        // quick run sees, so the full run asserts the weaker thing — that every one of
        // them was *named*, which the loop above already checked, and that the count did
        // not collapse to zero.
        assert!(
            unreadable.len() >= 3,
            "the deliberately broken files went missing: {unreadable:?}"
        );
        return;
    }
    assert_eq!(
        unreadable,
        [
            "TC1/TC1_T11_NonConform_L1/Combinations/Combination_6/20210125T1900Z_1D_ELIA_EQ_001.xml",
            "TC1/TC1_T2_NonConform/Combination1/20210125T1900Z_1D_ELIA_EQ_001.xml",
            "TC1/TC1_T4_NonConform/Combination_ModelDescription/20210125T1900Z_1D_ttn_EQ_001.xml",
            "TC1/TC1_T4_NonConform/Combination_ModelDescription/20210125T1900Z_1D_ttn_EQ_001.xml",
        ],
        "the set of deliberately unreadable files changed"
    );
}

/// Every file of the quality corpus that reads cleanly is re-exported as the document it
/// was.
///
/// The conformity models are what a *conforming* producer emits, and they turn out not to
/// exercise two things real exports do constantly. This test is where both were found
/// (D55, D56, D57):
///
/// * a boundary set introduces a node and says nothing else about it —
///   `<cim:ConnectivityNode rdf:ID="_x"/>` with no property children — and selecting what
///   to write by *content* dropped 512 of 591 of them from the file that introduced them,
///   while the other file kept referring to them with `rdf:about`;
/// * a real header under-declares its profiles, naming only `EquipmentCore/3/1` while
///   carrying `LoadArea`, which lives in Equipment Operation — and the identity form then
///   flipped to `rdf:about`, pointing at a definition no file in the set contains;
/// * a real boundary file writes `rdf:ID="7d06eea0-…"` with no leading underscore, which
///   is not an XML `NCName` and therefore not a usable `rdf:ID`. That one *is* repaired on
///   write, because writing it back would be writing an invalid document — so what is
///   asserted here is that the repair was **reported**, not that it did not happen.
///
/// Sets that do not read cleanly are skipped rather than judged: QoCDC breaks files on
/// purpose, and a document this crate could not fully read is not evidence about how it
/// writes.
#[test]
fn every_readable_quality_check_file_re_exports_as_the_document_it_was() {
    let Some(corpus) = corpus() else {
        eprintln!("skipping: QoCDC corpus not present (cargo xtask fetch-specs)");
        return;
    };
    let work = std::env::temp_dir().join(format!("cim-qocdc-rt-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&work);
    std::fs::create_dir_all(&work).unwrap();
    for name in QUICK {
        let file = std::fs::File::open(corpus.join(name)).unwrap();
        zip::ZipArchive::new(std::io::BufReader::new(file))
            .unwrap()
            .extract(&work)
            .unwrap();
    }
    let out = std::env::temp_dir().join(format!("cim-qocdc-rt-out-{}", std::process::id()));

    let (mut sets, mut files) = (0usize, 0usize);
    let mut problems: Vec<String> = Vec::new();
    let mut repaired = 0usize;

    for dir in model_sets(&work) {
        let rel = dir.strip_prefix(&work).unwrap().display().to_string();
        let inputs = cim_kit::instance_files(&dir);
        let Some(schema) = cim_kit::load::detect_vintage(&inputs) else {
            continue;
        };
        let mut ds = Dataset::new(schema);
        let Ok(report) = ds.load_files(&inputs, &ReadOptions::lenient()) else {
            continue;
        };
        // A set that did not read cleanly says nothing about how this crate writes.
        if report.report.iter().any(|d| d.severity == Severity::Error)
            || !ds.merge_conflicts().is_empty()
        {
            continue;
        }
        // Files whose identifiers this crate had to repair on the way in: the input is not
        // writable as it stands, so the output is *supposed* to differ (D56).
        let repaired_files: std::collections::BTreeSet<String> = report
            .report
            .iter()
            .filter(|d| d.rule == Rule::NonConformingMrid)
            .filter_map(|d| d.source.clone())
            .collect();
        repaired += repaired_files.len();

        let _ = std::fs::remove_dir_all(&out);
        std::fs::create_dir_all(&out).unwrap();
        let saved = ds.save_as_loaded(&out).unwrap();
        sets += 1;

        for original in &inputs {
            if original
                .extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("zip"))
            {
                continue;
            }
            let name = original.file_name().unwrap().to_string_lossy().into_owned();
            let Some(written) = saved
                .written
                .iter()
                .find(|p| p.file_name().map(|n| n.to_string_lossy()) == Some(name.as_str().into()))
            else {
                continue;
            };
            let (Ok(src), Ok(dst)) = (
                std::fs::read_to_string(original),
                std::fs::read_to_string(written),
            ) else {
                continue;
            };
            files += 1;
            common::assert_well_formed(&format!("{rel}/{name}"), &dst);

            let (before, after) = (common::element_census(&src), common::element_census(&dst));
            if before != after {
                problems.push(format!(
                    "{rel}/{name}: {}",
                    common::census_diff(&before, &after)
                ));
            }
            let drift = common::value_census_diff(
                &common::value_census(&src),
                &common::value_census(&dst),
                3,
            );
            if !drift.is_empty() {
                problems.push(format!("{rel}/{name}: values {}", drift.join(" | ")));
            }
            if !repaired_files.contains(&name) {
                let (ib, ia) = (
                    common::identifier_census(&src),
                    common::identifier_census(&dst),
                );
                if ib != ia {
                    problems.push(format!(
                        "{rel}/{name}: identifiers lost {:?} gained {:?}",
                        ib.difference(&ia).take(3).collect::<Vec<_>>(),
                        ia.difference(&ib).take(3).collect::<Vec<_>>()
                    ));
                }
            }
        }
    }
    let _ = std::fs::remove_dir_all(&work);
    let _ = std::fs::remove_dir_all(&out);

    println!("quality corpus re-export: {sets} model sets, {files} files compared");
    assert!(
        problems.is_empty(),
        "{} real exports did not come back as themselves:\n  {}",
        problems.len(),
        problems.join("\n  ")
    );
    assert!(files > 200, "only {files} files compared");
    // Pinned rather than tolerated: one published boundary file writes an `rdf:ID` that is
    // not an `NCName`. If that stops happening the exclusion above is dead and should go.
    assert!(
        repaired > 0,
        "no identifier repair was reported — the exclusion above now hides nothing, \
         or the diagnostic stopped firing"
    );
}

/// The other two questions this corpus can answer: does a real export differ from itself,
/// and is its RDF projection interchange?
///
/// Reading found two defects (D43, D44) and comparing documents found three more (D55–D57).
/// A corpus answers the question you ask it, so these are the remaining ones this crate has
/// gates for — and they matter most here because QoCDC is overwhelmingly CGMES 2.4.15, the
/// vintage whose RDF export no SHACL engine can check: every one of its published shapes
/// files is invalid SHACL (D51), so the N-Triples grammar over real 2.4.15 data is the
/// strongest structural evidence available for that half of the crate.
#[test]
fn the_quality_corpus_does_not_differ_from_itself_and_exports_as_interchange() {
    let Some(corpus) = corpus() else {
        eprintln!("skipping: QoCDC corpus not present (cargo xtask fetch-specs)");
        return;
    };
    let work = std::env::temp_dir().join(format!("cim-qocdc-rdf-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&work);
    std::fs::create_dir_all(&work).unwrap();
    for name in QUICK {
        let file = std::fs::File::open(corpus.join(name)).unwrap();
        zip::ZipArchive::new(std::io::BufReader::new(file))
            .unwrap()
            .extract(&work)
            .unwrap();
    }

    let (mut sets, mut triples) = (0usize, 0usize);
    let mut problems: Vec<String> = Vec::new();

    for dir in model_sets(&work) {
        let rel = dir.strip_prefix(&work).unwrap().display().to_string();
        let inputs = cim_kit::instance_files(&dir);
        let Some(schema) = cim_kit::load::detect_vintage(&inputs) else {
            continue;
        };
        let mut ds = Dataset::new(schema);
        let Ok(report) = ds.load_files(&inputs, &ReadOptions::lenient()) else {
            continue;
        };
        if report.report.iter().any(|d| d.severity == Severity::Error)
            || !ds.merge_conflicts().is_empty()
        {
            continue;
        }
        sets += 1;

        // A model differs from itself by nothing — the identity law of the difference
        // machinery, over inputs nobody designed for it.
        if !ds.difference_to(&ds, &Default::default()).is_empty() {
            problems.push(format!("{rel}: a model differs from itself"));
        }

        let mut buf = Vec::new();
        if cim_kit::rdf::write(
            &ds,
            &mut buf,
            &cim_kit::RdfOptions::new(cim_kit::Syntax::NTriples),
        )
        .is_err()
        {
            problems.push(format!("{rel}: RDF export failed"));
            continue;
        }
        match common::check_ntriples(&String::from_utf8(buf).unwrap()) {
            Ok(n) => triples += n,
            Err(e) => problems.push(format!("{rel}: {e}")),
        }
    }
    let _ = std::fs::remove_dir_all(&work);

    println!("quality corpus: {sets} model sets, {triples} triples exported");
    assert!(problems.is_empty(), "{}", problems.join("\n  "));
    assert!(sets > 80, "only {sets} model sets qualified");
    assert!(triples > 200_000, "only {triples} triples exported");
}