Skip to main content

sophia_reasoner/
lib.rs

1//! A provide a forward-chaining reasoning engine for Simple, RDF and RDFS entailment.
2//!
3//! See [`ReasonableGraph`].
4#![deny(missing_docs)]
5
6use std::{collections::BTreeSet, marker::PhantomData, ops::Range};
7
8use sophia_inmem::index::BasicTermIndex;
9
10use crate::d_entailment::IllTypedLiteral;
11
12pub mod d_entailment;
13pub mod dataset;
14pub mod ruleset;
15
16mod _dedup;
17mod _graph_impl;
18mod _range_n;
19
20/// An implementation of [`Graph`](sophia_api::graph::Graph)
21/// supporting an entailment regime captured by
22/// * a set `D` of recognized datatypes, and
23/// * a ruleset `R` representing the level of semantics (Simple, RDF, RDFS).
24///
25/// The entailment regime is reflected in
26/// * method [`Graph::triples_matching`](sophia_api::graph::Graph::triples_matching),
27///   which will match any asserted *or inferred* triple,
28/// * [`entails`](Self::entails) and [`entails_triples`](Self::entails_triples).
29pub struct ReasonableGraph<D, R> {
30    /// term index
31    terms: BasicTermIndex<usize>,
32    /// recognized datatypes, as (min-index, max-index)
33    rdt: Range<usize>,
34    /// asserted triples, sorted by subject-predicate-object
35    spo: BTreeSet<[usize; 3]>,
36    /// asserted triples, sorted by predicate-object-subject
37    pos: BTreeSet<[usize; 3]>,
38    /// asserted triples, sorted by object-subject-predicate
39    osp: BTreeSet<[usize; 3]>,
40    _phantom: PhantomData<(D, R)>,
41    #[cfg(debug_assertions)]
42    finalized: bool,
43}
44
45/// The error that can occur while collecting a [`ReasonableGraph`]
46#[derive(Clone, Debug, thiserror::Error)]
47pub enum Inconsistency {
48    /// An ill-typed literal was encountered
49    #[error("Ill-typed literal")]
50    IllTypedLiteral(
51        #[from]
52        #[source]
53        IllTypedLiteral,
54    ),
55    /// An inconsistency was derived
56    #[error("Other inconsistency: {0}")]
57    Other(String),
58}
59
60#[cfg(test)]
61mod test;