inference-engine 0.1.0-alpha.7

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
use super::{SchemaFreeGroundEntailment, SchemaFreeGroundError, SchemaFreeGroundRestriction};
use crate::{
    entailment::IriTermAccess,
    reasoning::{LiteralRole, Rdf12OccurrenceCursor, TermOccurrence, TermPosition, TripleSource},
};

pub(super) enum Admission<T> {
    Accepted(u64),
    Unsupported {
        restriction: SchemaFreeGroundRestriction,
        occurrence: TermOccurrence<T>,
    },
}
impl SchemaFreeGroundEntailment {
    pub(super) async fn admit<S, A>(
        &self,
        graph: &S,
        access: &A,
        role: LiteralRole,
    ) -> Result<Admission<S::Term>, SchemaFreeGroundError<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| SchemaFreeGroundError::Graph { role, error })?;
        Self::cancelled(access)?;
        let mut cursor = Rdf12OccurrenceCursor::new(roots, access, role, self.limits.traversal);
        let mut count = 0_u64;
        while let Some(occurrence) = cursor
            .next()
            .await
            .map_err(SchemaFreeGroundError::Traversal)?
        {
            count = count
                .checked_add(1)
                .ok_or(SchemaFreeGroundError::CounterOverflow)?;
            let restriction = self
                .restriction(access, &occurrence, cursor.current_is_triple_term())
                .await?;
            Self::cancelled(access)?;
            if let Some(restriction) = restriction {
                return Ok(Admission::Unsupported {
                    restriction,
                    occurrence,
                });
            }
        }
        Self::cancelled(access)?;
        Ok(Admission::Accepted(count))
    }
    async fn restriction<A: IriTermAccess>(
        &self,
        access: &A,
        occurrence: &TermOccurrence<A::Term>,
        is_triple_term: bool,
    ) -> Result<Option<SchemaFreeGroundRestriction>, SchemaFreeGroundError<A::Term, A::Error>> {
        if is_triple_term {
            return Ok(
                (occurrence.path.positions().last() != Some(&TermPosition::Object))
                    .then_some(SchemaFreeGroundRestriction::GeneralizedPosition),
            );
        }
        let iri = access
            .iri(&occurrence.term, self.limits.iri_bytes)
            .await
            .map_err(|error| SchemaFreeGroundError::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(SchemaFreeGroundError::IriLimit {
                    occurrence: occurrence.clone(),
                });
            }
            // bounded: exactly the three excluded standard namespace prefixes.
            let reserved = [
                "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                "http://www.w3.org/2000/01/rdf-schema#",
                "http://www.w3.org/2001/XMLSchema#",
            ]
            .iter()
            .any(|prefix| iri.starts_with(prefix));
            return Ok(reserved.then_some(SchemaFreeGroundRestriction::ReservedVocabulary));
        }
        let blank = access.is_blank(&occurrence.term).await.map_err(|error| {
            SchemaFreeGroundError::Metadata {
                occurrence: occurrence.clone(),
                error,
            }
        })?;
        Self::cancelled(access)?;
        Ok(if !blank {
            Some(SchemaFreeGroundRestriction::Literal)
        } else if occurrence.is_predicate() {
            Some(SchemaFreeGroundRestriction::GeneralizedPosition)
        } else if occurrence.role == LiteralRole::TargetContext {
            Some(SchemaFreeGroundRestriction::TargetBlank)
        } else {
            None
        })
    }
}