Getting started

Install cim-kit, load a CGMES model set, navigate it with typed views and check it against the profile.

Install#

cargo add cim-kit                        # the library, imported as cim_kit
cargo install cim-kit --features cli     # the cim command line
brew install hupe1980/tap/cim-kit        # the command line, macOS (Apple Silicon) and Linux
FeatureEffect
cgmes3 (default)CGMES 3.0 schema, typed views and named constants
cgmes2CGMES 2.4.15 schema, typed views and named constants
zipRead and write model sets packaged as zip archives
cliThe cim command line (implies zip)
cargo add cim-kit --no-default-features --features cgmes2   # only the 2.4.15 vintage
cargo add cim-kit --features cgmes2,zip                     # both vintages, plus archives

Both vintages can be enabled at once; their identifiers are separate and cannot be mixed.

Dataset::open reads the vintage off the document, so you need not know it:

fn main() -> cim_kit::Result<()> {
    // A directory, a zip archive or a single file.
    let grid = cim_kit::Dataset::open("MicroGrid-BE")?;
    println!("{} objects, read as {}", grid.len(), grid.schema().vintage);
    Ok(())
}

Dataset::load_dir(SCHEMA, …) names the vintage explicitly, as the rest of this guide does; reader::sniff returns the schema without loading. A model read against the wrong vintage resolves no class, so the mismatch is reported as CIM0021 at the root element. The cim command line detects by default; --vintage overrides it.

Load a model#

A CGMES model is a set of profile files describing the same objects, usually delivered as a directory or a zip archive.

use cim_kit::prelude::*;
use cim_kit::cgmes3::SCHEMA;

fn main() -> cim_kit::Result<()> {
    let grid = Dataset::load_dir(SCHEMA, "MicroGrid-BE")?;
    println!("{} objects from {} files", grid.len(), grid.headers().len());

    // Name the files directly …
    let grid = Dataset::load(SCHEMA, ["EQ.xml", "SSH.xml", "TP.xml", "SV.xml"])?;
    println!("{} objects", grid.len());

    // … or parse them separately and merge by mRID; the result is the same model.
    let mut model = Dataset::new(SCHEMA);
    for part in cim_kit::instance_files("MicroGrid-BE") {
        model.merge(Dataset::load(SCHEMA, [part])?)?;
    }
    Ok(())
}

Typed views are zero-cost wrappers generated from the vocabulary, carrying the standard's documentation.

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

fn report(grid: &Dataset) {
    for terminal in grid.view::<Terminal>() {
        if let Some(equipment) = terminal.conducting_equipment_in(grid) {
            println!("{:?} on {:?}, connected={:?}",
                     terminal.name(), equipment.name(), terminal.connected());
        }
    }

    // Enumerations resolve to schema literals, not strings.
    for machine in grid.view::<SynchronousMachine>() {
        if let Some(kind) = machine.type_() {
            println!("{:?}: {}", machine.name(), SCHEMA.enum_value(kind).name);
        }
    }
}

view::<T>() visits only T and its subclasses. Associations are stored as identifiers: x() returns a TypedRef, x_in(&dataset) resolves it without allocating, and InverseIndex makes repeated reverse lookups hash lookups — see Concepts.

Check it#

use cim_kit::prelude::*;
use cim_kit::ValidateOptions;

fn check(grid: &Dataset) -> bool {
    let report = grid.validate_with(&ValidateOptions::thorough());
    for (rule, count) in report.summary() {
        println!("{rule}: {count}");     // CIM0006: 43
    }
    !report.has_errors()
}

Every finding carries a stable rule code, a severity, and the object, class, attribute and source file it concerns. The catalogue is in Validation.

From a shell#

cim info     MicroGrid-BE/
cim validate MicroGrid-BE/ --rule CIM0007
cim export   MicroGrid-BE/ --out out/

See Command line.

Runnable examples#

cargo run --example build_model --features cgmes3                  # build a model, write CIM/XML (no input needed)
cargo run --example inspect     --features cgmes3 -- <model-dir>   # walk it with typed views
cargo run --example to_rdf      --features cgmes3 -- <model-dir> EQ
cargo run --example changes     --features cgmes3 -- <base> <target>