Skip to main content

inference_engine/entailment/fragment/
mod.rs

1//! Checked RDFS decisions over an explicitly limited literal-free source fragment.
2mod admission;
3mod evidence;
4mod property;
5mod triple_term;
6mod types;
7use super::{EntailmentError, IriTermAccess, SimpleEntailment};
8use crate::reasoning::{LiteralRole, TripleSource};
9use admission::Admission;
10use evidence::is_reserved_iri;
11pub use types::{
12    Rdfs12FragmentError, Rdfs12FragmentLimits, Rdfs12FragmentOutcome, Rdfs12FragmentRefutation,
13    Rdfs12FragmentReport, Rdfs12FragmentRestriction,
14};
15
16/// Decide targets against a checked, literal-free RDFS 1.2 source fragment.
17///
18/// Source roots may use ordinary predicates, declare an ordinary named property,
19/// or assert `Resource subClassOf Class` / `Proposition subClassOf Property`.
20/// Standard names in data components and quoted contents do not assert schema.
21/// Both graphs must be normative RDF; scoped blanks/nested object terms are supported.
22/// Free injective-constructor standard completions provide explicit countermodels:
23/// failed ordinary projection, fixed-name datatype/property membership, or a target
24/// triple term whose named predicate is unforced by the source. All candidate
25/// scans and consistent matching share one cumulative work budget. A matching
26/// projection with unresolved standard constraints remains inconclusive.
27/// This does not enable the full `rdfs` execution selector.
28pub struct Rdfs12FragmentEntailment {
29    limits: Rdfs12FragmentLimits,
30}
31impl Rdfs12FragmentEntailment {
32    /// Set every admission/cumulative-search budget explicitly.
33    pub const fn new(limits: Rdfs12FragmentLimits) -> Self {
34        Self { limits }
35    }
36
37    /// Admit the entire selected source and target before shared consistent matching.
38    ///
39    /// Reuse one immutable graph/dictionary domain through all scans. No facts,
40    /// dictionary entries, closures or countermodel graphs are constructed here.
41    /// Keep input snapshot/graph identity with the returned scoped result.
42    ///
43    /// # Errors
44    /// Storage, cancellation and exhausted work/metadata budgets abort the check;
45    /// even late failures invalidate any incomplete positive/negative result.
46    pub async fn check<S, E, A>(
47        &self,
48        source: &S,
49        target: &E,
50        access: &A,
51    ) -> Result<Rdfs12FragmentOutcome<S::Term>, Rdfs12FragmentError<S::Term, S::Error>>
52    where
53        S: TripleSource,
54        E: TripleSource<Term = S::Term, Error = S::Error>,
55        A: IriTermAccess<Term = S::Term, Error = S::Error>,
56    {
57        let source_occurrences = match self.admit(source, access, LiteralRole::Source).await? {
58            Admission::Accepted { count, .. } => count,
59            Admission::Unsupported {
60                restriction,
61                occurrence,
62            } => {
63                return Ok(Rdfs12FragmentOutcome::Unsupported {
64                    restriction,
65                    occurrence,
66                });
67            }
68        };
69        let (target_occurrences, standard_constraint, evidence) = match self
70            .admit(target, access, LiteralRole::TargetContext)
71            .await?
72        {
73            Admission::Accepted {
74                count,
75                standard_constraint,
76                evidence,
77            } => (count, standard_constraint, evidence),
78            Admission::Unsupported {
79                restriction,
80                occurrence,
81            } => {
82                return Ok(Rdfs12FragmentOutcome::Unsupported {
83                    restriction,
84                    occurrence,
85                });
86            }
87        };
88        Self::cancelled(access)?;
89        let mut report = Rdfs12FragmentReport {
90            source_occurrences,
91            target_occurrences,
92            projected_target_triples: 0,
93            search_steps: 0,
94        };
95        if let Some(refutation) = evidence.refutation {
96            return Ok(Rdfs12FragmentOutcome::NotEntailed { report, refutation });
97        }
98        if evidence.has_property_candidate {
99            let (refutation, steps) = self
100                .property_refutation(source, target, access, report.search_steps)
101                .await?;
102            report.search_steps = steps;
103            if let Some(refutation) = refutation {
104                return Ok(Rdfs12FragmentOutcome::NotEntailed { report, refutation });
105            }
106        }
107        if evidence.has_triple_term_candidate {
108            let (refutation, steps) = self
109                .triple_term_refutation(source, target, access, report.search_steps)
110                .await?;
111            report.search_steps = steps;
112            if let Some(refutation) = refutation {
113                return Ok(Rdfs12FragmentOutcome::NotEntailed { report, refutation });
114            }
115        }
116        let iri_bytes = self.limits.iri_bytes;
117        let select = |tuple: [S::Term; 3]| async move {
118            let Some(iri) = access
119                .iri(&tuple[1], iri_bytes)
120                .await
121                .map_err(EntailmentError::Storage)?
122            else {
123                return Err(EntailmentError::TargetSelectionMetadata);
124            };
125            if iri.as_ref().len() > iri_bytes.get() as usize {
126                return Err(EntailmentError::TargetSelectionMetadata);
127            }
128            Ok(!is_reserved_iri(iri.as_ref()))
129        };
130        let matching = SimpleEntailment::new(self.limits.search)
131            .check_selected(source, target, access, Some(select), report.search_steps)
132            .await
133            .map_err(Rdfs12FragmentError::Search)?;
134        Self::cancelled(access)?;
135        report.projected_target_triples = matching.target_triples;
136        report.search_steps = matching.steps;
137        Ok(if !matching.entailed {
138            Rdfs12FragmentOutcome::NotEntailed {
139                report,
140                refutation: Rdfs12FragmentRefutation::OrdinaryProjection,
141            }
142        } else if let Some(occurrence) = standard_constraint {
143            Rdfs12FragmentOutcome::Unsupported {
144                restriction: Rdfs12FragmentRestriction::UnresolvedStandardConstraint,
145                occurrence,
146            }
147        } else {
148            Rdfs12FragmentOutcome::Entailed(report)
149        })
150    }
151    fn cancelled<A: IriTermAccess>(
152        access: &A,
153    ) -> Result<(), Rdfs12FragmentError<A::Term, A::Error>> {
154        if access.is_cancelled() {
155            Err(Rdfs12FragmentError::Cancelled)
156        } else {
157            Ok(())
158        }
159    }
160}