Skip to main content

cimvalidation/
shapes.rs

1//! The shape table both families are validated against.
2//!
3//! Elements are property bags ([`cimmodel::Element`]) with no struct to
4//! downcast to, so the shapes are data and [`crate::bag`] interprets them, the
5//! same way classes are [`cimmodel::base::ClassDef`] rows interpreted by the
6//! decoder. `cimgen` emits the tables into `cgmes_shapes.rs` and
7//! `nc_shapes.rs`; with the `dynamic-shapes` feature they can be built from the
8//! SHACL TTL files at runtime instead.
9//!
10//! Everything a consumer would have to compute per element is resolved when
11//! the table is built: target classes are already family-qualified
12//! `CimDataset::by_type` keys with abstract classes expanded to their concrete
13//! descendants, and paths are already the field keys the decoder stores.
14
15/// Where a property shape's values come from.
16///
17/// Field keys are the local XML element name with the prefix stripped, which
18/// is exactly what the decoder puts in [`cimmodel::Element::fields`].
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum Path {
21    /// A field on the element itself.
22    Forward(&'static str),
23    /// `sh:inversePath` — the field on whatever points *at* this element.
24    /// Needs the reverse index, which is why [`ShapeSet`] tracks whether any
25    /// active shape uses one.
26    Inverse(&'static str),
27    /// `sh:alternativePath` — the union of every branch's values. A branch may
28    /// itself be inverse, hence [`AltBranch`] rather than a plain name.
29    Alternative(&'static [AltBranch]),
30    /// A sequence path: `( ^cim:Terminal.ConductingEquipment
31    /// cim:Terminal.phases )`, `( cim:Location.mainAddress
32    /// cim:StreetAddress.status cim:Status.dateTime )`, and — 170 of NCP's 178
33    /// chains, its main association value-type check — `( nc:X.y rdf:type )`.
34    ///
35    /// Stepping *from* an element absent from the dataset makes the result
36    /// unknown and every check on it silent: phase 1 runs per file, so leaving
37    /// the file is normal. A final `rdf:type` step needs nothing from the
38    /// elements it reaches, so they count even when absent, and class checks
39    /// skip the absent ones, as [`Constraint::RefClass`] describes.
40    ///
41    /// The referenced element may belong to *either* family: NCP's value-type
42    /// lists name CGMES classes (`cim17:ACLineSegment`) alongside NC ones, and
43    /// `Element::type_name` answers for both.
44    Chain(&'static [Step]),
45}
46
47/// One step of a [`Path::Chain`].
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum Step {
50    Forward(&'static str),
51    /// Needs the reverse index, like [`Path::Inverse`], but as a list of the
52    /// elements pointing here rather than a count.
53    Inverse(&'static str),
54    /// `rdf:type`, last only. Its values are the elements reached; the
55    /// constraints on such a path (`RefClass`, counts) read their classes.
56    Type,
57}
58
59/// One branch of an [`Path::Alternative`].
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum AltBranch {
62    Forward(&'static str),
63    Inverse(&'static str),
64}
65
66/// `sh:nodeKind`, restricted to the values the profiles actually use.
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum NodeKind {
69    /// Written as `rdf:resource`.
70    Iri,
71    /// Written as element text.
72    Literal,
73    BlankNode,
74}
75
76/// A single check on the values a [`Path`] yields.
77#[derive(Debug, Clone, Copy, PartialEq)]
78pub enum Constraint {
79    MinCount(u32),
80    MaxCount(u32),
81    /// The xsd type's local name (`"integer"`, `"dateTime"`, …). A bag holds
82    /// strings, so this is a real parse check rather than the tautology it was
83    /// against a typed struct.
84    Datatype(&'static str),
85    NodeKind(NodeKind),
86    /// `sh:class` — the referenced element must be an instance of this
87    /// family-qualified class, or a descendant of it. Already expanded, so the
88    /// check is a set membership.
89    Class(&'static [&'static str]),
90    /// `sh:in` — the value must be one of these. Enum IRIs are stored as the
91    /// decoder stores them: fragment only.
92    In(&'static [&'static str]),
93    /// `sh:hasValue`, and `sh:in` with exactly one member after
94    /// simplification.
95    HasValue(&'static str),
96    MaxLength(u32),
97    MinLength(u32),
98    /// The referenced element's class must be one of these family-qualified
99    /// names. Produced by `sh:in` on a path ending in `rdf:type`.
100    ///
101    /// Only checked when the referenced element is **present**. Phase-1
102    /// validation runs per file, so a reference out of the current file is
103    /// normal and is a different rule's business — reporting it here would make
104    /// every cross-file association a value-type violation.
105    RefClass(&'static [&'static str]),
106    /// `sh:minInclusive` and friends. Compared as `f64`; a value that does not
107    /// parse as a number is skipped, the way a typed numeric field that failed
108    /// to parse is `None` — malformed numbers are `sh:datatype`'s business.
109    MinInclusive(f64),
110    MaxInclusive(f64),
111    MinExclusive(f64),
112    MaxExclusive(f64),
113    /// `sh:lessThan` — this property's value must be below the named field's
114    /// on the same element. Only checked when both are present and numeric.
115    LessThan(&'static str),
116    LessThanOrEquals(&'static str),
117    /// `sh:not [ sh:class X ]` — the referenced element must not be an
118    /// instance of these family-qualified classes. Silent when the referenced
119    /// element is absent, like [`Constraint::Class`].
120    NotClass(&'static [&'static str]),
121    /// `sh:qualifiedValueShape [ sh:in (..) ]` with `sh:qualifiedMinCount` —
122    /// at least `min` values must be among `allowed`. NCP uses it to ask
123    /// whether a dataset declares conformance to one of a set of profiles.
124    QualifiedIn {
125        allowed: &'static [&'static str],
126        min: u32,
127    },
128    /// `sh:length` — exactly this many characters, e.g. the 16 of an EIC code.
129    Length(u32),
130}
131
132/// One constraint together with how to report it.
133///
134/// SHACL attaches `sh:message`, `sh:name` and `sh:severity` to the constraint,
135/// not to the property shape, and one property shape routinely carries several
136/// constraints with different messages — a cardinality rule and a value-range
137/// rule on the same path. Pairing them here keeps the reported text the one the
138/// schema wrote for that specific check.
139#[derive(Debug)]
140pub struct Check {
141    pub constraint: Constraint,
142    /// Shape IRI, the stable machine-readable rule id a caller can silence.
143    pub rule_id: &'static str,
144    pub name: &'static str,
145    pub message: &'static str,
146    pub description: &'static str,
147    /// `"sh:Violation"`, `"sh:Warning"` or `"sh:Info"`. NC leans on `sh:Info`
148    /// far more than CGMES does — 842 occurrences against 7 — so a caller that
149    /// treats every violation as a failure will report an NC dataset as broken
150    /// when it is merely being advised.
151    pub severity: &'static str,
152}
153
154/// One `sh:property` of a node shape: a path, and what must hold of its values.
155#[derive(Debug)]
156pub struct PropShape {
157    pub path: Path,
158    pub checks: &'static [Check],
159}
160
161/// What a node shape applies to.
162#[derive(Debug)]
163pub enum Target {
164    /// Family-qualified `by_type` keys. An abstract target is expanded to its
165    /// concrete descendants when the table is built — exact-match lookup on an
166    /// abstract class would silently check nothing.
167    Class(&'static [&'static str]),
168    /// `sh:targetSubjectsOf` — every element carrying this field.
169    SubjectsOf(&'static str),
170}
171
172/// How a [`Logic`] combines its branches.
173#[derive(Debug, Clone, Copy, PartialEq, Eq)]
174pub enum LogicOp {
175    And,
176    Or,
177    Xone,
178}
179
180/// One branch of a [`Logic`]. It conforms when none of its checks fail — or,
181/// for `[ sh:not X ]` (`negate`), when at least one does. NCP writes material
182/// implication that way: `sh:or ( [ sh:not conformsToNCProfile ] [ P required ] )`.
183#[derive(Debug)]
184pub struct Branch {
185    pub props: &'static [PropShape],
186    pub negate: bool,
187}
188
189/// A node-level `sh:and` / `sh:or` / `sh:xone`. The shape reports once, with
190/// its own name and message, when the combination does not hold; the branch
191/// checks carry no report text.
192#[derive(Debug)]
193pub struct Logic {
194    pub op: LogicOp,
195    pub branches: &'static [Branch],
196    pub rule_id: &'static str,
197    pub name: &'static str,
198    pub message: &'static str,
199    pub description: &'static str,
200    pub severity: &'static str,
201}
202
203/// One `sh:NodeShape`.
204#[derive(Debug)]
205pub struct ShapeDef {
206    pub targets: &'static [Target],
207    /// Borrowed rather than owned: NCP defines ~4,200 distinct property shapes
208    /// and references them ~14,800 times, because a shape like
209    /// `IdentifiedObject.mRID-cardinality` applies to hundreds of classes.
210    /// Sharing one definition per shape keeps the generated table a third of
211    /// the size it would otherwise be.
212    pub props: &'static [&'static PropShape],
213    /// `sh:closed` — the allowed field keys. An element carrying any other key
214    /// is reported.
215    ///
216    /// This is the constraint that only a property bag can answer: generated
217    /// structs drop unknown properties at decode, leaving nothing to compare
218    /// against. 823 NCP shapes use it.
219    pub closed: Option<&'static ClosedShape>,
220    /// Node-level `sh:and` / `sh:or` / `sh:xone`.
221    pub logic: &'static [Logic],
222    /// Every NC profile code whose manifest imports the file this shape came
223    /// from. Plural because four shared constraint files are imported by 17 or
224    /// 18 of the 18 manifests.
225    pub profiles: &'static [&'static str],
226    /// TTL base file name, for reports.
227    pub file: &'static str,
228}
229
230/// The `sh:closed` half of a shape, kept separate because it carries its own
231/// message and severity.
232#[derive(Debug)]
233pub struct ClosedShape {
234    pub allowed: &'static [&'static str],
235    pub rule_id: &'static str,
236    pub name: &'static str,
237    pub message: &'static str,
238    pub description: &'static str,
239    pub severity: &'static str,
240}