cimoxide-view 0.8.0

A CGMES or NC model set as views: its grid at substation level, laid out, and its objects as a tree, with validation findings on each; and its files as text: where each object is written, and edits across them
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! A model set's objects as a tree, for the extension's model view. Its
//! sections:
//! - **Geographical**: regions, their substations and lines, down through
//!   voltage levels, bays and equipment to terminals, limits and state
//!   variables; each object under the object the first of [`CONTAINED`] it
//!   writes names;
//! - **Operational**: control areas and load areas, with what names them
//!   ([`OPERATIONAL`]);
//! - **Not contained**: the rest of that containment, by class: what no
//!   container holds (base voltages, curves, diagrams, …) with what it holds;
//! - **By class**: every object, flat.
//!
//! A parent the set lacks (an IGM's region, which its boundary set names) is
//! an object of its own, labelled with its mRID. Each node counts the
//! findings of its subtree. The view asks for one node's children at a time,
//! by key; more than [`PAGE`] are grouped in ranges.

use cimmodel::base::FastMap;
use cimvalidation::Violation;
use serde_json::{Value, json};

use crate::Locations;
use crate::grid::{Counts, Grid, Model};

/// The fields that name an object's container, the first written winning.
pub const CONTAINED: &[&str] = &[
    "SubGeographicalRegion.Region",
    "Substation.Region",
    "Line.Region",
    "DCLine.Region",
    "VoltageLevel.Substation",
    "Bay.VoltageLevel",
    "DCConverterUnit.Substation",
    "Equipment.EquipmentContainer",
    "ConnectivityNode.ConnectivityNodeContainer",
    "TopologicalNode.ConnectivityNodeContainer",
    "DCNode.DCEquipmentContainer",
    "DCTopologicalNode.DCEquipmentContainer",
    "BoundaryPoint.ConnectivityNode",
    "Terminal.ConductingEquipment",
    "DCTerminal.DCConductingEquipment",
    "ACDCConverterDCTerminal.DCConductingEquipment",
    "PowerTransformerEnd.PowerTransformer",
    "RatioTapChanger.TransformerEnd",
    "PhaseTapChanger.TransformerEnd",
    "RatioTapChangerTablePoint.RatioTapChangerTable",
    "PhaseTapChangerTablePoint.PhaseTapChangerTable",
    "OperationalLimitSet.Equipment",
    "OperationalLimitSet.Terminal",
    "OperationalLimit.OperationalLimitSet",
    "RegulatingControl.Terminal",
    "CurveData.Curve",
    "Measurement.PowerSystemResource",
    "AnalogValue.Analog",
    "DiscreteValue.Discrete",
    "AccumulatorValue.Accumulator",
    "StringMeasurementValue.StringMeasurement",
    "Location.PowerSystemResources",
    "PositionPoint.Location",
    "SvVoltage.TopologicalNode",
    "SvInjection.TopologicalNode",
    "SvPowerFlow.Terminal",
    "SvStatus.ConductingEquipment",
    "SvSwitch.Switch",
    "SvTapStep.TapChanger",
    "SvShuntCompensatorSections.ShuntCompensator",
];

/// What the Geographical section starts from: classes that contain, but
/// need not be contained in the set.
const CONTAINERS: &[&str] = &[
    "GeographicalRegion",
    "SubGeographicalRegion",
    "Substation",
    "Line",
    "DCLine",
    "VoltageLevel",
    "Bay",
    "DCConverterUnit",
    "EquivalentNetwork",
    "Plant",
];

/// The fields that name an object's area, under [`AREAS`].
const OPERATIONAL: &[&str] = &[
    "TieFlow.ControlArea",
    "ControlAreaGeneratingUnit.ControlArea",
    "SubLoadArea.LoadArea",
    "LoadGroup.SubLoadArea",
    "ConformLoad.LoadGroup",
    "NonConformLoad.LoadGroup",
];

const AREAS: &[&str] = &["ControlArea", "LoadArea"];

/// Children a node lists at once; more are grouped in ranges of this size.
const PAGE: usize = 500;

const NONE: u32 = u32::MAX;

/// The class of a group of parents the set lacks.
const MISSING: &str = "";

struct Object {
    id: String,
    /// `IdentifiedObject.name`, if written.
    label: Option<Box<str>>,
    /// Empty for a parent the set lacks.
    class: &'static str,
    findings: Counts,
    /// Its node in the containment and in the operational forest.
    contained: Option<u32>,
    operational: Option<u32>,
}

enum What {
    Section {
        key: &'static str,
        label: &'static str,
    },
    /// Objects of a class: forest nodes in Not contained, leaf objects in By class.
    Group {
        section: &'static str,
        class: &'static str,
    },
    /// An object in the containment (`c`) or operational (`o`) forest.
    Object { object: u32, forest: char },
}

struct Node {
    what: What,
    children: Vec<u32>,
    /// The node listing it; [`NONE`] for a section.
    parent: u32,
    findings: Counts,
}

/// A child as listed: a node, or a leaf object of By class.
#[derive(Clone, Copy)]
enum Entry {
    Node(u32),
    Leaf(u32),
}

#[derive(Default)]
pub struct Tree {
    objects: Vec<Object>,
    by_id: FastMap<String, u32>,
    nodes: Vec<Node>,
    sections: Vec<u32>,
    /// Sections and groups, by key.
    named: FastMap<String, u32>,
}

/// The tree of a model set's files.
pub fn build(m: &Model<'_>) -> Tree {
    let mut t = Tree::default();
    let mut ids: Vec<&str> = m.objects.keys().copied().collect();
    ids.sort_unstable();
    for &id in &ids {
        let label = m
            .text(id, "IdentifiedObject.name")
            .filter(|n| !n.is_empty())
            .map(Box::from);
        t.object(id, label, m.class(id).unwrap_or(""));
    }

    let contained = forest(&mut t, m, &ids, CONTAINED, 'c');
    let mut roots: Vec<u32> = contained.roots.clone();
    let mut geo = Vec::new();
    let mut other: Vec<u32> = Vec::new();
    // A cycle has no root: start from its first object.
    for &id in &ids {
        let o = t.by_id[id];
        if t.objects[o as usize].contained.is_none() {
            roots.push(grow(&mut t, &contained.children, o, 'c'));
        }
    }
    let operational = forest(&mut t, m, &ids, OPERATIONAL, 'o');
    let areas = operational.roots;
    for n in roots {
        let class = t.class_of(n);
        let holds_container = || {
            t.nodes[n as usize]
                .children
                .iter()
                .any(|&c| CONTAINERS.contains(&t.class_of(c)))
        };
        if CONTAINERS.contains(&class) || (class == MISSING && holds_container()) {
            geo.push(n);
        } else {
            other.push(n);
        }
    }

    let geo = t.section("geo", "Geographical", geo);
    let op = t.section("op", "Operational", areas);
    let mut by_class: FastMap<&'static str, Vec<u32>> = FastMap::default();
    for n in other {
        by_class.entry(t.class_of(n)).or_default().push(n);
    }
    let mut groups: Vec<u32> = by_class
        .into_iter()
        .map(|(class, ns)| t.group("other", class, ns))
        .collect();
    groups.sort_by_key(|&g| t.class_of(g));
    let other = t.section("other", "Not contained", groups);
    let mut by_class: FastMap<&'static str, Vec<u32>> = FastMap::default();
    for (o, obj) in t.objects.iter().enumerate() {
        if obj.class != MISSING {
            by_class.entry(obj.class).or_default().push(o as u32);
        }
    }
    let mut groups: Vec<u32> = by_class
        .into_iter()
        .map(|(class, mut os)| {
            os.sort_by_cached_key(|&o| t.label_of(o).to_lowercase());
            t.group("class", class, os)
        })
        .collect();
    groups.sort_by_key(|&g| t.class_of(g));
    let class = t.section("class", "By class", groups);
    t.sections = [geo, op, other, class]
        .into_iter()
        .filter(|&s| !t.nodes[s as usize].children.is_empty())
        .collect();
    t
}

/// A forest built from parent fields: its roots (in the operational forest,
/// the areas), and each object's children.
struct Forest {
    roots: Vec<u32>,
    children: FastMap<u32, Vec<u32>>,
}

/// The forest `keys` make, each object under the target of the first of them
/// it writes, its nodes added to `t`.
fn forest(t: &mut Tree, m: &Model<'_>, ids: &[&str], keys: &[&str], name: char) -> Forest {
    let rank: FastMap<&str, usize> = keys.iter().enumerate().map(|(i, &k)| (k, i)).collect();
    let mut parent: FastMap<u32, u32> = FastMap::default();
    for &id in ids {
        let Some(els) = m.objects.get(id) else {
            continue;
        };
        let best = els
            .iter()
            .flat_map(|el| el.fields().keys())
            .filter_map(|k| rank.get(k).map(|&r| (r, *k)))
            .min();
        let Some(target) = best.and_then(|(_, k)| m.target(id, k)).filter(|p| *p != id) else {
            continue;
        };
        let p = match t.by_id.get(target) {
            Some(&p) => p,
            None => t.object(target, None, MISSING),
        };
        parent.insert(t.by_id[id], p);
    }
    let mut children: FastMap<u32, Vec<u32>> = FastMap::default();
    for (&c, &p) in &parent {
        children.entry(p).or_default().push(c);
    }
    let mut roots = Vec::new();
    for o in 0..t.objects.len() as u32 {
        let is_root = !parent.contains_key(&o)
            && (name == 'c' || AREAS.contains(&t.objects[o as usize].class));
        if is_root {
            roots.push(grow(t, &children, o, name));
        }
    }
    Forest { roots, children }
}

/// Add object `o` and what it holds to forest `name`; returns its node.
fn grow(t: &mut Tree, children: &FastMap<u32, Vec<u32>>, o: u32, name: char) -> u32 {
    let root = t.node(o, name);
    let mut stack = vec![(root, o)];
    while let Some((n, o)) = stack.pop() {
        let mut kids = Vec::new();
        for &c in children.get(&o).map_or(&[][..], Vec::as_slice) {
            let slot = if name == 'c' {
                t.objects[c as usize].contained
            } else {
                t.objects[c as usize].operational
            };
            if slot.is_some() {
                continue;
            }
            let cn = t.node(c, name);
            kids.push(cn);
            stack.push((cn, c));
        }
        t.sort(&mut kids);
        for &k in &kids {
            t.nodes[k as usize].parent = n;
        }
        t.nodes[n as usize].children = kids;
    }
    root
}

impl Tree {
    fn object(&mut self, id: &str, label: Option<Box<str>>, class: &'static str) -> u32 {
        let o = self.objects.len() as u32;
        self.objects.push(Object {
            id: id.into(),
            label,
            class,
            findings: Counts::default(),
            contained: None,
            operational: None,
        });
        self.by_id.insert(id.into(), o);
        o
    }

    fn node(&mut self, o: u32, forest: char) -> u32 {
        let n = self.nodes.len() as u32;
        self.nodes.push(Node {
            what: What::Object { object: o, forest },
            children: Vec::new(),
            parent: NONE,
            findings: Counts::default(),
        });
        let obj = &mut self.objects[o as usize];
        if forest == 'c' {
            obj.contained = Some(n)
        } else {
            obj.operational = Some(n)
        }
        n
    }

    fn section(&mut self, key: &'static str, label: &'static str, children: Vec<u32>) -> u32 {
        let n = self.nodes.len() as u32;
        for &c in &children {
            self.nodes[c as usize].parent = n;
        }
        self.nodes.push(Node {
            what: What::Section { key, label },
            children,
            parent: NONE,
            findings: Counts::default(),
        });
        self.named.insert(key.into(), n);
        n
    }

    fn group(&mut self, section: &'static str, class: &'static str, children: Vec<u32>) -> u32 {
        let n = self.nodes.len() as u32;
        if section != "class" {
            for &c in &children {
                self.nodes[c as usize].parent = n;
            }
        }
        self.nodes.push(Node {
            what: What::Group { section, class },
            children,
            parent: NONE,
            findings: Counts::default(),
        });
        self.named.insert(format!("{section}/{class}"), n);
        n
    }

    /// The key the view knows node `n` by.
    fn key_of(&self, n: u32) -> String {
        match self.nodes[n as usize].what {
            What::Section { key, .. } => key.into(),
            What::Group { section, class } => format!("{section}/{class}"),
            What::Object { object, forest } => {
                format!("{forest}/{}", self.objects[object as usize].id)
            }
        }
    }

    /// The objects whose name or mRID holds `query` (ignoring case), at most
    /// `limit`: a name equal to it first, then one starting with it, then one
    /// holding it, then an mRID holding it; by name within each.
    pub fn search(&self, query: &str, limit: usize) -> Vec<(&str, &str, &'static str)> {
        let q = query.trim().to_lowercase();
        if q.is_empty() {
            return Vec::new();
        }
        let mut hits: Vec<(u8, String, usize)> = self
            .objects
            .iter()
            .enumerate()
            .filter(|(_, o)| o.class != MISSING)
            .filter_map(|(i, o)| {
                let name = o
                    .label
                    .as_deref()
                    .map(str::to_lowercase)
                    .unwrap_or_default();
                let rank = if name == q {
                    0
                } else if name.starts_with(&q) {
                    1
                } else if name.contains(&q) {
                    2
                } else if o.id.to_lowercase().contains(&q) {
                    3
                } else {
                    return None;
                };
                Some((rank, name, i))
            })
            .collect();
        hits.sort();
        hits.into_iter()
            .take(limit)
            .map(|(_, _, i)| {
                let o = &self.objects[i];
                (o.id.as_str(), self.label_of(i as u32), o.class)
            })
            .collect()
    }

    /// Object `id`'s label and class.
    pub fn describe(&self, id: &str) -> Option<(&str, &'static str)> {
        let &o = self.by_id.get(id)?;
        Some((self.label_of(o), self.objects[o as usize].class))
    }

    /// What object `id` holds directly.
    pub fn children(&self, id: &str) -> Vec<&str> {
        let Some(n) = self
            .by_id
            .get(id)
            .and_then(|&o| self.objects[o as usize].contained)
        else {
            return Vec::new();
        };
        self.nodes[n as usize]
            .children
            .iter()
            .filter_map(|&c| match self.nodes[c as usize].what {
                What::Object { object, .. } => Some(self.objects[object as usize].id.as_str()),
                _ => None,
            })
            .collect()
    }

    /// What object `id` holds, at any depth: its subtree in the containment.
    pub fn descendants(&self, id: &str) -> Vec<&str> {
        let Some(root) = self
            .by_id
            .get(id)
            .and_then(|&o| self.objects[o as usize].contained)
        else {
            return Vec::new();
        };
        let mut out = Vec::new();
        let mut stack = self.nodes[root as usize].children.clone();
        while let Some(n) = stack.pop() {
            let node = &self.nodes[n as usize];
            if let What::Object { object, .. } = node.what {
                out.push(self.objects[object as usize].id.as_str());
            }
            stack.extend(&node.children);
        }
        out
    }

    /// Object `id`'s node in the containment and its containers' before it,
    /// outermost first, as node numbers; empty if it is not in the tree.
    pub fn chain(&self, id: &str) -> Vec<u32> {
        let Some(mut n) = self
            .by_id
            .get(id)
            .and_then(|&o| self.objects[o as usize].contained)
        else {
            return Vec::new();
        };
        let mut out = vec![n];
        while let Some(What::Object { .. }) = self
            .nodes
            .get(self.nodes[n as usize].parent as usize)
            .map(|p| &p.what)
        {
            n = self.nodes[n as usize].parent;
            out.push(n);
        }
        out.reverse();
        out
    }

    /// The labels of the containers of object `id`, outermost first; the
    /// regions left out below a substation or line.
    pub fn containers(&self, id: &str) -> Vec<&str> {
        let mut out = self.chain(id);
        out.pop();
        let region = |&n: &u32| {
            matches!(
                self.class_of(n),
                "GeographicalRegion" | "SubGeographicalRegion"
            )
        };
        if out.iter().any(|n| !region(n)) {
            out.retain(|n| !region(n));
        }
        out.into_iter().map(|n| self.node_label(n)).collect()
    }

    /// The keys the view expands to reach object `id` in Geographical or Not
    /// contained, from its section on, the class groups and pages a long
    /// level is split into included; `None` if it is not listed (filtered out).
    pub fn path(&self, id: &str, only_findings: bool) -> Option<Vec<String>> {
        let mut chain = vec![self.objects[*self.by_id.get(id)? as usize].contained?];
        while let Some(&p) = chain
            .last()
            .map(|&n| &self.nodes[n as usize].parent)
            .filter(|&&p| p != NONE)
        {
            chain.push(p);
        }
        chain.reverse();
        let mut keys = vec![self.key_of(chain[0])];
        for pair in chain.windows(2) {
            let (parent, child) = (self.key_of(pair[0]), pair[1]);
            let entries = self.entries(&parent, only_findings)?;
            let at = entries
                .iter()
                .position(|e| matches!(e, Entry::Node(c) if *c == child))?;
            // As `children_json` lists a long level.
            if entries.len() > PAGE {
                let class = self.class_of(child);
                if entries.iter().any(|&e| self.entry_class(e) != class) {
                    let within = entries
                        .iter()
                        .take(at)
                        .filter(|&&e| self.entry_class(e) == class)
                        .count();
                    keys.push(format!("{parent}|{class}"));
                    if entries
                        .iter()
                        .filter(|&&e| self.entry_class(e) == class)
                        .count()
                        > PAGE
                    {
                        keys.push(format!("{parent}|{class}~{}", within / PAGE * PAGE));
                    }
                } else {
                    keys.push(format!("{parent}~{}", at / PAGE * PAGE));
                }
            }
            keys.push(self.key_of(child));
        }
        Some(keys)
    }

    fn class_of(&self, n: u32) -> &'static str {
        match self.nodes[n as usize].what {
            What::Object { object, .. } => self.objects[object as usize].class,
            What::Group { class, .. } => class,
            What::Section { .. } => "",
        }
    }

    fn label_of(&self, o: u32) -> &str {
        let obj = &self.objects[o as usize];
        obj.label.as_deref().unwrap_or(&obj.id)
    }

    fn node_label(&self, n: u32) -> &str {
        match self.nodes[n as usize].what {
            What::Object { object, .. } => self.label_of(object),
            What::Group { class, .. } => class,
            What::Section { label, .. } => label,
        }
    }

    /// Containers first, then by class and label.
    fn sort(&self, nodes: &mut [u32]) {
        nodes.sort_by_cached_key(|&n| {
            let class = self.class_of(n);
            (
                !(class == MISSING || CONTAINERS.contains(&class)),
                class,
                self.node_label(n).to_lowercase(),
            )
        });
    }

    /// Count each finding on its object, and each node's subtree.
    pub fn add_findings(&mut self, violations: &[(Option<usize>, Violation)]) {
        for (_, v) in violations {
            if let Some(&o) = self.by_id.get(v.object_id.as_str()) {
                self.objects[o as usize].findings.add(&v.severity);
            }
        }
        // A forest node comes before its children; groups and sections after
        // what they hold, sections after groups.
        for pass in 0..3 {
            for n in (0..self.nodes.len()).rev() {
                let node = &self.nodes[n];
                let mut sum = match (&node.what, pass) {
                    (What::Object { object, .. }, 0) => self.objects[*object as usize].findings,
                    (
                        What::Group {
                            section: "class", ..
                        },
                        1,
                    ) => {
                        let mut sum = Counts::default();
                        for &o in &node.children {
                            sum.merge(self.objects[o as usize].findings);
                        }
                        self.nodes[n].findings = sum;
                        continue;
                    }
                    (What::Group { .. }, 1) | (What::Section { .. }, 2) => Counts::default(),
                    _ => continue,
                };
                for &c in &node.children {
                    sum.merge(self.nodes[c as usize].findings);
                }
                self.nodes[n].findings = sum;
            }
        }
    }

    /// What `key` lists: its children; for `<key>|<class>`, those of a
    /// class; for a range key, `…~<start>`, a page of them. `None` for an
    /// unknown key.
    fn entries(&self, key: &str, only_findings: bool) -> Option<Vec<Entry>> {
        let (key, start) = match key.rsplit_once('~') {
            Some((key, start)) if start.parse::<usize>().is_ok() => {
                (key, start.parse::<usize>().ok())
            }
            _ => (key, None),
        };
        let (base, class) = match key.split_once('|') {
            Some((base, class)) => (base, Some(class)),
            None => (key, None),
        };
        let n = match base.split_once('/') {
            Some(("c", id)) => self.objects[*self.by_id.get(id)? as usize].contained?,
            Some(("o", id)) => self.objects[*self.by_id.get(id)? as usize].operational?,
            _ => *self.named.get(base)?,
        };
        let node = &self.nodes[n as usize];
        let leaves = matches!(
            node.what,
            What::Group {
                section: "class",
                ..
            }
        );
        let mut out: Vec<Entry> = node
            .children
            .iter()
            .map(|&c| {
                if leaves {
                    Entry::Leaf(c)
                } else {
                    Entry::Node(c)
                }
            })
            .collect();
        if only_findings {
            out.retain(|&e| self.findings(e).total() > 0);
        }
        if let Some(class) = class {
            out.retain(|&e| self.entry_class(e) == class);
        }
        if let Some(start) = start {
            out = out.into_iter().skip(start).take(PAGE).collect();
        }
        Some(out)
    }

    fn entry_class(&self, e: Entry) -> &'static str {
        match e {
            Entry::Node(n) => self.class_of(n),
            Entry::Leaf(o) => self.objects[o as usize].class,
        }
    }

    fn findings(&self, e: Entry) -> Counts {
        match e {
            Entry::Node(n) => self.nodes[n as usize].findings,
            Entry::Leaf(o) => self.objects[o as usize].findings,
        }
    }

    /// The children of node `key`, or the sections for `None`, for the view:
    /// each with its key, label, class, number of children, findings, where
    /// it is written and where the grid view shows it. `None` for a key the
    /// tree does not have (any more).
    pub fn children_json(
        &self,
        set: &impl Locations,
        grid: &Grid,
        key: Option<&str>,
        only_findings: bool,
    ) -> Option<Value> {
        let entries = match key {
            Some(key) => self.entries(key, only_findings)?,
            None => {
                let mut sections: Vec<Entry> =
                    self.sections.iter().map(|&s| Entry::Node(s)).collect();
                if only_findings {
                    sections.retain(|&e| self.findings(e).total() > 0);
                }
                sections
            }
        };
        // Too many to list: by class if they differ, else in pages.
        if let Some(key) = key.filter(|k| !k.contains('~'))
            && entries.len() > PAGE
        {
            let sum = |es: &[Entry]| {
                let mut findings = Counts::default();
                for &e in es {
                    findings.merge(self.findings(e));
                }
                findings.json()
            };
            let mut classes: Vec<(&str, Vec<Entry>)> = Vec::new();
            if !key.contains('|') {
                for &e in &entries {
                    let class = self.entry_class(e);
                    match classes.iter_mut().find(|c| c.0 == class) {
                        Some(c) => c.1.push(e),
                        None => classes.push((class, vec![e])),
                    }
                }
            }
            let ranges: Vec<Value> = if classes.len() > 1 {
                classes
                    .iter()
                    .map(|(class, es)| {
                        json!({
                            "key": format!("{key}|{class}"), "kind": "group", "class": class,
                            "label": if *class == MISSING { "Not in the set" } else { class }, "children": es.len(), "findings": sum(es),
                        })
                    })
                    .collect()
            } else {
                entries
                    .chunks(PAGE)
                    .enumerate()
                    .map(|(i, chunk)| {
                        json!({
                            "key": format!("{key}~{}", i * PAGE), "kind": "range",
                            "label": format!("{}–{}", i * PAGE + 1, i * PAGE + chunk.len()), "children": chunk.len(), "findings": sum(chunk),
                        })
                    })
                    .collect()
            };
            return Some(Value::Array(ranges));
        }
        let location = |id: &str| set.location(id);
        // A line is shown as its segments are: the first of them on the grid.
        let reveal = |o: u32, kids: &[u32]| -> Option<Value> {
            grid.reveal(&self.objects[o as usize].id).or_else(|| {
                kids.iter()
                    .find_map(|&c| match self.nodes[c as usize].what {
                        What::Object { object, .. } => grid
                            .reveal(&self.objects[object as usize].id)
                            .filter(|r| r.get("enter").is_none()),
                        _ => None,
                    })
            })
        };
        let object = |o: u32, key: String, kids: &[u32], findings: Counts| -> Value {
            let obj = &self.objects[o as usize];
            json!({
                "key": key, "kind": if obj.class == MISSING { "missing" } else { "object" }, "id": obj.id,
                "label": self.label_of(o), "class": obj.class, "children": kids.len(), "findings": findings.json(),
                "location": location(&obj.id), "grid": reveal(o, kids),
            })
        };
        let items = entries
            .into_iter()
            .map(|e| match e {
                Entry::Leaf(o) => object(o, format!("k/{}", self.objects[o as usize].id), &[], self.objects[o as usize].findings),
                Entry::Node(n) => {
                    let node = &self.nodes[n as usize];
                    match node.what {
                        What::Object { object: o, forest } => {
                            object(o, format!("{forest}/{}", self.objects[o as usize].id), &node.children, node.findings)
                        }
                        What::Group { section, class } => json!({
                            "key": format!("{section}/{class}"), "kind": "group", "class": class,
                            "label": if class == MISSING { "Not in the set" } else { class },
                            "children": node.children.len(), "findings": node.findings.json(),
                        }),
                        What::Section { key, label } => json!({
                            "key": key, "kind": "section", "label": label, "children": node.children.len(), "findings": node.findings.json(),
                        }),
                    }
                }
            })
            .collect();
        Some(Value::Array(items))
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use serde_json::Value;

    use crate::testing::{collect, validate};

    fn labels(v: &Value) -> Vec<&str> {
        v.as_array()
            .unwrap()
            .iter()
            .map(|x| x["label"].as_str().unwrap())
            .collect()
    }

    /// MicroGrid: regions down to terminals, the boundary's lines among them;
    /// each object once in the containment, findings summed up the tree.
    #[test]
    fn micro_grid_tree() {
        let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../CGMES-Test-Configurations/v3.0/MicroGrid/MicroGid-BaseCase/MicroGrid-BaseCase-Merged");
        let files = collect(&dir);
        assert_eq!(
            files.len(),
            12,
            "missing test data — run `git submodule update --init`"
        );
        let set = validate(&files);
        let t = &set.tree;
        let children = |key: Option<&str>| t.children_json(&set, &set.grid, key, false).unwrap();
        assert_eq!(
            labels(&children(None)),
            ["Geographical", "Operational", "Not contained", "By class"]
        );

        let geo = children(Some("geo"));
        assert!(labels(&geo).contains(&"BE"), "{:?}", labels(&geo));
        let find = |list: &Value, label: &str| {
            list.as_array()
                .unwrap()
                .iter()
                .find(|x| x["label"] == label)
                .cloned()
                .unwrap()
        };
        let be = find(&geo, "BE");
        let sub_regions = children(be["key"].as_str());
        let brussels_region = find(&sub_regions, "ELIA-Brussels");
        let subs = children(brussels_region["key"].as_str());
        let brussels = find(&subs, "PP_Brussels");
        assert_eq!(brussels["class"], "Substation");
        assert_eq!(brussels["grid"]["inside"], brussels["id"]);
        // Its voltage levels first, then the transformers it holds itself.
        let held = children(brussels["key"].as_str());
        let classes: Vec<&str> = held
            .as_array()
            .unwrap()
            .iter()
            .map(|x| x["class"].as_str().unwrap())
            .collect();
        assert_eq!(&classes[..5], ["VoltageLevel"; 5], "{classes:?}");
        assert!(classes.contains(&"PowerTransformer"), "{classes:?}");
        assert_eq!(
            t.containers(brussels["id"].as_str().unwrap()),
            ["BE", "ELIA-Brussels"]
        );

        // Search: by name, ignoring case, the exact one first; by mRID.
        assert_eq!(t.search("anvers", 5)[0].1, "Anvers");
        // Names starting with it before names holding it; objects may share a name.
        let lines: Vec<&str> = t.search("be-line_", 30).into_iter().map(|h| h.1).collect();
        let starting = lines
            .iter()
            .take_while(|l| l.starts_with("BE-Line_"))
            .count();
        assert!(
            starting >= 7
                && lines[starting..]
                    .iter()
                    .all(|l| l.contains("BE-Line_") && !l.starts_with("BE-Line_")),
            "{lines:?}"
        );
        let id = brussels["id"].as_str().unwrap();
        assert_eq!(t.search(&id[3..15], 5)[0].0, id);
        assert!(t.search("  ", 5).is_empty());
        assert_eq!(
            t.containers(held[0]["id"].as_str().unwrap()),
            ["PP_Brussels"]
        );

        // Each object's path leads to it, a listed level at a time.
        for group in children(Some("class")).as_array().unwrap() {
            for x in children(group["key"].as_str()).as_array().unwrap() {
                let keys = t.path(x["id"].as_str().unwrap(), false).unwrap();
                assert!(["geo", "other"].contains(&keys[0].as_str()), "{keys:?}");
                for step in keys.windows(2) {
                    let listed = children(Some(&step[0]));
                    assert!(
                        listed
                            .as_array()
                            .unwrap()
                            .iter()
                            .any(|y| y["key"] == step[1].as_str()),
                        "{keys:?}"
                    );
                }
                assert_eq!(
                    keys.last().unwrap(),
                    &format!("c/{}", x["id"].as_str().unwrap())
                );
            }
        }

        // Each object of the set is once in Geographical or Not contained.
        let mut seen = 0;
        let mut stack: Vec<String> = vec!["geo".into(), "other".into()];
        while let Some(key) = stack.pop() {
            for x in children(Some(&key)).as_array().unwrap() {
                if x["kind"] == "object" {
                    seen += 1;
                }
                if x["children"].as_u64().unwrap() > 0 {
                    stack.push(x["key"].as_str().unwrap().into());
                }
            }
        }
        let by_class: u64 = children(Some("class"))
            .as_array()
            .unwrap()
            .iter()
            .map(|x| x["children"].as_u64().unwrap())
            .sum();
        assert_eq!(seen, by_class);

        // A section counts the findings of what it holds; By class, of every
        // object, so all the findings on objects of the set.
        let total = |x: &Value| {
            ["violation", "warning", "info"]
                .iter()
                .map(|s| x["findings"][s].as_u64().unwrap())
                .sum::<u64>()
        };
        let sections = children(None);
        let sum_of = |key: &str| {
            children(Some(key))
                .as_array()
                .unwrap()
                .iter()
                .map(total)
                .sum::<u64>()
        };
        for s in sections.as_array().unwrap() {
            assert_eq!(
                total(s),
                sum_of(s["key"].as_str().unwrap()),
                "{}",
                s["label"]
            );
        }
        let all = total(&find(&sections, "By class"));
        assert!(all > 0);
        assert_eq!(
            total(&find(&sections, "Geographical")) + total(&find(&sections, "Not contained")),
            all
        );
        assert!(
            t.children_json(&set, &set.grid, None, true)
                .unwrap()
                .as_array()
                .unwrap()
                .iter()
                .all(|x| total(x) > 0)
        );
    }
}