Skip to main content

oxilite_core/
reason.rs

1//! RDFS / OWL reasoning: the schema closure, query rewriting and OWL 2 RL materialization.
2//!
3//! Query-time reasoning rewrites each triple pattern into a derived table of *entailed*
4//! triples, computed from the asserted quads and `tbox_closure` (a small table recomputed by
5//! `optimize()` and by writes that touch schema predicates). Queries therefore stay single SQL
6//! statements. Materialization is explicit: OWL 2 RL rules run as `INSERT … SELECT`
7//! statements into `quads_inf` until nothing changes.
8//!
9// @lat: [[architecture#Reasoning]]
10
11use crate::encoding::{named_node_id, DEFAULT_GRAPH_ID, PAYLOAD_BITS};
12use crate::sql::{union_all, Statement};
13use oxrdf::vocab::{rdf, rdfs};
14use oxrdf::{QuadRef, Term, TermRef};
15use spargebra::term::{NamedNodePattern, TermPattern};
16use spargebra::{GraphUpdateOperation, Update};
17use std::collections::BTreeSet;
18
19/// Entailment regime of a query.
20#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
23pub enum Reasoning {
24    /// Asserted triples only (Oxigraph's behaviour).
25    #[default]
26    None,
27    /// RDFS: subclass, subproperty, domain and range (plus equivalent classes and properties).
28    Rdfs,
29    /// RDFS plus the OWL QL property axioms: inverse and symmetric properties, and transitive
30    /// properties.
31    OwlQl,
32}
33
34const OWL: &str = "http://www.w3.org/2002/07/owl#";
35
36fn owl(local: &str) -> i64 {
37    named_node_id(&format!("{OWL}{local}"))
38}
39
40/// Ids of the vocabulary used by the closure and the rules.
41#[derive(Debug, Clone, Copy)]
42struct Vocab {
43    ty: i64,
44    sco: i64,
45    spo: i64,
46    dom: i64,
47    rng: i64,
48    eqc: i64,
49    eqp: i64,
50    inv: i64,
51    sym: i64,
52    trans: i64,
53    same: i64,
54    func: i64,
55    ifunc: i64,
56    has_value: i64,
57    on_property: i64,
58    some_values: i64,
59    all_values: i64,
60    intersection: i64,
61    union: i64,
62    chain: i64,
63    first: i64,
64    rest: i64,
65    nil: i64,
66    thing: i64,
67}
68
69fn vocab() -> Vocab {
70    Vocab {
71        ty: named_node_id(rdf::TYPE.as_str()),
72        sco: named_node_id(rdfs::SUB_CLASS_OF.as_str()),
73        spo: named_node_id(rdfs::SUB_PROPERTY_OF.as_str()),
74        dom: named_node_id(rdfs::DOMAIN.as_str()),
75        rng: named_node_id(rdfs::RANGE.as_str()),
76        eqc: owl("equivalentClass"),
77        eqp: owl("equivalentProperty"),
78        inv: owl("inverseOf"),
79        sym: owl("SymmetricProperty"),
80        trans: owl("TransitiveProperty"),
81        same: owl("sameAs"),
82        func: owl("FunctionalProperty"),
83        ifunc: owl("InverseFunctionalProperty"),
84        has_value: owl("hasValue"),
85        on_property: owl("onProperty"),
86        some_values: owl("someValuesFrom"),
87        all_values: owl("allValuesFrom"),
88        intersection: owl("intersectionOf"),
89        union: owl("unionOf"),
90        chain: owl("propertyChainAxiom"),
91        first: named_node_id(rdf::FIRST.as_str()),
92        rest: named_node_id(rdf::REST.as_str()),
93        nil: named_node_id(rdf::NIL.as_str()),
94        thing: owl("Thing"),
95    }
96}
97
98/// `tbox_closure.kind` values.
99pub mod kind {
100    /// `sub ⊑ sup` for classes (subClassOf and equivalentClass, transitive, irreflexive rows).
101    pub const CLASS: i64 = 1;
102    /// RDFS sub-property closure (subPropertyOf and equivalentProperty).
103    pub const PROPERTY: i64 = 2;
104    /// OWL: `(x sub y)` entails `(x sup y)` (sub-properties, inverses of inverses).
105    pub const OWL_SAME: i64 = 3;
106    /// OWL: `(x sub y)` entails `(y sup x)` (inverse and symmetric properties).
107    pub const OWL_INVERSE: i64 = 4;
108    /// Transitive properties (`sub = sup`).
109    pub const TRANSITIVE: i64 = 5;
110    /// RDFS: the subject of `sub` has type `sup` (domains through sub-properties/classes).
111    pub const SUBJECT_TYPE: i64 = 6;
112    /// RDFS: the object of `sub` has type `sup`.
113    pub const OBJECT_TYPE: i64 = 7;
114    /// OWL: the subject of `sub` has type `sup` (also through inverse ranges).
115    pub const OWL_SUBJECT_TYPE: i64 = 8;
116    /// OWL: the object of `sub` has type `sup`.
117    pub const OWL_OBJECT_TYPE: i64 = 9;
118}
119
120/// Predicates whose triples change the schema closure.
121pub fn schema_predicates() -> [i64; 6] {
122    let v = vocab();
123    [v.sco, v.spo, v.dom, v.rng, v.eqc, v.eqp]
124}
125
126/// Does a triple with this predicate and object change the closure?
127pub fn is_schema_triple(p: i64, o: i64) -> bool {
128    let v = vocab();
129    schema_predicates().contains(&p) || p == v.inv || (p == v.ty && (o == v.sym || o == v.trans))
130}
131
132fn is_schema_iri(p: &str) -> bool {
133    p == rdfs::SUB_CLASS_OF.as_str()
134        || p == rdfs::SUB_PROPERTY_OF.as_str()
135        || p == rdfs::DOMAIN.as_str()
136        || p == rdfs::RANGE.as_str()
137        || p.strip_prefix(OWL).is_some_and(|l| {
138            // `owl:imports` pulls another registered ontology into a scope.
139            matches!(
140                l,
141                "equivalentClass" | "equivalentProperty" | "inverseOf" | "imports"
142            )
143        })
144}
145
146fn is_axiom_class(o: &str) -> bool {
147    o.strip_prefix(OWL)
148        .is_some_and(|l| matches!(l, "SymmetricProperty" | "TransitiveProperty"))
149}
150
151/// Does writing this quad change the schema closure? (A schema axiom, or anything in the
152/// registry graph, which scopes the closure.)
153pub fn is_schema_quad(q: QuadRef<'_>) -> bool {
154    crate::registry::is_registry_quad(q)
155        || is_schema_iri(q.predicate.as_str())
156        || (q.predicate == rdf::TYPE
157            && matches!(q.object, TermRef::NamedNode(n) if is_axiom_class(n.as_str())))
158}
159
160/// Can this update change the schema closure? (Conservative: variables count as schema.)
161pub fn update_touches_schema(update: &Update) -> bool {
162    crate::registry::update_touches_registry(update) || touches_axioms(update)
163}
164
165fn touches_axioms(update: &Update) -> bool {
166    let pattern = |p: &NamedNodePattern, o: &TermPattern| match p {
167        NamedNodePattern::Variable(_) => true,
168        NamedNodePattern::NamedNode(n) if is_schema_iri(n.as_str()) => true,
169        NamedNodePattern::NamedNode(n) if *n == rdf::TYPE => match o {
170            TermPattern::NamedNode(c) => is_axiom_class(c.as_str()),
171            TermPattern::Variable(_) => true,
172            _ => false,
173        },
174        NamedNodePattern::NamedNode(_) => false,
175    };
176    update.operations.iter().any(|op| match op {
177        GraphUpdateOperation::InsertData { data } => data.iter().any(|q| {
178            is_schema_iri(q.predicate.as_str())
179                || (q.predicate == rdf::TYPE && matches!(&q.object, Term::NamedNode(n) if is_axiom_class(n.as_str())))
180        }),
181        GraphUpdateOperation::DeleteData { data } => data.iter().any(|q| {
182            is_schema_iri(q.predicate.as_str())
183                || (q.predicate == rdf::TYPE
184                    && matches!(&q.object, spargebra::term::GroundTerm::NamedNode(n) if is_axiom_class(n.as_str())))
185        }),
186        GraphUpdateOperation::DeleteInsert { delete, insert, .. } => {
187            delete.iter().any(|q| {
188                let o: TermPattern = q.object.clone().into();
189                pattern(&q.predicate, &o)
190            }) || insert.iter().any(|q| pattern(&q.predicate, &q.object))
191        }
192        GraphUpdateOperation::Create { .. } => false,
193        GraphUpdateOperation::Load { .. }
194        | GraphUpdateOperation::Clear { .. }
195        | GraphUpdateOperation::Drop { .. } => true,
196    })
197}
198
199/// SQL: the id is an IRI, a blank node or a triple term (a valid subject).
200pub(crate) fn non_literal(x: &str) -> String {
201    format!("(({x}) >> {PAYLOAD_BITS}) IN (1, 2, 9)")
202}
203
204/// Statements recomputing `tbox_closure`, one closure per scope (see
205/// [`crate::registry::ontology_axioms`]): the scope of every graph, and one per graph that an
206/// active ontology is mapped to. Each statement reads the ontology axioms tagged with their
207/// scope; recursion only joins rows of the same scope.
208pub fn closure_statements() -> Vec<Statement> {
209    let v = vocab();
210    let ax = |cond: String| crate::registry::ontology_axioms(&cond);
211    let (c, p, s_, t) = (
212        kind::CLASS,
213        kind::PROPERTY,
214        kind::OWL_SAME,
215        kind::TRANSITIVE,
216    );
217    let class_edges = format!(
218        "SELECT scope AS k, s AS a, o AS b FROM {} UNION SELECT scope, o, s FROM {}",
219        ax(format!("q.p IN ({}, {})", v.sco, v.eqc)),
220        ax(format!("q.p = {}", v.eqc))
221    );
222    let prop_edges = format!(
223        "SELECT scope AS k, s AS a, o AS b FROM {} UNION SELECT scope, o, s FROM {}",
224        ax(format!("q.p IN ({}, {})", v.spo, v.eqp)),
225        ax(format!("q.p = {}", v.eqp))
226    );
227    let closure = |k: i64, edges: &str| {
228        Statement::new(format!(
229            "WITH RECURSIVE e(k, a, b) AS ({edges}), \
230             c(k, a, b) AS (SELECT k, a, b FROM e UNION SELECT c.k, c.a, e.b FROM c JOIN e ON e.k = c.k AND e.a = c.b) \
231             INSERT OR IGNORE INTO tbox_closure(kind, scope, sub, sup) SELECT {k}, k, a, b FROM c WHERE a <> b"
232        ))
233    };
234    // OWL: property edges with a direction bit (1 = inverse), composed modulo 2.
235    let owl = Statement::new(format!(
236        "WITH RECURSIVE e(k, a, b, d) AS (SELECT k, a, b, 0 FROM ({prop_edges}) \
237           UNION SELECT scope, s, o, 1 FROM {inv} UNION SELECT scope, o, s, 1 FROM {inv} \
238           UNION SELECT scope, s, s, 1 FROM {sym}), \
239         c(k, a, b, d) AS (SELECT k, a, b, d FROM e UNION SELECT c.k, c.a, e.b, (c.d + e.d) % 2 FROM c JOIN e ON e.k = c.k AND e.a = c.b) \
240         INSERT OR IGNORE INTO tbox_closure(kind, scope, sub, sup) SELECT {s_} + d, k, a, b FROM c WHERE d = 1 OR a <> b",
241        inv = ax(format!("q.p = {}", v.inv)),
242        sym = ax(format!("q.p = {} AND q.o = {}", v.ty, v.sym)),
243    ));
244    let dom_rng = || ax(format!("q.p IN ({}, {})", v.dom, v.rng));
245    // Classes a domain/range class is a subclass of (reflexive), per scope.
246    let classes = format!(
247        "SELECT scope AS k, o AS a, o AS b FROM {} UNION SELECT scope, sub, sup FROM tbox_closure WHERE kind = {c}",
248        dom_rng()
249    );
250    // Properties with (sub, sup) where sup is reflexive over properties having a domain/range.
251    let subs = |k: i64| {
252        format!(
253            "SELECT scope AS k, s AS a, s AS b FROM {} UNION SELECT scope, sub, sup FROM tbox_closure WHERE kind = {k}",
254            dom_rng()
255        )
256    };
257    let typed = |k: i64, props: &str, axiom: i64| {
258        format!(
259            "SELECT {k}, pq.k, pq.a, cd.b FROM ({props}) pq JOIN {} x ON x.s = pq.b AND x.scope = pq.k \
260             JOIN ({classes}) cd ON cd.a = x.o AND cd.k = pq.k",
261            ax(format!("q.p = {axiom}"))
262        )
263    };
264    let inverse_typed = |k: i64, axiom: i64| {
265        format!(
266            "SELECT {k}, pq.scope, pq.sub, cd.b FROM tbox_closure pq JOIN {} x ON x.s = pq.sup AND x.scope = pq.scope \
267             JOIN ({classes}) cd ON cd.a = x.o AND cd.k = pq.scope WHERE pq.kind = {inv}",
268            ax(format!("q.p = {axiom}")),
269            inv = kind::OWL_INVERSE
270        )
271    };
272    let insert = |sql: String| {
273        Statement::new(format!(
274            "INSERT OR IGNORE INTO tbox_closure(kind, scope, sub, sup) {sql}"
275        ))
276    };
277    vec![
278        Statement::new("DELETE FROM tbox_closure"),
279        closure(c, &class_edges),
280        closure(p, &prop_edges),
281        owl,
282        Statement::new(format!(
283            "INSERT OR IGNORE INTO tbox_closure(kind, scope, sub, sup) SELECT {t}, scope, s, s FROM {}",
284            ax(format!("q.p = {} AND q.o = {}", v.ty, v.trans))
285        )),
286        insert(typed(kind::SUBJECT_TYPE, &subs(p), v.dom)),
287        insert(typed(kind::OBJECT_TYPE, &subs(p), v.rng)),
288        insert(format!(
289            "{} UNION {}",
290            typed(kind::OWL_SUBJECT_TYPE, &subs(s_), v.dom),
291            inverse_typed(kind::OWL_SUBJECT_TYPE, v.rng)
292        )),
293        insert(format!(
294            "{} UNION {}",
295            typed(kind::OWL_OBJECT_TYPE, &subs(s_), v.rng),
296            inverse_typed(kind::OWL_OBJECT_TYPE, v.dom)
297        )),
298    ]
299}
300
301/// Loads the transitive properties (kept in memory with the planner statistics).
302pub fn transitive_statement(id_col: impl Fn(&str) -> String) -> Statement {
303    Statement::new(format!(
304        "SELECT DISTINCT {} FROM tbox_closure WHERE kind = {}",
305        id_col("sub"),
306        kind::TRANSITIVE
307    ))
308}
309
310/// Which graphs a reasoned pattern reads.
311#[derive(Debug, Clone)]
312pub enum GraphFilter {
313    /// Keep each quad's graph (the caller constrains `g`).
314    Keep,
315    /// The RDF merge of these graphs (`None`: every graph), exposed as graph 0.
316    Merge(Option<Vec<i64>>),
317}
318
319/// Builds derived tables of entailed triples.
320#[derive(Debug, Clone)]
321pub struct Entailment<'a> {
322    pub reasoning: Reasoning,
323    /// Also read materialized inferences (`quads_inf`).
324    pub inferred: bool,
325    /// Exclude every registered schema graph from the stored triples
326    /// (`QueryOptions::include_schema_graphs`).
327    pub hide_schema: bool,
328    pub transitive: &'a BTreeSet<i64>,
329    /// Graphs with a closure of their own (see `tbox_closure.scope`); every other graph uses
330    /// the closure of [`crate::registry::all_scope`].
331    pub scopes: &'a BTreeSet<i64>,
332    /// Maximum terms of a compound SELECT.
333    pub max_compound: usize,
334    /// Read the store as it was at this tick (see `version`), from the change log.
335    pub as_of: Option<i64>,
336}
337
338impl Entailment<'_> {
339    /// SQL: the closure scope of the quads whose graph column is `g`.
340    fn scope_of(&self, g: &str) -> String {
341        let all = crate::registry::all_scope();
342        if self.scopes.is_empty() {
343            return all.to_string();
344        }
345        let list = self
346            .scopes
347            .iter()
348            .map(i64::to_string)
349            .collect::<Vec<_>>()
350            .join(", ");
351        format!("(CASE WHEN {g} IN ({list}) THEN {g} ELSE {all} END)")
352    }
353
354    /// Is any rewriting needed?
355    pub fn active(&self) -> bool {
356        self.reasoning != Reasoning::None || self.inferred
357    }
358
359    /// The table of stored triples: asserted, plus materialized inferences on request, minus
360    /// the registered schema graphs when they are hidden.
361    ///
362    /// Every quad source of the compiler goes through this or [`Self::source`], so hiding is
363    /// applied once and covers patterns, paths, `OPTIONAL` and `GRAPH ?g` alike. Inferences
364    /// are conclusions rather than schema, so they are never hidden.
365    pub fn base(&self) -> String {
366        let asserted = match (self.as_of, self.hide_schema) {
367            (Some(t), true) => {
368                crate::registry::without_schema_graphs(&crate::version::as_of_sql(&t.to_string()))
369            }
370            (Some(t), false) => crate::version::as_of_sql(&t.to_string()),
371            (None, true) => crate::registry::quads_without_schema_graphs().to_owned(),
372            (None, false) => "quads".to_owned(),
373        };
374        if self.inferred {
375            format!(
376                "(SELECT s, p, o, g FROM {asserted} UNION ALL SELECT s, p, o, g FROM quads_inf)"
377            )
378        } else {
379            asserted.to_string()
380        }
381    }
382
383    fn kinds(&self) -> (i64, Option<i64>, i64, i64) {
384        match self.reasoning {
385            Reasoning::OwlQl => (
386                kind::OWL_SAME,
387                Some(kind::OWL_INVERSE),
388                kind::OWL_SUBJECT_TYPE,
389                kind::OWL_OBJECT_TYPE,
390            ),
391            _ => (kind::PROPERTY, None, kind::SUBJECT_TYPE, kind::OBJECT_TYPE),
392        }
393    }
394
395    /// A `(s, p, o, g)` derived table of the entailed triples matching the constants.
396    pub fn source(
397        &self,
398        s: Option<i64>,
399        p: Option<i64>,
400        o: Option<i64>,
401        graphs: &GraphFilter,
402    ) -> String {
403        let base = self.base();
404        let g = |x: &str| match graphs {
405            GraphFilter::Keep => format!("{x}.g"),
406            GraphFilter::Merge(_) => DEFAULT_GRAPH_ID.to_string(),
407        };
408        let gw = |x: &str| match graphs {
409            GraphFilter::Merge(Some(l)) => format!(
410                " AND {x}.g IN ({})",
411                l.iter().map(i64::to_string).collect::<Vec<_>>().join(", ")
412            ),
413            _ => String::new(),
414        };
415        let eq =
416            |col: &str, v: Option<i64>| v.map(|v| format!(" AND {col} = {v}")).unwrap_or_default();
417        if self.reasoning == Reasoning::None {
418            let mut w = String::from("1");
419            w.push_str(&eq("x.s", s));
420            w.push_str(&eq("x.p", p));
421            w.push_str(&eq("x.o", o));
422            w.push_str(&gw("x"));
423            let d = if matches!(graphs, GraphFilter::Merge(_)) {
424                "DISTINCT "
425            } else {
426                ""
427            };
428            return format!(
429                "(SELECT {d}x.s AS s, x.p AS p, x.o AS o, {} AS g FROM {base} x WHERE {w})",
430                g("x")
431            );
432        }
433        let v = vocab();
434        let (same, inv, subj, obj) = self.kinds();
435        // Each quad is entailed with the closure of its graph's scope.
436        let cs = format!(" AND c.scope = {}", self.scope_of("x.g"));
437        let cs = cs.as_str();
438        // One arm: `SELECT s AS s, p AS p, o AS o, g AS g FROM …` (compound SELECTs take their
439        // column names from the first arm, and derived tables cannot rename columns).
440        let row = |s: &str, p: &str, o: &str, gx: &str, rest: String| {
441            format!(
442                "SELECT {s} AS s, {p} AS p, {o} AS o, {} AS g FROM {rest}",
443                g(gx)
444            )
445        };
446        let ty = v.ty.to_string();
447        let type_arms = |s: Option<i64>, o: Option<i64>, asserted: bool| -> Vec<String> {
448            let mut a = Vec::new();
449            if asserted {
450                a.push(row(
451                    "x.s",
452                    &ty,
453                    "x.o",
454                    "x",
455                    format!(
456                        "{base} x WHERE x.p = {ty}{}{}{}",
457                        eq("x.s", s),
458                        eq("x.o", o),
459                        gw("x")
460                    ),
461                ));
462            }
463            // Superclasses of asserted types, then domains and ranges.
464            a.push(row("x.s", &ty, "c.sup", "x", format!(
465                "{base} x JOIN tbox_closure c ON c.kind = {} AND c.sub = x.o WHERE x.p = {ty}{cs}{}{}{}",
466                kind::CLASS, eq("x.s", s), eq("c.sup", o), gw("x")
467            )));
468            a.push(row(
469                "x.s",
470                &ty,
471                "c.sup",
472                "x",
473                format!(
474                    "tbox_closure c JOIN {base} x ON x.p = c.sub WHERE c.kind = {subj}{cs}{}{}{}",
475                    eq("x.s", s),
476                    eq("c.sup", o),
477                    gw("x")
478                ),
479            ));
480            a.push(row(
481                "x.o",
482                &ty,
483                "c.sup",
484                "x",
485                format!(
486                    "tbox_closure c JOIN {base} x ON x.p = c.sub WHERE c.kind = {obj}{cs} AND {}{}{}{}",
487                    non_literal("x.o"),
488                    eq("x.o", s),
489                    eq("c.sup", o),
490                    gw("x")
491                ),
492            ));
493            a
494        };
495        // Triples of property `pid` entailed without transitivity.
496        let prop_arms = |pid: i64, s: Option<i64>, o: Option<i64>| -> Vec<String> {
497            let pid_s = pid.to_string();
498            let mut a = vec![
499                row("x.s", &pid_s, "x.o", "x", format!("{base} x WHERE x.p = {pid}{}{}{}", eq("x.s", s), eq("x.o", o), gw("x"))),
500                row("x.s", &pid_s, "x.o", "x", format!(
501                    "tbox_closure c JOIN {base} x ON x.p = c.sub WHERE c.kind = {same} AND c.sup = {pid}{cs}{}{}{}",
502                    eq("x.s", s), eq("x.o", o), gw("x")
503                )),
504            ];
505            if let Some(inv) = inv {
506                a.push(row("x.o", &pid_s, "x.s", "x", format!(
507                    "tbox_closure c JOIN {base} x ON x.p = c.sub WHERE c.kind = {inv} AND c.sup = {pid}{cs} AND {}{}{}{}",
508                    non_literal("x.o"), eq("x.o", s), eq("x.s", o), gw("x")
509                )));
510            }
511            // The schema closure itself answers subClassOf / subPropertyOf patterns.
512            let schema_kind = if pid == v.sco {
513                Some(kind::CLASS)
514            } else if pid == v.spo {
515                Some(kind::PROPERTY)
516            } else {
517                None
518            };
519            if let Some(k) = schema_kind {
520                a.push(format!(
521                    "SELECT sub AS s, {pid} AS p, sup AS o, {DEFAULT_GRAPH_ID} AS g FROM tbox_closure WHERE kind = {k}{}{}",
522                    eq("sub", s),
523                    eq("sup", o)
524                ));
525            }
526            a
527        };
528        // A transitive property: the closure of its (otherwise) entailed triples, walked from a
529        // constant endpoint when there is one.
530        // With graphs of their own scope, a property is transitive only in the graphs whose
531        // scope declares it so; elsewhere its triples are entailed as for any property.
532        let declared = |pid: i64, g: &str| {
533            format!(
534                "EXISTS (SELECT 1 FROM tbox_closure t WHERE t.kind = {} AND t.sub = {pid} AND t.scope = {})",
535                kind::TRANSITIVE,
536                self.scope_of(g)
537            )
538        };
539        let transitive = |pid: i64, s: Option<i64>, o: Option<i64>| -> Vec<String> {
540            let mut u = union_all(prop_arms(pid, None, None), self.max_compound);
541            let mut arms = Vec::new();
542            if !self.scopes.is_empty() {
543                u = format!(
544                    "SELECT z.s, z.p, z.o, z.g FROM ({u}) z WHERE {}",
545                    declared(pid, "z.g")
546                );
547                arms.push(format!(
548                    "SELECT z.s, z.p, z.o, z.g FROM ({}) z WHERE NOT {}",
549                    union_all(prop_arms(pid, s, o), self.max_compound),
550                    declared(pid, "z.g")
551                ));
552            }
553            let rec = match (s, o) {
554                (Some(s), _) => format!(
555                    "r(n, g) AS (SELECT o, g FROM u WHERE s = {s} UNION SELECT u.o, u.g FROM r JOIN u ON u.s = r.n AND u.g = r.g) \
556                     SELECT {s} AS s, {pid} AS p, n AS o, g FROM r{}",
557                    o.map(|o| format!(" WHERE n = {o}")).unwrap_or_default()
558                ),
559                (None, Some(o)) => format!(
560                    "r(n, g) AS (SELECT s, g FROM u WHERE o = {o} UNION SELECT u.s, u.g FROM r JOIN u ON u.o = r.n AND u.g = r.g) \
561                     SELECT n AS s, {pid} AS p, {o} AS o, g FROM r"
562                ),
563                (None, None) => format!(
564                    "r(s, o, g) AS (SELECT s, o, g FROM u UNION SELECT r.s, u.o, r.g FROM r JOIN u ON u.s = r.o AND u.g = r.g) \
565                     SELECT s, {pid} AS p, o, g FROM r"
566                ),
567            };
568            arms.push(format!(
569                "SELECT * FROM (WITH RECURSIVE u(s, p, o, g) AS ({u}), {rec})"
570            ));
571            arms
572        };
573        let mut arms = Vec::new();
574        match p {
575            Some(pid) if pid == v.ty => arms.extend(type_arms(s, o, true)),
576            Some(pid) if self.reasoning == Reasoning::OwlQl && self.transitive.contains(&pid) => {
577                arms.extend(transitive(pid, s, o));
578            }
579            Some(pid) => arms.extend(prop_arms(pid, s, o)),
580            None => {
581                // Every entailed triple: asserted, through property axioms, the schema closure,
582                // types, and transitive properties.
583                arms.push(row(
584                    "x.s",
585                    "x.p",
586                    "x.o",
587                    "x",
588                    format!(
589                        "{base} x WHERE 1{}{}{}",
590                        eq("x.s", s),
591                        eq("x.o", o),
592                        gw("x")
593                    ),
594                ));
595                arms.push(row(
596                    "x.s",
597                    "c.sup",
598                    "x.o",
599                    "x",
600                    format!(
601                        "tbox_closure c JOIN {base} x ON x.p = c.sub WHERE c.kind = {same}{cs}{}{}{}",
602                        eq("x.s", s),
603                        eq("x.o", o),
604                        gw("x")
605                    ),
606                ));
607                if let Some(inv) = inv {
608                    arms.push(row("x.o", "c.sup", "x.s", "x", format!(
609                        "tbox_closure c JOIN {base} x ON x.p = c.sub WHERE c.kind = {inv}{cs} AND {}{}{}{}",
610                        non_literal("x.o"), eq("x.o", s), eq("x.s", o), gw("x")
611                    )));
612                }
613                for (k, pid) in [(kind::CLASS, v.sco), (kind::PROPERTY, v.spo)] {
614                    arms.push(format!(
615                        "SELECT sub AS s, {pid} AS p, sup AS o, {DEFAULT_GRAPH_ID} AS g FROM tbox_closure WHERE kind = {k}{}{}",
616                        eq("sub", s),
617                        eq("sup", o)
618                    ));
619                }
620                arms.extend(type_arms(s, o, false));
621                if self.reasoning == Reasoning::OwlQl {
622                    for &pid in self.transitive {
623                        arms.extend(transitive(pid, s, o));
624                    }
625                }
626            }
627        }
628        format!(
629            "(SELECT DISTINCT s, p, o, g FROM ({}))",
630            union_all(arms, self.max_compound)
631        )
632    }
633}
634
635/// Statements of one OWL 2 RL round: every rule inserts its new conclusions into
636/// `quads_inf` (graph 0), reading asserted and inferred triples of every graph.
637pub fn materialize_round() -> Vec<Statement> {
638    let v = vocab();
639    let a = "(SELECT s, p, o FROM quads UNION ALL SELECT s, p, o FROM quads_inf)";
640    let nl = |x: &str| non_literal(x);
641    let lists = format!(
642        "RECURSIVE l(head, node, item) AS (SELECT s, s, o FROM {a} WHERE p = {first} \
643           UNION SELECT l.head, r.o, f.o FROM l JOIN {a} r ON r.s = l.node AND r.p = {rest} JOIN {a} f ON f.s = r.o AND f.p = {first})",
644        first = v.first,
645        rest = v.rest
646    );
647    let rules: Vec<String> = vec![
648        // eq-sym, eq-trans
649        format!("SELECT o, {same}, s FROM {a} WHERE p = {same}", same = v.same),
650        format!("SELECT x.s, {same}, y.o FROM {a} x JOIN {a} y ON y.s = x.o AND y.p = {same} WHERE x.p = {same}", same = v.same),
651        // eq-rep-s, eq-rep-p, eq-rep-o
652        format!("SELECT e.o, t.p, t.o FROM {a} e JOIN {a} t ON t.s = e.s WHERE e.p = {same} AND {}", nl("e.o"), same = v.same),
653        format!("SELECT t.s, e.o, t.o FROM {a} e JOIN {a} t ON t.p = e.s WHERE e.p = {same} AND ((e.o) >> {PAYLOAD_BITS}) = 1", same = v.same),
654        format!("SELECT t.s, t.p, e.o FROM {a} e JOIN {a} t ON t.o = e.s WHERE e.p = {same}", same = v.same),
655        // prp-dom, prp-rng
656        format!("SELECT t.s, {ty}, d.o FROM {a} d JOIN {a} t ON t.p = d.s WHERE d.p = {dom}", ty = v.ty, dom = v.dom),
657        format!("SELECT t.o, {ty}, d.o FROM {a} d JOIN {a} t ON t.p = d.s WHERE d.p = {rng} AND {}", nl("t.o"), ty = v.ty, rng = v.rng),
658        // prp-fp, prp-ifp
659        format!(
660            "SELECT t1.o, {same}, t2.o FROM {a} f JOIN {a} t1 ON t1.p = f.s JOIN {a} t2 ON t2.p = f.s AND t2.s = t1.s \
661             WHERE f.p = {ty} AND f.o = {func} AND t1.o <> t2.o AND {} AND {}",
662            nl("t1.o"), nl("t2.o"), same = v.same, ty = v.ty, func = v.func
663        ),
664        format!(
665            "SELECT t1.s, {same}, t2.s FROM {a} f JOIN {a} t1 ON t1.p = f.s JOIN {a} t2 ON t2.p = f.s AND t2.o = t1.o \
666             WHERE f.p = {ty} AND f.o = {ifunc} AND t1.s <> t2.s",
667            same = v.same, ty = v.ty, ifunc = v.ifunc
668        ),
669        // prp-symp, prp-trp
670        format!("SELECT t.o, t.p, t.s FROM {a} f JOIN {a} t ON t.p = f.s WHERE f.p = {ty} AND f.o = {sym} AND {}", nl("t.o"), ty = v.ty, sym = v.sym),
671        format!(
672            "SELECT t1.s, t1.p, t2.o FROM {a} f JOIN {a} t1 ON t1.p = f.s JOIN {a} t2 ON t2.p = f.s AND t2.s = t1.o WHERE f.p = {ty} AND f.o = {trans}",
673            ty = v.ty, trans = v.trans
674        ),
675        // prp-spo1, prp-eqp1, prp-eqp2
676        format!("SELECT t.s, x.o, t.o FROM {a} x JOIN {a} t ON t.p = x.s WHERE x.p = {spo}", spo = v.spo),
677        format!("SELECT t.s, x.o, t.o FROM {a} x JOIN {a} t ON t.p = x.s WHERE x.p = {eqp}", eqp = v.eqp),
678        format!("SELECT t.s, x.s, t.o FROM {a} x JOIN {a} t ON t.p = x.o WHERE x.p = {eqp}", eqp = v.eqp),
679        // prp-inv1, prp-inv2
680        format!("SELECT t.o, x.o, t.s FROM {a} x JOIN {a} t ON t.p = x.s WHERE x.p = {inv} AND {}", nl("t.o"), inv = v.inv),
681        format!("SELECT t.o, x.s, t.s FROM {a} x JOIN {a} t ON t.p = x.o WHERE x.p = {inv} AND {}", nl("t.o"), inv = v.inv),
682        // cax-sco, cax-eqc1, cax-eqc2
683        format!("SELECT t.s, {ty}, x.o FROM {a} x JOIN {a} t ON t.p = {ty} AND t.o = x.s WHERE x.p = {sco}", ty = v.ty, sco = v.sco),
684        format!("SELECT t.s, {ty}, x.o FROM {a} x JOIN {a} t ON t.p = {ty} AND t.o = x.s WHERE x.p = {eqc}", ty = v.ty, eqc = v.eqc),
685        format!("SELECT t.s, {ty}, x.s FROM {a} x JOIN {a} t ON t.p = {ty} AND t.o = x.o WHERE x.p = {eqc}", ty = v.ty, eqc = v.eqc),
686        // scm-sco (transitive subclasses)
687        format!("SELECT x.s, {sco}, y.o FROM {a} x JOIN {a} y ON y.s = x.o AND y.p = {sco} WHERE x.p = {sco}", sco = v.sco),
688        // scm-eqc1 (the other scm-* rules only restate schema triples whose instance-level
689        // consequences the rules above already derive; `reasonable` omits them too)
690        format!("SELECT s, {sco}, o FROM {a} WHERE p = {eqc} UNION ALL SELECT o, {sco}, s FROM {a} WHERE p = {eqc}", sco = v.sco, eqc = v.eqc),
691        // Every subject is an owl:Thing; owl:Thing and owl:Nothing are classes (cls-thing, cls-nothing1)
692        format!("SELECT s, {ty}, {thing} FROM {a} WHERE {} UNION SELECT {thing}, {ty}, {class} UNION SELECT {nothing}, {ty}, {class}", nl("s"), ty = v.ty, thing = v.thing, class = owl("Class"), nothing = owl("Nothing")),
693        // cls-hv1, cls-hv2
694        format!(
695            "SELECT t.s, op.o, hv.o FROM {a} hv JOIN {a} op ON op.s = hv.s AND op.p = {onp} JOIN {a} t ON t.p = {ty} AND t.o = hv.s WHERE hv.p = {hv}",
696            onp = v.on_property, ty = v.ty, hv = v.has_value
697        ),
698        format!(
699            "SELECT t.s, {ty}, hv.s FROM {a} hv JOIN {a} op ON op.s = hv.s AND op.p = {onp} JOIN {a} t ON t.p = op.o AND t.o = hv.o WHERE hv.p = {hv}",
700            onp = v.on_property, ty = v.ty, hv = v.has_value
701        ),
702        // cls-svf1, cls-svf2
703        format!(
704            "SELECT t.s, {ty}, sv.s FROM {a} sv JOIN {a} op ON op.s = sv.s AND op.p = {onp} JOIN {a} t ON t.p = op.o \
705             JOIN {a} vt ON vt.s = t.o AND vt.p = {ty} AND vt.o = sv.o WHERE sv.p = {svf}",
706            onp = v.on_property, ty = v.ty, svf = v.some_values
707        ),
708        format!(
709            "SELECT t.s, {ty}, sv.s FROM {a} sv JOIN {a} op ON op.s = sv.s AND op.p = {onp} JOIN {a} t ON t.p = op.o WHERE sv.p = {svf} AND sv.o = {thing}",
710            onp = v.on_property, ty = v.ty, svf = v.some_values, thing = v.thing
711        ),
712        // cls-avf
713        format!(
714            "SELECT t.o, {ty}, av.o FROM {a} av JOIN {a} op ON op.s = av.s AND op.p = {onp} JOIN {a} xt ON xt.p = {ty} AND xt.o = av.s \
715             JOIN {a} t ON t.s = xt.s AND t.p = op.o WHERE av.p = {avf} AND {}",
716            nl("t.o"), onp = v.on_property, ty = v.ty, avf = v.all_values
717        ),
718        // cls-int1, cls-int2, cls-uni (list members through rdf:first / rdf:rest)
719        format!(
720            "WITH {lists}, m(c, item) AS (SELECT x.s, l.item FROM {a} x JOIN l ON l.head = x.o WHERE x.p = {int}) \
721             SELECT t.s, {ty}, m.c FROM m JOIN {a} t ON t.p = {ty} AND t.o = m.item GROUP BY m.c, t.s \
722             HAVING COUNT(DISTINCT m.item) = (SELECT COUNT(DISTINCT m2.item) FROM m m2 WHERE m2.c = m.c)",
723            ty = v.ty, int = v.intersection
724        ),
725        format!(
726            "WITH {lists} SELECT t.s, {ty}, l.item FROM {a} x JOIN l ON l.head = x.o JOIN {a} t ON t.p = {ty} AND t.o = x.s WHERE x.p = {int}",
727            ty = v.ty, int = v.intersection
728        ),
729        format!(
730            "WITH {lists} SELECT t.s, {ty}, x.s FROM {a} x JOIN l ON l.head = x.o JOIN {a} t ON t.p = {ty} AND t.o = l.item WHERE x.p = {uni}",
731            ty = v.ty, uni = v.union
732        ),
733        // prp-spo2 (property chains)
734        format!(
735            "WITH {lists}, \
736               ix(head, node, i) AS (SELECT s, s, 1 FROM {a} WHERE p = {first} UNION SELECT ix.head, r.o, ix.i + 1 FROM ix JOIN {a} r ON r.s = ix.node AND r.p = {rest} WHERE r.o <> {nil}), \
737               steps(chain, i, prop) AS (SELECT x.s, ix.i, f.o FROM {a} x JOIN ix ON ix.head = x.o JOIN {a} f ON f.s = ix.node AND f.p = {first} WHERE x.p = {chain}), \
738               w(chain, start, i, cur) AS (SELECT st.chain, t.s, 1, t.o FROM steps st JOIN {a} t ON t.p = st.prop WHERE st.i = 1 \
739                 UNION SELECT w.chain, w.start, w.i + 1, t.o FROM w JOIN steps st ON st.chain = w.chain AND st.i = w.i + 1 JOIN {a} t ON t.s = w.cur AND t.p = st.prop) \
740             SELECT w.start, w.chain, w.cur FROM w WHERE w.i = (SELECT MAX(i) FROM steps s2 WHERE s2.chain = w.chain)",
741            first = v.first, rest = v.rest, nil = v.nil, chain = v.chain
742        ),
743    ];
744    let mut statements: Vec<Statement> = rules
745        .into_iter()
746        .map(|r| {
747            let (with, body) = match r.strip_prefix("WITH ") {
748                Some(rest) => split_with(rest),
749                None => (String::new(), r),
750            };
751            let body = name_columns(&body);
752            Statement::new(format!(
753                "{with}INSERT OR IGNORE INTO quads_inf(s, p, o, g) SELECT DISTINCT r.s, r.p, r.o, {DEFAULT_GRAPH_ID} FROM ({body}) AS r \
754                 WHERE {} AND NOT EXISTS (SELECT 1 FROM quads a WHERE a.s = r.s AND a.p = r.p AND a.o = r.o AND a.g = {DEFAULT_GRAPH_ID})",
755                non_literal("r.s")
756            ))
757        })
758        .collect();
759    statements.push(inference_attribute(OWL_PRODUCER));
760    statements
761}
762
763/// Renames the three result columns of a rule's first SELECT to `s, p, o`.
764fn name_columns(select: &str) -> String {
765    let rest = select.strip_prefix("SELECT ").unwrap_or(select);
766    let from = top_level(rest, " FROM ").unwrap_or(rest.len());
767    let cols: Vec<&str> = split_top(&rest[..from], ',');
768    if cols.len() != 3 {
769        return select.to_string();
770    }
771    format!(
772        "SELECT {} AS s, {} AS p, {} AS o{}",
773        cols[0].trim(),
774        cols[1].trim(),
775        cols[2].trim(),
776        &rest[from..]
777    )
778}
779
780/// Byte offset of the first top-level (outside parentheses) occurrence of `pat`.
781fn top_level(s: &str, pat: &str) -> Option<usize> {
782    let mut depth = 0i32;
783    for (i, c) in s.char_indices() {
784        match c {
785            '(' => depth += 1,
786            ')' => depth -= 1,
787            _ if depth == 0 && s[i..].starts_with(pat) => return Some(i),
788            _ => {}
789        }
790    }
791    None
792}
793
794fn split_top(s: &str, sep: char) -> Vec<&str> {
795    let (mut depth, mut start, mut out) = (0i32, 0usize, Vec::new());
796    for (i, c) in s.char_indices() {
797        match c {
798            '(' => depth += 1,
799            ')' => depth -= 1,
800            c if c == sep && depth == 0 => {
801                out.push(&s[start..i]);
802                start = i + 1;
803            }
804            _ => {}
805        }
806    }
807    out.push(&s[start..]);
808    out
809}
810
811/// Splits `ctes SELECT …` (after `WITH `) into the `WITH … ` prefix and the final SELECT.
812fn split_with(rest: &str) -> (String, String) {
813    // The final SELECT is the last top-level "SELECT" (outside parentheses).
814    let bytes = rest.as_bytes();
815    let (mut depth, mut last) = (0i32, 0usize);
816    for (i, &c) in bytes.iter().enumerate() {
817        match c {
818            b'(' => depth += 1,
819            b')' => depth -= 1,
820            b'S' if depth == 0 && rest[i..].starts_with("SELECT ") => last = i,
821            _ => {}
822        }
823    }
824    (
825        format!("WITH {} ", rest[..last].trim_end()),
826        rest[last..].to_string(),
827    )
828}
829
830/// The producer id of OWL 2 RL materialization (SQL rules and `reasonable` alike).
831pub const OWL_PRODUCER: &str = "owl2rl";
832
833/// The id a producer name is stored under in `quads_inf_src` and `inf_producers`.
834pub fn producer_id(name: &str) -> i64 {
835    // A positive 62-bit hash: stable across runs and backends, computed without a lookup.
836    (xxhash_rust::xxh3::xxh3_64(name.as_bytes()) >> 2) as i64
837}
838
839/// Discards one producer's inferences: its attributions go, and so does every inferred quad no
840/// other producer derived. The producer is (re)registered under its name.
841pub fn inference_reset(name: &str) -> Vec<Statement> {
842    let id = producer_id(name);
843    vec![
844        Statement::new(format!(
845            "INSERT OR REPLACE INTO inf_producers(id, name) VALUES ({id}, '{}')",
846            name.replace('\'', "''")
847        )),
848        Statement::new(format!("DELETE FROM quads_inf_src WHERE src = {id}")),
849        Statement::new(
850            "DELETE FROM quads_inf WHERE NOT EXISTS (SELECT 1 FROM quads_inf_src q \
851             WHERE q.s = quads_inf.s AND q.p = quads_inf.p AND q.o = quads_inf.o AND q.g = quads_inf.g)",
852        ),
853    ]
854}
855
856/// Attributes every inferred quad no producer claims yet to `name`. Run after a producer
857/// writes, so what it derived (and nobody had derived before) is recorded as its own.
858pub fn inference_attribute(name: &str) -> Statement {
859    Statement::new(format!(
860        "INSERT OR IGNORE INTO quads_inf_src(src, s, p, o, g) SELECT {}, i.s, i.p, i.o, i.g FROM quads_inf i \
861         WHERE NOT EXISTS (SELECT 1 FROM quads_inf_src q WHERE q.s = i.s AND q.p = i.p AND q.o = i.o AND q.g = i.g)",
862        producer_id(name)
863    ))
864}
865
866/// Statements run before OWL 2 RL materialization (see [`materialize_reset_for`]).
867pub fn materialize_reset(caps: &crate::sql::Capabilities) -> Vec<Statement> {
868    materialize_reset_for(caps, OWL_PRODUCER)
869}
870
871/// Statements run before a producer materializes: its previous inferences are discarded, and
872/// the vocabulary that rule conclusions use (`owl:sameAs`, `rdfs:subClassOf`, …) gets its term
873/// rows, since it may not appear in the data.
874pub fn materialize_reset_for(caps: &crate::sql::Capabilities, producer: &str) -> Vec<Statement> {
875    let mut rows = crate::encoding::EncodedRows::default();
876    for iri in [
877        rdf::TYPE.as_str(),
878        rdfs::SUB_CLASS_OF.as_str(),
879        rdfs::SUB_PROPERTY_OF.as_str(),
880        rdfs::DOMAIN.as_str(),
881        rdfs::RANGE.as_str(),
882    ] {
883        rows.iri(iri);
884    }
885    for local in [
886        "sameAs",
887        "equivalentClass",
888        "equivalentProperty",
889        "Thing",
890        "Nothing",
891        "Class",
892    ] {
893        rows.iri(&format!("{OWL}{local}"));
894    }
895    rows.dedup();
896    let mut out = inference_reset(producer);
897    out.extend(crate::writer::term_statements(&rows, caps));
898    out
899}