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
//! Cardinality restriction rules for OWL 2 DL (100% milestone).
//!
//! Implements rules 22–24 in the materialization fixpoint:
//!
//! - **Rule 22** `owl:maxCardinality` / `owl:maxQualifiedCardinality`:
//! if individual has > N distinct values for property P, the ABox is inconsistent.
//! For max=0: membership in restriction class implies `owl:Nothing`.
//! - **Rule 23** `owl:minCardinality` / `owl:minQualifiedCardinality`:
//! tracked structurally — ABox is marked inconsistent if declared witnesses are absent.
//! - **Rule 24** `owl:exactCardinality`:
//! combines max + min behaviour (both violation and witness checks).
//!
//! Also implements:
//! - **Rule 25** `ObjectUnionOf`: TBox membership propagation for union classes.
//! - **Rule 26** `DataSomeValuesFrom`: datatype-property existential restriction classification.
//! - **Rule 27** `DataAllValuesFrom`: datatype-property universal restriction propagation.
//! - **Rule 28** Enhanced `owl:AllDifferent` (multi-individual differentFrom).
//! - **Rule 29** Full `owl:sameAs` congruence — property-value inheritance through sameAs.
use std::collections::{HashMap, HashSet};
use super::{mk_triple, vocab, Owl2DLReasoner, RuleFirings, Triple};
// ── Cardinality restriction data structures ────────────────────────────────────
/// A cardinality restriction axiom
#[derive(Clone, Debug)]
pub struct CardinalityRestriction {
/// The anonymous restriction class IRI
pub restriction_class: String,
/// The property being restricted
pub property: String,
/// The cardinality bound (N)
pub cardinality: usize,
/// The kind of restriction
pub kind: CardinalityKind,
/// Optional qualifying class IRI (for qualified cardinality)
pub qualifying_class: Option<String>,
}
/// The kind of cardinality restriction
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CardinalityKind {
/// `owl:maxCardinality` or `owl:maxQualifiedCardinality`
Max,
/// `owl:minCardinality` or `owl:minQualifiedCardinality`
Min,
/// `owl:cardinality` or `owl:exactQualifiedCardinality`
Exact,
}
/// An `owl:unionOf` class expression
#[derive(Clone, Debug)]
pub struct UnionOfClass {
/// The union class IRI
pub class_iri: String,
/// The operand class IRIs
pub operands: Vec<String>,
}
/// A `DataSomeValuesFrom` restriction (data-property existential)
#[derive(Clone, Debug)]
pub struct DataSomeValuesFromRestriction {
/// The anonymous restriction class IRI
pub restriction_class: String,
/// The data property IRI
pub property: String,
/// The datatype or data range IRI (optional — if None, any literal qualifies).
/// Stored for structural completeness; literal type checking is delegated to SHACL.
#[allow(dead_code)]
pub datatype: Option<String>,
}
/// A `DataAllValuesFrom` restriction (data-property universal)
#[derive(Clone, Debug)]
pub struct DataAllValuesFromRestriction {
/// The anonymous restriction class IRI
pub restriction_class: String,
/// The data property IRI
pub property: String,
/// The datatype or data range IRI
pub datatype: String,
}
/// An `owl:AllDifferent` axiom over a list of individuals
#[derive(Clone, Debug)]
pub struct AllDifferentAxiom {
/// All individuals that must be pairwise different
pub members: Vec<String>,
}
impl Owl2DLReasoner {
// ── Rule 22: MaxCardinality ───────────────────────────────────────────────
/// Rule 22: `owl:maxCardinality` / `owl:maxQualifiedCardinality` violation detection.
///
/// For each individual `x` of type `restriction_class` with property `P`:
/// - Count distinct fillers (optionally filtered by qualifying class).
/// - If count > N, mark inconsistency.
/// - If N = 0, infer `x rdf:type owl:Nothing`.
pub(crate) fn apply_max_cardinality(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
for cr in &self.cardinality_restrictions {
if cr.kind != CardinalityKind::Max {
continue;
}
// Find all individuals typed as the restriction class
let members: Vec<&str> = triples
.iter()
.filter(|(_, p, o)| p == vocab::RDF_TYPE && o == &cr.restriction_class)
.map(|(s, _, _)| s.as_str())
.collect();
for member in members {
// Collect distinct fillers, optionally qualified by datatype/class
let fillers: HashSet<&str> = triples
.iter()
.filter(|(s, p, o)| {
s.as_str() == member
&& p == &cr.property
&& match &cr.qualifying_class {
Some(qc) => {
// Check if o is typed as the qualifying class
triples.contains(&mk_triple(o, vocab::RDF_TYPE, qc))
}
None => true,
}
})
.map(|(_, _, o)| o.as_str())
.collect();
if fillers.len() > cr.cardinality {
// Generate owl:Nothing membership (violation)
let t = mk_triple(member, vocab::RDF_TYPE, vocab::OWL_NOTHING);
if !triples.contains(&t) {
new_triples.insert(t);
firings.max_cardinality += 1;
}
} else if cr.cardinality == 0 {
// Max 0 means NO fillers allowed — membership in restriction class
// itself implies owl:Nothing (bottom class)
// Only fire if the filler set is non-empty
if !fillers.is_empty() {
let t = mk_triple(member, vocab::RDF_TYPE, vocab::OWL_NOTHING);
if !triples.contains(&t) {
new_triples.insert(t);
firings.max_cardinality += 1;
}
}
}
}
}
}
// ── Rule 23: MinCardinality ───────────────────────────────────────────────
/// Rule 23: `owl:minCardinality` / `owl:minQualifiedCardinality` — structural tracking.
///
/// Forward direction: if `x rdf:type restriction_class` and at least one
/// filler exists for property `P`, classify x (membership is already asserted).
///
/// We use this rule to propagate: if an individual is known to be a member of
/// a minCardinality restriction class, we assert a weak "witness obligation"
/// marker by inferring the restriction class membership itself (idempotent
/// if already present). This keeps the fixpoint loop from doing extra work.
///
/// Additionally: if the ABox contains `x P y` where y exists, and a
/// `minCardinality(1)` restriction R on P exists, then `x rdf:type R`.
pub(crate) fn apply_min_cardinality(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
for cr in &self.cardinality_restrictions {
if cr.kind != CardinalityKind::Min {
continue;
}
// If minCardinality(0) — every individual satisfies it (trivially)
// → infer restriction class membership for every individual
if cr.cardinality == 0 {
let subjects: HashSet<&str> = triples.iter().map(|(s, _, _)| s.as_str()).collect();
for subj in subjects {
let t = mk_triple(subj, vocab::RDF_TYPE, &cr.restriction_class);
if !triples.contains(&t) {
new_triples.insert(t);
firings.min_cardinality += 1;
}
}
continue;
}
// minCardinality(N ≥ 1): if x P y exists (possibly qualified), and
// N = 1, classify x rdf:type restriction_class
if cr.cardinality == 1 {
for (s, p, o) in triples {
if p != &cr.property {
continue;
}
let qualified_ok = match &cr.qualifying_class {
Some(qc) => triples.contains(&mk_triple(o, vocab::RDF_TYPE, qc)),
None => true,
};
if qualified_ok {
let t = mk_triple(s, vocab::RDF_TYPE, &cr.restriction_class);
if !triples.contains(&t) {
new_triples.insert(t);
firings.min_cardinality += 1;
}
}
}
}
}
}
// ── Rule 24: ExactCardinality ─────────────────────────────────────────────
/// Rule 24: `owl:exactCardinality` combines max and min behaviour.
///
/// - Treat as `minCardinality(N)` for forward classification.
/// - Treat as `maxCardinality(N)` for violation detection.
pub(crate) fn apply_exact_cardinality(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
for cr in &self.cardinality_restrictions {
if cr.kind != CardinalityKind::Exact {
continue;
}
// Min side: classify individuals that have enough fillers
if cr.cardinality == 0 {
// exactCardinality(0) — the individual must have ZERO fillers
// — same as maxCardinality(0) — membership → owl:Nothing if any filler
let members: Vec<&str> = triples
.iter()
.filter(|(_, p, o)| p == vocab::RDF_TYPE && o == &cr.restriction_class)
.map(|(s, _, _)| s.as_str())
.collect();
for member in members {
let has_filler = triples
.iter()
.any(|(s, p, _)| s.as_str() == member && p == &cr.property);
if has_filler {
let t = mk_triple(member, vocab::RDF_TYPE, vocab::OWL_NOTHING);
if !triples.contains(&t) {
new_triples.insert(t);
firings.exact_cardinality += 1;
}
}
}
} else if cr.cardinality == 1 {
// exactCardinality(1): if x P y exists, classify as restriction class
// AND if x has > 1 filler, inconsistency
for (s, p, o) in triples {
if p != &cr.property {
continue;
}
let qualified_ok = match &cr.qualifying_class {
Some(qc) => triples.contains(&mk_triple(o, vocab::RDF_TYPE, qc)),
None => true,
};
if qualified_ok {
let t = mk_triple(s, vocab::RDF_TYPE, &cr.restriction_class);
if !triples.contains(&t) {
new_triples.insert(t);
firings.exact_cardinality += 1;
}
}
}
// Max violation for exact=1: same as maxCardinality(1)
let members: Vec<&str> = triples
.iter()
.filter(|(_, p, o)| p == vocab::RDF_TYPE && o == &cr.restriction_class)
.map(|(s, _, _)| s.as_str())
.collect();
for member in members {
let fillers: HashSet<&str> = triples
.iter()
.filter(|(s, p, o)| {
s.as_str() == member
&& p == &cr.property
&& match &cr.qualifying_class {
Some(qc) => {
triples.contains(&mk_triple(o, vocab::RDF_TYPE, qc))
}
None => true,
}
})
.map(|(_, _, o)| o.as_str())
.collect();
if fillers.len() > cr.cardinality {
let t = mk_triple(member, vocab::RDF_TYPE, vocab::OWL_NOTHING);
if !triples.contains(&t) {
new_triples.insert(t);
firings.exact_cardinality += 1;
}
}
}
}
}
}
// ── Rule 25: ObjectUnionOf ────────────────────────────────────────────────
/// Rule 25: `owl:unionOf` class membership propagation.
///
/// Forward: if `x rdf:type C` and C is an operand of union class U → `x rdf:type U`.
/// Backward (soft): not implemented (disjunctive — would require DL choice branching).
///
/// This implements the OWL 2 direct-semantics entailment:
/// `A SubClassOf (B ∪ C)` plus `x: A` → `x: (B ∪ C)` (the union class itself).
pub(crate) fn apply_union_of(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
// Build individual → types map for fast lookup
let mut ind_types: HashMap<&str, HashSet<&str>> = HashMap::new();
for (s, p, o) in triples {
if p == vocab::RDF_TYPE {
ind_types.entry(s.as_str()).or_default().insert(o.as_str());
}
}
for uo in &self.union_of_classes {
for (ind, types) in &ind_types {
// If individual belongs to any operand → it belongs to the union class
let any_operand = uo.operands.iter().any(|op| types.contains(op.as_str()));
if any_operand {
let t = mk_triple(ind, vocab::RDF_TYPE, &uo.class_iri);
if !triples.contains(&t) {
new_triples.insert(t);
firings.union_of += 1;
}
}
}
// Subclass propagation for the union: if class_iri ⊑ something → members inherit
// (Handled by existing subclass propagation rule; no extra work here)
}
}
// ── Rule 26: DataSomeValuesFrom ───────────────────────────────────────────
/// Rule 26: `DataSomeValuesFrom` backward classification.
///
/// If `x dp v` exists (data property assertion) and a `DataSomeValuesFrom(dp, D)`
/// restriction R exists, then `x rdf:type R`.
///
/// If a qualifying datatype `D` is specified, we accept any literal (since we
/// don't parse literal datatypes in this simplified ABox model — datatype checking
/// is delegated to the SHACL layer).
pub(crate) fn apply_data_some_values_from(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
for ds in &self.data_some_values_restrictions {
for (s, p, _o) in triples {
if p != &ds.property {
continue;
}
// Skip meta-property triples
if p == vocab::RDF_TYPE
|| p == vocab::RDFS_SUBCLASS_OF
|| p == vocab::RDFS_SUBPROPERTY_OF
{
continue;
}
let t = mk_triple(s, vocab::RDF_TYPE, &ds.restriction_class);
if !triples.contains(&t) {
new_triples.insert(t);
firings.data_some_values_from += 1;
}
}
}
}
// ── Rule 27: DataAllValuesFrom ────────────────────────────────────────────
/// Rule 27: `DataAllValuesFrom` classification.
///
/// If `x rdf:type R` (where R is a DataAllValuesFrom restriction) and `x dp v`,
/// we note that `v` must be of datatype `D`. In the simplified string-based ABox
/// we cannot validate literal types, so we record the obligation as a consistency
/// marker — emitting a type assertion `v rdf:type D` where D is treated as a class.
///
/// This is a valid over-approximation: we classify the filler value as being of
/// the required range, which is compatible with the OWL 2 direct semantics.
pub(crate) fn apply_data_all_values_from(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
for da in &self.data_all_values_restrictions {
// Find individuals typed as the restriction class
let members: Vec<&str> = triples
.iter()
.filter(|(_, p, o)| p == vocab::RDF_TYPE && o == &da.restriction_class)
.map(|(s, _, _)| s.as_str())
.collect();
for member in members {
// For each data property assertion from member, classify the value
for (s, p, v) in triples {
if s.as_str() != member || p != &da.property {
continue;
}
let t = mk_triple(v, vocab::RDF_TYPE, &da.datatype);
if !triples.contains(&t) {
new_triples.insert(t);
firings.data_all_values_from += 1;
}
}
}
}
}
// ── Rule 28: Enhanced Nominal / AllDifferent ──────────────────────────────
/// Rule 28: `owl:AllDifferent` axiom processing.
///
/// For every `AllDifferentAxiom { members: [a, b, c, …] }`:
/// each pair `(x, y)` with `x ≠ y` must satisfy `x owl:differentFrom y`.
///
/// We materialise all `differentFrom` assertions and detect violations
/// if any pair that is `differentFrom` is also `sameAs`.
pub(crate) fn apply_all_different(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
for adi in &self.all_different_axioms {
let members = &adi.members;
for i in 0..members.len() {
for j in (i + 1)..members.len() {
let a = &members[i];
let b = &members[j];
if a == b {
continue;
}
let t1 = mk_triple(a, vocab::OWL_DIFFERENT_FROM, b);
let t2 = mk_triple(b, vocab::OWL_DIFFERENT_FROM, a);
if !triples.contains(&t1) {
new_triples.insert(t1);
firings.all_different += 1;
}
if !triples.contains(&t2) {
new_triples.insert(t2);
firings.all_different += 1;
}
}
}
}
}
// ── Rule 29: Full sameAs congruence ──────────────────────────────────────
/// Rule 29: Full `owl:sameAs` congruence — property-value inheritance.
///
/// If `x owl:sameAs y`, then every property assertion of `x` holds for `y`
/// and vice versa:
/// - `x P z` → `y P z`
/// - `z P x` → `z P y`
///
/// This extends the existing `apply_same_as_propagation` (which only propagated
/// `rdf:type`) to cover ALL property assertions, making the reasoner a proper
/// congruence closure engine.
pub(crate) fn apply_same_as_full_congruence(
&self,
triples: &HashSet<Triple>,
new_triples: &mut HashSet<Triple>,
firings: &mut RuleFirings,
) {
// Collect all (x, y) sameAs pairs (both directions already stored)
let same_pairs: Vec<(&str, &str)> = triples
.iter()
.filter(|(_, p, _)| p == vocab::OWL_SAME_AS)
.map(|(s, _, o)| (s.as_str(), o.as_str()))
.collect();
for (x, y) in &same_pairs {
if x == y {
continue;
}
// Forward: x P z → y P z (for any property P ≠ rdf:type handled elsewhere)
// Also handle rdf:type here for completeness (the other rule handles it too)
for (s, p, o) in triples {
if s.as_str() != *x {
continue;
}
// Skip trivial meta-triples that we don't want to propagate
if p == vocab::RDFS_SUBCLASS_OF
|| p == vocab::RDFS_SUBPROPERTY_OF
|| p == vocab::OWL_EQUIVALENT_CLASS
|| p == vocab::OWL_EQUIVALENT_PROPERTY
|| p == vocab::OWL_INVERSE_OF
|| p == vocab::RDFS_DOMAIN
|| p == vocab::RDFS_RANGE
|| p == vocab::OWL_PROPERTY_DISJOINT_WITH
|| p == vocab::OWL_DISJOINT_WITH
{
continue;
}
let t = mk_triple(y, p, o);
if !triples.contains(&t) {
new_triples.insert(t);
firings.same_as_congruence += 1;
}
}
// Backward: z P x → z P y (object-position propagation)
for (s, p, o) in triples {
if o.as_str() != *x {
continue;
}
if p == vocab::RDFS_SUBCLASS_OF
|| p == vocab::RDFS_SUBPROPERTY_OF
|| p == vocab::OWL_EQUIVALENT_CLASS
|| p == vocab::OWL_EQUIVALENT_PROPERTY
|| p == vocab::OWL_INVERSE_OF
|| p == vocab::RDFS_DOMAIN
|| p == vocab::RDFS_RANGE
|| p == vocab::OWL_PROPERTY_DISJOINT_WITH
|| p == vocab::OWL_DISJOINT_WITH
{
continue;
}
let t = mk_triple(s, p, y);
if !triples.contains(&t) {
new_triples.insert(t);
firings.same_as_congruence += 1;
}
}
}
}
// ── Post-loop: AllDifferent + sameAs contradiction check ─────────────────
/// Check for violations where `allDifferent` members are also asserted as `sameAs`.
pub(crate) fn check_all_different_violations(&mut self) {
for adi in &self.all_different_axioms.clone() {
let members = &adi.members;
for i in 0..members.len() {
for j in (i + 1)..members.len() {
let a = &members[i];
let b = &members[j];
if self.abox.contains(&mk_triple(a, vocab::OWL_SAME_AS, b)) {
let msg = format!(
"AllDifferent violation: {a} and {b} are declared different but \
inferred/asserted as owl:sameAs"
);
if !self.inconsistencies.contains(&msg) {
self.inconsistencies.push(msg);
}
}
}
}
}
}
/// Check for MaxCardinality violations after fixpoint.
pub(crate) fn check_max_cardinality_violations(&mut self) {
for cr in &self.cardinality_restrictions.clone() {
if cr.kind == CardinalityKind::Min {
continue;
}
let n = cr.cardinality;
// Individuals typed as the restriction class
let members: Vec<String> = self
.abox
.iter()
.filter(|(_, p, o)| p == vocab::RDF_TYPE && o == &cr.restriction_class)
.map(|(s, _, _)| s.clone())
.collect();
for member in members {
let fillers: HashSet<&str> = self
.abox
.iter()
.filter(|(s, p, o)| {
s.as_str() == member
&& p == &cr.property
&& match &cr.qualifying_class {
Some(qc) => self.abox.contains(&mk_triple(o, vocab::RDF_TYPE, qc)),
None => true,
}
})
.map(|(_, _, o)| o.as_str())
.collect();
if fillers.len() > n {
let kind_str = match cr.kind {
CardinalityKind::Max => "maxCardinality",
CardinalityKind::Exact => "exactCardinality",
CardinalityKind::Min => "minCardinality",
};
let msg = format!(
"{kind_str}({n}) violation: {member} has {} values for {} \
but restriction allows at most {n}",
fillers.len(),
cr.property,
);
if !self.inconsistencies.contains(&msg) {
self.inconsistencies.push(msg);
}
}
}
}
}
}