panproto-lens 0.52.0

Bidirectional lens combinators for panproto
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
//! Per-pipeline-step edit translation for the edit lens.
//!
//! The [`EditPipeline`] mirrors the five steps of `wtype_restrict`
//! incrementally, translating individual `TreeEdit` values through
//! reachability tracking, ancestor contraction, edge resolution,
//! and fan reconstruction.

use panproto_inst::{
    CompiledMigration, ContractionRecord, ContractionTracker, ReachabilityIndex, TreeEdit,
    WInstance,
};
use panproto_schema::Schema;
use smallvec::SmallVec;

use crate::EditLens;
use crate::asymmetric::Complement;
use crate::edit_error::EditLensError;

/// Incremental state for per-step edit translation.
///
/// Initialized from an [`EditLens`] and source instance via a
/// whole-state pass, then processes individual edits incrementally.
/// The five pipeline steps correspond to the `wtype_restrict` stages:
///
/// 1. Anchor survival
/// 2. Reachability
/// 3. Ancestor contraction
/// 4. Edge resolution
/// 5. Fan reconstruction
#[derive(Clone)]
pub struct EditPipeline {
    /// Incremental reachability tracking from root.
    pub reachability: ReachabilityIndex,
    contraction: ContractionTracker,
    compiled: CompiledMigration,
    tgt_schema: Schema,
}

impl EditPipeline {
    /// Build an `EditPipeline` from an [`EditLens`] and source instance.
    ///
    /// Performs a whole-state pass to initialize the reachability index.
    #[must_use]
    pub fn from_lens_and_instance(lens: &EditLens, source: &WInstance) -> Self {
        Self {
            reachability: ReachabilityIndex::from_instance(source),
            contraction: ContractionTracker::new(),
            compiled: lens.compiled.clone(),
            tgt_schema: lens.tgt_schema.clone(),
        }
    }

    /// Translate a source edit to a view edit through the 5 pipeline steps.
    ///
    /// Each step may transform the edit or absorb it into the complement.
    ///
    /// # Errors
    ///
    /// Returns [`EditLensError`] if edge resolution fails for a
    /// contracted arc.
    pub fn translate_get(
        &mut self,
        edit: &TreeEdit,
        complement: &mut Complement,
    ) -> Result<TreeEdit, EditLensError> {
        let edit = self.step1_anchor_survival(edit, complement);
        if edit.is_identity() {
            return Ok(edit);
        }
        let edit = self.step2_reachability(&edit, complement);
        if edit.is_identity() {
            return Ok(edit);
        }
        let edit = self.step3_ancestor_contraction(&edit, complement);
        if edit.is_identity() {
            return Ok(edit);
        }
        let edit = self.step4_edge_resolution(&edit)?;
        let edit = Self::step5_fan_reconstruction(&edit, complement);
        Ok(edit)
    }

    /// Translate a view edit back to a source edit (reverse pipeline).
    ///
    /// Runs the 5 steps in reverse order.
    ///
    /// # Errors
    ///
    /// Returns [`EditLensError`] if edge resolution fails.
    pub fn translate_put(
        &mut self,
        edit: &TreeEdit,
        complement: &mut Complement,
    ) -> Result<TreeEdit, EditLensError> {
        let edit = Self::step5_fan_reconstruction(edit, complement);
        let edit = self.step4_edge_resolution(&edit)?;
        let edit = self.step3_ancestor_contraction(&edit, complement);
        let edit = self.step2_reachability(&edit, complement);
        let edit = self.step1_anchor_survival(&edit, complement);
        Ok(edit)
    }

    /// Step 1: anchor survival.
    ///
    /// Checks whether the edit targets a surviving vertex. Non-surviving
    /// edits are absorbed into the complement.
    fn step1_anchor_survival(&self, edit: &TreeEdit, complement: &mut Complement) -> TreeEdit {
        match edit {
            TreeEdit::InsertNode {
                parent,
                child_id,
                node,
                edge,
            } => {
                if self.anchor_survives(&node.anchor) {
                    edit.clone()
                } else {
                    complement.dropped_nodes.insert(*child_id, node.clone());
                    complement
                        .dropped_arcs
                        .push((*parent, *child_id, edge.clone()));
                    TreeEdit::Identity
                }
            }
            TreeEdit::DeleteNode { id } => {
                if complement.dropped_nodes.contains_key(id) {
                    complement.dropped_nodes.remove(id);
                    complement
                        .dropped_arcs
                        .retain(|&(_, child, _)| child != *id);
                    TreeEdit::Identity
                } else {
                    edit.clone()
                }
            }
            TreeEdit::SetField {
                node_id,
                field,
                value,
            } => {
                if let Some(node) = complement.dropped_nodes.get_mut(node_id) {
                    node.extra_fields.insert(field.to_string(), value.clone());
                    TreeEdit::Identity
                } else {
                    edit.clone()
                }
            }
            TreeEdit::RelabelNode { id, new_anchor } => {
                let was_complement = complement.dropped_nodes.contains_key(id);
                let survives = self.anchor_survives(new_anchor);
                self.relabel_dispatch(*id, new_anchor, was_complement, survives, complement)
            }
            _ => edit.clone(),
        }
    }

    /// Dispatch for relabel edits in step 1 (split out for line length).
    fn relabel_dispatch(
        &self,
        id: u32,
        new_anchor: &panproto_gat::Name,
        was_complement: bool,
        survives: bool,
        complement: &mut Complement,
    ) -> TreeEdit {
        match (was_complement, survives) {
            (true, true) => {
                if let Some(node) = complement.dropped_nodes.remove(&id) {
                    complement.dropped_arcs.retain(|&(_, child, _)| child != id);
                    let parent = complement
                        .original_parent
                        .get(&id)
                        .copied()
                        .or_else(|| self.reachability.root())
                        .unwrap_or(0);
                    // Find the parent's anchor to resolve the correct edge.
                    let parent_anchor = complement
                        .dropped_nodes
                        .get(&parent)
                        .map(|n| n.anchor.clone())
                        .or_else(|| self.tgt_schema.vertices.keys().next().cloned())
                        .unwrap_or_else(|| panproto_gat::Name::from("root"));
                    // Resolve the edge from the target schema between parent and child anchors.
                    let edge = self
                        .tgt_schema
                        .edges_between(&parent_anchor, new_anchor)
                        .first()
                        .cloned()
                        .unwrap_or_else(|| panproto_schema::Edge {
                            src: parent_anchor,
                            tgt: new_anchor.clone(),
                            kind: "prop".into(),
                            name: None,
                        });
                    TreeEdit::InsertNode {
                        parent,
                        child_id: id,
                        node,
                        edge,
                    }
                } else {
                    TreeEdit::Identity
                }
            }
            (true, false) => {
                if let Some(node) = complement.dropped_nodes.get_mut(&id) {
                    node.anchor.clone_from(new_anchor);
                }
                TreeEdit::Identity
            }
            (false, true) => TreeEdit::RelabelNode {
                id,
                new_anchor: new_anchor.clone(),
            },
            (false, false) => TreeEdit::DeleteNode { id },
        }
    }

    /// Step 2: reachability tracking.
    ///
    /// Updates the [`ReachabilityIndex`] and cascades reachability
    /// changes into additional edits or complement updates.
    fn step2_reachability(&mut self, edit: &TreeEdit, complement: &mut Complement) -> TreeEdit {
        match edit {
            TreeEdit::InsertNode {
                parent, child_id, ..
            } => {
                let newly = self.reachability.insert_edge(*parent, *child_id);
                for nid in &newly {
                    complement.dropped_nodes.remove(nid);
                    complement
                        .dropped_arcs
                        .retain(|&(_, child, _)| child != *nid);
                }
                edit.clone()
            }
            TreeEdit::DeleteNode { id } => {
                let parent = self.reachability.parent_of(*id);
                let newly_unreachable =
                    parent.map_or_else(Vec::new, |p| self.reachability.delete_edge(p, *id));
                if newly_unreachable.is_empty() {
                    return edit.clone();
                }
                let mut edits = vec![edit.clone()];
                for nid in newly_unreachable {
                    if nid != *id && !complement.dropped_nodes.contains_key(&nid) {
                        edits.push(TreeEdit::DeleteNode { id: nid });
                    }
                }
                flatten_edits(edits)
            }
            TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                ..
            } => {
                let old_parent = self.reachability.parent_of(*node_id);
                if let Some(p) = old_parent {
                    let unreachable = self.reachability.delete_edge(p, *node_id);
                    // Nodes that become unreachable from the old position should
                    // be tracked; they will become reachable again when the edge
                    // is re-inserted below if the new parent is reachable.
                    for &nid in &unreachable {
                        if nid != *node_id {
                            complement
                                .dropped_arcs
                                .retain(|&(_, child, _)| child != nid);
                        }
                    }
                }
                let newly = self.reachability.insert_edge(*new_parent, *node_id);
                for nid in &newly {
                    complement.dropped_nodes.remove(nid);
                    complement
                        .dropped_arcs
                        .retain(|&(_, child, _)| child != *nid);
                }
                edit.clone()
            }
            _ => edit.clone(),
        }
    }

    /// Step 3: ancestor contraction.
    ///
    /// When a non-surviving node is deleted and its children survive,
    /// the children are reattached to the nearest surviving ancestor.
    fn step3_ancestor_contraction(&mut self, edit: &TreeEdit, complement: &Complement) -> TreeEdit {
        match edit {
            TreeEdit::ContractNode { id } => {
                let parent = self
                    .reachability
                    .parent_of(*id)
                    .or_else(|| complement.original_parent.get(id).copied())
                    .or_else(|| self.reachability.root())
                    .unwrap_or(0);
                // Collect actual children from the reachability index.
                let children: SmallVec<u32, 4> =
                    self.reachability.children_of(*id).iter().copied().collect();
                // Look up the actual edge from parent to this node in the target schema.
                // Look up the parent's anchor. Check the complement first (the parent
                // may have been dropped), then fall back to the first vertex in the
                // target schema.
                let parent_anchor = complement
                    .dropped_nodes
                    .get(&parent)
                    .or_else(|| {
                        complement
                            .original_parent
                            .get(id)
                            .and_then(|&p| complement.dropped_nodes.get(&p))
                    })
                    .map(|n| n.anchor.clone())
                    .or_else(|| self.tgt_schema.vertices.keys().next().cloned())
                    .unwrap_or_else(|| panproto_gat::Name::from("unknown"));
                let node_anchor = complement
                    .dropped_nodes
                    .get(id)
                    .map_or_else(|| panproto_gat::Name::from("unknown"), |n| n.anchor.clone());
                let edge = self
                    .tgt_schema
                    .edges_between(&parent_anchor, &node_anchor)
                    .first()
                    .cloned()
                    .unwrap_or_else(|| panproto_schema::Edge {
                        src: parent_anchor,
                        tgt: node_anchor,
                        kind: "contracted".into(),
                        name: None,
                    });
                let record = ContractionRecord {
                    original_parent: parent,
                    children,
                    original_edge: edge,
                };
                self.contraction.contract(*id, record);
                edit.clone()
            }
            TreeEdit::InsertNode { child_id, .. } => {
                if self.contraction.is_contracted(*child_id) {
                    self.contraction.expand(*child_id);
                }
                edit.clone()
            }
            _ => edit.clone(),
        }
    }

    /// Step 4: edge resolution.
    ///
    /// For edits that create new arcs (from contraction), resolve
    /// which schema edge to use via the resolver or unique-edge lookup.
    fn step4_edge_resolution(&self, edit: &TreeEdit) -> Result<TreeEdit, EditLensError> {
        match edit {
            TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                edge,
            } if edge.kind.as_ref() == "contracted" => {
                let resolved = panproto_inst::resolve_edge(
                    &self.tgt_schema,
                    &self.compiled.resolver,
                    edge.src.as_ref(),
                    edge.tgt.as_ref(),
                )?;
                Ok(TreeEdit::MoveSubtree {
                    node_id: *node_id,
                    new_parent: *new_parent,
                    edge: resolved,
                })
            }
            TreeEdit::InsertNode {
                parent,
                child_id,
                node,
                edge,
            } if edge.kind.as_ref() == "contracted" => {
                let resolved = panproto_inst::resolve_edge(
                    &self.tgt_schema,
                    &self.compiled.resolver,
                    edge.src.as_ref(),
                    edge.tgt.as_ref(),
                )?;
                Ok(TreeEdit::InsertNode {
                    parent: *parent,
                    child_id: *child_id,
                    node: node.clone(),
                    edge: resolved,
                })
            }
            _ => Ok(edit.clone()),
        }
    }

    /// Step 5: fan reconstruction.
    ///
    /// Handles fan-related edits, absorbing fans with dropped
    /// participants into the complement.
    fn step5_fan_reconstruction(edit: &TreeEdit, complement: &mut Complement) -> TreeEdit {
        match edit {
            TreeEdit::InsertFan { fan } => {
                let all_survive = fan
                    .children
                    .values()
                    .all(|&cid| !complement.dropped_nodes.contains_key(&cid))
                    && !complement.dropped_nodes.contains_key(&fan.parent);

                if all_survive {
                    edit.clone()
                } else {
                    complement.dropped_fans.push(fan.clone());
                    TreeEdit::Identity
                }
            }
            TreeEdit::DeleteFan { hyper_edge_id } => {
                let id_str = hyper_edge_id.as_ref();
                let in_complement = complement
                    .dropped_fans
                    .iter()
                    .any(|f| f.hyper_edge_id == id_str);
                if in_complement {
                    complement
                        .dropped_fans
                        .retain(|f| f.hyper_edge_id != id_str);
                    TreeEdit::Identity
                } else {
                    edit.clone()
                }
            }
            _ => edit.clone(),
        }
    }

    /// Check whether a vertex anchor survives the migration.
    fn anchor_survives(&self, anchor: &panproto_gat::Name) -> bool {
        if !self.compiled.surviving_verts.contains(anchor) {
            return false;
        }
        self.compiled
            .conditional_survival
            .get(anchor)
            .is_none_or(|pred| {
                let env = panproto_expr::Env::new();
                let config = panproto_expr::EvalConfig::default();
                !matches!(
                    panproto_expr::eval(pred, &env, &config),
                    Ok(panproto_expr::Literal::Bool(false))
                )
            })
    }
}

/// Collapse a list of edits into a single edit.
fn flatten_edits(edits: Vec<TreeEdit>) -> TreeEdit {
    let non_identity: Vec<TreeEdit> = edits.into_iter().filter(|e| !e.is_identity()).collect();
    match non_identity.len() {
        0 => TreeEdit::Identity,
        1 => non_identity
            .into_iter()
            .next()
            .unwrap_or(TreeEdit::Identity),
        _ => TreeEdit::Sequence(non_identity),
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::collections::HashMap;

    use panproto_gat::Name;
    use panproto_inst::{Node, TreeEdit, WInstance};
    use panproto_schema::{Edge, Protocol};

    use crate::EditLens;
    use crate::asymmetric::Complement;
    use crate::tests::{identity_lens, projection_lens, three_node_instance, three_node_schema};

    use super::EditPipeline;

    fn test_protocol() -> Protocol {
        Protocol {
            name: "test".into(),
            schema_theory: "ThTest".into(),
            instance_theory: "ThWType".into(),
            schema_composition: None,
            instance_composition: None,
            edge_rules: vec![],
            obj_kinds: vec![],
            constraint_sorts: vec![],
            has_order: false,
            has_coproducts: false,
            has_recursion: false,
            has_causal: false,
            nominal_identity: false,
            has_defaults: false,
            has_coercions: false,
            has_mergers: false,
            has_policies: false,
        }
    }

    fn sample_edge(src: &str, tgt: &str) -> Edge {
        Edge {
            src: src.into(),
            tgt: tgt.into(),
            kind: "prop".into(),
            name: None,
        }
    }

    #[test]
    fn pipeline_identity_lens_passes_through() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let instance = three_node_instance();
        let edit_lens = EditLens::from_lens(lens, test_protocol());

        let mut pipeline = EditPipeline::from_lens_and_instance(&edit_lens, &instance);
        let mut complement = Complement::empty();

        let edit = TreeEdit::SetField {
            node_id: 1,
            field: Name::from("text"),
            value: panproto_inst::Value::Str("updated".into()),
        };

        let result = pipeline.translate_get(&edit, &mut complement).unwrap();
        match &result {
            TreeEdit::SetField { node_id, field, .. } => {
                assert_eq!(*node_id, 1);
                assert_eq!(field, &Name::from("text"));
            }
            other => panic!("expected SetField, got {other:?}"),
        }
    }

    #[test]
    fn pipeline_tracks_reachability_on_insert() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let instance = three_node_instance();
        let edit_lens = EditLens::from_lens(lens, test_protocol());

        let mut pipeline = EditPipeline::from_lens_and_instance(&edit_lens, &instance);
        let mut complement = Complement::empty();

        let new_node = Node::new(99, "post:body.text");
        let edit = TreeEdit::InsertNode {
            parent: 0,
            child_id: 99,
            node: new_node,
            edge: sample_edge("post:body", "post:body.text"),
        };

        let result = pipeline.translate_get(&edit, &mut complement).unwrap();
        assert!(
            !result.is_identity(),
            "insert of surviving node should pass through"
        );
        assert!(
            pipeline.reachability.is_reachable(99),
            "newly inserted node should be reachable"
        );
    }

    #[test]
    fn pipeline_tracks_reachability_on_delete() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let mut nodes = HashMap::new();
        nodes.insert(0, Node::new(0, "post:body"));
        nodes.insert(1, Node::new(1, "post:body.text"));
        nodes.insert(10, Node::new(10, "post:body.text"));
        let arcs = vec![
            (
                0,
                1,
                Edge {
                    src: "post:body".into(),
                    tgt: "post:body.text".into(),
                    kind: "prop".into(),
                    name: Some("text".into()),
                },
            ),
            (
                1,
                10,
                Edge {
                    src: "post:body.text".into(),
                    tgt: "post:body.text".into(),
                    kind: "prop".into(),
                    name: None,
                },
            ),
        ];
        let instance = WInstance::new(nodes, arcs, vec![], 0, Name::from("post:body"));

        let edit_lens = EditLens::from_lens(lens, test_protocol());
        let mut pipeline = EditPipeline::from_lens_and_instance(&edit_lens, &instance);
        let mut complement = Complement::empty();

        assert!(pipeline.reachability.is_reachable(1));
        assert!(pipeline.reachability.is_reachable(10));

        let edit = TreeEdit::DeleteNode { id: 1 };
        let _result = pipeline.translate_get(&edit, &mut complement).unwrap();
    }

    #[test]
    fn pipeline_fan_with_dropped_participant() {
        let schema = three_node_schema();
        let lens = projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let mut pipeline = EditPipeline::from_lens_and_instance(&edit_lens, &instance);
        let mut complement = edit_lens.complement.clone();

        assert!(complement.dropped_nodes.contains_key(&2));

        let fan = panproto_inst::Fan::new("test_he", 0)
            .with_child("a", 1)
            .with_child("b", 2);
        let edit = TreeEdit::InsertFan { fan };

        let result = pipeline.translate_get(&edit, &mut complement).unwrap();
        assert!(
            result.is_identity(),
            "fan with dropped participant should be absorbed"
        );
        assert_eq!(
            complement.dropped_fans.len(),
            1,
            "fan should be in complement"
        );
    }

    #[test]
    fn pipeline_matches_batch_restrict() {
        use panproto_inst::Value;

        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let mut instance = three_node_instance();
        let edit_lens = EditLens::from_lens(lens, test_protocol());

        let mut pipeline = EditPipeline::from_lens_and_instance(&edit_lens, &instance);
        let mut complement = Complement::empty();

        let edits = vec![
            TreeEdit::SetField {
                node_id: 1,
                field: Name::from("text"),
                value: Value::Str("modified".into()),
            },
            TreeEdit::SetField {
                node_id: 2,
                field: Name::from("extra"),
                value: Value::Int(42),
            },
        ];

        for edit in &edits {
            let _translated = pipeline.translate_get(edit, &mut complement).unwrap();
        }

        for edit in &edits {
            edit.apply(&mut instance).unwrap();
        }

        let compiled = edit_lens.compiled.clone();
        let batch_view = panproto_inst::wtype_restrict(
            &instance,
            &edit_lens.src_schema,
            &edit_lens.tgt_schema,
            &compiled,
        )
        .unwrap();

        assert_eq!(
            batch_view.node_count(),
            instance.node_count(),
            "identity lens batch view should have same node count"
        );

        let n1 = batch_view.nodes.get(&1).expect("node 1 in view");
        assert_eq!(
            n1.extra_fields.get("text"),
            Some(&Value::Str("modified".into())),
            "SetField edit should be reflected in batch restrict output"
        );
    }
}