inference-engine 0.1.0-alpha.9

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
use super::evidence::{Inspection, IriKind, TargetEvidence, is_reserved_iri};
use super::{SchemaFreeEntailment, SchemaFreeError, SchemaFreeRestriction};
use crate::{
    entailment::IriTermAccess,
    reasoning::{LiteralRole, Rdf12OccurrenceCursor, TermOccurrence, TermPosition, TripleSource},
};

pub(super) enum Admission<T> {
    Accepted {
        count: u64,
        standard_constraint: Option<TermOccurrence<T>>,
        evidence: TargetEvidence<T>,
    },
    Unsupported {
        restriction: SchemaFreeRestriction,
        occurrence: TermOccurrence<T>,
    },
}
impl SchemaFreeEntailment {
    pub(super) async fn admit<S, A>(
        &self,
        graph: &S,
        access: &A,
        role: LiteralRole,
    ) -> Result<Admission<S::Term>, SchemaFreeError<S::Term, S::Error>>
    where
        S: TripleSource,
        A: IriTermAccess<Term = S::Term, Error = S::Error>,
    {
        Self::cancelled(access)?;
        let roots = graph
            .scan([None, None, None])
            .await
            .map_err(|error| SchemaFreeError::Graph { role, error })?;
        Self::cancelled(access)?;
        let mut cursor = Rdf12OccurrenceCursor::new(roots, access, role, self.limits.traversal);
        let mut count = 0_u64;
        // bounded: retain only the first unresolved standard predicate appearance.
        let mut standard_constraint = None;
        let mut evidence = TargetEvidence::new();
        while let Some(occurrence) = cursor.next().await.map_err(SchemaFreeError::Traversal)? {
            count = count
                .checked_add(1)
                .ok_or(SchemaFreeError::CounterOverflow)?;
            let inspected = self
                .inspect_term(access, &occurrence, cursor.current_is_triple_term())
                .await?;
            Self::cancelled(access)?;
            evidence.observe(&occurrence, inspected.iri);
            if let Some(restriction) = inspected.restriction {
                if restriction == SchemaFreeRestriction::UnresolvedStandardConstraint {
                    if standard_constraint.is_none() {
                        standard_constraint = Some(occurrence);
                    }
                    continue;
                }
                return Ok(Admission::Unsupported {
                    restriction,
                    occurrence,
                });
            }
        }
        Self::cancelled(access)?;
        Ok(Admission::Accepted {
            count,
            standard_constraint,
            evidence,
        })
    }
    async fn inspect_term<A: IriTermAccess>(
        &self,
        access: &A,
        occurrence: &TermOccurrence<A::Term>,
        is_triple_term: bool,
    ) -> Result<Inspection, SchemaFreeError<A::Term, A::Error>> {
        if is_triple_term {
            return Ok(Inspection::non_iri(
                (occurrence.path.positions().last() != Some(&TermPosition::Object))
                    .then_some(SchemaFreeRestriction::GeneralizedPosition),
            ));
        }
        let iri = access
            .iri(&occurrence.term, self.limits.iri_bytes)
            .await
            .map_err(|error| SchemaFreeError::Metadata {
                occurrence: occurrence.clone(),
                error,
            })?;
        Self::cancelled(access)?;
        if let Some(iri) = iri {
            let iri = iri.as_ref();
            if iri.len() > self.limits.iri_bytes.get() as usize {
                return Err(SchemaFreeError::IriLimit {
                    occurrence: occurrence.clone(),
                });
            }
            let restriction = if !is_reserved_iri(iri) {
                None
            } else if occurrence.role == LiteralRole::Source {
                Some(SchemaFreeRestriction::ReservedSourceVocabulary)
            } else if occurrence.path.positions().eq([&TermPosition::Predicate]) {
                Some(SchemaFreeRestriction::UnresolvedStandardConstraint)
            } else {
                None
            };
            return Ok(Inspection {
                restriction,
                iri: Some(IriKind::inspect(iri)),
            });
        }
        let blank =
            access
                .is_blank(&occurrence.term)
                .await
                .map_err(|error| SchemaFreeError::Metadata {
                    occurrence: occurrence.clone(),
                    error,
                })?;
        Self::cancelled(access)?;
        Ok(Inspection::non_iri(if !blank {
            Some(SchemaFreeRestriction::Literal)
        } else if occurrence.is_predicate() {
            Some(SchemaFreeRestriction::GeneralizedPosition)
        } else {
            None
        }))
    }
}