Skip to main content

Dataset

Struct Dataset 

Source
pub struct Dataset { /* private fields */ }
Expand description

A set of CIM objects together with the headers of the files that produced them.

CGMES splits one grid model across profile files (EQ, SSH, TP, SV, …) that describe the same objects; loading them into one dataset merges attributes by mRID.

Implementations§

Source§

impl Dataset

Source

pub const MAX_CONFLICTS: usize = 1000

How many merge conflicts are retained before only their number is kept.

Bounds memory when mismatched files disagree about every object.

Source

pub const DERIVED_NS: Mrid

The namespace under which this crate derives identifiers.

A fixed version-4 UUID, so derived mRIDs cannot collide with those derived elsewhere.

Source

pub fn new(schema: &'static Schema) -> Dataset

Source

pub fn schema(&self) -> &'static Schema

Source

pub fn headers(&self) -> &[ModelHeader]

Headers of every file loaded into this dataset, in load order.

Source

pub fn push_header(&mut self, header: ModelHeader) -> usize

Register a file’s header and return the source slot its objects belong to.

Called before a file’s objects are stored, so each is recorded against its file.

Source

pub fn set_header(&mut self, index: usize, header: ModelHeader)

Replace the header in slot index, keeping the objects already recorded against it.

Used when a document’s md:FullModel follows its first object, which has already claimed a placeholder slot.

Source

pub fn record_source(&mut self, source: usize, id: ObjectId)

Record that the file in header slot source contained id.

Source

pub fn objects_from(&self, source: usize) -> impl Iterator<Item = ObjectId> + '_

Objects the file in header slot source contributed, in the order it listed them.

Needed where profile provenance is not enough, e.g. a merged model with one Equipment file per modelling authority. Objects since removed with Dataset::remove are skipped (checked here, not on removal).

Source

pub fn differences(&self) -> &[DifferenceModel]

Difference models read into this dataset, in load order.

Not applied on load (Dataset::apply_difference does that); kept so a model set with change files can be written back whole.

Source

pub fn model_ids(&self) -> impl Iterator<Item = &Mrid> + '_

The model states this dataset holds now: its headers’ identifiers, less every model an applied change set superseded, plus the change sets applied. A difference file only read, not applied, is not among them.

Source

pub fn push_difference(&mut self, diff: DifferenceModel)

Source

pub fn profiles(&self) -> ProfileMask

Profiles present across all loaded headers.

Source

pub fn len(&self) -> usize

Number of live objects.

Source

pub fn is_empty(&self) -> bool

Source

pub fn get(&self, id: ObjectId) -> Option<&Object>

Source

pub fn get_mut(&mut self, id: ObjectId) -> Option<&mut Object>

Source

pub fn find(&self, mrid: &Mrid) -> Option<ObjectId>

Source

pub fn by_mrid(&self, mrid: &Mrid) -> Option<&Object>

Source

pub fn insert(&mut self, object: Object) -> ObjectId

Insert an object, or merge into the existing one with the same mRID.

Attributes are added to the existing object, not replacing it. The more specific class wins, since profiles may describe an object through a base class.

Source

pub fn merge(&mut self, other: Dataset) -> Result<()>

Absorb another dataset: its objects, its file headers, which file each object came from, and any change files it carries.

Lets files be parsed in parallel, one dataset each, and combined afterwards into the same model a sequential load produces.

let mut model = Dataset::new(SCHEMA);
for part in ["EQ.xml", "SSH.xml", "TP.xml", "SV.xml"]
    .map(|f| Dataset::load(SCHEMA, [f]))
{
    model.merge(part?)?;
}

As with load_files, a conflicting single-valued attribute takes the later value and is recorded as a MergeConflict; conflicts other recorded are carried over.

Fails if the two datasets were built from different schema vintages.

Source

pub fn merge_conflicts(&self) -> &[MergeConflict]

Single-valued attributes two loaded files gave different values for.

Empty for a consistent model set; each entry is a discarded value. validate turns these into Rule::ConflictingValue diagnostics.

Source

pub fn merge_conflicts_dropped(&self) -> usize

Conflicts that occurred beyond Dataset::MAX_CONFLICTS and were only counted.

Source

pub fn reclassify(&mut self, id: ObjectId, class: ClassId) -> Vec<Slot>

Change an object’s class, keeping the per-class index consistent, and return any values the new class cannot carry.

Used when a difference model reclassifies an object (IEC 61970-552).

Values are shed, not kept. Narrowing to a subclass sheds nothing; moving sideways sheds attributes the new class lacks (e.g. LinearShuntCompensator.bPerSection on a NonlinearShuntCompensator), since writer and validation would never see them. They are returned so the caller can report the loss.

Source

pub fn remove(&mut self, id: ObjectId) -> Option<Object>

Remove an object. Its ObjectId is never reused.

Source

pub fn iter(&self) -> impl Iterator<Item = (ObjectId, &Object)> + '_

Every live object.

Source

pub fn iter_exact( &self, class: ClassId, ) -> impl Iterator<Item = (ObjectId, &Object)> + '_

Objects of exactly class, excluding subclasses.

Source

pub fn iter_class( &self, class: ClassId, ) -> impl Iterator<Item = (ObjectId, &Object)> + '_

Objects of class or any subclass — usually what callers want.

Walks per-class buckets, so the cost is the number of matches, not the model size. Order is unspecified but stable for a given load.

Source

pub fn count_class(&self, class: ClassId) -> usize

Source

pub fn follow(&self, object: &Object, attr: AttrId) -> Option<&Object>

Resolve a single-valued association to the referenced object.

Source

pub fn follow_all<'a>( &'a self, object: &'a Object, attr: AttrId, ) -> impl Iterator<Item = &'a Object> + 'a

Resolve every value of a many-valued association.

References that do not resolve are skipped; use validate to report them.

Source

pub fn referrers<'a>( &'a self, target: &'a Mrid, attr: AttrId, ) -> impl Iterator<Item = (ObjectId, &'a Object)> + 'a

Objects whose association attr points at target.

Computed, since only one side of each association is serialized (IEC 61970-501). Build an InverseIndex when querying repeatedly.

Source

pub fn content_id(&self) -> Mrid

A UUID that identifies what this model contains.

Used as md:FullModel rdf:about when exporting a model that has none: unchanged content gives an unchanged identifier, so exports stay deterministic.

Covers the vintage and, per object in mRID order, its identifier, class and stored values; not headers, per-file provenance or load order.

Derived with SHA-1: guards against accident, not an adversary.

Source

pub fn shrink_to_fit(&mut self)

Release unused capacity after bulk loading.

Source

pub fn dangling_references(&self) -> Vec<(ObjectId, AttrId, Mrid)>

References that point at objects not present in the dataset.

Fail if the link policy is LinkPolicy::Strict and references are unresolved.

Source§

impl Dataset

Source

pub fn validate(&self) -> Report

Validate with the default checks.

Shorthand for validate::validate.

Source

pub fn validate_with(&self, options: &ValidateOptions) -> Report

Validate with explicit options.

Source§

impl Dataset

Source

pub fn difference_to( &self, target: &Dataset, options: &DiffOptions, ) -> DiffReport

The change set that turns this model into target.

See the module documentation for what the result guarantees and for the two things a statement-level difference cannot express.

Source

pub fn prune_empty(&mut self) -> usize

Remove objects that carry no values at all.

A difference cannot delete an object, only retract all its values; this removes the shells left behind. Returns how many were removed.

Separate from Dataset::apply_difference because a model may legitimately hold objects described only by files that are not loaded.

Source§

impl Dataset

Source

pub fn load<I, P>(schema: &'static Schema, paths: I) -> Result<Dataset>
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Load a set of instance files into a new dataset.

Files may be given in any order: objects are merged by mRID, so an SSH file read before its EQ file produces the same result.

Source

pub fn load_files<I, P>( &mut self, paths: I, options: &ReadOptions, ) -> Result<LoadReport>
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Load files into an existing dataset.

All files and archive entries share one input budget (ReadOptions::max_input_bytes).

§Errors

Error::InputTooLarge under either strictness once the budget is spent; the dataset is then partial and must be discarded. Under Strictness::Strict, the first file that cannot be read. Otherwise an unreadable file or archive entry is reported as CIM0022, listed in LoadReport::failed, and contributes nothing: each document is applied whole or not at all.

Source

pub fn load_file( &mut self, path: &Path, options: &ReadOptions, ) -> Result<LoadReport>

Load one instance file, or every instance file inside a zip archive.

Source

pub fn load_dir( schema: &'static Schema, dir: impl AsRef<Path>, ) -> Result<Dataset>

Load every instance file under a directory, in sorted order.

Model sets usually ship as directories; to name files individually use Dataset::load.

Source

pub fn open(path: impl AsRef<Path>) -> Result<Dataset>

Load a model set, reading its vintage out of the files themselves.

path is a directory, a zip archive or a single instance file; the schema comes from the namespace declarations (see detect_vintage). Use Dataset::load_dir or Dataset::load to state the vintage explicitly.

§Errors

Error::UnknownVintage when no file under path declares a vocabulary this build has a feature for, including when path holds no CIM input at all.

let grid = cim_kit::Dataset::open("MicroGrid-BE")?;
println!("{} objects", grid.len());
Source

pub fn peek_headers<I, P>( schema: &'static Schema, paths: I, ) -> Result<Vec<(PathBuf, Option<ModelHeader>)>>
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Read only the headers of a set of files, without loading their objects.

Useful for deciding what to load: headers declare profiles and dependencies.

Source

pub fn apply_difference(&mut self, diff: &DifferenceModel) -> Report

Apply a difference model (IEC 61970-552): check it applies to this model, remove reverse statements, then assert forward statements.

Refused, with one CIM0014 error and the dataset unchanged, when a model named in md:Model.Supersedes is neither loaded nor an earlier applied change set (compared as identifiers), or when a dm:preconditions statement does not hold. Nothing is checked if the dataset has no header identifiers or the change set names no base; to skip the check, clear diff.header.supersedes or diff.preconditions.

Otherwise, statements naming unknown classes or properties are reported and skipped, and a retraction that does not hold is a warning, not a refusal.

Source

pub fn save_profile( &self, path: &Path, profile: ProfileId, header: Option<ModelHeader>, ) -> Result<()>

Write one profile of this dataset to a file.

Source

pub fn save_all_profiles(&self, dir: &Path, stem: &str) -> Result<Vec<PathBuf>>

Write every profile the dataset carries data for, into dir, one file each.

Files are named <stem>_<KEYWORD>.xml. Use Dataset::save_as_loaded instead when the goal is to reproduce the file set the model was read from.

Source

pub fn save_as_loaded(&self, dir: &Path) -> Result<SaveReport>

Write the model back out as the file set it was read from.

One output file per loaded header, carrying that header, its profiles and the objects read from that file (Dataset::objects_from), so a merged model with two Equipment files writes two. Objects built programmatically go with the first header whose profiles can carry them.

A header without a resolvable profile is skipped and named in SaveReport::skipped; objects only it held are counted in SaveReport::unwritten. Dataset::save_all_profiles always writes something.

Source§

impl Dataset

Source

pub fn load_zip( &mut self, path: &Path, options: &ReadOptions, ) -> Result<LoadReport>

Load every CIM/XML entry in a zip archive.

Entries are read in name order and share one input budget.

Source

pub fn save_zip(&self, path: &Path, stem: &str) -> Result<Vec<String>>

Write every populated profile into one zip archive.

Source§

impl Dataset

Source

pub fn view<'a, T: TypedView<'a>>(&'a self) -> impl Iterator<Item = T> + 'a

Every object of T’s class, including subclasses, as typed views.

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

let dataset = Dataset::new(SCHEMA);
for line in dataset.view::<ACLineSegment>() {
    println!("{:?} r={:?}", line.name(), line.r());
}
Source

pub fn view_by_mrid<'a, T: TypedView<'a>>(&'a self, mrid: &Mrid) -> Option<T>

Look up one object by mRID as a typed view, checking its class.

Source

pub fn count_view<'a, T: TypedView<'a>>(&'a self) -> usize

Count objects of T’s class.

Trait Implementations§

Source§

impl Debug for Dataset

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Dataset

§

impl Freeze for Dataset

§

impl Move for Dataset

§

impl RefUnwindSafe for Dataset

§

impl Send for Dataset
where &'static Schema: Send, RawVecInner: Send, BoxRaw<str>: Send, BoxRaw<(f64, Box<str>)>: Send, BoxRaw<Compound>: Send, RawTable<(Mrid, ObjectId)>: Send,

§

impl Sync for Dataset
where RawVecInner: Sync, BoxRaw<str>: Sync, BoxRaw<(f64, Box<str>)>: Sync, BoxRaw<Compound>: Sync, RawTable<(Mrid, ObjectId)>: Sync,

§

impl Unpin for Dataset
where &'static Schema: Unpin, *const u8 is !null: Unpin, Box<str>: Unpin, Box<(f64, Box<str>)>: Unpin, Box<Compound>: Unpin,

§

impl UnsafeUnpin for Dataset

§

impl UnwindSafe for Dataset
where &'static Schema: UnwindSafe, RawVecInner: UnwindSafe, BoxRaw<str>: UnwindSafe, BoxRaw<(f64, Box<str>)>: UnwindSafe, BoxRaw<Compound>: UnwindSafe, HashMap<Mrid, ObjectId>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.