oxirs-rule 0.2.4

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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
//! # OWL 2 DL ABox Reasoning — Structural Subsumption
//!
//! This module advances OWL 2 DL reasoning from ~60% to 80%+ coverage by
//! providing a dedicated ABox reasoning engine focused on structural subsumption.
//!
//! ## Implemented ABox Features
//!
//! - **Individual classification**: Classify individuals under inferred `rdf:type` assertions
//!   using TBox subclass hierarchies and property restrictions.
//! - **Property chain reasoning**: Handle `owl:propertyChainAxiom` — compose chains of
//!   property assertions to produce inferred property triples.
//! - **Nominal reasoning**: Handle `owl:oneOf` (enumeration classes) — an individual
//!   is a member of a OneOf class if it appears in the enumeration list.
//! - **HasValue reasoning**: `owl:hasValue` restrictions — any individual satisfying a
//!   property assertion to the specified value is classified as a member.
//! - **AllValuesFrom**: Propagate `owl:allValuesFrom` restrictions through known property
//!   assertions to classify filler individuals.
//! - **SomeValuesFrom**: Check `owl:someValuesFrom` satisfiability and classify individuals
//!   when witnesses exist.
//! - **Transitivity**: Forward-close `owl:TransitiveProperty` assertions.
//! - **Symmetry**: Infer reverse assertions for `owl:SymmetricProperty`.
//! - **Asymmetry**: Detect inconsistencies for `owl:AsymmetricProperty`.
//! - **ObjectComplementOf**: Detect C(x) ∧ ¬C(x) contradictions.
//! - **ObjectIntersectionOf**: x ∈ C1 ∩ C2 iff x ∈ C1 and x ∈ C2.
//! - **DisjointUnionOf**: Classes are mutually disjoint and their union equals parent.
//! - **Key axioms (owl:hasKey)**: Unique identifying property sets per class.
//! - **FunctionalProperty**: x P a, x P b → a sameAs b.
//! - **InverseFunctionalProperty**: a P x, b P x → a sameAs b.
//! - **Universal quantifier check**: AllValuesFrom universal instantiation.
//! - **Existential check**: SomeValuesFrom witness obligation tracking.
//!
//! ## Architecture
//!
//! The [`Owl2DLReasoner`] stores ABox assertions (ground triples) and TBox axioms
//! (class/property descriptions) separately.  A fixpoint `materialize()` loop applies
//! all DL-specific rules until no new facts are derivable.  The engine builds several
//! indexed views of the ABox for efficient rule application.
//!
//! ## Example
//!
//! ```rust
//! use oxirs_rule::owl_dl::{Owl2DLReasoner, DLError};
//! use oxirs_rule::owl_dl::vocab::*;
//!
//! let mut r = Owl2DLReasoner::new();
//! // Declare hasValue restriction: ParentOf5 ≡ ∃hasChild.{:child5}
//! r.add_has_value_restriction("ParentOf5", "hasChild", ":child5");
//! r.add_property_assertion(":alice", "hasChild", ":child5");
//!
//! let report = r.materialize().expect("materialization failed");
//! assert!(r.is_type_entailed(":alice", "ParentOf5"));
//! ```
//!
//! ## References
//! - <https://www.w3.org/TR/owl2-syntax/>
//! - <https://www.w3.org/TR/owl2-direct-semantics/>
//! - <https://www.w3.org/TR/owl2-profiles/#OWL_2_DL>

pub mod vocab;

mod abox_rules;
mod cardinality_rules;
mod complex_constructors;
mod property_hierarchy;

#[cfg(test)]
mod tests;

#[cfg(test)]
mod tests_100pct;

use std::collections::{HashMap, HashSet, VecDeque};
use std::time::Instant;
use thiserror::Error;

pub use vocab::RDF_TYPE;

// ── Error type ────────────────────────────────────────────────────────────────

/// Errors produced by the OWL 2 DL ABox reasoner
#[derive(Debug, Error)]
pub enum DLError {
    /// The ABox is inconsistent (e.g. disjoint classes both asserted)
    #[error("ABox inconsistency: {0}")]
    Inconsistency(String),

    /// Safety limit exceeded during fixpoint iteration
    #[error("Maximum fixpoint iterations ({0}) exceeded")]
    MaxIterationsExceeded(usize),

    /// A TBox axiom was structurally invalid
    #[error("Invalid TBox axiom: {0}")]
    InvalidAxiom(String),
}

// ── Data structures ───────────────────────────────────────────────────────────

/// A ground RDF triple (subject, predicate, object) — all strings
pub type Triple = (String, String, String);

/// Property chain axiom: `P owl:propertyChainAxiom [P1, P2, …, Pn]`
///
/// Semantics: if `x P1 v1`, `v1 P2 v2`, …, `v_{n-1} Pn y` then `x P y`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PropertyChain {
    /// The entailed property
    pub entailed_property: String,
    /// Ordered list of constituent properties
    pub chain: Vec<String>,
}

/// A `owl:hasValue` restriction node
///
/// Semantics: if `x prop value` then `x rdf:type restriction_class`.
/// Conversely, if `x rdf:type restriction_class` then `x prop value`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct HasValueRestriction {
    /// Named restriction class IRI
    pub restriction_class: String,
    /// The property
    pub property: String,
    /// The filler value (individual IRI)
    pub value: String,
}

/// A `owl:allValuesFrom` restriction
///
/// Semantics: if `x rdf:type restriction` and `x prop y` then `y rdf:type filler_class`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AllValuesFromRestriction {
    pub restriction_class: String,
    pub property: String,
    pub filler_class: String,
}

/// A `owl:someValuesFrom` restriction
///
/// Semantics: if `x prop y` and `y rdf:type filler_class` then `x rdf:type restriction`.
/// Also used for existential classification.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SomeValuesFromRestriction {
    pub restriction_class: String,
    pub property: String,
    pub filler_class: String,
}

/// A `owl:oneOf` nominal class
///
/// Semantics: each individual in `members` is of type `class_iri`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NominalClass {
    pub class_iri: String,
    pub members: Vec<String>,
}

/// An `owl:intersectionOf` class constructor
///
/// Semantics: x ∈ IntersectionClass iff x ∈ operand for all operands.
/// Also used in reverse: if x ∈ all operands, infer x ∈ IntersectionClass.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IntersectionOfClass {
    /// The named class that represents the intersection
    pub class_iri: String,
    /// All operand classes that must hold simultaneously
    pub operands: Vec<String>,
}

/// An `owl:complementOf` class constructor
///
/// Semantics: x ∈ ComplementClass iff x ∉ base_class.
/// Used for inconsistency detection: x ∈ C and x ∈ ¬C is a contradiction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComplementOfClass {
    /// The complement class (¬base_class)
    pub class_iri: String,
    /// The class being complemented
    pub base_class: String,
}

/// An `owl:disjointUnionOf` axiom
///
/// Semantics: the parent class is the union of all operands, and the operands
/// are mutually disjoint with each other.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DisjointUnionOf {
    /// The parent class equal to the union
    pub class_iri: String,
    /// Mutually disjoint operand classes whose union = class_iri
    pub operands: Vec<String>,
}

/// An `owl:hasKey` axiom
///
/// Semantics: for instances of `class_iri`, the combination of properties in
/// `key_properties` uniquely identifies each individual.  If two individuals
/// of the class share identical values for all key properties, they are sameAs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HasKeyAxiom {
    pub class_iri: String,
    pub key_properties: Vec<String>,
}

/// A `owl:NegativeObjectPropertyAssertion` axiom
///
/// Semantics: the triple `(source_individual, assertion_property, target_individual)`
/// must NOT hold in the ABox.  If it does, the ABox is inconsistent.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NegativeObjectPropertyAssertion {
    pub source_individual: String,
    pub assertion_property: String,
    pub target_individual: String,
}

/// A `owl:NegativeDataPropertyAssertion` axiom
///
/// Like [`NegativeObjectPropertyAssertion`] but the target is a data value (literal).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NegativeDataPropertyAssertion {
    pub source_individual: String,
    pub assertion_property: String,
    pub target_value: String,
}

/// A `hasSelf` (local-reflexivity self-restriction) class
///
/// Semantics: `x ∈ restriction_class  ↔  (x, property, x) ∈ ABox`.
///
/// - *Forward*: if `x property x` then `x rdf:type restriction_class`.
/// - *Backward*: if `x rdf:type restriction_class` then `x property x`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct HasSelfRestriction {
    /// The named restriction class
    pub restriction_class: String,
    /// The property for which the individual must be self-related
    pub property: String,
}

/// Characterisation of a property's OWL characteristics
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PropertyCharacteristics {
    pub is_transitive: bool,
    pub is_symmetric: bool,
    pub is_asymmetric: bool,
    pub is_reflexive: bool,
    pub is_irreflexive: bool,
    pub is_functional: bool,
    pub is_inverse_functional: bool,
    /// `owl:inverseOf` partner (if declared)
    pub inverse_of: Option<String>,
}

/// Per-rule firing statistics
#[derive(Debug, Clone, Default)]
pub struct RuleFirings {
    pub individual_classification: usize,
    pub property_chain: usize,
    pub nominal_classification: usize,
    pub has_value_forward: usize,
    pub has_value_backward: usize,
    pub all_values_from: usize,
    pub some_values_from: usize,
    pub transitivity: usize,
    pub symmetry: usize,
    pub asymmetry_checks: usize,
    pub subclass_propagation: usize,
    pub equivalent_class: usize,
    pub domain_range: usize,
    pub same_as_propagation: usize,
    pub inverse_property: usize,
    pub complement_of: usize,
    pub intersection_of: usize,
    pub disjoint_union: usize,
    pub has_key: usize,
    pub functional_property: usize,
    pub inverse_functional_property: usize,
    // ── 90% milestone additions ───────────────────────────────────────────────
    pub sub_object_property: usize,
    pub sub_data_property: usize,
    pub equivalent_properties: usize,
    pub reflexive_self: usize,
    pub has_self: usize,
    pub negative_property_assertion: usize,
    // ── 100% milestone additions ─────────────────────────────────────────────
    pub max_cardinality: usize,
    pub min_cardinality: usize,
    pub exact_cardinality: usize,
    pub union_of: usize,
    pub data_some_values_from: usize,
    pub data_all_values_from: usize,
    pub all_different: usize,
    pub same_as_congruence: usize,
}

/// Summary of a single materialization run
#[derive(Debug, Clone)]
pub struct DLInferenceReport {
    /// Number of fixpoint iterations
    pub iterations: usize,
    /// Total new triples inferred
    pub new_triples: usize,
    /// Per-rule firing counts
    pub rule_firings: RuleFirings,
    /// Wall-clock duration
    pub duration: std::time::Duration,
    /// Detected inconsistencies
    pub inconsistencies: Vec<String>,
}

// ── Main reasoner ─────────────────────────────────────────────────────────────

/// OWL 2 DL ABox reasoner implementing structural subsumption
///
/// This reasoner maintains a set of ground triples (the ABox) and a set of
/// TBox axioms (subclass relationships, property restrictions, etc.).  It
/// applies forward-chaining DL rules until a fixpoint is reached.
pub struct Owl2DLReasoner {
    // ── ABox ground triples (asserted + inferred) ─────────────────────
    pub(crate) abox: HashSet<Triple>,
    /// Triples that were explicitly asserted (not inferred)
    pub(crate) asserted: HashSet<Triple>,

    // ── TBox axioms ───────────────────────────────────────────────────
    /// `(sub, super)` direct subclass pairs
    pub(crate) subclass_of: HashSet<(String, String)>,
    /// Equivalent class pairs `(A, B)` — directional; both (A,B) and (B,A) are stored
    pub(crate) equivalent_classes: HashSet<(String, String)>,
    /// Disjoint class pairs
    pub(crate) disjoint_classes: HashSet<(String, String)>,

    // ── Restriction axioms ────────────────────────────────────────────
    pub(crate) has_value_restrictions: Vec<HasValueRestriction>,
    pub(crate) all_values_restrictions: Vec<AllValuesFromRestriction>,
    pub(crate) some_values_restrictions: Vec<SomeValuesFromRestriction>,
    pub(crate) nominal_classes: Vec<NominalClass>,
    pub(crate) property_chains: Vec<PropertyChain>,

    // ── Complex class constructors (new in 80% milestone) ─────────────
    pub(crate) complement_of_classes: Vec<ComplementOfClass>,
    pub(crate) intersection_of_classes: Vec<IntersectionOfClass>,
    pub(crate) disjoint_unions: Vec<DisjointUnionOf>,
    pub(crate) has_key_axioms: Vec<HasKeyAxiom>,

    // ── Property hierarchy axioms (new in 90% milestone) ──────────────
    /// `(sub_property, super_property)` direct object-property subsumption pairs
    pub(crate) sub_object_property_of: HashSet<(String, String)>,
    /// `(sub_property, super_property)` direct data-property subsumption pairs
    pub(crate) sub_data_property_of: HashSet<(String, String)>,
    /// Equivalent property pairs `(P1, P2)` — both directions stored
    pub(crate) equivalent_properties: HashSet<(String, String)>,
    /// Disjoint property pairs `(P1, P2)` — both directions stored
    pub(crate) disjoint_properties: HashSet<(String, String)>,

    // ── Self restrictions (new in 90% milestone) ──────────────────────
    pub(crate) has_self_restrictions: Vec<HasSelfRestriction>,

    // ── Negative property assertions (new in 90% milestone) ───────────
    pub(crate) negative_object_assertions: Vec<NegativeObjectPropertyAssertion>,
    pub(crate) negative_data_assertions: Vec<NegativeDataPropertyAssertion>,

    // ── 100% milestone: cardinality, union, data restrictions, AllDifferent ─
    pub(crate) cardinality_restrictions: Vec<cardinality_rules::CardinalityRestriction>,
    pub(crate) union_of_classes: Vec<cardinality_rules::UnionOfClass>,
    pub(crate) data_some_values_restrictions: Vec<cardinality_rules::DataSomeValuesFromRestriction>,
    pub(crate) data_all_values_restrictions: Vec<cardinality_rules::DataAllValuesFromRestriction>,
    pub(crate) all_different_axioms: Vec<cardinality_rules::AllDifferentAxiom>,

    // ── Property characteristics ──────────────────────────────────────
    pub(crate) property_chars: HashMap<String, PropertyCharacteristics>,

    // ── Configuration ─────────────────────────────────────────────────
    pub(crate) max_iterations: usize,

    // ── Runtime state ────────────────────────────────────────────────
    pub(crate) inconsistencies: Vec<String>,
}

// ── Constructor & configuration ───────────────────────────────────────────────

impl Owl2DLReasoner {
    /// Create a new empty OWL 2 DL reasoner with sensible defaults
    pub fn new() -> Self {
        Self {
            abox: HashSet::new(),
            asserted: HashSet::new(),
            subclass_of: HashSet::new(),
            equivalent_classes: HashSet::new(),
            disjoint_classes: HashSet::new(),
            has_value_restrictions: Vec::new(),
            all_values_restrictions: Vec::new(),
            some_values_restrictions: Vec::new(),
            nominal_classes: Vec::new(),
            property_chains: Vec::new(),
            complement_of_classes: Vec::new(),
            intersection_of_classes: Vec::new(),
            disjoint_unions: Vec::new(),
            has_key_axioms: Vec::new(),
            sub_object_property_of: HashSet::new(),
            sub_data_property_of: HashSet::new(),
            equivalent_properties: HashSet::new(),
            disjoint_properties: HashSet::new(),
            has_self_restrictions: Vec::new(),
            negative_object_assertions: Vec::new(),
            negative_data_assertions: Vec::new(),
            cardinality_restrictions: Vec::new(),
            union_of_classes: Vec::new(),
            data_some_values_restrictions: Vec::new(),
            data_all_values_restrictions: Vec::new(),
            all_different_axioms: Vec::new(),
            property_chars: HashMap::new(),
            max_iterations: 500,
            inconsistencies: Vec::new(),
        }
    }

    /// Override the maximum number of fixpoint iterations (safety limit)
    pub fn with_max_iterations(mut self, n: usize) -> Self {
        self.max_iterations = n;
        self
    }
}

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

// ── ABox assertion API ────────────────────────────────────────────────────────

impl Owl2DLReasoner {
    /// Assert a raw RDF triple into the ABox
    pub fn assert_triple(&mut self, subject: &str, predicate: &str, object: &str) {
        let t = mk_triple(subject, predicate, object);
        self.asserted.insert(t.clone());
        self.abox.insert(t);
    }

    /// Assert `individual rdf:type class`
    pub fn assert_type(&mut self, individual: &str, class: &str) {
        self.assert_triple(individual, vocab::RDF_TYPE, class);
    }

    /// Assert a property assertion `subject property object`
    pub fn add_property_assertion(&mut self, subject: &str, property: &str, object: &str) {
        self.assert_triple(subject, property, object);
    }

    /// Assert `ind1 owl:sameAs ind2` (and its symmetric counterpart)
    pub fn assert_same_as(&mut self, ind1: &str, ind2: &str) {
        self.assert_triple(ind1, vocab::OWL_SAME_AS, ind2);
        self.assert_triple(ind2, vocab::OWL_SAME_AS, ind1);
    }

    /// Assert `ind1 owl:differentFrom ind2`
    pub fn assert_different_from(&mut self, ind1: &str, ind2: &str) {
        self.assert_triple(ind1, vocab::OWL_DIFFERENT_FROM, ind2);
        self.assert_triple(ind2, vocab::OWL_DIFFERENT_FROM, ind1);
    }
}

// ── TBox axiom API ────────────────────────────────────────────────────────────

impl Owl2DLReasoner {
    /// Add `sub rdfs:subClassOf sup`
    pub fn add_subclass_of(&mut self, sub: &str, sup: &str) {
        self.subclass_of.insert((sub.to_string(), sup.to_string()));
        // Also store as ABox triple so rule patterns can match
        self.abox
            .insert(mk_triple(sub, vocab::RDFS_SUBCLASS_OF, sup));
        self.asserted
            .insert(mk_triple(sub, vocab::RDFS_SUBCLASS_OF, sup));
    }

    /// Add `c1 owl:equivalentClass c2` (symmetric)
    pub fn add_equivalent_classes(&mut self, c1: &str, c2: &str) {
        self.equivalent_classes
            .insert((c1.to_string(), c2.to_string()));
        self.equivalent_classes
            .insert((c2.to_string(), c1.to_string()));
        self.abox
            .insert(mk_triple(c1, vocab::OWL_EQUIVALENT_CLASS, c2));
        self.abox
            .insert(mk_triple(c2, vocab::OWL_EQUIVALENT_CLASS, c1));
    }

    /// Add `c1 owl:disjointWith c2` (symmetric)
    pub fn add_disjoint_classes(&mut self, c1: &str, c2: &str) {
        self.disjoint_classes
            .insert((c1.to_string(), c2.to_string()));
        self.disjoint_classes
            .insert((c2.to_string(), c1.to_string()));
        self.abox
            .insert(mk_triple(c1, vocab::OWL_DISJOINT_WITH, c2));
        self.abox
            .insert(mk_triple(c2, vocab::OWL_DISJOINT_WITH, c1));
    }

    /// Add rdfs:domain axiom: `property rdfs:domain class`
    pub fn add_domain(&mut self, property: &str, class: &str) {
        self.abox
            .insert(mk_triple(property, vocab::RDFS_DOMAIN, class));
        self.asserted
            .insert(mk_triple(property, vocab::RDFS_DOMAIN, class));
    }

    /// Add rdfs:range axiom: `property rdfs:range class`
    pub fn add_range(&mut self, property: &str, class: &str) {
        self.abox
            .insert(mk_triple(property, vocab::RDFS_RANGE, class));
        self.asserted
            .insert(mk_triple(property, vocab::RDFS_RANGE, class));
    }

    /// Add `owl:inverseOf` declaration
    pub fn add_inverse_of(&mut self, p1: &str, p2: &str) {
        let chars1 = self.property_chars.entry(p1.to_string()).or_default();
        chars1.inverse_of = Some(p2.to_string());

        let chars2 = self.property_chars.entry(p2.to_string()).or_default();
        chars2.inverse_of = Some(p1.to_string());

        self.abox.insert(mk_triple(p1, vocab::OWL_INVERSE_OF, p2));
        self.asserted
            .insert(mk_triple(p1, vocab::OWL_INVERSE_OF, p2));
    }
}

// ── Property characteristic API ───────────────────────────────────────────────

impl Owl2DLReasoner {
    fn declare_property_char(&mut self, property: &str, char_type: &str) {
        self.property_chars.entry(property.to_string()).or_default();
        self.abox
            .insert(mk_triple(property, vocab::RDF_TYPE, char_type));
        self.asserted
            .insert(mk_triple(property, vocab::RDF_TYPE, char_type));
    }

    /// Declare `property rdf:type owl:TransitiveProperty`
    pub fn add_transitive_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_transitive = true;
        self.declare_property_char(property, vocab::OWL_TRANSITIVE_PROPERTY);
    }

    /// Declare `property rdf:type owl:SymmetricProperty`
    pub fn add_symmetric_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_symmetric = true;
        self.declare_property_char(property, vocab::OWL_SYMMETRIC_PROPERTY);
    }

    /// Declare `property rdf:type owl:AsymmetricProperty`
    pub fn add_asymmetric_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_asymmetric = true;
        self.declare_property_char(property, vocab::OWL_ASYMMETRIC_PROPERTY);
    }

    /// Declare `property rdf:type owl:ReflexiveProperty`
    pub fn add_reflexive_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_reflexive = true;
        self.declare_property_char(property, vocab::OWL_REFLEXIVE_PROPERTY);
    }

    /// Declare `property rdf:type owl:IrreflexiveProperty`
    pub fn add_irreflexive_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_irreflexive = true;
        self.declare_property_char(property, vocab::OWL_IRREFLEXIVE_PROPERTY);
    }

    /// Declare `property rdf:type owl:FunctionalProperty`
    pub fn add_functional_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_functional = true;
        self.declare_property_char(property, vocab::OWL_FUNCTIONAL_PROPERTY);
    }

    /// Declare `property rdf:type owl:InverseFunctionalProperty`
    pub fn add_inverse_functional_property(&mut self, property: &str) {
        self.property_chars
            .entry(property.to_string())
            .or_default()
            .is_inverse_functional = true;
        self.declare_property_char(property, vocab::OWL_INVERSE_FUNCTIONAL_PROPERTY);
    }
}

// ── Restriction & chain API ───────────────────────────────────────────────────

impl Owl2DLReasoner {
    /// Register a `owl:hasValue` restriction
    ///
    /// Declares that the class `restriction_class` is equivalent to
    /// `owl:Restriction owl:onProperty property; owl:hasValue value`.
    pub fn add_has_value_restriction(
        &mut self,
        restriction_class: &str,
        property: &str,
        value: &str,
    ) {
        self.has_value_restrictions.push(HasValueRestriction {
            restriction_class: restriction_class.to_string(),
            property: property.to_string(),
            value: value.to_string(),
        });
    }

    /// Register an `owl:allValuesFrom` restriction
    pub fn add_all_values_from_restriction(
        &mut self,
        restriction_class: &str,
        property: &str,
        filler_class: &str,
    ) {
        self.all_values_restrictions.push(AllValuesFromRestriction {
            restriction_class: restriction_class.to_string(),
            property: property.to_string(),
            filler_class: filler_class.to_string(),
        });
    }

    /// Register a `owl:someValuesFrom` restriction
    pub fn add_some_values_from_restriction(
        &mut self,
        restriction_class: &str,
        property: &str,
        filler_class: &str,
    ) {
        self.some_values_restrictions
            .push(SomeValuesFromRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                filler_class: filler_class.to_string(),
            });
    }

    /// Register an `owl:oneOf` nominal class
    pub fn add_nominal_class(&mut self, class_iri: &str, members: Vec<String>) {
        self.nominal_classes.push(NominalClass {
            class_iri: class_iri.to_string(),
            members,
        });
    }

    /// Register an `owl:propertyChainAxiom` — `entailed_property` is derived
    /// when all `chain` properties are traversed in sequence.
    pub fn add_property_chain(&mut self, entailed_property: &str, chain: Vec<String>) {
        self.property_chains.push(PropertyChain {
            entailed_property: entailed_property.to_string(),
            chain,
        });
    }

    /// Register an `owl:complementOf` class:
    /// `class_iri owl:complementOf base_class`
    ///
    /// Used for inconsistency detection: if `x rdf:type class_iri` and
    /// `x rdf:type base_class` both hold, that is a contradiction.
    pub fn add_complement_of(&mut self, class_iri: &str, base_class: &str) {
        self.complement_of_classes.push(ComplementOfClass {
            class_iri: class_iri.to_string(),
            base_class: base_class.to_string(),
        });
    }

    /// Register an `owl:intersectionOf` class:
    /// `class_iri owl:intersectionOf (op1 op2 … opN)`
    ///
    /// Forward: if `x rdf:type op_i` for ALL operands → infer `x rdf:type class_iri`.
    /// Backward: if `x rdf:type class_iri` → infer `x rdf:type op_i` for each operand.
    pub fn add_intersection_of(&mut self, class_iri: &str, operands: Vec<String>) {
        self.intersection_of_classes.push(IntersectionOfClass {
            class_iri: class_iri.to_string(),
            operands,
        });
    }

    /// Register an `owl:disjointUnionOf` axiom:
    /// `class_iri owl:disjointUnionOf (op1 op2 … opN)`
    ///
    /// Entails:
    /// - Each operand is a subclass of `class_iri` (union membership).
    /// - All pairs of operands are mutually disjoint.
    pub fn add_disjoint_union(&mut self, class_iri: &str, operands: Vec<String>) {
        self.disjoint_unions.push(DisjointUnionOf {
            class_iri: class_iri.to_string(),
            operands,
        });
    }

    /// Register an `owl:hasKey` axiom:
    /// `class_iri owl:hasKey (prop1 prop2 … propN)`
    ///
    /// If two individuals of `class_iri` share the same values for all key
    /// properties, they are asserted to be `owl:sameAs`.
    pub fn add_has_key(&mut self, class_iri: &str, key_properties: Vec<String>) {
        self.has_key_axioms.push(HasKeyAxiom {
            class_iri: class_iri.to_string(),
            key_properties,
        });
    }
}

// ── Property hierarchy API (90% milestone) ────────────────────────────────────

impl Owl2DLReasoner {
    /// Add `p1 rdfs:subPropertyOf p2` (object property subsumption)
    ///
    /// Semantics: if `x P1 y` then `x P2 y`.
    pub fn add_sub_object_property_of(&mut self, sub: &str, sup: &str) {
        self.sub_object_property_of
            .insert((sub.to_string(), sup.to_string()));
        self.abox
            .insert(mk_triple(sub, vocab::RDFS_SUBPROPERTY_OF, sup));
        self.asserted
            .insert(mk_triple(sub, vocab::RDFS_SUBPROPERTY_OF, sup));
    }

    /// Add `p1 rdfs:subPropertyOf p2` (data property subsumption)
    ///
    /// Semantics: if `x P1 v` (data value) then `x P2 v`.
    pub fn add_sub_data_property_of(&mut self, sub: &str, sup: &str) {
        self.sub_data_property_of
            .insert((sub.to_string(), sup.to_string()));
        self.abox
            .insert(mk_triple(sub, vocab::RDFS_SUBPROPERTY_OF, sup));
        self.asserted
            .insert(mk_triple(sub, vocab::RDFS_SUBPROPERTY_OF, sup));
    }

    /// Add `p1 owl:equivalentProperty p2` (bidirectional property subsumption)
    ///
    /// Both (p1, p2) and (p2, p1) are stored so that rules fire in both directions.
    pub fn add_equivalent_properties(&mut self, p1: &str, p2: &str) {
        self.equivalent_properties
            .insert((p1.to_string(), p2.to_string()));
        self.equivalent_properties
            .insert((p2.to_string(), p1.to_string()));
        self.abox
            .insert(mk_triple(p1, vocab::OWL_EQUIVALENT_PROPERTY, p2));
        self.abox
            .insert(mk_triple(p2, vocab::OWL_EQUIVALENT_PROPERTY, p1));
    }

    /// Add `p1 owl:propertyDisjointWith p2` (symmetric)
    ///
    /// If `x P1 y` and `x P2 y` both hold, the ABox is inconsistent.
    pub fn add_disjoint_properties(&mut self, p1: &str, p2: &str) {
        self.disjoint_properties
            .insert((p1.to_string(), p2.to_string()));
        self.disjoint_properties
            .insert((p2.to_string(), p1.to_string()));
        self.abox
            .insert(mk_triple(p1, vocab::OWL_PROPERTY_DISJOINT_WITH, p2));
        self.abox
            .insert(mk_triple(p2, vocab::OWL_PROPERTY_DISJOINT_WITH, p1));
    }

    /// Register a `hasSelf` local-reflexivity restriction
    ///
    /// Declares that `restriction_class` is equivalent to the set of individuals
    /// that are related to themselves via `property`.
    pub fn add_has_self_restriction(&mut self, restriction_class: &str, property: &str) {
        self.has_self_restrictions.push(HasSelfRestriction {
            restriction_class: restriction_class.to_string(),
            property: property.to_string(),
        });
    }

    /// Assert a `owl:NegativeObjectPropertyAssertion`
    ///
    /// The triple `(source, property, target)` must NOT hold.
    /// If it does appear in the ABox after materialisation, an inconsistency
    /// is reported.
    pub fn assert_negative_object_property_assertion(
        &mut self,
        source: &str,
        property: &str,
        target: &str,
    ) {
        self.negative_object_assertions
            .push(NegativeObjectPropertyAssertion {
                source_individual: source.to_string(),
                assertion_property: property.to_string(),
                target_individual: target.to_string(),
            });
    }

    /// Assert a `owl:NegativeDataPropertyAssertion`
    ///
    /// The triple `(source, property, target_value)` must NOT hold.
    /// If it appears in the ABox after materialisation, an inconsistency is reported.
    pub fn assert_negative_data_property_assertion(
        &mut self,
        source: &str,
        property: &str,
        target_value: &str,
    ) {
        self.negative_data_assertions
            .push(NegativeDataPropertyAssertion {
                source_individual: source.to_string(),
                assertion_property: property.to_string(),
                target_value: target_value.to_string(),
            });
    }
}

// ── 100% milestone: new axiom builder methods ─────────────────────────────────
impl Owl2DLReasoner {
    /// Add a `owl:maxCardinality` restriction.
    pub fn add_max_cardinality(&mut self, restriction_class: &str, property: &str, n: usize) {
        self.cardinality_restrictions
            .push(cardinality_rules::CardinalityRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                cardinality: n,
                kind: cardinality_rules::CardinalityKind::Max,
                qualifying_class: None,
            });
    }

    /// Add a `owl:maxQualifiedCardinality` restriction with a qualifying class.
    pub fn add_max_qualified_cardinality(
        &mut self,
        restriction_class: &str,
        property: &str,
        n: usize,
        qualifying_class: &str,
    ) {
        self.cardinality_restrictions
            .push(cardinality_rules::CardinalityRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                cardinality: n,
                kind: cardinality_rules::CardinalityKind::Max,
                qualifying_class: Some(qualifying_class.to_string()),
            });
    }

    /// Add a `owl:minCardinality` restriction.
    pub fn add_min_cardinality(&mut self, restriction_class: &str, property: &str, n: usize) {
        self.cardinality_restrictions
            .push(cardinality_rules::CardinalityRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                cardinality: n,
                kind: cardinality_rules::CardinalityKind::Min,
                qualifying_class: None,
            });
    }

    /// Add a `owl:minQualifiedCardinality` restriction with a qualifying class.
    pub fn add_min_qualified_cardinality(
        &mut self,
        restriction_class: &str,
        property: &str,
        n: usize,
        qualifying_class: &str,
    ) {
        self.cardinality_restrictions
            .push(cardinality_rules::CardinalityRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                cardinality: n,
                kind: cardinality_rules::CardinalityKind::Min,
                qualifying_class: Some(qualifying_class.to_string()),
            });
    }

    /// Add a `owl:exactCardinality` restriction.
    pub fn add_exact_cardinality(&mut self, restriction_class: &str, property: &str, n: usize) {
        self.cardinality_restrictions
            .push(cardinality_rules::CardinalityRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                cardinality: n,
                kind: cardinality_rules::CardinalityKind::Exact,
                qualifying_class: None,
            });
    }

    /// Add an `owl:unionOf` class expression.
    pub fn add_union_of(&mut self, class_iri: &str, operands: Vec<String>) {
        if operands.is_empty() {
            return;
        }
        self.union_of_classes.push(cardinality_rules::UnionOfClass {
            class_iri: class_iri.to_string(),
            operands,
        });
    }

    /// Add a `DataSomeValuesFrom` restriction.
    pub fn add_data_some_values_from(
        &mut self,
        restriction_class: &str,
        property: &str,
        datatype: Option<&str>,
    ) {
        self.data_some_values_restrictions
            .push(cardinality_rules::DataSomeValuesFromRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                datatype: datatype.map(|s| s.to_string()),
            });
    }

    /// Add a `DataAllValuesFrom` restriction.
    pub fn add_data_all_values_from(
        &mut self,
        restriction_class: &str,
        property: &str,
        datatype: &str,
    ) {
        self.data_all_values_restrictions
            .push(cardinality_rules::DataAllValuesFromRestriction {
                restriction_class: restriction_class.to_string(),
                property: property.to_string(),
                datatype: datatype.to_string(),
            });
    }

    /// Declare an `owl:AllDifferent` axiom.
    pub fn add_all_different(&mut self, members: Vec<String>) {
        if members.len() < 2 {
            return;
        }
        self.all_different_axioms
            .push(cardinality_rules::AllDifferentAxiom { members });
    }
}

// ── Query API ─────────────────────────────────────────────────────────────────

impl Owl2DLReasoner {
    /// Check whether `individual rdf:type class` is entailed (asserted or inferred)
    pub fn is_type_entailed(&self, individual: &str, class: &str) -> bool {
        self.abox
            .contains(&mk_triple(individual, vocab::RDF_TYPE, class))
    }

    /// Check whether a triple `(s, p, o)` is entailed
    pub fn is_triple_entailed(&self, s: &str, p: &str, o: &str) -> bool {
        self.abox.contains(&mk_triple(s, p, o))
    }

    /// Return all `rdf:type` classes asserted or inferred for `individual`
    pub fn get_types(&self, individual: &str) -> Vec<String> {
        self.abox
            .iter()
            .filter(|(s, p, _)| s == individual && p == vocab::RDF_TYPE)
            .map(|(_, _, o)| o.clone())
            .collect()
    }

    /// Return all inferred triples (those not in the original asserted set)
    pub fn inferred_triples(&self) -> Vec<Triple> {
        self.abox
            .iter()
            .filter(|t| !self.asserted.contains(*t))
            .cloned()
            .collect()
    }

    /// Whether the ABox is consistent (no inconsistencies detected)
    pub fn is_consistent(&self) -> bool {
        self.inconsistencies.is_empty()
    }

    /// Return all detected inconsistencies
    pub fn inconsistencies(&self) -> &[String] {
        &self.inconsistencies
    }

    /// Return all distinct individuals mentioned in the ABox
    pub fn all_individuals(&self) -> HashSet<String> {
        let mut individuals = HashSet::new();
        for (s, p, _o) in &self.abox {
            if p == vocab::RDF_TYPE {
                individuals.insert(s.clone());
            }
        }
        individuals
    }
}

// ── Materialization (fixpoint) ────────────────────────────────────────────────

impl Owl2DLReasoner {
    /// Run ABox reasoning to fixpoint.
    ///
    /// Applies all OWL 2 DL structural subsumption rules repeatedly until
    /// no new triples can be derived.  Returns a [`DLInferenceReport`] with
    /// statistics, or a [`DLError`] on inconsistency / iteration overflow.
    pub fn materialize(&mut self) -> Result<DLInferenceReport, DLError> {
        let start = Instant::now();
        self.inconsistencies.clear();
        let mut firings = RuleFirings::default();
        let mut total_new = 0usize;
        let mut iterations = 0usize;

        // Derive transitive closure of subclass axioms before main loop
        // so that rule application picks up inherited subsumptions.
        self.close_subclass_hierarchy();

        // Derive transitive closure of sub-property axioms before main loop.
        self.close_sub_property_hierarchy();

        // Pre-materialise disjoint union subclass and disjointness axioms (TBox expansion)
        self.expand_disjoint_unions();

        loop {
            if iterations >= self.max_iterations {
                return Err(DLError::MaxIterationsExceeded(self.max_iterations));
            }
            iterations += 1;

            let snapshot: HashSet<Triple> = self.abox.clone();
            let mut new_triples: HashSet<Triple> = HashSet::new();

            // --- Rule 1: Individual classification via subclass hierarchy -------
            self.apply_subclass_propagation(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 2: Equivalent class propagation ---------------------------
            self.apply_equivalent_class_propagation(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 3: nominal classification (owl:oneOf) ---------------------
            self.apply_nominal_classification(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 4: HasValue — forward (type → property) -------------------
            self.apply_has_value_forward(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 5: HasValue — backward (property → type) ------------------
            self.apply_has_value_backward(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 6: AllValuesFrom propagation ------------------------------
            self.apply_all_values_from(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 7: SomeValuesFrom classification --------------------------
            self.apply_some_values_from(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 8: Property chain reasoning --------------------------------
            self.apply_property_chains(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 9: Transitivity -------------------------------------------
            self.apply_transitivity(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 10: Symmetry ----------------------------------------------
            self.apply_symmetry(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 11: InverseOf ---------------------------------------------
            self.apply_inverse_of(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 12: Domain / range type inference -------------------------
            self.apply_domain_range(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 13: owl:sameAs propagation --------------------------------
            self.apply_same_as_propagation(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 14 (NEW): ObjectIntersectionOf (forward + backward) -------
            self.apply_intersection_of(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 15 (NEW): FunctionalProperty sameAs merging ---------------
            self.apply_functional_property(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 16 (NEW): InverseFunctionalProperty sameAs merging --------
            self.apply_inverse_functional_property(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 17 (NEW): HasKey unique-key sameAs entailment -------------
            self.apply_has_key(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 18 (90%): SubObjectPropertyOf / SubDataPropertyOf ---------
            self.apply_sub_property_of(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 19 (90%): EquivalentObjectProperties ----------------------
            self.apply_equivalent_properties(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 20 (90%): ReflexiveObjectProperty (self-loops) ------------
            self.apply_reflexive_property(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 21 (90%): hasSelf restrictions ----------------------------
            self.apply_has_self(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 22 (100%): MaxCardinality violation → owl:Nothing ----------
            self.apply_max_cardinality(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 23 (100%): MinCardinality backward classification ----------
            self.apply_min_cardinality(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 24 (100%): ExactCardinality (max+min combined) -------------
            self.apply_exact_cardinality(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 25 (100%): ObjectUnionOf membership propagation -----------
            self.apply_union_of(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 26 (100%): DataSomeValuesFrom backward classification -----
            self.apply_data_some_values_from(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 27 (100%): DataAllValuesFrom filler classification --------
            self.apply_data_all_values_from(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 28 (100%): AllDifferent axiom materialization -------------
            self.apply_all_different(&snapshot, &mut new_triples, &mut firings);

            // --- Rule 29 (100%): Full sameAs congruence (property inheritance) --
            self.apply_same_as_full_congruence(&snapshot, &mut new_triples, &mut firings);

            // Filter triples already in the ABox
            let genuinely_new: HashSet<Triple> = new_triples
                .into_iter()
                .filter(|t| !snapshot.contains(t))
                .collect();

            if genuinely_new.is_empty() {
                break;
            }

            total_new += genuinely_new.len();
            self.abox.extend(genuinely_new);
        }

        // Asymmetry and irreflexivity checks (inconsistency detection)
        self.check_asymmetry_violations(&firings);
        self.check_irreflexivity_violations();
        self.check_disjoint_violations();
        self.check_nothing_violations();

        // NEW: ComplementOf inconsistency detection
        self.check_complement_of_violations(&mut firings);

        // NEW: DisjointUnionOf operand pair disjointness violations
        self.check_disjoint_union_violations();

        // 90% milestone: DisjointProperties inconsistency check
        self.check_disjoint_property_violations();

        // 90% milestone: NegativePropertyAssertion inconsistency check
        self.check_negative_property_assertion_violations(&mut firings);

        // 100% milestone: AllDifferent + sameAs contradiction check
        self.check_all_different_violations();

        // 100% milestone: MaxCardinality post-loop violation check
        self.check_max_cardinality_violations();

        Ok(DLInferenceReport {
            iterations,
            new_triples: total_new,
            rule_firings: firings,
            duration: start.elapsed(),
            inconsistencies: self.inconsistencies.clone(),
        })
    }

    // ── Private: subproperty closure ──────────────────────────────────────────

    /// Compute the transitive closure of `rdfs:subPropertyOf` for both object-
    /// and data-property hierarchies before the main fixpoint loop.
    pub(crate) fn close_sub_property_hierarchy(&mut self) {
        // --- Object property closure ---
        let mut obj_pairs: HashSet<(String, String)> = self.sub_object_property_of.clone();
        let mut changed = true;
        while changed {
            changed = false;
            let snapshot: Vec<(String, String)> = obj_pairs.iter().cloned().collect();
            for (a, b) in &snapshot {
                for (c, d) in &snapshot {
                    if b == c && !obj_pairs.contains(&(a.clone(), d.clone())) {
                        obj_pairs.insert((a.clone(), d.clone()));
                        self.abox
                            .insert(mk_triple(a, vocab::RDFS_SUBPROPERTY_OF, d));
                        changed = true;
                    }
                }
            }
        }
        self.sub_object_property_of = obj_pairs;

        // --- Data property closure ---
        let mut data_pairs: HashSet<(String, String)> = self.sub_data_property_of.clone();
        let mut data_changed = true;
        while data_changed {
            data_changed = false;
            let snapshot: Vec<(String, String)> = data_pairs.iter().cloned().collect();
            for (a, b) in &snapshot {
                for (c, d) in &snapshot {
                    if b == c && !data_pairs.contains(&(a.clone(), d.clone())) {
                        data_pairs.insert((a.clone(), d.clone()));
                        self.abox
                            .insert(mk_triple(a, vocab::RDFS_SUBPROPERTY_OF, d));
                        data_changed = true;
                    }
                }
            }
        }
        self.sub_data_property_of = data_pairs;
    }

    // ── Private: subclass closure ─────────────────────────────────────────────

    /// Compute the transitive closure of `rdfs:subClassOf` in the TBox
    pub(crate) fn close_subclass_hierarchy(&mut self) {
        // Copy to avoid borrow conflicts
        let mut pairs: HashSet<(String, String)> = self.subclass_of.clone();

        // Floyd-Warshall style transitive closure
        let mut changed = true;
        while changed {
            changed = false;
            let snapshot: Vec<(String, String)> = pairs.iter().cloned().collect();
            for (a, b) in &snapshot {
                for (c, d) in &snapshot {
                    if b == c && !pairs.contains(&(a.clone(), d.clone())) {
                        pairs.insert((a.clone(), d.clone()));
                        self.abox.insert(mk_triple(a, vocab::RDFS_SUBCLASS_OF, d));
                        changed = true;
                    }
                }
            }
        }
        self.subclass_of = pairs;
    }
}

// ── Private helpers ───────────────────────────────────────────────────────────

/// Construct a [`Triple`] from string slices
#[inline]
pub(crate) fn mk_triple(s: &str, p: &str, o: &str) -> Triple {
    (s.to_string(), p.to_string(), o.to_string())
}

/// BFS reachability: returns all nodes reachable from `start` via `adj`
pub(crate) fn bfs_reachable<'a>(
    start: &'a str,
    adj: &'a HashMap<&'a str, Vec<&'a str>>,
) -> Vec<&'a str> {
    let mut visited: HashSet<&str> = HashSet::new();
    let mut queue: VecDeque<&str> = VecDeque::new();
    queue.push_back(start);

    while let Some(node) = queue.pop_front() {
        if visited.contains(node) {
            continue;
        }
        visited.insert(node);
        if let Some(neighbors) = adj.get(node) {
            for &n in neighbors {
                if !visited.contains(n) {
                    queue.push_back(n);
                }
            }
        }
    }

    visited.into_iter().collect()
}

/// Evaluate a property chain by joining through each segment.
///
/// Returns `(start, end)` pairs derivable from the chain.
pub(crate) fn evaluate_chain<'a>(
    chain: &[String],
    pred_index: &HashMap<&'a str, Vec<(&'a str, &'a str)>>,
) -> Vec<(&'a str, &'a str)> {
    if chain.is_empty() {
        return Vec::new();
    }

    // Start with all (s, o) pairs for chain[0]
    let first_pred = chain[0].as_str();
    let mut current: Vec<(&str, &str)> = pred_index.get(first_pred).cloned().unwrap_or_default();

    // Join through each subsequent chain step
    for step_pred in chain.iter().skip(1) {
        let step_pairs = pred_index
            .get(step_pred.as_str())
            .cloned()
            .unwrap_or_default();

        // Build a lookup: from → [to] for this step
        let mut step_map: HashMap<&str, Vec<&str>> = HashMap::new();
        for (s, o) in &step_pairs {
            step_map.entry(s).or_default().push(o);
        }

        let mut next: Vec<(&str, &str)> = Vec::new();
        for (start, mid) in &current {
            if let Some(ends) = step_map.get(mid) {
                for &end in ends {
                    next.push((start, end));
                }
            }
        }
        current = next;
    }

    current
}