Skip to main content

oxilite_reason/
lib.rs

1//! OWL 2 RL materialization with [`reasonable`], for native backends.
2//!
3//! `materialize` reads every asserted triple (all graphs, merged), runs the `reasonable`
4//! Datalog engine in memory and writes the new conclusions into `quads_inf` (graph 0), exactly
5//! where the SQL rules of `oxilite_core::reason` put theirs. It is much faster than the SQL
6//! fixpoint on large data but needs the whole dataset in memory, so it is not used on D1.
7//!
8// @lat: [[architecture#Reasoning]]
9
10use oxilite_core::encoding::{EncodedRows, DEFAULT_GRAPH_ID};
11use oxilite_core::writer::{insert_statements_into, term_statements};
12use oxilite_core::{ops, run_sync, Request, Result, SyncBackend};
13use oxrdf::{GraphNameRef, QuadRef, Triple};
14use reasonable::reasoner::Reasoner;
15use std::collections::HashSet;
16
17/// The triples `reasonable` infers from the asserted triples (every graph merged), minus
18/// the triples asserted in the default graph.
19pub fn infer<B: SyncBackend>(backend: &B) -> Result<Vec<Triple>> {
20    let caps = backend.capabilities().clone();
21    let quads = run_sync(backend, ops::scan_job(None, None, None, None, &caps))?;
22    let default: HashSet<Triple> = quads
23        .iter()
24        .filter(|q| q.graph_name.is_default_graph())
25        .map(|q| Triple::from(q.clone()))
26        .collect();
27    let input: Vec<Triple> = quads
28        .into_iter()
29        .map(Triple::from)
30        .collect::<HashSet<_>>()
31        .into_iter()
32        .collect();
33    let mut reasoner = Reasoner::new();
34    reasoner.load_triples(input);
35    reasoner.reason();
36    // `reasonable` reports consistency diagnostics (e.g. `cax-dw`) without failing; like the
37    // SQL rules, materialization keeps going on inconsistent data.
38    let mut out: Vec<Triple> = reasoner
39        .get_triples()
40        .into_iter()
41        .filter(|t| !default.contains(t))
42        .collect::<HashSet<_>>()
43        .into_iter()
44        .collect();
45    out.sort_by_cached_key(ToString::to_string);
46    Ok(out)
47}
48
49/// Replaces `quads_inf` with the inferences of `reasonable`; returns their number.
50pub fn materialize<B: SyncBackend>(backend: &B) -> Result<u64> {
51    let caps = backend.capabilities().clone();
52    let triples = infer(backend)?;
53    let mut rows = EncodedRows::default();
54    let ids: Vec<[i64; 4]> = triples
55        .iter()
56        .map(|t| {
57            let [s, p, o, _] = rows.quad(QuadRef::new(
58                t.subject.as_ref(),
59                t.predicate.as_ref(),
60                t.object.as_ref(),
61                GraphNameRef::DefaultGraph,
62            ));
63            [s, p, o, DEFAULT_GRAPH_ID]
64        })
65        .collect();
66    rows.dedup();
67    let mut stmts = oxilite_core::reason::inference_reset(oxilite_core::reason::OWL_PRODUCER);
68    stmts.extend(term_statements(&rows, &caps));
69    stmts.extend(insert_statements_into("quads_inf", &ids, &caps));
70    stmts.push(oxilite_core::reason::inference_attribute(
71        oxilite_core::reason::OWL_PRODUCER,
72    ));
73    backend.execute(&Request::atomic(stmts))?;
74    Ok(ids.len() as u64)
75}