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
//! Read the entire published CGMES 3.0 conformity corpus and check nothing is lost.

#![cfg(feature = "cgmes3")]

mod common;

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::Instant;

use cim_kit::Severity;
use cim_kit::cgmes3::{SCHEMA, views};
use cim_kit::prelude::*;

/// Every directory in the corpus that directly contains instance files.
fn model_dirs(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_xml = 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")) {
                has_xml = true;
            }
        }
        if has_xml {
            out.push(d.to_path_buf());
        }
    }
    let mut out = Vec::new();
    walk(root, &mut out);
    out.sort();
    out
}

#[test]
fn every_conformity_model_reads_without_diagnostics() {
    let root = require_corpus!(common::cgmes3_models());
    let dirs = model_dirs(&root);
    assert!(
        dirs.len() > 10,
        "corpus looks incomplete: {} dirs",
        dirs.len()
    );

    let mut by_rule: BTreeMap<&'static str, usize> = BTreeMap::new();
    let mut samples: Vec<String> = Vec::new();
    let (mut files, mut objects) = (0usize, 0usize);
    let started = Instant::now();

    for dir in &dirs {
        let paths = common::xml_files(dir);
        let mut ds = Dataset::new(SCHEMA);
        let report = ds
            .load_files(&paths, &ReadOptions::lenient())
            .unwrap_or_else(|e| panic!("{}: {e}", dir.display()));
        files += report.files.len();
        objects += ds.len();
        for (rule, n) in report.report.summary() {
            *by_rule.entry(rule.code()).or_default() += n;
        }
        if !report.report.is_empty() && samples.len() < 10 {
            let rel = dir.strip_prefix(&root).unwrap_or(dir);
            for d in report.report.iter().take(2) {
                samples.push(format!("{}: {d}", rel.display()));
            }
        }
    }

    println!(
        "{} model sets, {files} files, {objects} objects in {:.2}s",
        dirs.len(),
        started.elapsed().as_secs_f64()
    );
    for s in &samples {
        println!("  {s}");
    }
    assert!(
        by_rule.is_empty(),
        "reading the published conformity corpus produced diagnostics: {by_rule:?}"
    );
}

/// Export every model set as the files it was read from and compare the serialized form.
///
/// The projection-based round-trip tests compare the assembled *model*, which is blind to
/// how objects are identified and to which file they land in. Comparing per file, per
/// class, catches both — and both were wrong before: State Variables objects were written
/// with `rdf:about` where the published files use `rdf:ID`, and a merged model put every
/// modelling authority's equipment into every authority's Equipment file.
#[test]
fn every_model_set_re_exports_as_the_files_it_came_from() {
    let root = require_corpus!(common::cgmes3_models());
    let out = std::env::temp_dir().join(format!("cim-sweep-export-{}", std::process::id()));
    let mut checked = 0usize;
    let mut problems: Vec<String> = Vec::new();

    for dir in model_dirs(&root) {
        let paths = common::xml_files(&dir);
        let mut ds = Dataset::new(SCHEMA);
        ds.load_files(&paths, &ReadOptions::lenient()).unwrap();

        let _ = std::fs::remove_dir_all(&out);
        let saved = ds.save_as_loaded(&out).unwrap();
        let rel = dir
            .strip_prefix(&root)
            .unwrap_or(&dir)
            .display()
            .to_string();

        for original in &paths {
            let name = original.file_name().unwrap();
            let Some(written) = saved.written.iter().find(|p| p.file_name() == Some(name)) else {
                problems.push(format!("{rel}/{}: not re-exported", name.to_string_lossy()));
                continue;
            };
            let output = std::fs::read_to_string(written).unwrap();
            // Comparing censuses says the output holds the right objects; it says nothing
            // about whether an XML parser will accept the file at all.
            if let Err(e) = common::check_well_formed(&output) {
                problems.push(format!("{rel}/{}: {e}", name.to_string_lossy()));
            }
            let source = std::fs::read_to_string(original).unwrap();
            let before = common::element_census(&source);
            let after = common::element_census(&output);
            if before != after {
                problems.push(format!(
                    "{rel}/{}: {}",
                    name.to_string_lossy(),
                    common::census_diff(&before, &after)
                ));
            }
            // And the identifiers themselves, letter for letter. The census above compares
            // classes and identity styles, so it cannot see an identifier being *rewritten*
            // — which is how a lower-cased UUID reached the published boundary files'
            // export unnoticed.
            let ids_before = common::identifier_census(&source);
            let ids_after = common::identifier_census(&output);
            if ids_before != ids_after {
                let lost: Vec<&String> = ids_before.difference(&ids_after).take(3).collect();
                let gained: Vec<&String> = ids_after.difference(&ids_before).take(3).collect();
                problems.push(format!(
                    "{rel}/{}: identifiers changed — {} lost {lost:?}, {} gained {gained:?}",
                    name.to_string_lossy(),
                    ids_before.difference(&ids_after).count(),
                    ids_after.difference(&ids_before).count(),
                ));
            }
            checked += 1;
        }
    }
    let _ = std::fs::remove_dir_all(&out);

    assert!(checked > 100, "only {checked} files compared");
    assert!(
        problems.is_empty(),
        "{} of {checked} files re-exported differently:\n  {}",
        problems.len(),
        problems
            .iter()
            .take(15)
            .cloned()
            .collect::<Vec<_>>()
            .join("\n  ")
    );
    println!("{checked} files re-exported with the same objects and identity forms");
}

/// Validate every model set and pin exactly what the checks find.
///
/// Two errors survive, and both are properties of the published models rather than of this
/// crate: FullGrid's `Cut` — a class CGMES 3.0 added — is carried in the Steady State
/// Hypothesis file as a bare `<cim:Equipment>` with only `inService`, so the switch state
/// that profile makes mandatory is genuinely absent. Pinning the census rather than
/// asserting "no errors" keeps the test a regression guard: a new finding, or the
/// disappearance of one of these, fails.
#[test]
fn corpus_validation_findings_are_exactly_the_known_ones() {
    let root = require_corpus!(common::cgmes3_models());
    let mut errors: BTreeMap<String, usize> = BTreeMap::new();
    let mut by_rule: BTreeMap<&'static str, usize> = BTreeMap::new();
    let mut contradicting: BTreeMap<String, usize> = BTreeMap::new();

    for dir in model_dirs(&root) {
        let paths = common::xml_files(&dir);
        let mut ds = Dataset::new(SCHEMA);
        ds.load_files(&paths, &ReadOptions::lenient()).unwrap();
        if !ds.merge_conflicts().is_empty() {
            let rel = dir.strip_prefix(&root).unwrap_or(&dir);
            contradicting.insert(rel.display().to_string(), ds.merge_conflicts().len());
        }
        let report = ds.validate();
        for (rule, n) in report.summary() {
            *by_rule.entry(rule.code()).or_default() += n;
        }
        for d in report.iter().filter(|d| d.severity == Severity::Error) {
            *errors
                .entry(format!(
                    "{}: {}",
                    d.class.unwrap_or("?"),
                    d.attribute.unwrap_or(&d.message)
                ))
                .or_default() += 1;
        }
    }

    println!("validation findings across the corpus, by rule: {by_rule:?}");
    let expected: BTreeMap<String, usize> = [
        ("Cut: Switch.locked".to_owned(), 1),
        ("Cut: Switch.open".to_owned(), 1),
    ]
    .into_iter()
    .collect();
    assert_eq!(errors, expected, "corpus validation errors changed");

    // Merging assembles a model from its profile files, and a contradiction between two of
    // them means one reading was discarded. Exactly two directories in the corpus produce
    // any, and neither is a defective model set: `MicroGrid-Type3/IGMs` and its assembled
    // counterpart `CGMs` each hold **24 hourly snapshots** of one grid, and this sweep
    // loads a directory at a time. Merging 24 states of the same objects is supposed to
    // contradict itself, and what contradicts is exactly what a time series varies —
    // dispatch (`EquivalentInjection.p/q`, `RotatingMachine.p`, `EnergyConsumer.p`),
    // switching (`ACDCTerminal.connected`, `Equipment.inService`), tap positions
    // (`TapChanger.step`) and the topology those produce (`Terminal.TopologicalNode`).
    // Every other model set in the corpus agrees with itself completely.
    //
    // Pinning the *location* rather than "none" is what makes this a regression guard: a
    // new entry means a model set genuinely disagrees with itself, and an empty result
    // means the detection stopped working.
    println!("directories whose files contradict each other: {contradicting:?}");
    let dirs: Vec<&str> = contradicting.keys().map(String::as_str).collect();
    assert_eq!(
        dirs,
        [
            "MicroGrid/MicroGrid-Type3/CGMs",
            "MicroGrid/MicroGrid-Type3/IGMs"
        ],
        "a different set of model sets now contradicts itself"
    );
    assert!(
        by_rule
            .get(cim_kit::Rule::ConflictingValue.code())
            .copied()
            .unwrap_or(0)
            > 0,
        "the merge-conflict check stopped reporting"
    );
}

/// Every model set must also come out as syntactically valid RDF.
///
/// The point of the RDF export is that somebody else's toolchain reads it, so "this crate
/// can read it back" is not the bar — the N-Triples grammar is. Running the whole corpus
/// through it is what catches an escaping mistake in the one identifier, name or IRI in a
/// quarter of a million objects that happens to need escaping.
///
/// Validating the result against ENTSO-E's SHACL shapes needs a SHACL engine and is
/// therefore a script rather than a test: `cargo xtask shacl`.
#[test]
fn every_model_set_exports_as_valid_rdf() {
    use cim_kit::rdf::{RdfOptions, Syntax};

    let root = require_corpus!(common::cgmes3_models());
    let (mut models, mut triples) = (0usize, 0usize);
    let mut problems: Vec<String> = Vec::new();

    for dir in model_dirs(&root) {
        let mut ds = Dataset::new(SCHEMA);
        ds.load_files(common::xml_files(&dir), &ReadOptions::lenient())
            .unwrap();
        let rel = dir
            .strip_prefix(&root)
            .unwrap_or(&dir)
            .display()
            .to_string();

        let nt = cim_kit::rdf::to_string(&ds, &RdfOptions::new(Syntax::NTriples)).unwrap();
        match common::check_ntriples(&nt) {
            Ok(n) => triples += n,
            Err(e) => problems.push(format!("{rel}: {e}")),
        }
        // Each profile's own graph too: that is the form a consumer validates against the
        // shapes for that profile, and it exercises the element-class rule as well.
        for (i, p) in SCHEMA.profiles.iter().enumerate() {
            let mask = cim_kit::ProfileId(i as u16).mask();
            let slice =
                cim_kit::rdf::to_string(&ds, &RdfOptions::new(Syntax::NTriples).profiles(mask))
                    .unwrap();
            if let Err(e) = common::check_ntriples(&slice) {
                problems.push(format!("{rel} [{}]: {e}", p.keyword));
            }
        }
        models += 1;
    }

    assert!(models > 10, "only {models} model sets");
    assert!(
        problems.is_empty(),
        "{} model sets produced invalid RDF:\n  {}",
        problems.len(),
        problems
            .iter()
            .take(10)
            .cloned()
            .collect::<Vec<_>>()
            .join("\n  ")
    );
    println!("{models} model sets exported as {triples} valid N-Triples");
}

#[test]
fn microgrid_content_is_actually_populated() {
    let dir = require_corpus!(common::cgmes3_model(
        "MicroGrid/MicroGid-BaseCase/MicroGrid-NL-MAS"
    ));
    let files = common::xml_files(&dir);
    let mut ds = Dataset::new(SCHEMA);
    ds.load_files(&files, &ReadOptions::lenient()).unwrap();

    let total_values: usize = ds.iter().map(|(_, o)| o.len()).sum();
    println!("objects={} attribute values={total_values}", ds.len());
    assert!(total_values > 1000);

    // Typed views must expose real numeric data.
    let lines: Vec<_> = ds.view::<views::ACLineSegment>().collect();
    assert!(!lines.is_empty(), "MicroGrid must contain AC line segments");
    assert!(
        lines.iter().any(|l| l.r().is_some()),
        "no resistance values read"
    );
    assert!(
        lines.iter().all(|l| l.name().is_some()),
        "every line has a name"
    );

    // Associations must resolve through the dataset.
    let terminals: Vec<_> = ds.view::<views::Terminal>().collect();
    let resolved = terminals
        .iter()
        .filter(|t| t.conducting_equipment_in(&ds).is_some())
        .count();
    assert_eq!(
        resolved,
        terminals.len(),
        "all terminals resolve their equipment"
    );

    // Profile merging: SSH data lands on objects that EQ defined.
    let with_ssh = terminals.iter().filter(|t| t.connected().is_some()).count();
    assert!(with_ssh > 0, "SSH attributes did not merge onto EQ objects");

    // Enumerations resolve to literals.
    let typed = ds
        .view::<views::SynchronousMachine>()
        .filter_map(|m| m.type_())
        .count();
    assert!(typed > 0, "enumeration values did not read");

    // Every object is attributed to the profiles its files declared.
    assert!(
        ds.iter().all(|(_, o)| o.profiles() != 0),
        "objects were not attributed to any profile"
    );
}

#[test]
fn objects_merge_rather_than_duplicate_across_profiles() {
    let dir = require_corpus!(common::cgmes3_model(
        "MicroGrid/MicroGid-BaseCase/MicroGrid-NL-MAS"
    ));
    let files = common::xml_files(&dir);

    let mut forward = Dataset::new(SCHEMA);
    let r = forward.load_files(&files, &ReadOptions::lenient()).unwrap();
    // More object elements than objects proves merging actually happened.
    assert!(
        r.objects_read > forward.len(),
        "expected merging: {} elements, {} objects",
        r.objects_read,
        forward.len()
    );

    // Load order must not matter.
    let mut reversed: Vec<_> = files.clone();
    reversed.reverse();
    let mut backward = Dataset::new(SCHEMA);
    backward
        .load_files(&reversed, &ReadOptions::lenient())
        .unwrap();
    assert_eq!(
        forward.len(),
        backward.len(),
        "load order changed the model"
    );

    for (_, o) in forward.iter() {
        let other = backward
            .by_mrid(o.mrid())
            .unwrap_or_else(|| panic!("{} missing after reversed load", o.mrid()));
        assert_eq!(o.class(), other.class(), "{}: class differs", o.mrid());
        assert_eq!(
            o.len(),
            other.len(),
            "{}: attribute count differs",
            o.mrid()
        );
    }
}