inference-engine 0.1.0-alpha.11

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
//! Checked RDFS decisions over a literal-free, ordinary-vocabulary source.
mod admission;
mod evidence;
mod property;
mod types;
use super::{EntailmentError, IriTermAccess, SimpleEntailment};
use crate::reasoning::{LiteralRole, TripleSource};
use admission::Admission;
use evidence::is_reserved_iri;
pub use types::{
    SchemaFreeError, SchemaFreeLimits, SchemaFreeOutcome, SchemaFreeRefutation, 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.
/// A fixed ordinary IRI's membership in a default datatype class also has a
/// fresh-atom countermodel, recorded only after complete source/target admission.
/// A selective-property model additionally refutes a fixed ordinary name's
/// property membership when no selected root/nested source predicate uses it.
/// This does not enable the full `rdfs` execution selector.
pub struct SchemaFreeEntailment {
    limits: SchemaFreeLimits,
}
impl SchemaFreeEntailment {
    /// Set every admission/cumulative-search 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, evidence) = match self
            .admit(target, access, LiteralRole::TargetContext)
            .await?
        {
            Admission::Accepted {
                count,
                standard_constraint,
                evidence,
            } => (count, standard_constraint, evidence),
            Admission::Unsupported {
                restriction,
                occurrence,
            } => {
                return Ok(SchemaFreeOutcome::Unsupported {
                    restriction,
                    occurrence,
                });
            }
        };
        Self::cancelled(access)?;
        let mut report = SchemaFreeReport {
            source_occurrences,
            target_occurrences,
            projected_target_triples: 0,
            search_steps: 0,
        };
        if let Some(refutation) = evidence.refutation {
            return Ok(SchemaFreeOutcome::NotEntailed { report, refutation });
        }
        if evidence.has_property_candidate {
            let (refutation, steps) = self.property_refutation(source, target, access).await?;
            report.search_steps = steps;
            if let Some(refutation) = refutation {
                return Ok(SchemaFreeOutcome::NotEntailed { report, refutation });
            }
        }
        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.search)
            .check_selected(source, target, access, Some(select), report.search_steps)
            .await
            .map_err(SchemaFreeError::Search)?;
        Self::cancelled(access)?;
        report.projected_target_triples = matching.target_triples;
        report.search_steps = matching.steps;
        Ok(if !matching.entailed {
            SchemaFreeOutcome::NotEntailed {
                report,
                refutation: SchemaFreeRefutation::OrdinaryProjection,
            }
        } 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(())
        }
    }
}