use super::{
Rdfs12FragmentEntailment, Rdfs12FragmentError, Rdfs12FragmentRefutation, evidence::IriKind,
};
use crate::{
entailment::{EntailmentError, IriTermAccess, matcher::State},
reasoning::{LiteralRole, Rdf12OccurrenceCursor, TripleSource},
};
impl Rdfs12FragmentEntailment {
pub(super) async fn triple_term_refutation<S, E, A>(
&self,
source: &S,
target: &E,
access: &A,
initial_steps: u64,
) -> Result<
(Option<Rdfs12FragmentRefutation<S::Term>>, u64),
Rdfs12FragmentError<S::Term, S::Error>,
>
where
S: TripleSource,
E: TripleSource<Term = S::Term, Error = S::Error>,
A: IriTermAccess<Term = S::Term, Error = S::Error>,
{
let mut work = State {
limits: self.limits.search,
steps: initial_steps,
};
work.step(access)
.await
.map_err(Rdfs12FragmentError::Search)?;
let roots =
target
.scan([None, None, None])
.await
.map_err(|error| Rdfs12FragmentError::Graph {
role: LiteralRole::TargetContext,
error,
})?;
let mut cursor = Rdf12OccurrenceCursor::new(
roots,
access,
LiteralRole::TargetContext,
self.limits.traversal,
);
let mut refutation = None;
loop {
work.step(access)
.await
.map_err(Rdfs12FragmentError::Search)?;
work.step(access)
.await
.map_err(Rdfs12FragmentError::Search)?;
let Some(occurrence) = cursor
.next()
.await
.map_err(Rdfs12FragmentError::Traversal)?
else {
break;
};
if refutation.is_some()
|| !occurrence.is_predicate()
|| occurrence.path.positions().nth(1).is_none()
{
continue;
}
let kind = self
.occurrence_iri(access, &occurrence, &mut work)
.await?
.ok_or(Rdfs12FragmentError::Search(
EntailmentError::TargetSelectionMetadata,
))?;
if kind != IriKind::Ordinary {
continue;
}
if !self
.source_property_required(source, access, &occurrence.term, &mut work)
.await?
{
refutation = Some(Rdfs12FragmentRefutation::TripleTermPredicate { occurrence });
}
}
Self::cancelled(access)?;
Ok((refutation, work.steps))
}
}