oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
//! Subgraph isomorphism / pattern matching for RDF graphs.
//!
//! Implements a VF2-inspired algorithm for finding subgraph matches
//! between a pattern graph and a data graph.

use std::collections::HashMap;

/// A node in an RDF graph with a unique id and label.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RdfNode {
    pub id: u32,
    pub label: String,
}

/// A directed edge in an RDF graph.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RdfEdge {
    pub from: u32,
    pub to: u32,
    pub label: String,
}

/// An RDF graph represented as adjacency maps.
#[derive(Debug, Clone, Default)]
pub struct RdfGraph {
    pub nodes: HashMap<u32, RdfNode>,
    pub edges: Vec<RdfEdge>,
}

/// A single match result: mapping from pattern node id → data node id,
/// plus the indices into `data.edges` for each matched pattern edge.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MatchResult {
    /// pattern_node_id -> data_node_id
    pub node_mapping: HashMap<u32, u32>,
    /// For each pattern edge index, the corresponding data edge index.
    pub edge_mapping: Vec<usize>,
}

/// Subgraph matcher using VF2-inspired backtracking.
pub struct SubgraphMatcher {
    max_results: usize,
}

impl RdfGraph {
    /// Create an empty graph.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a node with the given id and label.
    pub fn add_node(&mut self, id: u32, label: &str) {
        self.nodes.insert(
            id,
            RdfNode {
                id,
                label: label.to_string(),
            },
        );
    }

    /// Add a directed edge from `from` to `to` with the given label.
    pub fn add_edge(&mut self, from: u32, to: u32, label: &str) {
        self.edges.push(RdfEdge {
            from,
            to,
            label: label.to_string(),
        });
    }

    /// Return outgoing neighbour ids of `node_id`.
    pub fn out_neighbors(&self, node_id: u32) -> Vec<u32> {
        self.edges
            .iter()
            .filter(|e| e.from == node_id)
            .map(|e| e.to)
            .collect()
    }

    /// Return incoming neighbour ids of `node_id`.
    pub fn in_neighbors(&self, node_id: u32) -> Vec<u32> {
        self.edges
            .iter()
            .filter(|e| e.to == node_id)
            .map(|e| e.from)
            .collect()
    }

    /// Return all node ids in deterministic order.
    fn node_ids_sorted(&self) -> Vec<u32> {
        let mut ids: Vec<u32> = self.nodes.keys().copied().collect();
        ids.sort_unstable();
        ids
    }
}

// ── Internal state used during backtracking ──────────────────────────────────

struct MatchState<'a> {
    pattern: &'a RdfGraph,
    data: &'a RdfGraph,
    /// pattern_node_id -> data_node_id
    node_map: HashMap<u32, u32>,
    /// data_node_id -> pattern_node_id (reverse)
    rev_map: HashMap<u32, u32>,
    /// Ordered list of pattern node ids that we match one at a time.
    pattern_order: Vec<u32>,
    results: Vec<MatchResult>,
    max_results: usize,
}

impl<'a> MatchState<'a> {
    fn new(
        pattern: &'a RdfGraph,
        data: &'a RdfGraph,
        pattern_order: Vec<u32>,
        max_results: usize,
    ) -> Self {
        Self {
            pattern,
            data,
            node_map: HashMap::new(),
            rev_map: HashMap::new(),
            pattern_order,
            results: Vec::new(),
            max_results,
        }
    }

    /// Depth-first search: try to map `pattern_order[depth]` to some data node.
    fn dfs(&mut self, depth: usize) {
        if self.results.len() >= self.max_results {
            return;
        }
        if depth == self.pattern_order.len() {
            // All pattern nodes mapped — record the result.
            if let Some(result) = self.build_result() {
                self.results.push(result);
            }
            return;
        }

        let p_node_id = self.pattern_order[depth];
        let p_label = match self.pattern.nodes.get(&p_node_id) {
            Some(n) => n.label.clone(),
            None => return,
        };

        let data_candidates: Vec<u32> = self.data.node_ids_sorted();

        for d_node_id in data_candidates {
            if self.results.len() >= self.max_results {
                break;
            }
            // Must not already be mapped.
            if self.rev_map.contains_key(&d_node_id) {
                continue;
            }
            // Labels must match.
            match self.data.nodes.get(&d_node_id) {
                Some(dn) if dn.label == p_label => {}
                _ => continue,
            }
            // Structural feasibility: edges already in the partial mapping must be satisfied.
            if !self.is_feasible(p_node_id, d_node_id) {
                continue;
            }

            // Extend state.
            self.node_map.insert(p_node_id, d_node_id);
            self.rev_map.insert(d_node_id, p_node_id);

            self.dfs(depth + 1);

            // Retract.
            self.node_map.remove(&p_node_id);
            self.rev_map.remove(&d_node_id);
        }
    }

    /// Check that mapping p_node → d_node is consistent with edges already mapped.
    fn is_feasible(&self, p_node: u32, d_node: u32) -> bool {
        // For every already-mapped predecessor of p_node, the corresponding
        // data predecessor of d_node must have the same edge label.
        for p_edge in &self.pattern.edges {
            if p_edge.to == p_node {
                // p_edge.from → p_node  (incoming to p_node)
                if let Some(&d_from) = self.node_map.get(&p_edge.from) {
                    // There must be an edge d_from → d_node with the same label.
                    if !self
                        .data
                        .edges
                        .iter()
                        .any(|e| e.from == d_from && e.to == d_node && e.label == p_edge.label)
                    {
                        return false;
                    }
                }
            }
            if p_edge.from == p_node {
                // p_node → p_edge.to  (outgoing from p_node)
                if let Some(&d_to) = self.node_map.get(&p_edge.to) {
                    // There must be an edge d_node → d_to with the same label.
                    if !self
                        .data
                        .edges
                        .iter()
                        .any(|e| e.from == d_node && e.to == d_to && e.label == p_edge.label)
                    {
                        return false;
                    }
                }
            }
        }
        true
    }

    /// Build a MatchResult from the current full node mapping.
    fn build_result(&self) -> Option<MatchResult> {
        let mut edge_mapping = Vec::with_capacity(self.pattern.edges.len());
        for p_edge in &self.pattern.edges {
            let d_from = *self.node_map.get(&p_edge.from)?;
            let d_to = *self.node_map.get(&p_edge.to)?;
            let idx = self
                .data
                .edges
                .iter()
                .position(|e| e.from == d_from && e.to == d_to && e.label == p_edge.label)?;
            edge_mapping.push(idx);
        }
        Some(MatchResult {
            node_mapping: self.node_map.clone(),
            edge_mapping,
        })
    }
}

impl SubgraphMatcher {
    /// Create a new matcher that returns at most `max_results` matches.
    pub fn new(max_results: usize) -> Self {
        Self { max_results }
    }

    /// Find all subgraph isomorphism matches of `pattern` in `data`.
    pub fn find_matches(&self, pattern: &RdfGraph, data: &RdfGraph) -> Vec<MatchResult> {
        if pattern.nodes.is_empty() {
            // Empty pattern matches trivially once.
            return vec![MatchResult {
                node_mapping: HashMap::new(),
                edge_mapping: Vec::new(),
            }];
        }
        // Order pattern nodes: start with highest-degree node.
        let order = Self::compute_match_order(pattern);
        let mut state = MatchState::new(pattern, data, order, self.max_results);
        state.dfs(0);
        state.results
    }

    /// Test whether `g1` and `g2` are exactly isomorphic (same size, bijective mapping).
    pub fn is_isomorphic(&self, g1: &RdfGraph, g2: &RdfGraph) -> bool {
        if g1.nodes.len() != g2.nodes.len() || g1.edges.len() != g2.edges.len() {
            return false;
        }
        let matcher = SubgraphMatcher::new(1);
        !matcher.find_matches(g1, g2).is_empty()
    }

    /// Count the number of subgraph matches of `pattern` in `data`.
    pub fn count_matches(&self, pattern: &RdfGraph, data: &RdfGraph) -> usize {
        let unlimited = SubgraphMatcher::new(usize::MAX);
        unlimited.find_matches(pattern, data).len()
    }

    /// Compute a good matching order: start from the node with the highest
    /// combined degree, then extend to connected neighbours.
    fn compute_match_order(pattern: &RdfGraph) -> Vec<u32> {
        if pattern.nodes.is_empty() {
            return Vec::new();
        }
        // Degree = out-degree + in-degree.
        let mut degree: HashMap<u32, usize> = pattern.nodes.keys().map(|&k| (k, 0)).collect();
        for edge in &pattern.edges {
            *degree.entry(edge.from).or_insert(0) += 1;
            *degree.entry(edge.to).or_insert(0) += 1;
        }
        let mut sorted: Vec<u32> = degree.keys().copied().collect();
        sorted.sort_unstable_by(|a, b| degree[b].cmp(&degree[a]).then_with(|| a.cmp(b)));

        // BFS-style extension so the order stays connected.
        let mut order = Vec::with_capacity(sorted.len());
        let mut visited: std::collections::HashSet<u32> = std::collections::HashSet::new();
        if let Some(&first) = sorted.first() {
            let mut queue = std::collections::VecDeque::new();
            queue.push_back(first);
            visited.insert(first);
            while let Some(node) = queue.pop_front() {
                order.push(node);
                // Add neighbours (out and in) in sorted order.
                let mut neighbours: Vec<u32> = pattern
                    .edges
                    .iter()
                    .filter(|e| e.from == node || e.to == node)
                    .flat_map(|e| [e.from, e.to])
                    .filter(|n| !visited.contains(n))
                    .collect();
                neighbours.sort_unstable();
                neighbours.dedup();
                for n in neighbours {
                    if visited.insert(n) {
                        queue.push_back(n);
                    }
                }
            }
        }
        // Append any isolated nodes not reachable from BFS start.
        for n in sorted {
            if !visited.contains(&n) {
                order.push(n);
            }
        }
        order
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn make_matcher() -> SubgraphMatcher {
        SubgraphMatcher::new(100)
    }

    // ── Basic graph helpers ───────────────────────────────────────────────

    fn single_node_graph(label: &str) -> RdfGraph {
        let mut g = RdfGraph::new();
        g.add_node(0, label);
        g
    }

    fn single_edge_graph(from_label: &str, to_label: &str, edge_label: &str) -> RdfGraph {
        let mut g = RdfGraph::new();
        g.add_node(0, from_label);
        g.add_node(1, to_label);
        g.add_edge(0, 1, edge_label);
        g
    }

    // ── Empty / trivial cases ─────────────────────────────────────────────

    #[test]
    fn test_empty_pattern_empty_data() {
        let m = make_matcher();
        let p = RdfGraph::new();
        let d = RdfGraph::new();
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
        assert!(r[0].node_mapping.is_empty());
    }

    #[test]
    fn test_empty_pattern_nonempty_data() {
        let m = make_matcher();
        let p = RdfGraph::new();
        let mut d = RdfGraph::new();
        d.add_node(0, "A");
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
    }

    #[test]
    fn test_single_node_match() {
        let m = make_matcher();
        let p = single_node_graph("A");
        let d = single_node_graph("A");
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
        assert_eq!(r[0].node_mapping[&0], 0);
    }

    #[test]
    fn test_single_node_label_mismatch() {
        let m = make_matcher();
        let p = single_node_graph("A");
        let d = single_node_graph("B");
        assert!(m.find_matches(&p, &d).is_empty());
    }

    #[test]
    fn test_single_node_multiple_candidates() {
        let m = make_matcher();
        let p = single_node_graph("A");
        let mut d = RdfGraph::new();
        d.add_node(0, "A");
        d.add_node(1, "B");
        d.add_node(2, "A");
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 2);
    }

    // ── Single-edge patterns ──────────────────────────────────────────────

    #[test]
    fn test_single_edge_exact_match() {
        let m = make_matcher();
        let p = single_edge_graph("S", "O", "p");
        let d = single_edge_graph("S", "O", "p");
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
    }

    #[test]
    fn test_single_edge_label_mismatch() {
        let m = make_matcher();
        let p = single_edge_graph("S", "O", "p");
        let d = single_edge_graph("S", "O", "q");
        assert!(m.find_matches(&p, &d).is_empty());
    }

    #[test]
    fn test_single_edge_node_label_mismatch() {
        let m = make_matcher();
        let p = single_edge_graph("S", "O", "p");
        let d = single_edge_graph("X", "O", "p");
        assert!(m.find_matches(&p, &d).is_empty());
    }

    #[test]
    fn test_single_edge_multiple_matches() {
        let m = make_matcher();
        let p = single_edge_graph("A", "B", "r");
        let mut d = RdfGraph::new();
        d.add_node(0, "A");
        d.add_node(1, "B");
        d.add_node(2, "A");
        d.add_node(3, "B");
        d.add_edge(0, 1, "r");
        d.add_edge(2, 3, "r");
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 2);
    }

    // ── Path patterns ─────────────────────────────────────────────────────

    #[test]
    fn test_path_pattern_match() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "A");
        p.add_node(1, "B");
        p.add_node(2, "C");
        p.add_edge(0, 1, "e1");
        p.add_edge(1, 2, "e2");

        let mut d = RdfGraph::new();
        d.add_node(10, "A");
        d.add_node(11, "B");
        d.add_node(12, "C");
        d.add_edge(10, 11, "e1");
        d.add_edge(11, 12, "e2");

        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
    }

    #[test]
    fn test_path_pattern_no_match_wrong_order() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "A");
        p.add_node(1, "B");
        p.add_edge(0, 1, "e");

        let mut d = RdfGraph::new();
        d.add_node(0, "B");
        d.add_node(1, "A");
        d.add_edge(0, 1, "e"); // B→A not A→B

        assert!(m.find_matches(&p, &d).is_empty());
    }

    // ── Star patterns ─────────────────────────────────────────────────────

    #[test]
    fn test_star_pattern_match() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "hub");
        p.add_node(1, "leaf");
        p.add_node(2, "leaf");
        p.add_edge(0, 1, "spoke");
        p.add_edge(0, 2, "spoke");

        let mut d = RdfGraph::new();
        d.add_node(0, "hub");
        d.add_node(1, "leaf");
        d.add_node(2, "leaf");
        d.add_edge(0, 1, "spoke");
        d.add_edge(0, 2, "spoke");

        let r = m.find_matches(&p, &d);
        assert!(!r.is_empty());
    }

    #[test]
    fn test_star_pattern_insufficient_leaves() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "hub");
        p.add_node(1, "leaf");
        p.add_node(2, "leaf");
        p.add_node(3, "leaf");
        p.add_edge(0, 1, "spoke");
        p.add_edge(0, 2, "spoke");
        p.add_edge(0, 3, "spoke");

        let mut d = RdfGraph::new();
        d.add_node(0, "hub");
        d.add_node(1, "leaf");
        d.add_node(2, "leaf"); // only 2 leaves
        d.add_edge(0, 1, "spoke");
        d.add_edge(0, 2, "spoke");

        assert!(m.find_matches(&p, &d).is_empty());
    }

    // ── Cycle patterns ────────────────────────────────────────────────────

    #[test]
    fn test_triangle_cycle_match() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "N");
        p.add_node(1, "N");
        p.add_node(2, "N");
        p.add_edge(0, 1, "e");
        p.add_edge(1, 2, "e");
        p.add_edge(2, 0, "e");

        let mut d = RdfGraph::new();
        d.add_node(0, "N");
        d.add_node(1, "N");
        d.add_node(2, "N");
        d.add_edge(0, 1, "e");
        d.add_edge(1, 2, "e");
        d.add_edge(2, 0, "e");

        let r = m.find_matches(&p, &d);
        assert!(!r.is_empty());
    }

    #[test]
    fn test_cycle_not_in_path() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "N");
        p.add_node(1, "N");
        p.add_edge(0, 1, "e");
        p.add_edge(1, 0, "e"); // bidirectional = cycle

        let mut d = RdfGraph::new();
        d.add_node(0, "N");
        d.add_node(1, "N");
        d.add_edge(0, 1, "e"); // only one direction

        assert!(m.find_matches(&p, &d).is_empty());
    }

    // ── max_results limit ─────────────────────────────────────────────────

    #[test]
    fn test_max_results_limit() {
        let m = SubgraphMatcher::new(2);
        let p = single_node_graph("X");
        let mut d = RdfGraph::new();
        for i in 0..10u32 {
            d.add_node(i, "X");
        }
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 2);
    }

    #[test]
    fn test_max_results_zero() {
        let m = SubgraphMatcher::new(0);
        let p = single_node_graph("X");
        let d = single_node_graph("X");
        // With max_results=0, the empty-pattern branch always returns 1 trivial result,
        // but for non-empty patterns we expect 0.
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 0);
    }

    // ── is_isomorphic ─────────────────────────────────────────────────────

    #[test]
    fn test_isomorphic_empty_graphs() {
        let m = make_matcher();
        assert!(m.is_isomorphic(&RdfGraph::new(), &RdfGraph::new()));
    }

    #[test]
    fn test_isomorphic_single_node() {
        let m = make_matcher();
        let g1 = single_node_graph("A");
        let g2 = single_node_graph("A");
        assert!(m.is_isomorphic(&g1, &g2));
    }

    #[test]
    fn test_isomorphic_different_labels() {
        let m = make_matcher();
        let g1 = single_node_graph("A");
        let g2 = single_node_graph("B");
        assert!(!m.is_isomorphic(&g1, &g2));
    }

    #[test]
    fn test_isomorphic_different_sizes() {
        let m = make_matcher();
        let mut g1 = RdfGraph::new();
        g1.add_node(0, "A");
        g1.add_node(1, "B");
        let g2 = single_node_graph("A");
        assert!(!m.is_isomorphic(&g1, &g2));
    }

    #[test]
    fn test_isomorphic_edge_graphs() {
        let m = make_matcher();
        let g1 = single_edge_graph("A", "B", "p");
        let g2 = single_edge_graph("A", "B", "p");
        assert!(m.is_isomorphic(&g1, &g2));
    }

    #[test]
    fn test_not_isomorphic_edge_label_differs() {
        let m = make_matcher();
        let g1 = single_edge_graph("A", "B", "p");
        let g2 = single_edge_graph("A", "B", "q");
        assert!(!m.is_isomorphic(&g1, &g2));
    }

    // ── count_matches ─────────────────────────────────────────────────────

    #[test]
    fn test_count_matches_zero() {
        let m = make_matcher();
        let p = single_node_graph("Z");
        let d = single_node_graph("A");
        assert_eq!(m.count_matches(&p, &d), 0);
    }

    #[test]
    fn test_count_matches_many() {
        let m = make_matcher();
        let p = single_node_graph("N");
        let mut d = RdfGraph::new();
        for i in 0..5u32 {
            d.add_node(i, "N");
        }
        assert_eq!(m.count_matches(&p, &d), 5);
    }

    // ── out/in neighbor methods ───────────────────────────────────────────

    #[test]
    fn test_out_neighbors() {
        let g = single_edge_graph("A", "B", "p");
        let out = g.out_neighbors(0);
        assert_eq!(out, vec![1]);
    }

    #[test]
    fn test_in_neighbors() {
        let g = single_edge_graph("A", "B", "p");
        let inn = g.in_neighbors(1);
        assert_eq!(inn, vec![0]);
    }

    #[test]
    fn test_no_neighbors() {
        let g = single_node_graph("A");
        assert!(g.out_neighbors(0).is_empty());
        assert!(g.in_neighbors(0).is_empty());
    }

    // ── edge_mapping correctness ──────────────────────────────────────────

    #[test]
    fn test_edge_mapping_single_edge() {
        let m = make_matcher();
        let p = single_edge_graph("A", "B", "p");
        let d = single_edge_graph("A", "B", "p");
        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
        assert_eq!(r[0].edge_mapping, vec![0usize]);
    }

    #[test]
    fn test_edge_mapping_two_edges() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "A");
        p.add_node(1, "B");
        p.add_node(2, "C");
        p.add_edge(0, 1, "e");
        p.add_edge(1, 2, "e");

        let mut d = RdfGraph::new();
        d.add_node(10, "A");
        d.add_node(11, "B");
        d.add_node(12, "C");
        d.add_edge(10, 11, "e");
        d.add_edge(11, 12, "e");

        let r = m.find_matches(&p, &d);
        assert_eq!(r.len(), 1);
        assert_eq!(r[0].edge_mapping.len(), 2);
    }

    // ── Subgraph match (pattern smaller than data) ────────────────────────

    #[test]
    fn test_subgraph_match_smaller_pattern() {
        let m = make_matcher();
        // Pattern: node(N) → node(N) with edge label "knows"
        // Both endpoints have label "N" so multiple matches are possible.
        let mut p = RdfGraph::new();
        p.add_node(0, "N");
        p.add_node(1, "N");
        p.add_edge(0, 1, "knows");

        let mut d = RdfGraph::new();
        d.add_node(0, "N");
        d.add_node(1, "N");
        d.add_node(2, "N");
        d.add_edge(0, 1, "knows");
        d.add_edge(1, 2, "knows");

        let r = m.find_matches(&p, &d);
        // Should find 0→1 and 1→2 (and also 0→2 if that edge existed, but it doesn't).
        // Two directed edges N→N means 2 matches.
        assert_eq!(r.len(), 2);
    }

    #[test]
    fn test_large_data_no_match() {
        let m = make_matcher();
        let p = single_edge_graph("X", "Y", "special");
        let mut d = RdfGraph::new();
        for i in 0..10u32 {
            d.add_node(i, if i % 2 == 0 { "X" } else { "Y" });
        }
        for i in (0..10u32).step_by(2) {
            d.add_edge(i, i + 1, "other"); // wrong edge label
        }
        assert!(m.find_matches(&p, &d).is_empty());
    }

    #[test]
    fn test_isolated_nodes_in_pattern() {
        let m = make_matcher();
        let mut p = RdfGraph::new();
        p.add_node(0, "A");
        p.add_node(1, "B"); // isolated

        let mut d = RdfGraph::new();
        d.add_node(0, "A");
        d.add_node(1, "B");
        d.add_node(2, "C");

        let r = m.find_matches(&p, &d);
        // (0→0,1→1) and (0→0,1→2 — but label C≠B so no) → only 1
        assert!(!r.is_empty());
    }
}