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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Structural validation derived from the schema.
//!
//! The checks the RDFS vocabulary justifies: cardinality, datatype, class membership,
//! reference targets, profile membership and identifier conformance. ENTSO-E's SHACL
//! rules are out of scope; export with [`rdf`](crate::rdf) and use a SHACL engine.

use std::collections::BTreeMap;

use crate::dataset::Dataset;
use crate::error::{Diagnostic, Report, Rule, Severity};
use crate::header::ModelHeader;
use crate::mrid::{Mrid, MridForm};
use crate::object::Object;
use crate::schema::{AttrId, AttrKind, ProfileId, ProfileMask, Schema};
use crate::value::Value;

/// Which checks to run.
#[derive(Clone, Debug)]
pub struct ValidateOptions {
    /// Check that required attributes are present.
    pub required_attributes: bool,
    /// Check that multiplicities are respected.
    pub cardinality: bool,
    /// Check that references resolve within the dataset.
    pub references: bool,
    /// Check that a reference points at an object of the declared target class.
    pub reference_targets: bool,
    /// Check that every stored value's attribute belongs to the object's class.
    ///
    /// The one check about what the *store* holds rather than about what a document said.
    /// Everything else walks the class's attribute list, so a value filed under an
    /// attribute the class does not have is examined by nothing and dropped on export.
    pub class_membership: bool,
    /// Check that each stored value has the shape its attribute declares.
    ///
    /// CIM/XML carries no datatype information, so this is only decidable against the
    /// RDFS — which is exactly why ENTSO-E's own guidance tells importers to enrich the
    /// data graph from the profile before validating it.
    pub datatypes: bool,
    /// Check attributes the schema pins with `cims:isFixed`.
    pub fixed_values: bool,
    /// Check that identifiers conform to IEC 61970-552.
    pub mrid_conformance: bool,
    /// Report use of deprecated classes and attributes.
    pub deprecation: bool,
    /// Check that headers declare dependencies that were actually loaded.
    pub dependencies: bool,
    /// Report attributes whose data belongs to a profile none of the loaded files
    /// declares — a header that under-declares what its file contains.
    pub profile_reach: bool,
    /// Check each loaded file's header against the structural requirements of
    /// IEC 61970-552.
    pub headers: bool,
    /// Report single-valued attributes two loaded files gave different values for.
    ///
    /// Recorded by [`Dataset`] as the files are merged, because by the time validation
    /// runs one of the two values is gone.
    pub merge_conflicts: bool,
    /// Check that the model can actually be serialized as CIM/XML.
    ///
    /// Text values must hold only characters XML 1.0 can represent (`CIM0019`), and
    /// identifiers must have an `rdf:ID` or `rdf:about` form (`CIM0020`). Always true for
    /// conforming input; reachable from a mis-encoded file or
    /// [`Object::set`](crate::Object::set). See [`xml`](crate::xml).
    pub serializable: bool,
    /// Restrict checks to these profiles. Empty means "the profiles the headers declare",
    /// falling back to every profile the schema knows.
    pub profiles: ProfileMask,
    /// Stop after this many diagnostics. Prevents a broken file producing millions.
    pub max_diagnostics: usize,
}

impl Default for ValidateOptions {
    fn default() -> Self {
        ValidateOptions {
            required_attributes: true,
            cardinality: true,
            class_membership: true,
            references: true,
            reference_targets: true,
            datatypes: true,
            fixed_values: true,
            mrid_conformance: true,
            deprecation: false,
            dependencies: true,
            profile_reach: true,
            headers: true,
            merge_conflicts: true,
            serializable: true,
            profiles: 0,
            max_diagnostics: 10_000,
        }
    }
}

impl ValidateOptions {
    /// Only the checks that indicate genuinely broken data.
    pub fn essential() -> ValidateOptions {
        ValidateOptions {
            required_attributes: false,
            cardinality: true,
            // A value nothing will ever look at is broken data by any definition.
            class_membership: true,
            references: true,
            reference_targets: true,
            datatypes: true,
            fixed_values: false,
            mrid_conformance: false,
            deprecation: false,
            dependencies: false,
            profile_reach: false,
            headers: false,
            // A model set whose files contradict each other is broken data by any
            // definition, so this stays on even in the reduced set.
            merge_conflicts: true,
            // So is a model that cannot be written back out.
            serializable: true,
            ..Default::default()
        }
    }

    /// Every check, including advisory ones.
    pub fn thorough() -> ValidateOptions {
        ValidateOptions {
            deprecation: true,
            ..Default::default()
        }
    }

    pub fn for_profiles(mut self, profiles: ProfileMask) -> Self {
        self.profiles = profiles;
        self
    }
}

/// Validate a dataset with default options.
pub fn validate(dataset: &Dataset) -> Report {
    validate_with(dataset, &ValidateOptions::default())
}

/// Validate a dataset.
pub fn validate_with(dataset: &Dataset, options: &ValidateOptions) -> Report {
    let schema = dataset.schema();
    let mut report = Report::default();

    // Which profiles to judge required-ness against. An attribute that is mandatory in
    // Equipment must not be reported as missing when only a Topology file is loaded.
    let scope = if options.profiles != 0 {
        options.profiles
    } else {
        let declared = dataset.profiles();
        if declared != 0 {
            declared
        } else {
            ProfileMask::MAX
        }
    };

    if options.headers {
        for h in dataset.headers() {
            report.extend(validate_header(h));
        }
    }
    if options.dependencies {
        check_dependencies(dataset, &mut report);
    }
    if options.profile_reach {
        check_profile_reach(dataset, &mut report);
    }
    if options.merge_conflicts {
        check_merge_conflicts(dataset, &mut report);
    }

    for (_, obj) in dataset.iter() {
        if report.len() >= options.max_diagnostics {
            report.push(Diagnostic::warning(
                Rule::Structure,
                format!(
                    "validation stopped after {} diagnostics",
                    options.max_diagnostics
                ),
            ));
            break;
        }
        check_object(schema, dataset, obj, scope, options, &mut report);
    }

    report
}

fn check_object(
    schema: &'static Schema,
    dataset: &Dataset,
    obj: &Object,
    scope: ProfileMask,
    options: &ValidateOptions,
    report: &mut Report,
) {
    let class = schema.class(obj.class());
    let mrid = obj.mrid();

    if options.mrid_conformance && !mrid.is_conforming() {
        report.push(
            Diagnostic::warning(Rule::NonConformingMrid, mrid_complaint(mrid))
                .with_class(class.name)
                .with_object(mrid.clone()),
        );
    }

    if options.serializable {
        check_serializable(schema, obj, class.name, mrid, report);
    }

    if !class.concrete {
        report.push(
            Diagnostic::error(
                Rule::AbstractInstantiated,
                format!("abstract class {} has an instance", class.name),
            )
            .with_class(class.name)
            .with_object(mrid.clone()),
        );
    }

    if options.deprecation && class.deprecated {
        report.push(
            Diagnostic::info(
                Rule::Deprecated,
                format!("class {} is deprecated", class.name),
            )
            .with_class(class.name)
            .with_object(mrid.clone()),
        );
    }

    // A value stored under an attribute this class does not have is otherwise *invisible*:
    // every other check below walks `class.all_attrs`, and so does the writer, so such a
    // value is never examined and is silently dropped on the next export. The reader
    // refuses to store one and so does `apply_difference`, but `Object::set` is public and
    // a class can be reclassified out from under its values, so the finished model is
    // checked rather than only the paths into it.
    if options.class_membership {
        for slot in obj.slots() {
            // A compound is an object in miniature, so the same question applies to its
            // fields — and the consequence is worse there. An object's stray value is
            // dropped by the writer's schema-ordered walk; a compound's fields are written
            // exactly as held, so a foreign one leaves inside a document that is
            // well-formed XML and not a conforming CGMES file.
            check_compound_membership(schema, &slot.value, class.name, mrid, report);

            let def = schema.attr(slot.attr);
            if !schema.is_a(obj.class(), def.owner) {
                report.push(
                    Diagnostic::error(
                        Rule::UnknownAttribute,
                        format!(
                            "{} is declared on {} and cannot be stored on a {}; it would be \
                             dropped on export",
                            def.name,
                            schema.class(def.owner).name,
                            class.name
                        ),
                    )
                    .with_class(class.name)
                    .with_object(mrid.clone())
                    .with_attribute(def.name),
                );
            }
        }
    }

    for attr_id in class.all_attrs {
        let def = schema.attr(*attr_id);
        let in_scope = def.profiles & scope != 0;
        let count = obj.count(*attr_id) as u32;

        if count == 0 {
            // An association side marked `AssociationUsed = No` is never written; it is
            // derived by inverting the other side, so its absence is not a defect.
            let derived = matches!(def.kind, AttrKind::Association { .. }) && def.used_in == 0;
            if options.required_attributes && in_scope && def.mult.is_required() && !derived {
                // Judge only with enough of the model loaded: the object participates in
                // a profile declaring the attribute, and some loaded file *defines* its
                // class (a Topology file referring to an EQ `ConnectivityNode` need not
                // carry its mandatory attributes).
                let object_in_profile = obj.profiles() == 0 || obj.profiles() & def.profiles != 0;
                //
                // A class that no profile both introduces and declares concrete is never
                // the subject of a definition at all — `Equipment` is abstract in the
                // Equipment profile and concrete only in Steady State Hypothesis, where it
                // is the generic carrier for `inService` — so there is no file whose
                // absence or presence would settle the question.
                let defining = class.defined_in & class.concrete_in;
                let definition_loaded = defining & scope != 0;
                if object_in_profile && definition_loaded {
                    report.push(
                        Diagnostic::error(
                            Rule::MissingRequired,
                            format!("required attribute {} is missing", def.name),
                        )
                        .with_class(class.name)
                        .with_object(mrid.clone())
                        .with_attribute(def.name),
                    );
                }
            }
            continue;
        }

        if options.cardinality && count > def.mult.max() {
            report.push(
                Diagnostic::error(
                    Rule::CardinalityExceeded,
                    format!(
                        "{} occurs {} times but permits at most {}",
                        def.name,
                        count,
                        def.mult.max()
                    ),
                )
                .with_class(class.name)
                .with_object(mrid.clone())
                .with_attribute(def.name),
            );
        }
        // A bounded lower limit above one — `M:2..2` and friends — is not covered by the
        // "required attribute is missing" check, which only sees absence.
        if options.cardinality && in_scope && count < def.mult.min() && count > 0 {
            report.push(
                Diagnostic::error(
                    Rule::MissingRequired,
                    format!(
                        "{} occurs {} times but requires at least {}",
                        def.name,
                        count,
                        def.mult.min()
                    ),
                )
                .with_class(class.name)
                .with_object(mrid.clone())
                .with_attribute(def.name),
            );
        }

        if options.datatypes || options.fixed_values {
            for slot in obj.get_all(*attr_id) {
                check_value(
                    schema,
                    &slot.value,
                    *attr_id,
                    class.name,
                    mrid,
                    options,
                    report,
                );
            }
        }

        if options.deprecation && def.deprecated {
            report.push(
                Diagnostic::info(
                    Rule::Deprecated,
                    format!("attribute {} is deprecated", def.name),
                )
                .with_class(class.name)
                .with_object(mrid.clone())
                .with_attribute(def.name),
            );
        }

        // Reference checks.
        if let AttrKind::Association { target, .. } = def.kind {
            for slot in obj.get_all(*attr_id) {
                let Value::Reference(m) = &slot.value else {
                    continue;
                };
                match dataset.by_mrid(m) {
                    None => {
                        if options.references {
                            report.push(
                                Diagnostic::warning(
                                    Rule::DanglingReference,
                                    format!(
                                        "{} points at {} which is not in the dataset",
                                        def.name,
                                        m.canonical()
                                    ),
                                )
                                .with_class(class.name)
                                .with_object(mrid.clone())
                                .with_attribute(def.name),
                            );
                        }
                    }
                    Some(t) => {
                        // An object may be known only through a base class: a Steady State
                        // Hypothesis file writes `<cim:Equipment rdf:about="…">` for a
                        // breaker whose own class only the Equipment file declares. Until
                        // that file is loaded the object *is* an `Equipment`, and that is
                        // not evidence it is the wrong kind of thing — only two classes on
                        // different branches are.
                        let compatible =
                            schema.is_a(t.class(), target) || schema.is_a(target, t.class());
                        if options.reference_targets && !compatible {
                            report.push(
                                Diagnostic::error(
                                    Rule::WrongReferenceTarget,
                                    format!(
                                        "{} must point at {} but {} is a {}",
                                        def.name,
                                        schema.class(target).name,
                                        m.canonical(),
                                        schema.class(t.class()).name
                                    ),
                                )
                                .with_class(class.name)
                                .with_object(mrid.clone())
                                .with_attribute(def.name),
                            );
                        }
                    }
                }
            }
        }
    }
}

/// Report fields a compound's class does not declare, at any nesting depth.
fn check_compound_membership(
    schema: &'static Schema,
    value: &Value,
    class: &'static str,
    mrid: &Mrid,
    report: &mut Report,
) {
    let Value::Compound(c) = value else { return };
    for (attr, v) in c.values() {
        let def = schema.attr(*attr);
        if !schema.is_a(c.class(), def.owner) {
            report.push(
                Diagnostic::error(
                    Rule::UnknownAttribute,
                    format!(
                        "{} is declared on {} and cannot be stored on a {} compound; it \
                         would be written into the document anyway, which no consumer of \
                         that profile will accept",
                        def.name,
                        schema.class(def.owner).name,
                        schema.class(c.class()).name
                    ),
                )
                .with_class(class)
                .with_object(mrid.clone())
                .with_attribute(def.name),
            );
        }
        // Compounds nest: a `StreetAddress` holds a `StreetDetail`.
        check_compound_membership(schema, v, class, mrid, report);
    }
}

/// Check that an object can be written back out as CIM/XML.
///
/// The writer already strips illegal characters and picks a valid identifier form; this
/// tells the caller, per object, before an export differs from what was put in.
fn check_serializable(
    schema: &'static Schema,
    obj: &Object,
    class: &'static str,
    mrid: &Mrid,
    report: &mut Report,
) {
    use crate::xml::{IdentifierForm, describe_char, find_illegal};

    if mrid.form_in_xml() == IdentifierForm::Unwritable {
        report.push(
            Diagnostic::error(
                Rule::UnserializableIdentifier,
                format!(
                    "identifier {:?} is neither an XML NCName nor an absolute IRI, so it has \
                     no valid rdf:ID or rdf:about form; the document written will be \
                     well-formed XML but not valid RDF/XML",
                    mrid.as_written()
                ),
            )
            .with_class(class)
            .with_object(mrid.clone()),
        );
    }

    // Only text carries characters; enumerations, references and compounds serialize
    // through forms of their own — except that a compound's fields are text again.
    fn scan(
        schema: &'static Schema,
        value: &Value,
        attr: AttrId,
        class: &'static str,
        mrid: &Mrid,
        report: &mut Report,
    ) {
        match value {
            Value::Text(s) => {
                if let Some((offset, c)) = find_illegal(s) {
                    let def = schema.attr(attr);
                    report.push(
                        Diagnostic::error(
                            Rule::IllegalXmlCharacter,
                            format!(
                                "{} holds {}, which XML 1.0 cannot represent in any form; it \
                                 will be dropped on export (first at byte {offset} of the value)",
                                def.name,
                                describe_char(c)
                            ),
                        )
                        .with_class(class)
                        .with_object(mrid.clone())
                        .with_attribute(def.name),
                    );
                }
            }
            Value::Compound(c) => {
                for (a, v) in c.values() {
                    scan(schema, v, *a, class, mrid, report);
                }
            }
            _ => {}
        }
    }

    for slot in obj.slots() {
        scan(schema, &slot.value, slot.attr, class, mrid, report);
    }
}

/// Check one stored value against what its attribute declares, recursing into compounds.
///
/// This is the check CIM/XML cannot make for itself. IEC 61970-552 exchanges no datatype
/// information, so a parser without the profile reads every value as a string; only the
/// RDFS says that `ACLineSegment.r` is a float or that `Terminal.phases` must be a
/// `PhaseCode`, and only here can a value that is neither be caught.
fn check_value(
    schema: &'static Schema,
    value: &Value,
    attr: AttrId,
    class: &'static str,
    mrid: &Mrid,
    options: &ValidateOptions,
    report: &mut Report,
) {
    let def = schema.attr(attr);
    let mut mismatch = |expected: &str, found: &str| {
        report.push(
            Diagnostic::error(
                Rule::DatatypeMismatch,
                format!("{} expects {expected} but holds {found}", def.name),
            )
            .with_class(class)
            .with_object(mrid.clone())
            .with_attribute(def.name),
        );
    };

    if options.datatypes {
        match def.kind {
            AttrKind::Primitive(p) => {
                if let Some(found) = primitive_mismatch(p, value) {
                    mismatch(&format!("{p:?}"), found);
                }
            }
            AttrKind::Datatype(dt) => {
                let p = schema.datatype(dt).value;
                if let Some(found) = primitive_mismatch(p, value) {
                    mismatch(&format!("{} ({p:?})", schema.datatype(dt).name), found);
                }
            }
            AttrKind::Enumeration(want) => match value {
                // A literal recovered from the wrong namespace can belong to a different
                // enumeration; the qualified name alone cannot rule that out.
                Value::Enum(v) if schema.enum_value(*v).owner != want => mismatch(
                    schema.enumeration(want).name,
                    schema.enumeration(schema.enum_value(*v).owner).name,
                ),
                Value::Enum(_) => {}
                other => mismatch(schema.enumeration(want).name, shape_of(other)),
            },
            AttrKind::Association { .. } => {
                if !matches!(value, Value::Reference(_)) {
                    mismatch("a reference", shape_of(value));
                }
            }
            AttrKind::Compound(want) => match value {
                Value::Compound(c) if c.class() != want => {
                    mismatch(schema.class(want).name, schema.class(c.class()).name)
                }
                Value::Compound(c) => {
                    // Compounds nest, and their fields are ordinary attributes.
                    for (a, v) in c.values() {
                        check_value(schema, v, *a, class, mrid, options, report);
                    }
                }
                other => mismatch(schema.class(want).name, shape_of(other)),
            },
        }
    }

    if options.fixed_values
        && let Some(fixed) = def.fixed
    {
        let actual = crate::value::display_value(schema, value);
        // A fixed value is compared as text: the schema pins the lexical form.
        if actual != fixed && !actual.is_empty() {
            report.push(
                Diagnostic::warning(
                    Rule::FixedValueMismatch,
                    format!("{} is fixed to {fixed:?} but holds {actual:?}", def.name),
                )
                .with_class(class)
                .with_object(mrid.clone())
                .with_attribute(def.name),
            );
        }
    }
}

/// Whether `value` can serve as `p`, and what it is if not.
///
/// The compatibility rule itself lives on [`Value::fits`], because the RDF writer has to
/// answer the same question — whether to label a literal with the type the profile
/// declares — and the two must not drift apart.
fn primitive_mismatch(p: crate::schema::Primitive, value: &Value) -> Option<&'static str> {
    (!value.fits(p)).then(|| value.shape())
}

fn shape_of(v: &Value) -> &'static str {
    v.shape()
}

/// Report values whose own file declared no profile that declares the attribute.
///
/// A header/content mismatch (e.g. an Equipment file carrying ShortCircuit attributes
/// without declaring that profile), checked against each value's own file, not the union.
/// Aggregated **per attribute**, not per value.
fn check_profile_reach(dataset: &Dataset, report: &mut Report) {
    let schema = dataset.schema();
    // Attribute -> (number of values affected, an example object).
    let mut undeclared: BTreeMap<AttrId, (usize, Mrid)> = BTreeMap::new();

    for (_, obj) in dataset.iter() {
        for slot in obj.slots() {
            if !schema.declaration_covers(slot.attr, slot.profiles) {
                let entry = undeclared
                    .entry(slot.attr)
                    .or_insert_with(|| (0, obj.mrid().clone()));
                entry.0 += 1;
            }
        }
    }

    for (attr, (count, example)) in undeclared {
        let def = schema.attr(attr);
        let profiles: Vec<&str> = schema
            .profiles
            .iter()
            .enumerate()
            // `ProfileId::mask` rather than a bare shift, which Rust masks in release
            // builds: an over-wide shift would name the wrong profile instead of failing.
            .filter(|(i, _)| def.used_in & ProfileId(*i as u16).mask() != 0)
            .map(|(_, p)| p.keyword)
            .collect();
        report.push(
            Diagnostic::warning(
                Rule::AttributeNotInProfile,
                format!(
                    "{} is set on {count} object(s) by files whose header does not declare \
                     profile(s) {}, which is where the schema puts it",
                    def.name,
                    profiles.join("+")
                ),
            )
            .with_class(schema.class(def.owner).name)
            .with_object(example)
            .with_attribute(def.name),
        );
    }
}

/// Report the disagreements that assembling the model set had to resolve.
///
/// Read back from [`Dataset`], which records conflicts while merging. A warning, not an
/// error: a change file loaded over its base produces them legitimately.
fn check_merge_conflicts(dataset: &Dataset, report: &mut Report) {
    let schema = dataset.schema();
    for c in dataset.merge_conflicts() {
        let def = schema.attr(c.attr);
        report.push(
            Diagnostic::warning(
                Rule::ConflictingValue,
                format!(
                    "{} was given {} by one file and {} by another; the later value is kept",
                    def.name,
                    crate::value::display_value(schema, &c.discarded),
                    crate::value::display_value(schema, &c.kept),
                ),
            )
            .with_class(schema.class(def.owner).name)
            .with_object(c.object.clone())
            .with_attribute(def.name),
        );
    }
    let dropped = dataset.merge_conflicts_dropped();
    if dropped > 0 {
        report.push(Diagnostic::warning(
            Rule::ConflictingValue,
            format!("{dropped} further conflicting value(s) were counted but not recorded"),
        ));
    }
}

/// Check that every `md:Model.DependentOn` was satisfied by a loaded file.
fn check_dependencies(dataset: &Dataset, report: &mut Report) {
    let loaded: Vec<&Mrid> = dataset
        .headers()
        .iter()
        .filter_map(|h| h.id.as_ref())
        .collect();
    for h in dataset.headers() {
        for dep in &h.dependent_on {
            if !loaded.contains(&dep) {
                report.push(
                    Diagnostic::warning(
                        Rule::UnsatisfiedDependency,
                        format!("model depends on {} which was not loaded", dep.canonical()),
                    )
                    .with_source(h.source.clone().unwrap_or_else(|| "<header>".into())),
                );
            }
        }
    }
}

/// Say precisely what is wrong with an identifier IEC 61970-552 would reject.
///
/// The two cases call for different action and must not read alike: a compact UUID is the
/// right value in the wrong spelling — every tool can still resolve it, and re-hyphenating
/// it fixes the file — while an opaque identifier has no UUID behind it at all, so it has
/// no `urn:uuid:` IRI and no amount of reformatting will give it one.
fn mrid_complaint(mrid: &Mrid) -> String {
    match mrid.form() {
        MridForm::Uuid => format!("identifier {:?} conforms", mrid.canonical()),
        MridForm::Compact => format!(
            "identifier {:?} is a UUID written without hyphens; IEC 61970-552 requires \
             the {:?} form",
            mrid.as_written(),
            mrid.canonical()
        ),
        MridForm::Opaque => format!(
            "identifier {:?} is not a UUID, which IEC 61970-552 requires",
            mrid.canonical()
        ),
    }
}

/// Check a set of headers for the structural requirements of IEC 61970-552.
pub fn validate_header(header: &ModelHeader) -> Report {
    let mut report = Report::default();
    let src = header.source.clone().unwrap_or_else(|| "<header>".into());

    if header.id.is_none() {
        report.push(
            Diagnostic::error(Rule::MalformedHeader, "header has no rdf:about identifier")
                .with_source(src.clone()),
        );
    } else if let Some(id) = &header.id
        && !id.is_conforming()
    {
        report.push(
            Diagnostic::warning(
                Rule::MalformedHeader,
                format!("header identifier: {}", mrid_complaint(id)),
            )
            .with_source(src.clone()),
        );
    }
    if header.profiles.is_empty() {
        report.push(
            Diagnostic::error(Rule::MalformedHeader, "header declares no md:Model.profile")
                .with_source(src.clone()),
        );
    }
    if header.scenario_time.is_none() {
        report.push(
            Diagnostic::warning(Rule::MalformedHeader, "header has no md:Model.scenarioTime")
                .with_source(src.clone()),
        );
    }
    if header.created.is_none() {
        report.push(
            Diagnostic::warning(Rule::MalformedHeader, "header has no md:Model.created")
                .with_source(src),
        );
    }
    report
}

/// Summarize a dataset against one profile: what it would export, and what is missing.
#[derive(Clone, Debug)]
pub struct ProfileCoverage {
    pub profile: ProfileId,
    pub keyword: &'static str,
    /// Objects that would appear in an export of this profile.
    pub objects: usize,
    /// Attribute occurrences that would be written.
    pub attributes: usize,
    /// Required attributes of in-profile objects that are absent.
    pub missing_required: usize,
}

/// Report, per profile, how much of it the dataset actually carries.
///
/// One pass over the model rather than one per profile: on a nation-scale model the
/// difference is eleven walks of a quarter of a million objects versus one.
pub fn profile_coverage(dataset: &Dataset) -> Vec<ProfileCoverage> {
    let schema = dataset.schema();
    let n = schema.profiles.len();
    let mut objects = vec![0usize; n];
    let mut attributes = vec![0usize; n];
    let mut missing_required = vec![0usize; n];

    for (_, obj) in dataset.iter() {
        let class = schema.class(obj.class());
        // Profiles this object contributes at least one value to.
        let mut touched: ProfileMask = 0;
        for attr_id in class.all_attrs {
            let def = schema.attr(*attr_id);
            let slots = obj.get_all(*attr_id);
            if slots.is_empty() {
                if def.mult.is_required() {
                    for (i, missing) in missing_required.iter_mut().enumerate() {
                        let mask = ProfileId(i as u16).mask();
                        if class.profiles & mask != 0 && def.used_in & mask != 0 {
                            *missing += 1;
                        }
                    }
                }
                continue;
            }
            // Count exactly what the writer would emit, so the report cannot drift from
            // the export: both go through `effective_profiles`.
            for s in slots {
                let effective = schema.effective_profiles(s.attr, s.profiles);
                for (i, count) in attributes.iter_mut().enumerate() {
                    let mask = ProfileId(i as u16).mask();
                    if effective & mask != 0 && class.profiles & mask != 0 {
                        *count += 1;
                        touched |= mask;
                    }
                }
            }
        }
        for (i, count) in objects.iter_mut().enumerate() {
            if touched & ProfileId(i as u16).mask() != 0 {
                *count += 1;
            }
        }
    }

    schema
        .profiles
        .iter()
        .enumerate()
        .map(|(i, p)| ProfileCoverage {
            profile: ProfileId(i as u16),
            keyword: p.keyword,
            objects: objects[i],
            attributes: attributes[i],
            missing_required: missing_required[i],
        })
        .collect()
}

/// Convenience: does this report block a conforming export?
pub fn is_conforming(report: &Report) -> bool {
    report.count(Severity::Error) == 0
}