inference-engine 0.1.0-alpha.7

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
use crate::{
    entailment::{EntailmentError, EntailmentLimits},
    reasoning::{LiteralRole, OccurrenceError, OccurrenceLimits, TermOccurrence},
};
use serde::{Deserialize, Serialize};
use std::{error::Error, fmt, num::NonZeroU32};

/// Explicit admission, matching and per-IRI metadata budgets; no defaults.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SchemaFreeGroundLimits {
    pub(super) traversal: OccurrenceLimits,
    pub(super) matching: EntailmentLimits,
    pub(super) iri_bytes: NonZeroU32,
}
impl SchemaFreeGroundLimits {
    /// Bound each source/target admission traversal and the complete matcher.
    /// IRI reads and blank checks occur at most once per admitted appearance.
    pub const fn new(
        traversal: OccurrenceLimits,
        matching: EntailmentLimits,
        iri_bytes: NonZeroU32,
    ) -> Self {
        Self {
            traversal,
            matching,
            iri_bytes,
        }
    }
}

/// Completed work for a decision within the checked schema-free ground fragment.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SchemaFreeGroundReport {
    /// Fully examined source appearances, including recursive/repeated paths.
    pub source_occurrences: u64,
    /// Fully examined target appearances, including recursive/repeated paths.
    pub target_occurrences: u64,
    /// Ground target triples examined by the shared matcher.
    pub target_triples: u32,
    /// Shared matcher's bounded scan/probe/unification work.
    pub matching_steps: u64,
}

/// A condition outside this proven fragment, not a non-entailment witness.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum SchemaFreeGroundRestriction {
    /// An IRI in the RDF, RDFS or XSD namespace appears in either graph.
    ReservedVocabulary,
    /// A literal appears in either graph; no lexical payload was inspected.
    Literal,
    /// A target blank occurs, including within a triple term.
    TargetBlank,
    /// A generalized position occurs: blank predicate or non-object triple term.
    GeneralizedPosition,
}

/// A complete decision only after both graphs satisfy all admission conditions.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub enum SchemaFreeGroundOutcome<T> {
    /// Exact ground matching proves entailment under default-map RDFS 1.2.
    Entailed(SchemaFreeGroundReport),
    /// The free-constructor standard completion supplies a countermodel.
    /// This is not failure to find a proof within a construction/pass budget.
    NotEntailed(SchemaFreeGroundReport),
    /// This fragment has no decision for the rejected source/target appearance.
    Unsupported {
        /// Specific failed admission condition.
        restriction: SchemaFreeGroundRestriction,
        /// Exact original root, term, phase and recursive path.
        occurrence: TermOccurrence<T>,
    },
}

/// Incomplete admission/matching; never convert these failures into `NotEntailed`.
#[derive(Debug)]
pub enum SchemaFreeGroundError<T, E> {
    /// Opening the selected immutable graph failed.
    Graph {
        /// Owning semantic phase.
        role: LiteralRole,
        /// Backend failure.
        error: E,
    },
    /// Bounded structural traversal failed, including late cursor errors.
    Traversal(OccurrenceError<E>),
    /// Authoritative IRI or blank classification failed for this appearance.
    Metadata {
        /// Exact failed appearance.
        occurrence: TermOccurrence<T>,
        /// Backend failure.
        error: E,
    },
    /// An adapter returned IRI metadata larger than its explicit cap.
    IriLimit {
        /// Exact over-budget appearance.
        occurrence: TermOccurrence<T>,
    },
    /// The shared matcher did not complete.
    Matching(EntailmentError<E>),
    /// The attempt was cancelled/superseded.
    Cancelled,
    /// Work counters cannot represent a completed traversal.
    CounterOverflow,
}
impl<T, E: fmt::Display> fmt::Display for SchemaFreeGroundError<T, E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Graph { error, .. } => write!(f, "ground-fragment graph access failed: {error}"),
            Self::Traversal(error) => error.fmt(f),
            Self::Metadata { error, .. } => write!(f, "ground-fragment metadata failed: {error}"),
            Self::IriLimit { .. } => f.write_str("ground-fragment IRI metadata limit exceeded"),
            Self::Matching(error) => error.fmt(f),
            Self::Cancelled => f.write_str("ground-fragment decision cancelled"),
            Self::CounterOverflow => f.write_str("ground-fragment work counter overflow"),
        }
    }
}
impl<T: fmt::Debug, E: Error + 'static> Error for SchemaFreeGroundError<T, E> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Graph { error, .. } | Self::Metadata { error, .. } => Some(error),
            Self::Traversal(error) => Some(error),
            Self::Matching(error) => Some(error),
            _ => None,
        }
    }
}