All files / reasoners/owl-rl/rules class-axioms.ts

100% Statements 53/53
100% Branches 22/22
100% Functions 4/4
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                            8x     956x                             25407x 25407x 25407x 25407x   25407x   11210x 99x                 11210x 57x               11210x 56x                 11210x 4x 3x                   11210x 4x 3x                     25407x   12740x 110x                 25407x   586x 71x               586x 71x                                     436x 30x   8x 8x   8x 14x 14x   14x 12x   12x 10x 2x                                 16x   16x   14x 14x   14x   12x   12x   8x    
/**
 * OWL 2 RL — Table 7: Class axiom rules (cax-*)
 *
 * Implements rules cax-sco, cax-eqc1, cax-eqc2, cax-dw, and cax-adc.
 *
 * @see https://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs_using_Rules
 * @see Table 7 — https://www.w3.org/TR/owl2-profiles/#tab-rules-class-axioms
 */
import * as rdfjs from '@rdfjs/types';
import DataFactory from '@rdfjs/data-model';
import { TripleIndex } from '../../triple-index.js';
import { InferenceResult, infer } from '../../reasoner.js';
import { RDF, RDFS, OWL, rdf, rdfs, owl } from '../vocabulary.js';
 
const { namedNode } = DataFactory;
 
function makeTriple(subject: rdfjs.Quad_Subject, predicate: rdfjs.Quad_Predicate, object: rdfjs.Quad_Object): rdfjs.Quad {
    return DataFactory.quad(subject, predicate, object);
}
 
/**
 * Single-quad class axiom rules that fire once per incoming quad.
 *
 * Rules applied:
 * - cax-sco:  x type c1, c1 subClassOf c2  →  x type c2
 * - cax-eqc1: x type c1, c1 equivalentClass c2  →  x type c2
 * - cax-eqc2: x type c2, c1 equivalentClass c2  →  x type c1
 * - cax-dw:   x type c1, c1 disjointWith c2, x type c2  →  inconsistency
 *
 * @see https://www.w3.org/TR/owl2-profiles/#tab-rules-class-axioms
 */
export function* classAxiomSingleQuad(quad: rdfjs.Quad, index: TripleIndex): Iterable<InferenceResult> {
    const { subject, predicate, object } = quad;
    const predicateIri = predicate.value;
    const subjectIri   = subject.value;
    const objectIri    = object.value;
 
    if (predicateIri === RDF.type) {
        // cax-sco: x type c1, c1 subClassOf c2 → x type c2
        for (const superClass of index.getObjects(RDFS.subClassOf, objectIri)) {
            yield infer(
                makeTriple(subject, rdf.type, superClass),
                'cax-sco',
                quad,
                makeTriple(object as rdfjs.Quad_Subject, namedNode(RDFS.subClassOf), superClass),
            );
        }
 
        // cax-eqc1/2: x type c1, c1 equivalentClass c2 → x type c2 (and vice versa)
        for (const equivalentClass of index.getObjects(OWL.equivalentClass, objectIri)) {
            yield infer(
                makeTriple(subject, rdf.type, equivalentClass),
                'cax-eqc1',
                quad,
                makeTriple(object as rdfjs.Quad_Subject, namedNode(OWL.equivalentClass), equivalentClass),
            );
        }
 
        for (const equivalentClass of index.getSubjects(OWL.equivalentClass, objectIri)) {
            yield infer(
                makeTriple(subject, rdf.type, equivalentClass as rdfjs.Quad_Object),
                'cax-eqc2',
                quad,
                makeTriple(equivalentClass as rdfjs.Quad_Subject, namedNode(OWL.equivalentClass), object),
            );
        }
 
        // cax-dw: x type c1, c1 disjointWith c2, x type c2 → inconsistency
        for (const disjointClass of index.getObjects(OWL.disjointWith, objectIri)) {
            if (index.has(RDF.type, subjectIri, disjointClass.value)) {
                yield infer(
                    makeTriple(owl.Thing, rdfs.subClassOf, owl.Nothing),
                    'cax-dw',
                    quad,
                    makeTriple(object as rdfjs.Quad_Subject, namedNode(OWL.disjointWith), disjointClass),
                    makeTriple(subject, rdf.type, disjointClass),
                );
            }
        }
 
        for (const disjointClass of index.getSubjects(OWL.disjointWith, objectIri)) {
            if (index.has(RDF.type, subjectIri, disjointClass.value)) {
                yield infer(
                    makeTriple(owl.Thing, rdfs.subClassOf, owl.Nothing),
                    'cax-dw',
                    quad,
                    makeTriple(disjointClass as rdfjs.Quad_Subject, namedNode(OWL.disjointWith), object),
                    makeTriple(subject, rdf.type, disjointClass),
                );
            }
        }
    }
 
    if (predicateIri === RDFS.subClassOf) {
        // cax-sco propagation: if anything was typed as subject, it is now typed as object
        for (const instance of index.getSubjects(RDF.type, subjectIri)) {
            yield infer(
                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, object),
                'cax-sco',
                quad,
                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, subject as rdfjs.Quad_Object),
            );
        }
    }
 
    if (predicateIri === OWL.equivalentClass) {
        // Propagate type for all existing instances of subject → typed as object too
        for (const instance of index.getSubjects(RDF.type, subjectIri)) {
            yield infer(
                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, object),
                'cax-eqc1',
                quad,
                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, subject as rdfjs.Quad_Object),
            );
        }
 
        for (const instance of index.getSubjects(RDF.type, objectIri)) {
            yield infer(
                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, subject as rdfjs.Quad_Object),
                'cax-eqc2',
                quad,
                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, object),
            );
        }
    }
}
 
/**
 * Join-based class axiom rules that require scanning the full index.
 *
 * Rules applied:
 * - cax-adc: AllDisjointClasses, any individual typed as two member classes  →  inconsistency
 *
 * @see https://www.w3.org/TR/owl2-profiles/#tab-rules-class-axioms
 */
export function* classAxiomJoin(index: TripleIndex): Iterable<InferenceResult> {
    for (const [allDisjointIri, listHeads] of index.byPredSubj.get(OWL.members) ?? []) {
        if (!index.has(RDF.type, allDisjointIri, OWL.AllDisjointClasses)) continue;
 
        for (const listHead of listHeads) {
            const memberClasses = walkRdfList(listHead.value, index);
 
            for (let i = 0; i < memberClasses.length; i++) {
                const class1Iri = memberClasses[i].value;
                const typedAsClass1 = index.getSubjects(RDF.type, class1Iri);
 
                for (let j = i + 1; j < memberClasses.length; j++) {
                    const class2Iri = memberClasses[j].value;
 
                    for (const instance of typedAsClass1) {
                        if (index.has(RDF.type, instance.value, class2Iri)) {
                            yield infer(
                                makeTriple(owl.Thing, rdfs.subClassOf, owl.Nothing),
                                'cax-adc',
                                makeTriple(namedNode(allDisjointIri), rdf.type, namedNode(OWL.AllDisjointClasses)),
                                makeTriple(namedNode(allDisjointIri), namedNode(OWL.members), listHead),
                                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, namedNode(class1Iri)),
                                makeTriple(instance as rdfjs.Quad_Subject, rdf.type, namedNode(class2Iri)),
                            );
                        }
                    }
                }
            }
        }
    }
}
 
function walkRdfList(listHeadIri: string, index: TripleIndex): rdfjs.Quad_Object[] {
    const firstNodes = index.getObjects(RDF.first, listHeadIri);
 
    if (firstNodes.size === 0) return [];
 
    const [first] = firstNodes;
    const restNodes = index.getObjects(RDF.rest, listHeadIri);
 
    if (restNodes.size === 0) return [first];
 
    const [rest] = restNodes;
 
    if (rest.value === RDF.nil) return [first];
 
    return [first, ...walkRdfList(rest.value, index)];
}