oxirs-rule 0.3.2

Forward/backward rule engine for RDFS, OWL, and SWRL reasoning
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
//! OWL 2 RL Entailment Engine — implements a subset of the W3C OWL 2 RL profile
//! rules via the [`EntailmentEngine`] trait.
//!
//! OWL 2 RL is a tractable profile of OWL 2 designed for scalable rule-based
//! forward chaining. This module implements the key rules from the W3C OWL 2
//! Profiles specification (https://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs_using_Rules).
//!
//! # Rules implemented
//!
//! | Rule        | Description                                          |
//! |-------------|------------------------------------------------------|
//! | cls-svf1    | someValuesFrom restriction membership                |
//! | cls-avf     | allValuesFrom restriction propagation                |
//! | cls-hv1     | hasValue type entailment                             |
//! | cls-hv2     | hasValue instance matching                           |
//! | cls-int1    | intersectionOf membership (N-ary)                    |
//! | cls-int2    | intersectionOf decomposition                         |
//! | prp-symp    | SymmetricProperty inversion                          |
//! | prp-trp     | TransitiveProperty chaining                          |
//! | prp-eqp1    | equivalentProperty forward                           |
//! | prp-eqp2    | equivalentProperty backward                          |
//! | cax-sco     | subClassOf type propagation                          |
//! | cax-eqc1    | equivalentClass forward                              |
//! | cax-eqc2    | equivalentClass backward                             |
//!
//! # References
//!
//! - <https://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs_using_Rules>
//! - <https://www.w3.org/TR/sparql11-entailment/>

use super::{EntailmentEngine, EntailmentError, Triple, TripleStore};
use crate::entailment::rdf_entailment::{rdfs_iri, rdf_iri};

// ── OWL vocabulary helpers ────────────────────────────────────────────────────

const OWL_NS: &str = "http://www.w3.org/2002/07/owl#";

#[inline]
fn owl_iri(local: &str) -> String {
    format!("{OWL_NS}{local}")
}

// ── OwlRlRuleSet ─────────────────────────────────────────────────────────────

/// Configuration flags to enable or disable individual OWL 2 RL rule groups.
///
/// By default all rule groups are enabled.
#[derive(Debug, Clone)]
pub struct OwlRlRuleSet {
    /// Enable class restriction rules (cls-svf1, cls-avf, cls-hv1/2, cls-int1/2)
    pub class_restrictions: bool,
    /// Enable property characteristic rules (prp-symp, prp-trp)
    pub property_characteristics: bool,
    /// Enable equivalent-property rules (prp-eqp1/2)
    pub equivalent_properties: bool,
    /// Enable class axiom rules (cax-sco, cax-eqc1/2)
    pub class_axioms: bool,
}

impl Default for OwlRlRuleSet {
    fn default() -> Self {
        Self {
            class_restrictions: true,
            property_characteristics: true,
            equivalent_properties: true,
            class_axioms: true,
        }
    }
}

// ── OwlRlEntailmentEngine ─────────────────────────────────────────────────────

/// An [`EntailmentEngine`] that applies the W3C OWL 2 RL profile rules.
///
/// This engine operates on the flat [`TripleStore`] API and performs a single
/// forward-chaining pass. To reach the full OWL 2 RL closure, the caller must
/// iterate via [`crate::entailment::ClosureGraph`] (or [`crate::entailment::EntailmentGraph`])
/// until no new triples are produced.
///
/// The engine also includes RDFS entailment as a subset (cax-sco is equivalent
/// to rdfs9; rdfs rules are applied first so that OWL rules can build on them).
#[derive(Debug, Clone)]
pub struct OwlRlEntailmentEngine {
    /// Which rule groups are active
    pub rule_set: OwlRlRuleSet,
}

impl OwlRlEntailmentEngine {
    /// Create a new `OwlRlEntailmentEngine` with all rule groups enabled.
    pub fn new() -> Self {
        Self {
            rule_set: OwlRlRuleSet::default(),
        }
    }

    /// Create a new engine with a custom rule set.
    pub fn with_rule_set(rule_set: OwlRlRuleSet) -> Self {
        Self { rule_set }
    }

    // ── Helper ────────────────────────────────────────────────────────────

    /// Add a triple to `result` if not already in `store` or `result`.
    fn maybe_add(store: &TripleStore, result: &mut Vec<Triple>, t: Triple) {
        if !store.contains(&t.subject, &t.predicate, &t.object)
            && !result.iter().any(|r| r == &t)
        {
            result.push(t);
        }
    }

    // ── RDFS subset rules (needed as foundation for OWL RL) ───────────────

    /// rdfs9 / cax-sco: `(?c rdfs:subClassOf ?d), (?x rdf:type ?c)` → `(?x rdf:type ?d)`
    fn rule_cax_sco(store: &TripleStore, result: &mut Vec<Triple>) {
        let sub_class = rdfs_iri("subClassOf");
        let rdf_type = rdf_iri("type");

        let class_pairs: Vec<(String, String)> = store
            .get_all_p(&sub_class)
            .into_iter()
            .map(|t| (t.subject.clone(), t.object.clone()))
            .collect();

        for (c, d) in &class_pairs {
            if c == d {
                continue;
            }
            for triple in store.get_all_p(&rdf_type) {
                if &triple.object == c {
                    let inferred = Triple::new(&triple.subject, &rdf_type, d);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }

    // ── Class axiom rules ─────────────────────────────────────────────────

    /// cax-eqc1: `(?c owl:equivalentClass ?d), (?x rdf:type ?c)` → `(?x rdf:type ?d)`
    fn rule_cax_eqc1(store: &TripleStore, result: &mut Vec<Triple>) {
        let equiv_class = owl_iri("equivalentClass");
        let sub_class = rdfs_iri("subClassOf");
        let rdf_type = rdf_iri("type");

        let equiv_pairs: Vec<(String, String)> = store
            .get_all_p(&equiv_class)
            .into_iter()
            .map(|t| (t.subject.clone(), t.object.clone()))
            .collect();

        for (c, d) in &equiv_pairs {
            // equivalentClass implies bidirectional subClassOf
            let t1 = Triple::new(c, &sub_class, d);
            Self::maybe_add(store, result, t1);
            let t2 = Triple::new(d, &sub_class, c);
            Self::maybe_add(store, result, t2);

            // Also forward-apply type membership
            for triple in store.get_all_p(&rdf_type) {
                if &triple.object == c {
                    let inferred = Triple::new(&triple.subject, &rdf_type, d);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }

    /// cax-eqc2: `(?c owl:equivalentClass ?d), (?x rdf:type ?d)` → `(?x rdf:type ?c)`
    fn rule_cax_eqc2(store: &TripleStore, result: &mut Vec<Triple>) {
        let equiv_class = owl_iri("equivalentClass");
        let rdf_type = rdf_iri("type");

        let equiv_pairs: Vec<(String, String)> = store
            .get_all_p(&equiv_class)
            .into_iter()
            .map(|t| (t.subject.clone(), t.object.clone()))
            .collect();

        for (c, d) in &equiv_pairs {
            for triple in store.get_all_p(&rdf_type) {
                if &triple.object == d {
                    let inferred = Triple::new(&triple.subject, &rdf_type, c);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }

    // ── Property characteristic rules ─────────────────────────────────────

    /// prp-symp: `(?p rdf:type owl:SymmetricProperty), (?x ?p ?y)` → `(?y ?p ?x)`
    fn rule_prp_symp(store: &TripleStore, result: &mut Vec<Triple>) {
        let rdf_type = rdf_iri("type");
        let symmetric = owl_iri("SymmetricProperty");

        let sym_props: Vec<String> = store
            .get_all_p(&rdf_type)
            .into_iter()
            .filter(|t| t.object == symmetric)
            .map(|t| t.subject.clone())
            .collect();

        for p in &sym_props {
            for triple in store.get_all_p(p) {
                let inferred = Triple::new(&triple.object, p, &triple.subject);
                Self::maybe_add(store, result, inferred);
            }
        }
    }

    /// prp-trp: `(?p rdf:type owl:TransitiveProperty), (?x ?p ?y), (?y ?p ?z)` → `(?x ?p ?z)`
    fn rule_prp_trp(store: &TripleStore, result: &mut Vec<Triple>) {
        let rdf_type = rdf_iri("type");
        let transitive = owl_iri("TransitiveProperty");

        let trans_props: Vec<String> = store
            .get_all_p(&rdf_type)
            .into_iter()
            .filter(|t| t.object == transitive)
            .map(|t| t.subject.clone())
            .collect();

        for p in &trans_props {
            let pairs: Vec<(String, String)> = store
                .get_all_p(p)
                .into_iter()
                .map(|t| (t.subject.clone(), t.object.clone()))
                .collect();

            for (x, y1) in &pairs {
                for (y2, z) in &pairs {
                    if y1 == y2 && x != z {
                        let inferred = Triple::new(x, p, z);
                        Self::maybe_add(store, result, inferred);
                    }
                }
            }
        }
    }

    // ── Property equivalence rules ────────────────────────────────────────

    /// prp-eqp1: `(?p owl:equivalentProperty ?q), (?x ?p ?y)` → `(?x ?q ?y)`
    fn rule_prp_eqp1(store: &TripleStore, result: &mut Vec<Triple>) {
        let equiv_prop = owl_iri("equivalentProperty");
        let sub_prop = rdfs_iri("subPropertyOf");

        let equiv_pairs: Vec<(String, String)> = store
            .get_all_p(&equiv_prop)
            .into_iter()
            .map(|t| (t.subject.clone(), t.object.clone()))
            .collect();

        for (p, q) in &equiv_pairs {
            // equivalentProperty implies bidirectional subPropertyOf
            let t1 = Triple::new(p, &sub_prop, q);
            Self::maybe_add(store, result, t1);
            let t2 = Triple::new(q, &sub_prop, p);
            Self::maybe_add(store, result, t2);

            // Forward-apply: every triple with predicate p also holds with q
            for triple in store.get_all_p(p) {
                let inferred = Triple::new(&triple.subject, q, &triple.object);
                Self::maybe_add(store, result, inferred);
            }
        }
    }

    /// prp-eqp2: `(?p owl:equivalentProperty ?q), (?x ?q ?y)` → `(?x ?p ?y)`
    fn rule_prp_eqp2(store: &TripleStore, result: &mut Vec<Triple>) {
        let equiv_prop = owl_iri("equivalentProperty");

        let equiv_pairs: Vec<(String, String)> = store
            .get_all_p(&equiv_prop)
            .into_iter()
            .map(|t| (t.subject.clone(), t.object.clone()))
            .collect();

        for (p, q) in &equiv_pairs {
            for triple in store.get_all_p(q) {
                let inferred = Triple::new(&triple.subject, p, &triple.object);
                Self::maybe_add(store, result, inferred);
            }
        }
    }

    // ── Class restriction rules ───────────────────────────────────────────

    /// cls-hv1: `(?x owl:hasValue ?y), (?x owl:onProperty ?p), (?u rdf:type ?x)` → `(?u ?p ?y)`
    fn rule_cls_hv1(store: &TripleStore, result: &mut Vec<Triple>) {
        let has_value = owl_iri("hasValue");
        let on_property = owl_iri("onProperty");
        let rdf_type = rdf_iri("type");

        // Collect hasValue restrictions: restriction → (value, property)
        let restrictions: Vec<(String, String, String)> = {
            let mut v = Vec::new();
            for hv_triple in store.get_all_p(&has_value) {
                let x = &hv_triple.subject;
                let y = &hv_triple.object;
                for op_triple in store.get_by_sp(x, &on_property) {
                    v.push((x.clone(), y.clone(), op_triple.object.clone()));
                }
            }
            v
        };

        for (x, y, p) in &restrictions {
            // For every u rdf:type x, infer u p y
            for triple in store.get_by_po(&rdf_type, x) {
                let u = &triple.subject;
                let inferred = Triple::new(u, p, y);
                Self::maybe_add(store, result, inferred);
            }
        }
    }

    /// cls-hv2: `(?x owl:hasValue ?y), (?x owl:onProperty ?p), (?u ?p ?y)` → `(?u rdf:type ?x)`
    fn rule_cls_hv2(store: &TripleStore, result: &mut Vec<Triple>) {
        let has_value = owl_iri("hasValue");
        let on_property = owl_iri("onProperty");
        let rdf_type = rdf_iri("type");

        let restrictions: Vec<(String, String, String)> = {
            let mut v = Vec::new();
            for hv_triple in store.get_all_p(&has_value) {
                let x = &hv_triple.subject;
                let y = &hv_triple.object;
                for op_triple in store.get_by_sp(x, &on_property) {
                    v.push((x.clone(), y.clone(), op_triple.object.clone()));
                }
            }
            v
        };

        for (x, y, p) in &restrictions {
            // For every u p y, infer u rdf:type x
            for triple in store.get_by_po(p, y) {
                let u = &triple.subject;
                let inferred = Triple::new(u, &rdf_type, x);
                Self::maybe_add(store, result, inferred);
            }
        }
    }

    /// cls-svf1: `(?x owl:someValuesFrom ?y), (?x owl:onProperty ?p),
    ///            (?u ?p ?v), (?v rdf:type ?y)` → `(?u rdf:type ?x)`
    fn rule_cls_svf1(store: &TripleStore, result: &mut Vec<Triple>) {
        let some_values_from = owl_iri("someValuesFrom");
        let on_property = owl_iri("onProperty");
        let rdf_type = rdf_iri("type");

        // restrictions: (x, y=filler, p=property)
        let restrictions: Vec<(String, String, String)> = {
            let mut v = Vec::new();
            for svf_triple in store.get_all_p(&some_values_from) {
                let x = &svf_triple.subject;
                let y = &svf_triple.object;
                for op_triple in store.get_by_sp(x, &on_property) {
                    v.push((x.clone(), y.clone(), op_triple.object.clone()));
                }
            }
            v
        };

        for (x, y, p) in &restrictions {
            // For every (u p v) where v rdf:type y, infer u rdf:type x
            for p_triple in store.get_all_p(p) {
                let u = &p_triple.subject;
                let v = &p_triple.object;
                if store.contains(v, &rdf_type, y) {
                    let inferred = Triple::new(u, &rdf_type, x);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }

    /// cls-avf: `(?x owl:allValuesFrom ?y), (?x owl:onProperty ?p),
    ///           (?u rdf:type ?x), (?u ?p ?v)` → `(?v rdf:type ?y)`
    fn rule_cls_avf(store: &TripleStore, result: &mut Vec<Triple>) {
        let all_values_from = owl_iri("allValuesFrom");
        let on_property = owl_iri("onProperty");
        let rdf_type = rdf_iri("type");

        let restrictions: Vec<(String, String, String)> = {
            let mut v = Vec::new();
            for avf_triple in store.get_all_p(&all_values_from) {
                let x = &avf_triple.subject;
                let y = &avf_triple.object;
                for op_triple in store.get_by_sp(x, &on_property) {
                    v.push((x.clone(), y.clone(), op_triple.object.clone()));
                }
            }
            v
        };

        for (x, y, p) in &restrictions {
            // For every u rdf:type x and u p v, infer v rdf:type y
            for type_triple in store.get_by_po(&rdf_type, x) {
                let u = &type_triple.subject;
                for p_triple in store.get_by_sp(u, p) {
                    let v = &p_triple.object;
                    let inferred = Triple::new(v, &rdf_type, y);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }

    /// cls-int2: `(?c owl:intersectionOf ?list), (?y rdf:type ?c)`
    ///           → `(?y rdf:type ?ci)` for each `?ci` in the intersection list.
    ///
    /// This rule decomposes an intersection class: if something is typed as
    /// an intersection, it must be typed as each component.
    fn rule_cls_int2(store: &TripleStore, result: &mut Vec<Triple>) {
        let intersection_of = owl_iri("intersectionOf");
        let rdf_type = rdf_iri("type");
        let first_pred = rdf_iri("first");
        let rest_pred = rdf_iri("rest");
        let rdf_nil = rdf_iri("nil");

        // For each intersection class c
        for int_triple in store.get_all_p(&intersection_of) {
            let c = &int_triple.subject;
            let mut list_node = int_triple.object.clone();

            // Collect all members of the RDF list
            let mut members: Vec<String> = Vec::new();
            while list_node != rdf_nil {
                let firsts = store.get_by_sp(&list_node, &first_pred);
                if firsts.is_empty() {
                    break;
                }
                members.push(firsts[0].object.clone());
                let rests = store.get_by_sp(&list_node, &rest_pred);
                if rests.is_empty() {
                    break;
                }
                list_node = rests[0].object.clone();
            }

            if members.is_empty() {
                continue;
            }

            // For every y rdf:type c, infer y rdf:type each member
            for type_triple in store.get_by_po(&rdf_type, c) {
                let y = &type_triple.subject;
                for member in &members {
                    let inferred = Triple::new(y, &rdf_type, member);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }

    /// cls-int1: `(?c owl:intersectionOf ?list), members = {c1, c2, ...},
    ///            (?y rdf:type c1), (?y rdf:type c2), ...` → `(?y rdf:type ?c)`
    ///
    /// If something is typed as every member of an intersection, it is typed
    /// as the intersection class.
    fn rule_cls_int1(store: &TripleStore, result: &mut Vec<Triple>) {
        let intersection_of = owl_iri("intersectionOf");
        let rdf_type = rdf_iri("type");
        let first_pred = rdf_iri("first");
        let rest_pred = rdf_iri("rest");
        let rdf_nil = rdf_iri("nil");

        for int_triple in store.get_all_p(&intersection_of) {
            let c = &int_triple.subject;
            let mut list_node = int_triple.object.clone();

            let mut members: Vec<String> = Vec::new();
            while list_node != rdf_nil {
                let firsts = store.get_by_sp(&list_node, &first_pred);
                if firsts.is_empty() {
                    break;
                }
                members.push(firsts[0].object.clone());
                let rests = store.get_by_sp(&list_node, &rest_pred);
                if rests.is_empty() {
                    break;
                }
                list_node = rests[0].object.clone();
            }

            if members.is_empty() {
                continue;
            }

            // Find all individuals that are typed as *every* member of the intersection
            // Start with individuals typed as the first member
            let first_typed: Vec<String> = store
                .get_by_po(&rdf_type, &members[0])
                .into_iter()
                .map(|t| t.subject.clone())
                .collect();

            for y in first_typed {
                let all_typed = members
                    .iter()
                    .all(|m| store.contains(&y, &rdf_type, m));
                if all_typed {
                    let inferred = Triple::new(&y, &rdf_type, c);
                    Self::maybe_add(store, result, inferred);
                }
            }
        }
    }
}

impl Default for OwlRlEntailmentEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl EntailmentEngine for OwlRlEntailmentEngine {
    fn entail(&self, store: &TripleStore) -> Result<Vec<Triple>, EntailmentError> {
        let mut result: Vec<Triple> = Vec::new();

        // Apply RDFS foundation rules first (needed by OWL RL on top)
        if self.rule_set.class_axioms {
            Self::rule_cax_sco(store, &mut result);
        }

        // Apply OWL RL class axiom rules
        if self.rule_set.class_axioms {
            Self::rule_cax_eqc1(store, &mut result);
            Self::rule_cax_eqc2(store, &mut result);
        }

        // Apply property characteristic rules
        if self.rule_set.property_characteristics {
            Self::rule_prp_symp(store, &mut result);
            Self::rule_prp_trp(store, &mut result);
        }

        // Apply equivalent property rules
        if self.rule_set.equivalent_properties {
            Self::rule_prp_eqp1(store, &mut result);
            Self::rule_prp_eqp2(store, &mut result);
        }

        // Apply class restriction rules
        if self.rule_set.class_restrictions {
            Self::rule_cls_hv1(store, &mut result);
            Self::rule_cls_hv2(store, &mut result);
            Self::rule_cls_svf1(store, &mut result);
            Self::rule_cls_avf(store, &mut result);
            Self::rule_cls_int1(store, &mut result);
            Self::rule_cls_int2(store, &mut result);
        }

        Ok(result)
    }
}

// ── EntailmentRegime trait implementation for OwlRlEntailmentEngine ───────────
//
// This allows OwlRlEntailmentEngine to be used with the rich-term EntailmentGraph
// API (via the EntailmentRegime trait) as well as the flat-string ClosureGraph API
// (via the EntailmentEngine trait).

impl super::EntailmentRegime for OwlRlEntailmentEngine {
    fn name(&self) -> &str {
        "OWL 2 RL Entailment"
    }

    fn entail(
        &self,
        triples: &[super::RichEntailmentTriple],
    ) -> Vec<super::RichEntailmentTriple> {
        // Convert rich triples → flat TripleStore, run rules, convert back
        let mut store = TripleStore::new();
        for rt in triples {
            let s = rich_term_to_str(&rt.subject);
            let o = rich_term_to_str(&rt.object);
            store.add(Triple::new(&s, &rt.predicate, &o));
        }

        let flat_results = match EntailmentEngine::entail(self, &store) {
            Ok(v) => v,
            Err(_) => return Vec::new(),
        };

        // Filter out triples that are already present in the input
        flat_results
            .into_iter()
            .filter_map(|t| {
                let already = triples.iter().any(|rt| {
                    let s = rich_term_to_str(&rt.subject);
                    let o = rich_term_to_str(&rt.object);
                    s == t.subject && rt.predicate == t.predicate && o == t.object
                });
                if already {
                    None
                } else {
                    Some(super::RichEntailmentTriple::named_triple(
                        &t.subject,
                        &t.predicate,
                        &t.object,
                    ))
                }
            })
            .collect()
    }

    fn is_consistent(&self, _triples: &[super::RichEntailmentTriple]) -> bool {
        // OWL 2 RL consistency checking is a separate concern (requires full ABox reasoning).
        // A conservative implementation always returns true here.
        true
    }
}

/// Convert a rich [`EntailmentTerm`] to a flat string for use with the flat-string API.
fn rich_term_to_str(term: &super::EntailmentTerm) -> String {
    match term {
        super::EntailmentTerm::NamedNode(s) | super::EntailmentTerm::BlankNode(s) => s.clone(),
        super::EntailmentTerm::Literal { value, .. } => value.clone(),
    }
}

// ── Impl EntailmentRegime for RdfEntailmentEngine ────────────────────────────
//
// Provide EntailmentRegime blanket impls for the other engines so they can also
// be used with the rich-term EntailmentGraph API. Placed here to avoid
// circular module deps (rdf_entailment.rs would need to import regime types).

impl super::EntailmentRegime for super::rdf_entailment::RdfEntailmentEngine {
    fn name(&self) -> &str {
        "RDF Entailment"
    }

    fn entail(
        &self,
        triples: &[super::RichEntailmentTriple],
    ) -> Vec<super::RichEntailmentTriple> {
        let mut store = TripleStore::new();
        for rt in triples {
            let s = rich_term_to_str(&rt.subject);
            let o = rich_term_to_str(&rt.object);
            store.add(Triple::new(&s, &rt.predicate, &o));
        }

        let flat_results = match EntailmentEngine::entail(self, &store) {
            Ok(v) => v,
            Err(_) => return Vec::new(),
        };

        flat_results
            .into_iter()
            .filter_map(|t| {
                let already = triples.iter().any(|rt| {
                    let s = rich_term_to_str(&rt.subject);
                    let o = rich_term_to_str(&rt.object);
                    s == t.subject && rt.predicate == t.predicate && o == t.object
                });
                if already {
                    None
                } else {
                    Some(super::RichEntailmentTriple::named_triple(
                        &t.subject,
                        &t.predicate,
                        &t.object,
                    ))
                }
            })
            .collect()
    }
}

// ── Impl EntailmentRegime for RdfsEntailmentEngine ────────────────────────────

impl super::EntailmentRegime for super::rdfs_entailment::RdfsEntailmentEngine {
    fn name(&self) -> &str {
        "RDFS Entailment"
    }

    fn entail(
        &self,
        triples: &[super::RichEntailmentTriple],
    ) -> Vec<super::RichEntailmentTriple> {
        let mut store = TripleStore::new();
        for rt in triples {
            let s = rich_term_to_str(&rt.subject);
            let o = rich_term_to_str(&rt.object);
            store.add(Triple::new(&s, &rt.predicate, &o));
        }

        let flat_results = match EntailmentEngine::entail(self, &store) {
            Ok(v) => v,
            Err(_) => return Vec::new(),
        };

        flat_results
            .into_iter()
            .filter_map(|t| {
                let already = triples.iter().any(|rt| {
                    let s = rich_term_to_str(&rt.subject);
                    let o = rich_term_to_str(&rt.object);
                    s == t.subject && rt.predicate == t.predicate && o == t.object
                });
                if already {
                    None
                } else {
                    Some(super::RichEntailmentTriple::named_triple(
                        &t.subject,
                        &t.predicate,
                        &t.object,
                    ))
                }
            })
            .collect()
    }
}