use super::{
ExecutionProfile, Inference, Limit, ReasoningError, ReasoningStore, RunLimits, RunReport,
TripleCursor, TripleSource, plan::Clause,
};
pub struct Reasoner {
profile: ExecutionProfile,
limits: RunLimits,
}
impl Reasoner {
pub const fn new(profile: ExecutionProfile, limits: RunLimits) -> Self {
Self { profile, limits }
}
pub const fn profile(&self) -> ExecutionProfile {
self.profile
}
pub const fn implementation_version(&self) -> &'static str {
env!("CARGO_PKG_VERSION")
}
pub const fn rule_catalogue(&self) -> &'static str {
"rdf-reasoning/2.6.0"
}
pub async fn run<S: ReasoningStore>(
&self,
store: &mut S,
) -> Result<RunReport, ReasoningError<S::Error>> {
Self::check_cancelled(store)?;
let [first, second] = *self.profile.rules();
let clauses = [
Clause::compile(first, store).await?,
Clause::compile(second, store).await?,
];
let mut report = RunReport::default();
loop {
Self::check_cancelled(store)?;
if report.rounds == self.limits.rounds.get() {
return Err(ReasoningError::LimitExceeded(Limit::Rounds));
}
report.rounds += 1;
let snapshot = store.snapshot().await.map_err(ReasoningError::Storage)?;
let before = report.inferred;
for clause in &clauses {
self.evaluate(clause, &snapshot, store, &mut report).await?;
}
Self::check_cancelled(store)?;
if report.inferred == before {
return Ok(report);
}
}
}
async fn evaluate<S: ReasoningStore>(
&self,
clause: &Clause<S::Term>,
source: &S::Snapshot,
store: &mut S,
report: &mut RunReport,
) -> Result<(), ReasoningError<S::Error>> {
let empty = std::array::from_fn(|_| None);
let mut outer = source
.scan(clause.body[0].probe(&empty))
.await
.map_err(ReasoningError::Storage)?;
while let Some(first) = self.next(&mut outer, store, report).await? {
let Some(bindings) = clause.body[0].bind(&first, empty.clone()) else {
continue;
};
let mut inner = source
.scan(clause.body[1].probe(&bindings))
.await
.map_err(ReasoningError::Storage)?;
while let Some(second) = self.next(&mut inner, store, report).await? {
let Some(bindings) = clause.body[1].bind(&second, bindings.clone()) else {
continue;
};
let triple = clause.head.ground(&bindings, clause.id)?;
if store
.contains(&triple)
.await
.map_err(ReasoningError::Storage)?
{
continue;
}
Self::check_cancelled(store)?;
if report.inferred == self.limits.inferred.get() {
return Err(ReasoningError::LimitExceeded(Limit::Inferred));
}
let inference = Inference {
triple,
rule: clause.id,
premises: [first.clone(), second],
};
if store
.insert_inferred(inference)
.await
.map_err(ReasoningError::Storage)?
{
report.inferred += 1;
}
}
}
Ok(())
}
async fn next<S: ReasoningStore, C: TripleCursor<Term = S::Term, Error = S::Error>>(
&self,
cursor: &mut C,
store: &mut S,
report: &mut RunReport,
) -> Result<Option<[S::Term; 3]>, ReasoningError<S::Error>> {
Self::check_cancelled(store)?;
let tuple = cursor.next().await.map_err(ReasoningError::Storage)?;
if tuple.is_some() {
if report.matches == self.limits.matches.get() {
return Err(ReasoningError::LimitExceeded(Limit::Matches));
}
report.matches += 1;
}
Ok(tuple)
}
fn check_cancelled<S: ReasoningStore>(store: &S) -> Result<(), ReasoningError<S::Error>> {
if store.is_cancelled() {
Err(ReasoningError::Cancelled)
} else {
Ok(())
}
}
}