Skip to main content

inference_engine/reasoning/target/
engine.rs

1use super::{
2    TargetConstructionError, TargetConstructionLimits, TargetConstructionReport,
3    grounding::Grounding,
4};
5use crate::{
6    entailment::{EntailmentTerms, PartialMatches},
7    reasoning::{Rdf12PropositionConstructor, Rdf12TripleTermStore, ReasoningError, TripleSource},
8};
9
10/// Select substituted original-target triple terms for shared Grdfs14 construction.
11///
12/// Run after ordered source/context preparation. This is one finite positive-search
13/// phase, not a complete RDFS decision procedure. Newly staged facts cannot feed
14/// matching or constructor premises until the caller starts another pass, normally
15/// after core closure. Never feed it an unrestricted resource/property product.
16pub struct Rdf12TargetConstructor {
17    limits: TargetConstructionLimits,
18}
19impl Rdf12TargetConstructor {
20    /// Select explicit matching, grounding and constructor budgets.
21    pub const fn new(limits: TargetConstructionLimits) -> Self {
22        Self { limits }
23    }
24
25    /// Stream consistent partial matches, substitute original target triple terms,
26    /// and validate all three shared typing premises in one fixed snapshot.
27    ///
28    /// Matched subsets supply candidate bindings; skipped patterns are not proved.
29    /// Unbound variables receive the single `rdf:type` representative, consistently
30    /// within each substitution. This is a selected witness search, not an adequate
31    /// enumeration of all RDFS interpretations. Constructor premises still gate
32    /// every fact, and a separate whole-target match is required for a proof.
33    ///
34    /// Source, target, accessor and store must share exact append-only dictionary
35    /// identities and cancellation, with independently scoped target blanks. Keep
36    /// original source/target snapshots, phase snapshot, profile/catalogue and graph
37    /// provenance beside shared constructor evidence. Target contents are never
38    /// asserted. Actual substituted depth and dictionary growth are backend-bounded.
39    ///
40    /// # Errors
41    /// Any exhausted budget, cancellation, read/write or identity failure aborts
42    /// the entire attempt. Zero output never establishes negative entailment.
43    pub async fn run<S, A, T>(
44        &self,
45        store: &mut S,
46        access: &A,
47        target: &T,
48    ) -> Result<TargetConstructionReport, TargetConstructionError<S::Error>>
49    where
50        S: Rdf12TripleTermStore,
51        A: EntailmentTerms<Term = S::Term, Error = S::Error>,
52        T: TripleSource<Term = S::Term, Error = S::Error>,
53    {
54        let mut phase = Rdf12PropositionConstructor::new(self.limits.propositions)
55            .begin(store)
56            .await?;
57        let mut matches =
58            PartialMatches::new(phase.source(), target, access, self.limits.matching).await?;
59        let mut grounding = Grounding::new(self.limits);
60        let mut report = TargetConstructionReport::default();
61        if !grounding
62            .has_goals(store, access, matches.patterns())
63            .await?
64        {
65            grounding.finish(store, access).await?;
66            report.matching_steps = matches.steps();
67            report.grounding_steps = grounding.steps();
68            return Ok(report);
69        }
70        let representative = store
71            .resolve_iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
72            .await
73            .map_err(ReasoningError::Storage)?;
74        while matches.next(phase.source(), access).await? {
75            report.substitutions = report
76                .substitutions
77                .checked_add(1)
78                .ok_or(TargetConstructionError::CounterOverflow)?;
79            for pattern in matches.patterns() {
80                for term in pattern {
81                    grounding
82                        .run(
83                            store,
84                            access,
85                            term,
86                            matches.bindings(),
87                            &representative,
88                            &mut phase,
89                        )
90                        .await?;
91                }
92            }
93        }
94        grounding.finish(store, access).await?;
95        report.matching_steps = matches.steps();
96        report.grounding_steps = grounding.steps();
97        report.propositions = phase.report;
98        Ok(report)
99    }
100}