Skip to main content

inference_engine/entailment/fragment/
types.rs

1use crate::{
2    entailment::{EntailmentError, EntailmentLimits},
3    reasoning::{LiteralRole, OccurrenceError, OccurrenceLimits, TermOccurrence},
4};
5use rdf_reasoning::Rdf12Datatype;
6use serde::{Deserialize, Serialize};
7use std::{error::Error, fmt, num::NonZeroU32};
8
9/// Explicit admission, cumulative search and per-IRI metadata budgets; no defaults.
10#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct Rdfs12FragmentLimits {
13    pub(super) traversal: OccurrenceLimits,
14    pub(super) search: EntailmentLimits,
15    pub(super) iri_bytes: NonZeroU32,
16}
17impl Rdfs12FragmentLimits {
18    /// Bound admission/probe traversals and total property/denotation-probe plus matching work.
19    /// IRI reads and blank checks occur at most once per admitted appearance.
20    pub const fn new(
21        traversal: OccurrenceLimits,
22        search: EntailmentLimits,
23        iri_bytes: NonZeroU32,
24    ) -> Self {
25        Self {
26            traversal,
27            search,
28            iri_bytes,
29        }
30    }
31}
32
33/// Completed work for a decision within the checked RDFS source fragment.
34#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
35#[serde(deny_unknown_fields)]
36pub struct Rdfs12FragmentReport {
37    /// Fully examined source appearances, including recursive/repeated paths.
38    pub source_occurrences: u64,
39    /// Fully examined target appearances, including recursive/repeated paths.
40    pub target_occurrences: u64,
41    /// Ordinary-predicate patterns retained; zero when matching was not invoked.
42    pub projected_target_triples: u32,
43    /// Cumulative charged property/denotation-probe and matching work; zero before search.
44    pub search_steps: u64,
45}
46
47/// A condition outside this proven fragment, not a non-entailment witness.
48#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
49pub enum Rdfs12FragmentRestriction {
50    /// An asserted standard-predicate root is outside the three admitted schema forms.
51    UnsupportedSourceSchema,
52    /// A literal appears in either graph; no lexical payload was inspected.
53    Literal,
54    /// Ordinary constraints matched, but a standard-predicate constraint remains unproved.
55    UnresolvedStandardConstraint,
56    /// A generalized position occurs: blank predicate or non-object triple term.
57    GeneralizedPosition,
58}
59
60/// A complete decision only after both graphs satisfy all admission conditions.
61#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
62#[serde(deny_unknown_fields)]
63pub enum Rdfs12FragmentOutcome<T> {
64    /// One consistent instance of the entire target proves RDFS 1.2 entailment.
65    Entailed(Rdfs12FragmentReport),
66    /// The free-constructor standard completion supplies a countermodel.
67    /// This is not failure to find a proof within a construction/pass budget.
68    NotEntailed {
69        /// Completed admission/search work; search counters are zero if not invoked.
70        report: Rdfs12FragmentReport,
71        /// The explicit countermodel condition used for this negative decision.
72        refutation: Rdfs12FragmentRefutation<T>,
73    },
74    /// This fragment has no decision for the rejected source/target appearance.
75    Unsupported {
76        /// Specific failed admission condition.
77        restriction: Rdfs12FragmentRestriction,
78        /// Exact original root, term, phase and recursive path.
79        occurrence: TermOccurrence<T>,
80    },
81}
82
83/// A refutation condition in the checked source's fresh-atom standard completion.
84///
85/// This records an observation about a pinned input, not an independently trusted
86/// proof object. Keep source/target graph and snapshot identities with the report.
87#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
88#[serde(deny_unknown_fields)]
89pub enum Rdfs12FragmentRefutation<T> {
90    /// This nested triple term has no value in the selective-property source model.
91    /// Its fixed predicate is neither a source predicate nor declared a property.
92    TripleTermPredicate {
93        /// Exact nested target predicate appearance, including its parent triple term.
94        occurrence: TermOccurrence<T>,
95    },
96    /// No consistent substitution satisfies the ordinary-predicate projection.
97    OrdinaryProjection,
98    /// In a selective-property completion, this fixed ordinary name is not a property.
99    /// All scoped source predicates and asserted property declarations were inspected.
100    NamedPropertyMembership {
101        /// Exact original root-subject appearance of the unforced property claim.
102        occurrence: TermOccurrence<T>,
103    },
104    /// A fixed ordinary named subject can denote a fresh atom outside this value space.
105    /// This is one chosen model, not a rule that IRIs cannot denote literal values.
106    DefaultDatatypeMembership {
107        /// Exact shared mandatory default-map datatype.
108        datatype: Rdf12Datatype,
109        /// Exact original root-subject appearance with target role and path.
110        occurrence: TermOccurrence<T>,
111    },
112}
113
114/// Incomplete admission/search; never convert these failures into `NotEntailed`.
115#[derive(Debug)]
116pub enum Rdfs12FragmentError<T, E> {
117    /// Opening the selected immutable graph failed.
118    Graph {
119        /// Owning semantic phase.
120        role: LiteralRole,
121        /// Backend failure.
122        error: E,
123    },
124    /// Bounded structural traversal failed, including late cursor errors.
125    Traversal(OccurrenceError<E>),
126    /// Authoritative IRI or blank classification failed for this appearance.
127    Metadata {
128        /// Exact failed appearance.
129        occurrence: TermOccurrence<T>,
130        /// Backend failure.
131        error: E,
132    },
133    /// An adapter returned IRI metadata larger than its explicit cap.
134    IriLimit {
135        /// Exact over-budget appearance.
136        occurrence: TermOccurrence<T>,
137    },
138    /// Property/denotation candidate search or shared matching did not complete.
139    Search(EntailmentError<E>),
140    /// The attempt was cancelled/superseded.
141    Cancelled,
142    /// Work counters cannot represent a completed traversal.
143    CounterOverflow,
144}
145impl<T, E: fmt::Display> fmt::Display for Rdfs12FragmentError<T, E> {
146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147        match self {
148            Self::Graph { error, .. } => write!(f, "RDFS fragment graph access failed: {error}"),
149            Self::Traversal(error) => error.fmt(f),
150            Self::Metadata { error, .. } => write!(f, "RDFS fragment metadata failed: {error}"),
151            Self::IriLimit { .. } => f.write_str("RDFS fragment IRI metadata limit exceeded"),
152            Self::Search(error) => error.fmt(f),
153            Self::Cancelled => f.write_str("RDFS fragment decision cancelled"),
154            Self::CounterOverflow => f.write_str("RDFS fragment work counter overflow"),
155        }
156    }
157}
158impl<T: fmt::Debug, E: Error + 'static> Error for Rdfs12FragmentError<T, E> {
159    fn source(&self) -> Option<&(dyn Error + 'static)> {
160        match self {
161            Self::Graph { error, .. } | Self::Metadata { error, .. } => Some(error),
162            Self::Traversal(error) => Some(error),
163            Self::Search(error) => Some(error),
164            _ => None,
165        }
166    }
167}