cim-kit 0.5.1

IEC CIM/CGMES for power systems: CIM/XML I/O that reproduces the file set, profile-aware validation, typed RDF export
Documentation
# cim-kit

**A Rust implementation of the IEC Common Information Model for power systems.**

[![CI](https://github.com/hupe1980/cim-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/hupe1980/cim-kit/actions/workflows/ci.yml)
[![Docs](https://img.shields.io/badge/docs-hupe1980.github.io%2Fcim--kit-b8410f)](https://hupe1980.github.io/cim-kit/)
[![License](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue)](#-licensing-and-attribution)

`cim-kit` reads, navigates, validates and writes CIM grid models in **CGMES 3.0**
(IEC TS 61970-600-1/-2:2021) and **CGMES 2.4.15**. It exports typed RDF that ENTSO-E's
SHACL shapes accept, and computes difference models between two model states.

It reads both ENTSO-E conformity corpora โ€” 31 CGMES 3.0 model sets and 52 CGMES 2.4.15
archives, 523,842 objects โ€” without an error, and writes each back file for file and value
for value.

๐Ÿ“– **[Documentation](https://hupe1980.github.io/cim-kit/)** ยท
๐Ÿฆ€ **[API reference](https://docs.rs/cim-kit)**

```rust,no_run
use cim_kit::prelude::*;
use cim_kit::cgmes3::{SCHEMA, views::ACLineSegment};

// A CGMES model is a set of profile files describing the same objects.
let grid = Dataset::load_dir(SCHEMA, "MicroGrid-BE")?;

for line in grid.view::<ACLineSegment>() {
    let kv = line.base_voltage_in(&grid).and_then(|bv| bv.nominal_voltage());
    println!("{:?}  {kv:?} kV  r={:?} x={:?}", line.name(), line.r(), line.x());
}

let report = grid.validate();
println!("{} findings", report.len());
# Ok::<(), cim_kit::Error>(())
```

## ๐Ÿ“ฆ Install

```bash
cargo add cim-kit                       # the library
cargo install cim-kit --features cli    # the `cim` command line
brew install hupe1980/tap/cim-kit       # the `cim` command line (macOS Apple Silicon, Linux)
```

The package is `cim-kit`; the library is `cim_kit`.

| Feature | Effect |
|---|---|
| `cgmes3` *(default)* | CGMES 3.0 schema, typed views and named constants |
| `cgmes2` | CGMES 2.4.15 schema, typed views and named constants |
| `zip` | Read and write model sets packaged as zip archives |
| `cli` | The `cim` command line (implies `zip`) |

Both vintages can be enabled at once; identifiers are per-vintage, so they cannot be mixed.

## โŒจ๏ธ From a shell

```bash
cim info     MicroGrid-BE/                  # files, object counts, profile coverage, findings
cim validate MicroGrid-BE/ --rule CIM0007   # exits 1 on any error
cim export   MicroGrid-BE/ --out out/       # write the model back as the file set it came from
cim rdf      MicroGrid-BE/ --out graphs/    # one typed RDF graph per profile with data
cim diff     before/ after/ > change.xml    # the change set between two model states
cim apply    before/ --change change.xml --out after/   # and the receiving half
```

Inputs may be files, zip archives or directories. A document result is the whole of stdout;
reports go to stderr. See [the command-line reference][cli].

## โœ… What makes it correct

Each point is enforced by a test against the published conformity models.

* **A model is a set of files.** Objects merge by mRID across profiles; contradictions are
  reported (`CIM0018`), not resolved silently.
* **An identifier is a UUID, not its spelling.** Hyphenless and absolute-IRI forms resolve
  to the same object.
* **Provenance decides the export.** Each value and object records the file it came from,
  so a re-export reproduces the original file set.
* **Element classes and `rdf:ID`/`rdf:about` come from the profile RDFS**, not from guesses.
* **A number's spelling is kept.** `2.62637E-05` is written back as read; equality still
  compares numbers.
* **Compounds are values.** Nested `StreetAddress` and similar are read as structures.
* **The vintage is detected from the namespace.** A mismatch is reported (`CIM0021`).
* **A read succeeds only for a whole, well-formed document.** Truncated or broken XML is an
  error under either strictness, and a failed read changes nothing.
* **Lenient reading reports what it tolerated**, in a bounded report. A property given twice
  in one element is reported (`CIM0018`), with both positions.
* **Input is bounded.** 8 GiB per load by default, archives counted decompressed;
  `--max-input` raises it. Entities are never expanded or fetched.
* **Unrepresentable values are refused.** Characters XML 1.0 cannot hold and non-`NCName`
  `rdf:ID`s are reported (`CIM0019`, `CIM0020`) and never written.
* **Output is checked by other parsers.** XML and N-Triples grammars, PowSyBl (same network
  from a re-export as from the original) and rdflib, in CI.

Details: [conformance][conformance].

## ๐ŸŒ Typed RDF

CIM/XML is not RDF/XML and carries no datatypes. `cim-kit` exports N-Triples or Turtle per
profile, with every value typed from the profile, ready for a triple store or a SHACL engine.

```rust,no_run
use cim_kit::prelude::*;
use cim_kit::cgmes3::SCHEMA;
use cim_kit::rdf::{RdfOptions, Syntax};

# let grid = Dataset::load(SCHEMA, ["EQ.xml"])?;
let eq = SCHEMA.profile_by_keyword("EQ").unwrap();
let turtle = cim_kit::rdf::to_string(&grid, &RdfOptions::new(Syntax::Turtle).profiles(eq.mask()))?;
# Ok::<(), cim_kit::Error>(())
```

```turtle
<urn:uuid:17086487-56ba-4979-b8de-064025a6b4da>
    a cim:ACLineSegment ;
    cim:ACLineSegment.r "2.2"^^xsd:float ;
    cim:ConductingEquipment.BaseVoltage <urn:uuid:a7f1d8de-d658-428a-821b-3a5ae5965fd1> ;
    cim:Equipment.aggregate "false"^^xsd:boolean ;
    cim:IdentifiedObject.name "BE-Line_1" ;
    eu:IdentifiedObject.shortName "BE-L_1" .
```

Every profile with data in every CGMES 3.0 conformity model passes ENTSO-E's SHACL shapes
under `pyshacl` in CI. The library does not run SHACL itself. [More][rdf]

## ๐Ÿ”„ Difference models

Read, apply, write and **compute** IEC 61970-552 difference models.

```rust,no_run
use cim_kit::prelude::*;
use cim_kit::cgmes3::SCHEMA;
use cim_kit::diff::DiffOptions;

# fn main() -> Result<(), Box<dyn std::error::Error>> {
let base = Dataset::load_dir(SCHEMA, "before")?;
let updated = Dataset::load_dir(SCHEMA, "after")?;

let change = base.difference_to(&updated, &DiffOptions::default());
cim_kit::writer::write_difference(SCHEMA, &change.model, std::io::stdout(), &Default::default())?;
# Ok(()) }
```

Applying a computed change set to its base reproduces the target exactly. A change set
applies only to its own base: an unmet `md:Model.Supersedes` or `dm:preconditions` is
refused before anything changes. [More][diff]

## ๐Ÿ—๏ธ Design

The schema tables, typed views and named constants are generated from the official RDFS,
one module per vintage; the dataset, reader/writer, RDF, header, diff and validation layers
are hand-written and schema-agnostic. Objects are stored sparsely. [More][concepts]

| Vintage | Feature | Namespace | Classes | Attributes | Enums | Profiles |
|---|---|---|---|---|---|---|
| CGMES 3.0 | `cgmes3` *(default)* | `CIM100` + `eu` | 443 | 3,624 | 51 | 11 |
| CGMES 2.4.15 | `cgmes2` | `cim16` + `entsoe` | 401 | 3,539 | 48 | 12 |

Benchmarks: [performance][perf]; reproduce with `cargo bench -p cim-kit`.

## ๐Ÿ“‹ Conformance

| Standard | Role |
|---|---|
| IEC 61970-301:2020+AMD1:2022 | CIM base semantics (CIM17) |
| IEC 61970-501:2006 | RDFS profile representation โ€” the codegen input |
| IEC 61970-552:2016 | CIM/XML format: identity, headers, difference models |
| IEC TS 61970-600-1/-2:2021 | CGMES 3.0 profile set |

Profiles: **EQ, OP, SC, EQBD, SSH, TP, SV, DL, GL, DY** and the header vocabulary in both
vintages, plus **TPBD** in CGMES 2.4.15. Tested against both conformity corpora and
ENTSO-E's 100 QoCDC quality-check sets; builds for `wasm32-unknown-unknown`.
[The full record][conformance]

## ๐Ÿ› ๏ธ Development

```bash
cargo xtask fetch-specs         # ENTSO-E RDFS, SHACL and conformity models (not vendored)
cargo xtask codegen             # RDFS -> src/generated/
cargo test --workspace --all-features
```

Corpus-backed tests skip cleanly on a fresh clone. See [CONTRIBUTING.md](CONTRIBUTING.md)
and [CHANGELOG.md](CHANGELOG.md).

## โš–๏ธ Licensing and attribution

Licensed **MIT OR Apache-2.0**.

The RDFS vocabularies the generated code derives from are published by ENTSO-E and the UCA
International Users Group under the **Apache License 2.0**; generated files carry that
attribution. IEC standard documents are not redistributed and the build does not depend on
them. The ENTSO-E conformity models (**CC BY-SA 4.0**) are a local test corpus only and are
never redistributed.

[cli]: https://hupe1980.github.io/cim-kit/docs/cli/
[rdf]: https://hupe1980.github.io/cim-kit/docs/rdf/
[diff]: https://hupe1980.github.io/cim-kit/docs/difference-models/
[concepts]: https://hupe1980.github.io/cim-kit/docs/concepts/
[perf]: https://hupe1980.github.io/cim-kit/docs/performance/
[conformance]: https://hupe1980.github.io/cim-kit/docs/conformance/