inference-engine 0.1.0-alpha.11

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
use super::{SchemaFreeEntailment, SchemaFreeError, SchemaFreeRefutation, evidence::IriKind};
use crate::{
    entailment::{EntailmentError, IriTermAccess, matcher::State},
    reasoning::{
        LiteralRole, OccurrencePath, Rdf12OccurrenceCursor, TermOccurrence, TermPosition,
        TripleCursor, TripleSource,
    },
};
impl SchemaFreeEntailment {
    pub(super) async fn property_refutation<S, E, A>(
        &self,
        source: &S,
        target: &E,
        access: &A,
    ) -> Result<(Option<SchemaFreeRefutation<S::Term>>, u64), 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 mut work = State {
            limits: self.limits.search,
            steps: 0,
        };
        work.step(access).await.map_err(SchemaFreeError::Search)?;
        let mut cursor =
            target
                .scan([None, None, None])
                .await
                .map_err(|error| SchemaFreeError::Graph {
                    role: LiteralRole::TargetContext,
                    error,
                })?;
        // bounded: one current root/candidate, source cursor and first certificate.
        let mut refutation = None;
        loop {
            work.step(access).await.map_err(SchemaFreeError::Search)?;
            let row = cursor
                .next()
                .await
                .map_err(|error| SchemaFreeError::Search(EntailmentError::Storage(error)))?;
            Self::cancelled(access)?;
            let Some(root) = row else { break };
            // Still read through target EOF after evidence; do not issue more source probes.
            if refutation.is_some() {
                continue;
            }
            let predicate = self
                .root_iri(access, &root, TermPosition::Predicate, &mut work)
                .await?
                .ok_or(SchemaFreeError::Search(
                    EntailmentError::TargetSelectionMetadata,
                ))?;
            if predicate != IriKind::Type {
                continue;
            }
            if self
                .root_iri(access, &root, TermPosition::Subject, &mut work)
                .await?
                != Some(IriKind::Ordinary)
            {
                continue;
            }
            if self
                .root_iri(access, &root, TermPosition::Object, &mut work)
                .await?
                != Some(IriKind::Property)
            {
                continue;
            }
            if !self
                .source_predicate(source, access, &root[0], &mut work)
                .await?
            {
                refutation = Some(SchemaFreeRefutation::NamedPropertyMembership {
                    occurrence: root_occurrence(&root, TermPosition::Subject),
                });
            }
        }
        Self::cancelled(access)?;
        Ok((refutation, work.steps))
    }
    async fn source_predicate<S, A>(
        &self,
        source: &S,
        access: &A,
        key: &S::Term,
        work: &mut State,
    ) -> Result<bool, SchemaFreeError<S::Term, S::Error>>
    where
        S: TripleSource,
        A: IriTermAccess<Term = S::Term, Error = S::Error>,
    {
        work.step(access).await.map_err(SchemaFreeError::Search)?;
        let roots =
            source
                .scan([None, None, None])
                .await
                .map_err(|error| SchemaFreeError::Graph {
                    role: LiteralRole::Source,
                    error,
                })?;
        let mut cursor =
            Rdf12OccurrenceCursor::new(roots, access, LiteralRole::Source, self.limits.traversal);
        let mut required = false;
        loop {
            // One next() performs at most one root read plus one structural lookup.
            // Charge both before entering it; traversal also enforces its own caps.
            work.step(access).await.map_err(SchemaFreeError::Search)?;
            work.step(access).await.map_err(SchemaFreeError::Search)?;
            let Some(occurrence) = cursor.next().await.map_err(SchemaFreeError::Traversal)? else {
                break;
            };
            if occurrence.is_predicate() && occurrence.term == *key {
                required = true;
            }
        }
        Self::cancelled(access)?;
        Ok(required)
    }
    async fn root_iri<A: IriTermAccess>(
        &self,
        access: &A,
        root: &[A::Term; 3],
        position: TermPosition,
        work: &mut State,
    ) -> Result<Option<IriKind>, SchemaFreeError<A::Term, A::Error>> {
        let index = match position {
            TermPosition::Subject => 0,
            TermPosition::Predicate => 1,
            TermPosition::Object => 2,
        };
        work.step(access).await.map_err(SchemaFreeError::Search)?;
        let iri = access
            .iri(&root[index], self.limits.iri_bytes)
            .await
            .map_err(|error| SchemaFreeError::Metadata {
                occurrence: root_occurrence(root, position),
                error,
            })?;
        Self::cancelled(access)?;
        let Some(iri) = iri else { return Ok(None) };
        if iri.as_ref().len() > self.limits.iri_bytes.get() as usize {
            return Err(SchemaFreeError::IriLimit {
                occurrence: root_occurrence(root, position),
            });
        }
        Ok(Some(IriKind::inspect(iri.as_ref())))
    }
}
fn root_occurrence<T: Clone>(root: &[T; 3], position: TermPosition) -> TermOccurrence<T> {
    let index = match position {
        TermPosition::Subject => 0,
        TermPosition::Predicate => 1,
        TermPosition::Object => 2,
    };
    TermOccurrence {
        term: root[index].clone(),
        root: root.clone(),
        parent: root.clone(),
        role: LiteralRole::TargetContext,
        path: OccurrencePath::root(position),
    }
}