owl-dl-core 0.2.2

Core IR, normalization, and shared utilities for the rustdl OWL DL reasoner
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
//! Told-subsumer and told-disjoint tables.
//!
//! "Told" = explicitly stated in axioms, before any reasoning. We extract
//! the *trivial* subsumption graph between named classes — atomic-to-atomic
//! relationships only — and the pairwise disjointness graph. Complex shapes
//! (`And ⊑ B`, `Some(R, C) ⊑ D`, ...) are out of scope here; absorption and
//! the tableau handle those.
//!
//! Sources of told relationships:
//!
//! | source axiom                                       | told fact         |
//! |----------------------------------------------------|-------------------|
//! | `SubClassOf(Atomic(A), Atomic(B))`                 | A ⊑ B             |
//! | `SubClassOf(Atomic(A), Not(Atomic(B)))`            | A, B disjoint     |
//! | `EquivalentClasses([…, Atomic(A), …, Atomic(B), …])`| A ⊑ B and B ⊑ A   |
//! | `DisjointClasses([…, Atomic(A), …, Atomic(B), …])` | A, B disjoint     |
//! | `DisjointUnion { class: P, members: […, Atomic(Ci), …] }` | Ci ⊑ P + pairs disjoint |
//!
//! The subsumption table is closed (reflexive + transitive) at build time.
//! Disjointness is symmetric but not transitive, so we leave it as a flat
//! pairwise set.

use std::collections::VecDeque;

use smallvec::SmallVec;

use crate::ConceptPool;
use crate::ir::{ClassId, ConceptExpr, ConceptId};
use crate::ontology::{Axiom, InternalOntology};

/// Reflexive-transitive closure of told subsumption plus told-disjoint
/// pairs. Both sub-class and super-class queries return slices sorted by
/// [`ClassId`].
#[derive(Debug, Clone)]
pub struct ToldTables {
    super_closure: Vec<Box<[ClassId]>>,
    sub_closure: Vec<Box<[ClassId]>>,
    disjoint_with: Vec<Box<[ClassId]>>,
}

impl ToldTables {
    /// All named classes `B` such that the ontology told us `c ⊑ B`,
    /// including `c` itself. Sorted ascending.
    ///
    /// # Panics
    /// Panics if `c` is out of range for this table.
    #[must_use]
    pub fn super_classes(&self, c: ClassId) -> &[ClassId] {
        &self.super_closure[c.index() as usize]
    }

    /// All named classes `S` such that the ontology told us `S ⊑ c`,
    /// including `c` itself. Sorted ascending.
    ///
    /// # Panics
    /// Panics if `c` is out of range for this table.
    #[must_use]
    pub fn sub_classes(&self, c: ClassId) -> &[ClassId] {
        &self.sub_closure[c.index() as usize]
    }

    /// True iff `sub ⊑ sup` was told (reflexive, transitive).
    ///
    /// # Panics
    /// Panics if either id is out of range.
    #[must_use]
    pub fn is_told_sub(&self, sub: ClassId, sup: ClassId) -> bool {
        self.super_closure[sub.index() as usize]
            .binary_search(&sup)
            .is_ok()
    }

    /// All named classes told to be disjoint with `c`. Sorted ascending. Note
    /// `c` itself does not appear unless the ontology explicitly stated
    /// `DisjointClasses(c, c)` (a degenerate axiom).
    ///
    /// # Panics
    /// Panics if `c` is out of range.
    #[must_use]
    pub fn disjoints_of(&self, c: ClassId) -> &[ClassId] {
        &self.disjoint_with[c.index() as usize]
    }

    /// True iff `a` and `b` were told disjoint (symmetric).
    ///
    /// # Panics
    /// Panics if either id is out of range.
    #[must_use]
    pub fn are_told_disjoint(&self, a: ClassId, b: ClassId) -> bool {
        self.disjoint_with[a.index() as usize]
            .binary_search(&b)
            .is_ok()
    }

    #[must_use]
    pub fn num_classes(&self) -> usize {
        self.super_closure.len()
    }
}

/// Extract told-subsumer + told-disjoint relationships from the explicit
/// axioms of `ontology`. Only atomic-to-atomic (and `Not(Atomic)` for
/// disjointness) shapes are recognized; complex axioms are left to Phase 1
/// absorption.
#[must_use]
pub fn build_told_tables(ontology: &InternalOntology) -> ToldTables {
    let n = ontology.vocabulary.num_classes();
    let mut direct_super: Vec<SmallVec<[ClassId; 4]>> = vec![SmallVec::new(); n];
    let mut disjoint: Vec<SmallVec<[ClassId; 4]>> = vec![SmallVec::new(); n];

    let pool = &ontology.concepts;

    for axiom in &ontology.axioms {
        match axiom {
            Axiom::SubClassOf { sub, sup } => {
                let sub_atom = as_atomic(*sub, pool);
                if let (Some(a), Some(b)) = (sub_atom, as_atomic(*sup, pool)) {
                    add_edge(&mut direct_super, a, b);
                } else if let (Some(a), Some(b)) = (sub_atom, as_not_atomic(*sup, pool)) {
                    add_disjoint_pair(&mut disjoint, a, b);
                }
            }
            Axiom::EquivalentClasses(ids) => {
                let atoms: Vec<ClassId> =
                    ids.iter().filter_map(|&id| as_atomic(id, pool)).collect();
                for i in 0..atoms.len() {
                    for j in (i + 1)..atoms.len() {
                        add_edge(&mut direct_super, atoms[i], atoms[j]);
                        add_edge(&mut direct_super, atoms[j], atoms[i]);
                    }
                }
            }
            Axiom::DisjointClasses(ids) => {
                let atoms: Vec<ClassId> =
                    ids.iter().filter_map(|&id| as_atomic(id, pool)).collect();
                for i in 0..atoms.len() {
                    for j in (i + 1)..atoms.len() {
                        add_disjoint_pair(&mut disjoint, atoms[i], atoms[j]);
                    }
                }
            }
            Axiom::DisjointUnion { class, members } => {
                let atoms: Vec<ClassId> = members
                    .iter()
                    .filter_map(|&id| as_atomic(id, pool))
                    .collect();
                for &m in &atoms {
                    add_edge(&mut direct_super, m, *class);
                }
                for i in 0..atoms.len() {
                    for j in (i + 1)..atoms.len() {
                        add_disjoint_pair(&mut disjoint, atoms[i], atoms[j]);
                    }
                }
            }
            _ => {} // Role / ABox / declaration axioms contribute nothing to told tables.
        }
    }

    // Reflexive-transitive closure on the subsumption graph (BFS per node).
    let n_u32 = u32::try_from(n).expect("ToldTables: too many classes");
    let mut super_closure: Vec<Box<[ClassId]>> = Vec::with_capacity(n);
    let mut sub_closure_acc: Vec<Vec<ClassId>> = vec![Vec::new(); n];

    for c in 0..n_u32 {
        let mut visited = vec![false; n];
        let mut queue: VecDeque<u32> = VecDeque::new();
        queue.push_back(c);
        let mut ups: Vec<ClassId> = Vec::new();
        while let Some(curr) = queue.pop_front() {
            let curr_idx = curr as usize;
            if visited[curr_idx] {
                continue;
            }
            visited[curr_idx] = true;
            ups.push(ClassId::new(curr));
            for &sup in &direct_super[curr_idx] {
                queue.push_back(sup.index());
            }
        }
        ups.sort_unstable();
        for &sup in &ups {
            sub_closure_acc[sup.index() as usize].push(ClassId::new(c));
        }
        super_closure.push(ups.into_boxed_slice());
    }

    let sub_closure: Vec<Box<[ClassId]>> = sub_closure_acc
        .into_iter()
        .map(|mut v| {
            v.sort_unstable();
            v.into_boxed_slice()
        })
        .collect();

    let disjoint_with: Vec<Box<[ClassId]>> = disjoint
        .into_iter()
        .map(|sv| {
            let mut v: Vec<ClassId> = sv.into_iter().collect();
            v.sort_unstable();
            v.dedup();
            v.into_boxed_slice()
        })
        .collect();

    ToldTables {
        super_closure,
        sub_closure,
        disjoint_with,
    }
}

fn as_atomic(cid: ConceptId, pool: &ConceptPool) -> Option<ClassId> {
    if let ConceptExpr::Atomic(c) = pool.get(cid) {
        Some(*c)
    } else {
        None
    }
}

fn as_not_atomic(cid: ConceptId, pool: &ConceptPool) -> Option<ClassId> {
    if let ConceptExpr::Not(inner) = pool.get(cid)
        && let ConceptExpr::Atomic(c) = pool.get(*inner)
    {
        return Some(*c);
    }
    None
}

fn add_edge(direct_super: &mut [SmallVec<[ClassId; 4]>], sub: ClassId, sup: ClassId) {
    let idx = sub.index() as usize;
    if !direct_super[idx].contains(&sup) {
        direct_super[idx].push(sup);
    }
}

fn add_disjoint_pair(disjoint: &mut [SmallVec<[ClassId; 4]>], a: ClassId, b: ClassId) {
    let ai = a.index() as usize;
    let bi = b.index() as usize;
    if !disjoint[ai].contains(&b) {
        disjoint[ai].push(b);
    }
    if a != b && !disjoint[bi].contains(&a) {
        disjoint[bi].push(a);
    }
}

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

    /// Build an `InternalOntology` with the given class IRIs interned and
    /// axioms produced by the closure.
    fn build_ontology(
        class_iris: &[&str],
        make_axioms: impl FnOnce(&mut InternalOntology),
    ) -> InternalOntology {
        let mut o = InternalOntology::new();
        for iri in class_iris {
            o.vocabulary.intern_class(iri);
        }
        make_axioms(&mut o);
        o
    }

    fn c(o: &InternalOntology, name: &str) -> ClassId {
        o.vocabulary.class_id(name).expect("class missing")
    }

    fn atom(o: &mut InternalOntology, name: &str) -> ConceptId {
        let id = c(o, name);
        o.concepts.atomic(id)
    }

    #[test]
    fn empty_ontology_is_reflexive_only() {
        let o = build_ontology(&["A", "B", "C"], |_| {});
        let t = build_told_tables(&o);
        for name in ["A", "B", "C"] {
            let cid = c(&o, name);
            assert_eq!(t.super_classes(cid), [cid].as_slice());
            assert_eq!(t.sub_classes(cid), [cid].as_slice());
            assert_eq!(t.disjoints_of(cid), [].as_slice());
        }
    }

    #[test]
    fn sub_class_of_chain_closes_transitively() {
        // A ⊑ B ⊑ C
        let o = build_ontology(&["A", "B", "C"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            let cc = atom(o, "C");
            o.axioms.push(Axiom::SubClassOf { sub: a, sup: b });
            o.axioms.push(Axiom::SubClassOf { sub: b, sup: cc });
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let b = c(&o, "B");
        let cc = c(&o, "C");
        assert_eq!(t.super_classes(a), [a, b, cc].as_slice());
        assert_eq!(t.super_classes(b), [b, cc].as_slice());
        assert_eq!(t.super_classes(cc), [cc].as_slice());
        assert_eq!(t.sub_classes(cc), [a, b, cc].as_slice());
        assert!(t.is_told_sub(a, cc));
        assert!(!t.is_told_sub(cc, a));
    }

    #[test]
    fn equivalent_classes_creates_bidirectional_subsumption() {
        let o = build_ontology(&["A", "B"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            o.axioms.push(Axiom::EquivalentClasses(vec![a, b]));
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let b = c(&o, "B");
        assert!(t.is_told_sub(a, b));
        assert!(t.is_told_sub(b, a));
    }

    #[test]
    fn disjoint_classes_pairwise() {
        // DisjointClasses(A, B, C)
        let o = build_ontology(&["A", "B", "C"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            let cc = atom(o, "C");
            o.axioms.push(Axiom::DisjointClasses(vec![a, b, cc]));
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let b = c(&o, "B");
        let cc = c(&o, "C");
        assert!(t.are_told_disjoint(a, b));
        assert!(t.are_told_disjoint(a, cc));
        assert!(t.are_told_disjoint(b, cc));
        assert!(t.are_told_disjoint(b, a)); // symmetric
        assert_eq!(t.disjoints_of(a), [b, cc].as_slice());
    }

    #[test]
    fn sub_class_of_not_atomic_creates_disjoint() {
        // SubClassOf(A, Not(B))  ⇒  A and B disjoint.
        let o = build_ontology(&["A", "B"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            let not_b = o.concepts.not(b);
            o.axioms.push(Axiom::SubClassOf { sub: a, sup: not_b });
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let b = c(&o, "B");
        assert!(t.are_told_disjoint(a, b));
        // And no subsumption edge — Not(B) isn't a named class.
        assert!(!t.is_told_sub(a, b));
    }

    #[test]
    fn disjoint_union_contributes_both_subsumption_and_disjointness() {
        // DisjointUnion(P, [C1, C2])  ⇒  C1 ⊑ P, C2 ⊑ P, and C1 ⌖ C2.
        let o = build_ontology(&["P", "C1", "C2"], |o| {
            let c1 = atom(o, "C1");
            let c2 = atom(o, "C2");
            o.axioms.push(Axiom::DisjointUnion {
                class: c(o, "P"),
                members: vec![c1, c2],
            });
        });
        let t = build_told_tables(&o);
        let p = c(&o, "P");
        let c1 = c(&o, "C1");
        let c2 = c(&o, "C2");
        assert!(t.is_told_sub(c1, p));
        assert!(t.is_told_sub(c2, p));
        assert!(t.are_told_disjoint(c1, c2));
    }

    #[test]
    fn complex_shapes_are_skipped() {
        // SubClassOf(A ⊓ B, C)  — the LHS is And, not Atomic, so this
        // contributes nothing to told subsumers.
        let o = build_ontology(&["A", "B", "C"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            let cc = atom(o, "C");
            let and = o.concepts.and([a, b]);
            o.axioms.push(Axiom::SubClassOf { sub: and, sup: cc });
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let cc = c(&o, "C");
        assert!(!t.is_told_sub(a, cc));
    }

    #[test]
    fn equivalence_with_complex_member_only_uses_atomic_pairs() {
        // EquivalentClasses(A, B, And(C, D))  — A ⊑ B and B ⊑ A get added;
        // the third (complex) member does not contribute.
        let o = build_ontology(&["A", "B", "C", "D"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            let cc = atom(o, "C");
            let d = atom(o, "D");
            let and = o.concepts.and([cc, d]);
            o.axioms.push(Axiom::EquivalentClasses(vec![a, b, and]));
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let b = c(&o, "B");
        let cc = c(&o, "C");
        assert!(t.is_told_sub(a, b));
        assert!(t.is_told_sub(b, a));
        assert!(!t.is_told_sub(a, cc));
    }

    #[test]
    fn diamond_shape() {
        // A ⊑ B, A ⊑ C, B ⊑ D, C ⊑ D
        let o = build_ontology(&["A", "B", "C", "D"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            let cc = atom(o, "C");
            let d = atom(o, "D");
            o.axioms.push(Axiom::SubClassOf { sub: a, sup: b });
            o.axioms.push(Axiom::SubClassOf { sub: a, sup: cc });
            o.axioms.push(Axiom::SubClassOf { sub: b, sup: d });
            o.axioms.push(Axiom::SubClassOf { sub: cc, sup: d });
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let d = c(&o, "D");
        assert!(t.is_told_sub(a, d));
    }

    #[test]
    fn duplicate_axioms_idempotent() {
        let o = build_ontology(&["A", "B"], |o| {
            let a = atom(o, "A");
            let b = atom(o, "B");
            for _ in 0..3 {
                o.axioms.push(Axiom::SubClassOf { sub: a, sup: b });
            }
        });
        let t = build_told_tables(&o);
        let a = c(&o, "A");
        let b = c(&o, "B");
        assert_eq!(t.super_classes(a), [a, b].as_slice());
    }
}