use super::{
LiteralOccurrenceCursor, LiteralRole, LiteralTypingError, LiteralTypingLimits,
LiteralTypingReport,
};
use crate::reasoning::{
Evidence, Inference, Limit, Rdf12LiteralValidator, ReasoningError, ReasoningStore,
plan::LiteralClause,
};
use rdf_reasoning::Rdf12LiteralRuleId;
pub struct Rdf12LiteralTyper {
limits: LiteralTypingLimits,
role: LiteralRole,
}
impl Rdf12LiteralTyper {
pub const fn new(limits: LiteralTypingLimits, role: LiteralRole) -> Self {
Self { limits, role }
}
pub async fn run<S, C>(
&self,
store: &mut S,
cursor: &mut C,
) -> Result<LiteralTypingReport, LiteralTypingError<S::Term, S::Error>>
where
S: ReasoningStore,
C: LiteralOccurrenceCursor<Term = S::Term, Error = S::Error>,
{
Self::check_cancelled(store)?;
let mut clauses = Vec::with_capacity(3);
for &id in Rdf12LiteralRuleId::ALL {
clauses.push(
LiteralClause::compile(id, store)
.await
.map_err(LiteralTypingError::Reasoning)?,
);
}
let mut report = LiteralTypingReport::default();
loop {
Self::check_cancelled(store)?;
let next = cursor.next().await.map_err(LiteralTypingError::Cursor)?;
Self::check_cancelled(store)?;
let Some((occurrence, mut reader)) = next else {
return Ok(report);
};
if occurrence.role != self.role {
return Err(LiteralTypingError::RoleMismatch {
expected: self.role,
occurrence,
});
}
if report.occurrences == self.limits.occurrences.get() {
return Err(LiteralTypingError::OccurrenceLimit);
}
report.occurrences += 1;
crate::cooperative::checkpoint(report.occurrences).await;
Self::check_cancelled(store)?;
let inspection = Rdf12LiteralValidator::new(self.limits.bytes - report.bytes)
.validate(&mut reader)
.await
.map_err(|error| LiteralTypingError::Literal {
occurrence: occurrence.clone(),
error,
})?;
Self::check_cancelled(store)?;
report.bytes += inspection.bytes;
let Some(datatype) = inspection.datatype else {
report.opaque += 1;
continue;
};
let id = Rdf12LiteralRuleId::GrdfD1(datatype);
let clause = clauses
.iter()
.find(|clause| clause.id == id)
.ok_or_else(|| {
LiteralTypingError::Reasoning(ReasoningError::UnsupportedLiteralRule(id))
})?;
let metadata = [occurrence.literal.clone(), clause.datatype.clone()];
let bindings = clause
.body
.bind(&metadata, std::array::from_fn(|_| None))
.ok_or_else(|| {
LiteralTypingError::Reasoning(ReasoningError::UnsupportedLiteralRule(id))
})?;
let triple = clause.head.ground(&bindings).ok_or_else(|| {
LiteralTypingError::Reasoning(ReasoningError::UnsupportedLiteralRule(id))
})?;
if store
.contains(&triple)
.await
.map_err(|error| LiteralTypingError::Reasoning(ReasoningError::Storage(error)))?
{
continue;
}
Self::check_cancelled(store)?;
if report.inferred == self.limits.inferred.get() {
return Err(LiteralTypingError::Reasoning(
ReasoningError::LimitExceeded(Limit::Inferred),
));
}
let inference = Inference {
triple,
evidence: Evidence::Literal {
rule: id,
occurrence,
lexical_bytes: inspection.bytes,
},
};
if store
.insert_inferred(inference)
.await
.map_err(|error| LiteralTypingError::Reasoning(ReasoningError::Storage(error)))?
{
report.inferred += 1;
}
}
}
fn check_cancelled<S: ReasoningStore>(
store: &S,
) -> Result<(), LiteralTypingError<S::Term, S::Error>> {
if store.is_cancelled() {
Err(LiteralTypingError::Reasoning(ReasoningError::Cancelled))
} else {
Ok(())
}
}
}