use super::{
TargetConstructionError, TargetConstructionLimits, TargetConstructionReport,
grounding::Grounding,
};
use crate::{
entailment::{EntailmentTerms, PartialMatches},
reasoning::{Rdf12PropositionConstructor, Rdf12TripleTermStore, ReasoningError, TripleSource},
};
pub struct Rdf12TargetConstructor {
limits: TargetConstructionLimits,
}
impl Rdf12TargetConstructor {
pub const fn new(limits: TargetConstructionLimits) -> Self {
Self { limits }
}
pub async fn run<S, A, T>(
&self,
store: &mut S,
access: &A,
target: &T,
) -> Result<TargetConstructionReport, TargetConstructionError<S::Error>>
where
S: Rdf12TripleTermStore,
A: EntailmentTerms<Term = S::Term, Error = S::Error>,
T: TripleSource<Term = S::Term, Error = S::Error>,
{
let mut phase = Rdf12PropositionConstructor::new(self.limits.propositions)
.begin(store)
.await?;
let mut matches =
PartialMatches::new(phase.source(), target, access, self.limits.matching).await?;
let mut grounding = Grounding::new(self.limits);
let mut report = TargetConstructionReport::default();
if !grounding
.has_goals(store, access, matches.patterns())
.await?
{
grounding.finish(store, access).await?;
report.matching_steps = matches.steps();
report.grounding_steps = grounding.steps();
return Ok(report);
}
let representative = store
.resolve_iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
.await
.map_err(ReasoningError::Storage)?;
while matches.next(phase.source(), access).await? {
report.substitutions = report
.substitutions
.checked_add(1)
.ok_or(TargetConstructionError::CounterOverflow)?;
for pattern in matches.patterns() {
for term in pattern {
grounding
.run(
store,
access,
term,
matches.bindings(),
&representative,
&mut phase,
)
.await?;
}
}
}
grounding.finish(store, access).await?;
report.matching_steps = matches.steps();
report.grounding_steps = grounding.steps();
report.propositions = phase.report;
Ok(report)
}
}