inference-engine 0.1.0-alpha.8

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
//! Checked RDFS decisions over a literal-free, ordinary-vocabulary source.
mod admission;
mod types;
use super::{EntailmentError, IriTermAccess, SimpleEntailment};
use crate::reasoning::{LiteralRole, TripleSource};
use admission::{Admission, is_reserved_iri};
pub use types::{
    SchemaFreeError, SchemaFreeLimits, SchemaFreeOutcome, SchemaFreeReport, SchemaFreeRestriction,
};

/// Decide ordinary-predicate targets against a checked schema-free RDFS 1.2 source.
///
/// Both graphs must be literal-free normative RDF. Source IRIs must be outside
/// the RDF/RDFS/XSD namespaces. Scoped source and target blanks are supported.
/// A free injective constructor model with separate names/source blank atoms and
/// exact asserted ordinary-property extensions supplies every negative witness.
/// Standard vocabulary and datatype conditions are completed independently.
/// Standard-predicate target roots are excluded from matching. A failed projection
/// disproves the whole target; success with excluded constraints is inconclusive.
/// This does not enable the full `rdfs` execution selector.
pub struct SchemaFreeEntailment {
    limits: SchemaFreeLimits,
}
impl SchemaFreeEntailment {
    /// Set every admission/matching budget explicitly.
    pub const fn new(limits: SchemaFreeLimits) -> Self {
        Self { limits }
    }

    /// Admit the entire selected source and target before shared consistent matching.
    ///
    /// Reuse one immutable graph/dictionary domain through all scans. No facts,
    /// dictionary entries, closures or countermodel graphs are constructed here.
    /// Keep input snapshot/graph identity with the returned scoped result.
    ///
    /// # Errors
    /// Storage, cancellation and exhausted work/metadata budgets abort the check;
    /// even late failures invalidate any incomplete positive/negative result.
    pub async fn check<S, E, A>(
        &self,
        source: &S,
        target: &E,
        access: &A,
    ) -> Result<SchemaFreeOutcome<S::Term>, SchemaFreeError<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(SchemaFreeOutcome::Unsupported {
                    restriction,
                    occurrence,
                });
            }
        };
        let (target_occurrences, standard_constraint) = match self
            .admit(target, access, LiteralRole::TargetContext)
            .await?
        {
            Admission::Accepted {
                count,
                standard_constraint,
            } => (count, standard_constraint),
            Admission::Unsupported {
                restriction,
                occurrence,
            } => {
                return Ok(SchemaFreeOutcome::Unsupported {
                    restriction,
                    occurrence,
                });
            }
        };
        let iri_bytes = self.limits.iri_bytes;
        let select = |tuple: [S::Term; 3]| async move {
            let Some(iri) = access
                .iri(&tuple[1], iri_bytes)
                .await
                .map_err(EntailmentError::Storage)?
            else {
                return Err(EntailmentError::TargetSelectionMetadata);
            };
            if iri.as_ref().len() > iri_bytes.get() as usize {
                return Err(EntailmentError::TargetSelectionMetadata);
            }
            Ok(!is_reserved_iri(iri.as_ref()))
        };
        let matching = SimpleEntailment::new(self.limits.matching)
            .check_selected(source, target, access, Some(select))
            .await
            .map_err(SchemaFreeError::Matching)?;
        Self::cancelled(access)?;
        let report = SchemaFreeReport {
            source_occurrences,
            target_occurrences,
            projected_target_triples: matching.target_triples,
            matching_steps: matching.steps,
        };
        Ok(if !matching.entailed {
            SchemaFreeOutcome::NotEntailed(report)
        } else if let Some(occurrence) = standard_constraint {
            SchemaFreeOutcome::Unsupported {
                restriction: SchemaFreeRestriction::UnresolvedStandardConstraint,
                occurrence,
            }
        } else {
            SchemaFreeOutcome::Entailed(report)
        })
    }
    fn cancelled<A: IriTermAccess>(access: &A) -> Result<(), SchemaFreeError<A::Term, A::Error>> {
        if access.is_cancelled() {
            Err(SchemaFreeError::Cancelled)
        } else {
            Ok(())
        }
    }
}