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
+++
title = "Difference models"
description = "IEC 61970-552 incremental exchange with cim-kit: read, apply, write and compute a dm:DifferenceModel from two model states."
weight = 60
+++

A `dm:DifferenceModel` (IEC 61970-552) carries the statements to **retract** and the
statements to **assert** instead of a whole model. `cim-kit` reads, applies, writes and
computes them.

## Computing one

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

fn main() -> cim_kit::Result<()> {
    let base = Dataset::load_dir(SCHEMA, "before")?;
    let updated = Dataset::load_dir(SCHEMA, "after")?;

    // Restricted to the profile whose file will carry it.
    let ssh = SCHEMA.profile_by_keyword("SSH").expect("the SSH profile");
    let change = base.difference_to(&updated, &DiffOptions::default().profiles(ssh.mask()));

    println!("{} to retract, {} to assert",
             change.model.reverse.len(), change.model.forward.len());

    cim_kit::writer::write_difference(
        SCHEMA, &change.model, std::io::stdout(), &Default::default())
}
```

- A profile filter uses the CIM/XML writer's rule, so an SSH change set contains exactly
  what an SSH file would.
- Applying `base.difference_to(&target)` to the base reproduces the target, including class
  changes, also after serializing the change set.
- The header names the base in `md:Model.Supersedes`. Its identifier is derived from both
  sides, so the same pair of models always gives the same change file.
- A class change is a statement: the statement group names the new class when, and only
  when, the class changed, even if no attribute differs.

## Reading and applying one

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

fn main() -> cim_kit::Result<()> {
    let mut grid = Dataset::load_dir(SCHEMA, "base")?;
    let file = std::io::BufReader::new(std::fs::File::open("change_DIFF.xml")?);

    // The same options as a model file, including its input budget.
    let options = ReadOptions::default();
    if let Some(diff) = cim_kit::reader::read_difference(SCHEMA, file, None, &options)? {
        let report = grid.apply_difference(&diff);   // retract, re-type, assert
        grid.prune_empty();                          // 552 has no "delete object" statement
        println!("{} finding(s)", report.len());
    }
    Ok(())
}
```

Difference files read as part of a model set are retained, so `save_as_loaded` writes them
back with the set.

### A change set applies only to its base

Before any statement lands, `apply_difference` refuses — one `CIM0014` error, the dataset
unchanged — unless:

- **every model it supersedes is held by this dataset**, as a loaded file or a change set
  applied earlier. Once a change has applied, its superseded model is no longer held, so a
  second change set computed against the same base is refused;
- **every `dm:preconditions` statement holds**.

Identifiers are compared as identifiers, so hyphens and letter case do not matter. A model
built in memory has no header identifiers and is not checked; nor is a change set that names
no base. To bypass the check, clear `diff.header.supersedes` or `diff.preconditions`. A
*retraction* that does not hold is only a warning.

### Order of application

Retract → **re-type** → assert: reverse statements apply to the object's old class, forward
statements to its new one. When a reclassification leaves attributes the new class does not
have, they are dropped and reported.

## Limits of the format

- **Objects cannot be deleted, only emptied.** The syntax has no "delete object" statement.
  A computed difference reports how many objects it could not delete. `Dataset::prune_empty`
  removes empty objects; `apply_difference` does not call it, because a model may hold
  objects described only by files that are not loaded.
- **Compounds have no statement form.** A changed compound such as `Location.mainAddress`
  is reported as a diagnostic and left out of the change set.

## From a shell

```bash
cim diff before/ after/ > change_DIFF.xml
cim diff before/ after/ --profile SSH --out ssh_DIFF.xml
cim apply before/ --change change_DIFF.xml --out updated/
```

`cim diff` writes the summary to stderr and the document to stdout. `cim apply` takes change
sets only via `--change` (never from the inputs), applies several in the order given, and
prunes objects left empty. If a change set is refused, `cim apply` exits 1 and writes
nothing.

The `changes` example does the same from the library and replays the change set onto a
copy of the base to show how far the result is from the target.