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
impl Dataset
Sourcepub const MAX_CONFLICTS: usize = 1000
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.
Sourcepub const DERIVED_NS: Mrid
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.
pub fn new(schema: &'static Schema) -> Dataset
pub fn with_link_policy(self, policy: LinkPolicy) -> Dataset
pub fn schema(&self) -> &'static Schema
pub fn link_policy(&self) -> LinkPolicy
Sourcepub fn headers(&self) -> &[ModelHeader]
pub fn headers(&self) -> &[ModelHeader]
Headers of every file loaded into this dataset, in load order.
Sourcepub fn push_header(&mut self, header: ModelHeader) -> usize
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.
Sourcepub fn set_header(&mut self, index: usize, header: ModelHeader)
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.
Sourcepub fn record_source(&mut self, source: usize, id: ObjectId)
pub fn record_source(&mut self, source: usize, id: ObjectId)
Record that the file in header slot source contained id.
Sourcepub fn objects_from(&self, source: usize) -> impl Iterator<Item = ObjectId> + '_
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).
Sourcepub fn differences(&self) -> &[DifferenceModel]
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.
Sourcepub fn model_ids(&self) -> impl Iterator<Item = &Mrid> + '_
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.
pub fn push_difference(&mut self, diff: DifferenceModel)
Sourcepub fn profiles(&self) -> ProfileMask
pub fn profiles(&self) -> ProfileMask
Profiles present across all loaded headers.
pub fn is_empty(&self) -> bool
pub fn get(&self, id: ObjectId) -> Option<&Object>
pub fn get_mut(&mut self, id: ObjectId) -> Option<&mut Object>
pub fn find(&self, mrid: &Mrid) -> Option<ObjectId>
pub fn by_mrid(&self, mrid: &Mrid) -> Option<&Object>
Sourcepub fn insert(&mut self, object: Object) -> ObjectId
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.
Sourcepub fn merge(&mut self, other: Dataset) -> Result<()>
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.
Sourcepub fn merge_conflicts(&self) -> &[MergeConflict]
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.
Sourcepub fn merge_conflicts_dropped(&self) -> usize
pub fn merge_conflicts_dropped(&self) -> usize
Conflicts that occurred beyond Dataset::MAX_CONFLICTS and were only counted.
Sourcepub fn reclassify(&mut self, id: ObjectId, class: ClassId) -> Vec<Slot>
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.
Sourcepub fn remove(&mut self, id: ObjectId) -> Option<Object>
pub fn remove(&mut self, id: ObjectId) -> Option<Object>
Remove an object. Its ObjectId is never reused.
Sourcepub fn iter_exact(
&self,
class: ClassId,
) -> impl Iterator<Item = (ObjectId, &Object)> + '_
pub fn iter_exact( &self, class: ClassId, ) -> impl Iterator<Item = (ObjectId, &Object)> + '_
Objects of exactly class, excluding subclasses.
Sourcepub fn iter_class(
&self,
class: ClassId,
) -> impl Iterator<Item = (ObjectId, &Object)> + '_
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.
pub fn count_class(&self, class: ClassId) -> usize
Sourcepub fn follow(&self, object: &Object, attr: AttrId) -> Option<&Object>
pub fn follow(&self, object: &Object, attr: AttrId) -> Option<&Object>
Resolve a single-valued association to the referenced object.
Sourcepub fn follow_all<'a>(
&'a self,
object: &'a Object,
attr: AttrId,
) -> impl Iterator<Item = &'a Object> + 'a
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.
Sourcepub fn referrers<'a>(
&'a self,
target: &'a Mrid,
attr: AttrId,
) -> impl Iterator<Item = (ObjectId, &'a Object)> + 'a
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.
Sourcepub fn content_id(&self) -> Mrid
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.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Release unused capacity after bulk loading.
Sourcepub fn dangling_references(&self) -> Vec<(ObjectId, AttrId, Mrid)>
pub fn dangling_references(&self) -> Vec<(ObjectId, AttrId, Mrid)>
References that point at objects not present in the dataset.
Sourcepub fn check_links(&self) -> Result<()>
pub fn check_links(&self) -> Result<()>
Fail if the link policy is LinkPolicy::Strict and references are unresolved.
Source§impl Dataset
impl Dataset
Sourcepub fn validate(&self) -> Report
pub fn validate(&self) -> Report
Validate with the default checks.
Shorthand for validate::validate.
Sourcepub fn validate_with(&self, options: &ValidateOptions) -> Report
pub fn validate_with(&self, options: &ValidateOptions) -> Report
Validate with explicit options.
Source§impl Dataset
impl Dataset
Sourcepub fn difference_to(
&self,
target: &Dataset,
options: &DiffOptions,
) -> DiffReport
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.
Sourcepub fn prune_empty(&mut self) -> usize
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
impl Dataset
Sourcepub fn load<I, P>(schema: &'static Schema, paths: I) -> Result<Dataset>
pub fn load<I, P>(schema: &'static Schema, paths: I) -> Result<Dataset>
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.
Sourcepub fn load_files<I, P>(
&mut self,
paths: I,
options: &ReadOptions,
) -> Result<LoadReport>
pub fn load_files<I, P>( &mut self, paths: I, options: &ReadOptions, ) -> Result<LoadReport>
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 is reported as CIM0022 and listed in LoadReport::failed.
Sourcepub fn load_file(
&mut self,
path: &Path,
options: &ReadOptions,
) -> Result<LoadReport>
pub fn load_file( &mut self, path: &Path, options: &ReadOptions, ) -> Result<LoadReport>
Load one instance file, or every instance file inside a zip archive.
Sourcepub fn load_dir(
schema: &'static Schema,
dir: impl AsRef<Path>,
) -> Result<Dataset>
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.
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Dataset>
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());Sourcepub fn peek_headers<I, P>(
schema: &'static Schema,
paths: I,
) -> Result<Vec<(PathBuf, Option<ModelHeader>)>>
pub fn peek_headers<I, P>( schema: &'static Schema, paths: I, ) -> Result<Vec<(PathBuf, Option<ModelHeader>)>>
Read only the headers of a set of files, without loading their objects.
Useful for deciding what to load: headers declare profiles and dependencies.
Sourcepub fn apply_difference(&mut self, diff: &DifferenceModel) -> Report
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.
Sourcepub fn save_profile(
&self,
path: &Path,
profile: ProfileId,
header: Option<ModelHeader>,
) -> Result<()>
pub fn save_profile( &self, path: &Path, profile: ProfileId, header: Option<ModelHeader>, ) -> Result<()>
Write one profile of this dataset to a file.
Sourcepub fn save_all_profiles(&self, dir: &Path, stem: &str) -> Result<Vec<PathBuf>>
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.
Sourcepub fn save_as_loaded(&self, dir: &Path) -> Result<SaveReport>
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
impl Dataset
Sourcepub fn load_zip(
&mut self,
path: &Path,
options: &ReadOptions,
) -> Result<LoadReport>
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§impl Dataset
impl Dataset
Sourcepub fn view<'a, T: TypedView<'a>>(&'a self) -> impl Iterator<Item = T> + 'a
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());
}Sourcepub fn view_by_mrid<'a, T: TypedView<'a>>(&'a self, mrid: &Mrid) -> Option<T>
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.
Sourcepub fn count_view<'a, T: TypedView<'a>>(&'a self) -> usize
pub fn count_view<'a, T: TypedView<'a>>(&'a self) -> usize
Count objects of T’s class.