Skip to main content

inference_engine/entailment/ground/
types.rs

1use crate::{
2    entailment::{EntailmentError, EntailmentLimits},
3    reasoning::{LiteralRole, OccurrenceError, OccurrenceLimits, TermOccurrence},
4};
5use serde::{Deserialize, Serialize};
6use std::{error::Error, fmt, num::NonZeroU32};
7
8/// Explicit admission, matching and per-IRI metadata budgets; no defaults.
9#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
10#[serde(deny_unknown_fields)]
11pub struct SchemaFreeGroundLimits {
12    pub(super) traversal: OccurrenceLimits,
13    pub(super) matching: EntailmentLimits,
14    pub(super) iri_bytes: NonZeroU32,
15}
16impl SchemaFreeGroundLimits {
17    /// Bound each source/target admission traversal and the complete matcher.
18    /// IRI reads and blank checks occur at most once per admitted appearance.
19    pub const fn new(
20        traversal: OccurrenceLimits,
21        matching: EntailmentLimits,
22        iri_bytes: NonZeroU32,
23    ) -> Self {
24        Self {
25            traversal,
26            matching,
27            iri_bytes,
28        }
29    }
30}
31
32/// Completed work for a decision within the checked schema-free ground fragment.
33#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
34#[serde(deny_unknown_fields)]
35pub struct SchemaFreeGroundReport {
36    /// Fully examined source appearances, including recursive/repeated paths.
37    pub source_occurrences: u64,
38    /// Fully examined target appearances, including recursive/repeated paths.
39    pub target_occurrences: u64,
40    /// Ground target triples examined by the shared matcher.
41    pub target_triples: u32,
42    /// Shared matcher's bounded scan/probe/unification work.
43    pub matching_steps: u64,
44}
45
46/// A condition outside this proven fragment, not a non-entailment witness.
47#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
48pub enum SchemaFreeGroundRestriction {
49    /// An IRI in the RDF, RDFS or XSD namespace appears in either graph.
50    ReservedVocabulary,
51    /// A literal appears in either graph; no lexical payload was inspected.
52    Literal,
53    /// A target blank occurs, including within a triple term.
54    TargetBlank,
55    /// A generalized position occurs: blank predicate or non-object triple term.
56    GeneralizedPosition,
57}
58
59/// A complete decision only after both graphs satisfy all admission conditions.
60#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
61#[serde(deny_unknown_fields)]
62pub enum SchemaFreeGroundOutcome<T> {
63    /// Exact ground matching proves entailment under default-map RDFS 1.2.
64    Entailed(SchemaFreeGroundReport),
65    /// The free-constructor standard completion supplies a countermodel.
66    /// This is not failure to find a proof within a construction/pass budget.
67    NotEntailed(SchemaFreeGroundReport),
68    /// This fragment has no decision for the rejected source/target appearance.
69    Unsupported {
70        /// Specific failed admission condition.
71        restriction: SchemaFreeGroundRestriction,
72        /// Exact original root, term, phase and recursive path.
73        occurrence: TermOccurrence<T>,
74    },
75}
76
77/// Incomplete admission/matching; never convert these failures into `NotEntailed`.
78#[derive(Debug)]
79pub enum SchemaFreeGroundError<T, E> {
80    /// Opening the selected immutable graph failed.
81    Graph {
82        /// Owning semantic phase.
83        role: LiteralRole,
84        /// Backend failure.
85        error: E,
86    },
87    /// Bounded structural traversal failed, including late cursor errors.
88    Traversal(OccurrenceError<E>),
89    /// Authoritative IRI or blank classification failed for this appearance.
90    Metadata {
91        /// Exact failed appearance.
92        occurrence: TermOccurrence<T>,
93        /// Backend failure.
94        error: E,
95    },
96    /// An adapter returned IRI metadata larger than its explicit cap.
97    IriLimit {
98        /// Exact over-budget appearance.
99        occurrence: TermOccurrence<T>,
100    },
101    /// The shared matcher did not complete.
102    Matching(EntailmentError<E>),
103    /// The attempt was cancelled/superseded.
104    Cancelled,
105    /// Work counters cannot represent a completed traversal.
106    CounterOverflow,
107}
108impl<T, E: fmt::Display> fmt::Display for SchemaFreeGroundError<T, E> {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        match self {
111            Self::Graph { error, .. } => write!(f, "ground-fragment graph access failed: {error}"),
112            Self::Traversal(error) => error.fmt(f),
113            Self::Metadata { error, .. } => write!(f, "ground-fragment metadata failed: {error}"),
114            Self::IriLimit { .. } => f.write_str("ground-fragment IRI metadata limit exceeded"),
115            Self::Matching(error) => error.fmt(f),
116            Self::Cancelled => f.write_str("ground-fragment decision cancelled"),
117            Self::CounterOverflow => f.write_str("ground-fragment work counter overflow"),
118        }
119    }
120}
121impl<T: fmt::Debug, E: Error + 'static> Error for SchemaFreeGroundError<T, E> {
122    fn source(&self) -> Option<&(dyn Error + 'static)> {
123        match self {
124            Self::Graph { error, .. } | Self::Metadata { error, .. } => Some(error),
125            Self::Traversal(error) => Some(error),
126            Self::Matching(error) => Some(error),
127            _ => None,
128        }
129    }
130}