inference-engine 0.1.0-alpha.13

Native Rust RDF inference engine with a shared command-line interface
Documentation
//! Checked RDFS decisions over an explicitly limited literal-free source fragment.
mod admission;
mod evidence;
mod property;
mod triple_term;
mod types;
use super::{EntailmentError, IriTermAccess, SimpleEntailment};
use crate::reasoning::{LiteralRole, TripleSource};
use admission::Admission;
use evidence::is_reserved_iri;
pub use types::{
    Rdfs12FragmentError, Rdfs12FragmentLimits, Rdfs12FragmentOutcome, Rdfs12FragmentRefutation,
    Rdfs12FragmentReport, Rdfs12FragmentRestriction,
};

/// Decide targets against a checked, literal-free RDFS 1.2 source fragment.
///
/// Source roots may use ordinary predicates, declare an ordinary named property,
/// or assert `Resource subClassOf Class` / `Proposition subClassOf Property`.
/// Standard names in data components and quoted contents do not assert schema.
/// Both graphs must be normative RDF; scoped blanks/nested object terms are supported.
/// Free injective-constructor standard completions provide explicit countermodels:
/// failed ordinary projection, fixed-name datatype/property membership, or a target
/// triple term whose named predicate is unforced by the source. All candidate
/// scans and consistent matching share one cumulative work budget. A matching
/// projection with unresolved standard constraints remains inconclusive.
/// This does not enable the full `rdfs` execution selector.
pub struct Rdfs12FragmentEntailment {
    limits: Rdfs12FragmentLimits,
}
impl Rdfs12FragmentEntailment {
    /// Set every admission/cumulative-search budget explicitly.
    pub const fn new(limits: Rdfs12FragmentLimits) -> 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<Rdfs12FragmentOutcome<S::Term>, Rdfs12FragmentError<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(Rdfs12FragmentOutcome::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(Rdfs12FragmentOutcome::Unsupported {
                    restriction,
                    occurrence,
                });
            }
        };
        Self::cancelled(access)?;
        let mut report = Rdfs12FragmentReport {
            source_occurrences,
            target_occurrences,
            projected_target_triples: 0,
            search_steps: 0,
        };
        if let Some(refutation) = evidence.refutation {
            return Ok(Rdfs12FragmentOutcome::NotEntailed { report, refutation });
        }
        if evidence.has_property_candidate {
            let (refutation, steps) = self
                .property_refutation(source, target, access, report.search_steps)
                .await?;
            report.search_steps = steps;
            if let Some(refutation) = refutation {
                return Ok(Rdfs12FragmentOutcome::NotEntailed { report, refutation });
            }
        }
        if evidence.has_triple_term_candidate {
            let (refutation, steps) = self
                .triple_term_refutation(source, target, access, report.search_steps)
                .await?;
            report.search_steps = steps;
            if let Some(refutation) = refutation {
                return Ok(Rdfs12FragmentOutcome::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(Rdfs12FragmentError::Search)?;
        Self::cancelled(access)?;
        report.projected_target_triples = matching.target_triples;
        report.search_steps = matching.steps;
        Ok(if !matching.entailed {
            Rdfs12FragmentOutcome::NotEntailed {
                report,
                refutation: Rdfs12FragmentRefutation::OrdinaryProjection,
            }
        } else if let Some(occurrence) = standard_constraint {
            Rdfs12FragmentOutcome::Unsupported {
                restriction: Rdfs12FragmentRestriction::UnresolvedStandardConstraint,
                occurrence,
            }
        } else {
            Rdfs12FragmentOutcome::Entailed(report)
        })
    }
    fn cancelled<A: IriTermAccess>(
        access: &A,
    ) -> Result<(), Rdfs12FragmentError<A::Term, A::Error>> {
        if access.is_cancelled() {
            Err(Rdfs12FragmentError::Cancelled)
        } else {
            Ok(())
        }
    }
}