relateby-pattern 0.3.0

Core pattern data structures
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
//! Tests for graph transform operations: map, filter, fold, map_with_context,
//! para_graph, para_graph_fixed, unfold, unfold_graph.
//!
//! Covers T030.

use std::collections::{HashMap, HashSet};

use pattern_core::{
    canonical_classifier, filter_graph, fold_graph, from_pattern_graph, from_patterns,
    map_all_graph, map_graph, map_with_context, materialize, para_graph, para_graph_fixed, unfold,
    unfold_graph, CategoryMappers, GraphClass, GraphClassifier, Pattern, ReconciliationPolicy,
    Subject, SubjectMergeStrategy, Substitution, Symbol, Value,
};

fn classifier() -> GraphClassifier<(), Subject> {
    canonical_classifier::<Subject>()
}

fn lww() -> ReconciliationPolicy<SubjectMergeStrategy> {
    ReconciliationPolicy::LastWriteWins
}

// ============================================================================
// Helpers
// ============================================================================

fn subj(id: &str) -> Subject {
    Subject {
        identity: Symbol(id.to_string()),
        labels: HashSet::new(),
        properties: HashMap::new(),
    }
}

fn subj_with_prop(id: &str, key: &str, val: &str) -> Subject {
    let mut props = HashMap::new();
    props.insert(key.to_string(), Value::VString(val.to_string()));
    Subject {
        identity: Symbol(id.to_string()),
        labels: HashSet::new(),
        properties: props,
    }
}

fn node(id: &str) -> Pattern<Subject> {
    Pattern::point(subj(id))
}

fn rel(id: &str, src: &str, tgt: &str) -> Pattern<Subject> {
    Pattern {
        value: subj(id),
        elements: vec![node(src), node(tgt)],
    }
}

fn annot(id: &str, target_id: &str) -> Pattern<Subject> {
    Pattern {
        value: subj(id),
        elements: vec![node(target_id)],
    }
}

// ============================================================================
// unfold
// ============================================================================

#[test]
fn unfold_leaf_seed() {
    let tree = unfold(|n: u32| (n, vec![]), 42u32);
    assert_eq!(tree.value, 42);
    assert!(tree.elements.is_empty());
}

#[test]
fn unfold_depth_2_binary_tree() {
    let tree = unfold(
        |depth: u32| {
            if depth == 0 {
                (depth, vec![])
            } else {
                (depth, vec![depth - 1, depth - 1])
            }
        },
        2u32,
    );
    assert_eq!(tree.value, 2);
    assert_eq!(tree.elements.len(), 2);
    assert_eq!(tree.elements[0].value, 1);
    assert_eq!(tree.elements[0].elements.len(), 2);
    assert_eq!(tree.elements[0].elements[0].value, 0);
}

#[test]
fn unfold_linear_chain() {
    // Build a chain: 3 -> 2 -> 1 -> 0
    let tree = unfold(
        |n: u32| {
            if n == 0 {
                (n, vec![])
            } else {
                (n, vec![n - 1])
            }
        },
        3u32,
    );
    assert_eq!(tree.value, 3);
    assert_eq!(tree.elements.len(), 1);
    assert_eq!(tree.elements[0].value, 2);
    assert_eq!(tree.elements[0].elements[0].value, 1);
}

// ============================================================================
// unfold_graph
// ============================================================================

#[test]
fn unfold_graph_from_seeds() {
    let classifier = classifier();
    let policy = lww();

    let seeds = vec!["a", "b", "c"];
    let graph = unfold_graph(&classifier, &policy, |id: &str| vec![node(id)], seeds);

    assert_eq!(graph.pg_nodes.len(), 3);
}

#[test]
fn unfold_graph_empty_seeds() {
    let classifier = classifier();
    let policy = lww();

    let graph = unfold_graph(&classifier, &policy, |_: &str| vec![], vec!["x"]);

    assert_eq!(graph.pg_nodes.len(), 0);
}

// ============================================================================
// map_graph (by category)
// ============================================================================

#[test]
fn map_graph_transforms_only_nodes() {
    let classifier = classifier();
    let policy = lww();
    let graph = from_patterns(&classifier, vec![rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    // Add a property to every node
    let mappers = CategoryMappers {
        nodes: Box::new(|mut p: Pattern<Subject>| {
            p.value
                .properties
                .insert("transformed".to_string(), Value::VString("yes".to_string()));
            p
        }),
        ..CategoryMappers::identity()
    };

    let view = map_graph(&classifier, mappers, view);

    // Check view_elements directly: nodes should have the property, rels should not
    for (cls, pat) in &view.view_elements {
        match cls {
            GraphClass::GNode => {
                assert!(
                    pat.value.properties.contains_key("transformed"),
                    "node should have 'transformed' property"
                );
            }
            GraphClass::GRelationship => {
                assert!(
                    !pat.value.properties.contains_key("transformed"),
                    "relationship should not have 'transformed' property"
                );
            }
            _ => {}
        }
    }
}

// ============================================================================
// map_all_graph
// ============================================================================

#[test]
fn map_all_graph_transforms_all_elements() {
    let classifier = classifier();
    let policy = lww();
    let graph = from_patterns(&classifier, vec![node("a"), node("b")]);
    let view = from_pattern_graph(&classifier, &graph);

    let view = map_all_graph(
        |mut p: Pattern<Subject>| {
            p.value
                .properties
                .insert("touched".to_string(), Value::VString("1".to_string()));
            p
        },
        view,
    );
    let back = materialize(&classifier, &policy, view);

    for n in back.pg_nodes.values() {
        assert!(n.value.properties.contains_key("touched"));
    }
}

// ============================================================================
// filter_graph
// ============================================================================

#[test]
fn filter_graph_removes_non_matching() {
    let classifier = classifier();
    let policy = lww();
    let graph = from_patterns(
        &classifier,
        vec![node("keep1"), node("keep2"), node("drop1")],
    );
    let view = from_pattern_graph(&classifier, &graph);

    let view = filter_graph(
        &classifier,
        |_cls, p| p.value.identity.0.starts_with("keep"),
        Substitution::NoSubstitution,
        view,
    );
    let back = materialize(&classifier, &policy, view);

    assert_eq!(back.pg_nodes.len(), 2);
    assert!(back.pg_nodes.contains_key(&Symbol("keep1".to_string())));
    assert!(back.pg_nodes.contains_key(&Symbol("keep2".to_string())));
    assert!(!back.pg_nodes.contains_key(&Symbol("drop1".to_string())));
}

// ============================================================================
// fold_graph
// ============================================================================

#[test]
fn fold_graph_count_by_class() {
    let classifier = classifier();
    let graph = from_patterns(&classifier, vec![node("a"), node("b"), rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    let (node_count, rel_count) = fold_graph(
        |(nc, rc), cls, _p| match cls {
            GraphClass::GNode => (nc + 1, rc),
            GraphClass::GRelationship => (nc, rc + 1),
            _ => (nc, rc),
        },
        (0usize, 0usize),
        &view,
    );

    assert_eq!(node_count, 2);
    assert_eq!(rel_count, 1);
}

// ============================================================================
// map_with_context (snapshot semantics)
// ============================================================================

#[test]
fn map_with_context_uses_snapshot() {
    let classifier = classifier();
    let policy = lww();
    let graph = from_patterns(&classifier, vec![node("a"), node("b"), rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    // Enrich each node with its degree from the snapshot query
    let view = map_with_context(
        &classifier,
        |query, mut p| {
            let degree = (query.query_degree)(&p);
            p.value
                .properties
                .insert("degree".to_string(), Value::VString(degree.to_string()));
            p
        },
        view,
    );

    // Check view_elements directly: all elements should have the degree property
    for (cls, pat) in &view.view_elements {
        if matches!(cls, GraphClass::GNode) {
            assert!(
                pat.value.properties.contains_key("degree"),
                "node should have degree property"
            );
        }
    }
}

// ============================================================================
// para_graph — Pass 1: inter-bucket (shape class) ordering
// ============================================================================

#[test]
fn para_graph_nodes_have_no_sub_elements() {
    let classifier = classifier();
    let graph = from_patterns(&classifier, vec![node("a"), node("b"), rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    // Count sub-element results each element receives
    let sub_counts = para_graph(
        |_query, _element, sub_results: &[usize]| sub_results.len(),
        &view,
    );

    // Nodes are atomic (no syntactic children) → 0 sub-results
    assert_eq!(sub_counts[&Symbol("a".to_string())], 0);
    assert_eq!(sub_counts[&Symbol("b".to_string())], 0);
    // Relationship contains 2 node elements → 2 sub-results
    assert_eq!(sub_counts[&Symbol("r1".to_string())], 2);
}

#[test]
fn para_graph_structural_depth() {
    let classifier = classifier();
    let graph = from_patterns(&classifier, vec![node("a"), node("b"), rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    // Structural depth: no sub-elements → 0; container → max(sub_depths) + 1
    let depths = para_graph(
        |_query, _element, sub_results: &[usize]| {
            sub_results
                .iter()
                .cloned()
                .max()
                .map(|d| d + 1)
                .unwrap_or(0)
        },
        &view,
    );

    // Nodes have no syntactic children → depth 0
    assert_eq!(depths[&Symbol("a".to_string())], 0);
    assert_eq!(depths[&Symbol("b".to_string())], 0);
    // Relationship contains nodes (depth 0) → depth 1
    assert_eq!(depths[&Symbol("r1".to_string())], 1);
}

#[test]
fn para_graph_processes_all_element_types() {
    let classifier = classifier();
    let graph = from_patterns(&classifier, vec![node("a"), node("b"), rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    let results = para_graph(|_query, _element, _sub_results: &[usize]| 1usize, &view);

    // All 3 elements (2 nodes + 1 relationship) should be in the result map
    assert_eq!(results.len(), 3);
    assert!(results.contains_key(&Symbol("a".to_string())));
    assert!(results.contains_key(&Symbol("b".to_string())));
    assert!(results.contains_key(&Symbol("r1".to_string())));
}

// ============================================================================
// para_graph — Pass 2: within-bucket (Kahn's) ordering for GAnnotation
// ============================================================================

#[test]
fn para_graph_annotation_of_annotation_ordering() {
    let classifier = classifier();
    // n: node; b: annotation of n; a: annotation of b
    // Without within-bucket sort, a might be processed before b, getting subResults=[].
    // With Kahn's sort, b is guaranteed to come before a.
    let graph = from_patterns(
        &classifier,
        vec![
            node("n"),
            annot("b", "n"), // GAnnotation, elements=[node("n")]
            annot("a", "b"), // GAnnotation, elements=[node("b")]
        ],
    );
    let view = from_pattern_graph(&classifier, &graph);

    let sub_counts = para_graph(
        |_query, _element, sub_results: &[usize]| sub_results.len(),
        &view,
    );

    // node "n": no sub-elements → 0
    assert_eq!(sub_counts[&Symbol("n".to_string())], 0);
    // annotation "b" annotates node "n" (cross-bucket dep, always satisfied) → 1
    assert_eq!(sub_counts[&Symbol("b".to_string())], 1);
    // annotation "a" annotates annotation "b" (within-bucket dep, satisfied by Kahn's) → 1
    assert_eq!(sub_counts[&Symbol("a".to_string())], 1);
}

#[test]
fn para_graph_cycle_soft_miss() {
    // Use a classifier that puts ALL non-node patterns in GOther so that
    // insert_other is called (unlike insert_annotation, it does NOT recursively
    // extract sub-elements). This ensures the cycle-creating identities exist
    // *only* in the GOther bucket — a clean isolation of within-bucket cycle handling.
    let other_classifier: GraphClassifier<(), Subject> =
        GraphClassifier::new(|p: &Pattern<Subject>| {
            if p.elements.is_empty() {
                GraphClass::GNode
            } else {
                GraphClass::GOther(())
            }
        });

    // a "contains" b, b "contains" a → 2-element cycle in the GOther bucket.
    let pattern_a = Pattern {
        value: subj("a"),
        elements: vec![node("b")],
    };
    let pattern_b = Pattern {
        value: subj("b"),
        elements: vec![node("a")],
    };
    let graph = from_patterns(&other_classifier, vec![pattern_a, pattern_b]);
    let view = from_pattern_graph(&other_classifier, &graph);

    let sub_counts = para_graph(
        |_query, _element, sub_results: &[usize]| sub_results.len(),
        &view,
    );

    // Both elements present in result (no elements dropped, no panic)
    assert_eq!(sub_counts.len(), 2);

    let a = sub_counts[&Symbol("a".to_string())];
    let b = sub_counts[&Symbol("b".to_string())];

    // Cycle members are processed sequentially in encountered order.
    // The first-processed member cannot yet see the other → soft-miss (subResults=[]).
    // The second finds the first already in the accumulator (gets 1).
    // Total intra-cycle sub_count == 1 for a 2-element cycle.
    assert!(
        a == 0 || b == 0,
        "first cycle member must get subResults=[]"
    );
    assert_eq!(
        a + b,
        1,
        "exactly one intra-cycle result visible in a 2-element cycle"
    );
}

// ============================================================================
// para_graph_fixed (convergence)
// ============================================================================

#[test]
fn para_graph_fixed_converges_on_simple_graph() {
    let classifier = classifier();
    let graph = from_patterns(&classifier, vec![node("a"), node("b"), rel("r1", "a", "b")]);
    let view = from_pattern_graph(&classifier, &graph);

    // Structural depth: no sub-elements → 0; container → max(sub_depths) + 1
    let result = para_graph_fixed(
        |old: &usize, new: &usize| old == new,
        |_query, _element, sub_results: &[usize]| {
            sub_results
                .iter()
                .cloned()
                .max()
                .map(|d| d + 1)
                .unwrap_or(0)
        },
        0usize,
        &view,
    );

    // All 3 elements should be in the result
    assert!(result.contains_key(&Symbol("a".to_string())));
    assert!(result.contains_key(&Symbol("b".to_string())));
    assert!(result.contains_key(&Symbol("r1".to_string())));
    // Nodes: depth 0 (no sub-elements)
    assert_eq!(result[&Symbol("a".to_string())], 0);
    assert_eq!(result[&Symbol("b".to_string())], 0);
    // Relationship contains nodes (depth 0) → depth 1
    assert_eq!(result[&Symbol("r1".to_string())], 1);
}

#[test]
fn para_graph_fixed_annotation_of_annotation_converges() {
    let classifier = classifier();
    let graph = from_patterns(
        &classifier,
        vec![node("n"), annot("b", "n"), annot("a", "b")],
    );
    let view = from_pattern_graph(&classifier, &graph);

    let result = para_graph_fixed(
        |old: &usize, new: &usize| old == new,
        |_query, _element, sub_results: &[usize]| {
            sub_results
                .iter()
                .cloned()
                .max()
                .map(|d| d + 1)
                .unwrap_or(0)
        },
        0usize,
        &view,
    );

    assert_eq!(result[&Symbol("n".to_string())], 0); // node: depth 0
    assert_eq!(result[&Symbol("b".to_string())], 1); // annotation of n (depth 0) → 1
    assert_eq!(result[&Symbol("a".to_string())], 2); // annotation of b (depth 1) → 2
}