mod admission;
mod types;
use super::{IriTermAccess, SimpleEntailment};
use crate::reasoning::{LiteralRole, TripleSource};
use admission::Admission;
pub use types::{
SchemaFreeGroundError, SchemaFreeGroundLimits, SchemaFreeGroundOutcome, SchemaFreeGroundReport,
SchemaFreeGroundRestriction,
};
pub struct SchemaFreeGroundEntailment {
limits: SchemaFreeGroundLimits,
}
impl SchemaFreeGroundEntailment {
pub const fn new(limits: SchemaFreeGroundLimits) -> Self {
Self { limits }
}
pub async fn check<S, E, A>(
&self,
source: &S,
target: &E,
access: &A,
) -> Result<SchemaFreeGroundOutcome<S::Term>, SchemaFreeGroundError<S::Term, S::Error>>
where
S: TripleSource,
E: TripleSource<Term = S::Term, Error = S::Error>,
A: IriTermAccess<Term = S::Term, Error = S::Error>,
{
let source_occurrences = match self.admit(source, access, LiteralRole::Source).await? {
Admission::Accepted(count) => count,
Admission::Unsupported {
restriction,
occurrence,
} => {
return Ok(SchemaFreeGroundOutcome::Unsupported {
restriction,
occurrence,
});
}
};
let target_occurrences = match self
.admit(target, access, LiteralRole::TargetContext)
.await?
{
Admission::Accepted(count) => count,
Admission::Unsupported {
restriction,
occurrence,
} => {
return Ok(SchemaFreeGroundOutcome::Unsupported {
restriction,
occurrence,
});
}
};
let matching = SimpleEntailment::new(self.limits.matching)
.check(source, target, access)
.await
.map_err(SchemaFreeGroundError::Matching)?;
Self::cancelled(access)?;
let report = SchemaFreeGroundReport {
source_occurrences,
target_occurrences,
target_triples: matching.target_triples,
matching_steps: matching.steps,
};
Ok(if matching.entailed {
SchemaFreeGroundOutcome::Entailed(report)
} else {
SchemaFreeGroundOutcome::NotEntailed(report)
})
}
fn cancelled<A: IriTermAccess>(
access: &A,
) -> Result<(), SchemaFreeGroundError<A::Term, A::Error>> {
if access.is_cancelled() {
Err(SchemaFreeGroundError::Cancelled)
} else {
Ok(())
}
}
}