Skip to main content

inference_engine/reasoning/target/
mod.rs

1//! Finite target-selected construction over one immutable premise snapshot.
2mod engine;
3mod grounding;
4use super::{PropositionLimits, PropositionReport, ReasoningError};
5use crate::entailment::{EntailmentError, EntailmentLimits};
6pub use engine::Rdf12TargetConstructor;
7use serde::{Deserialize, Serialize};
8use std::{
9    error::Error,
10    fmt,
11    num::{NonZeroU32, NonZeroU64},
12};
13
14/// Explicit budgets for one target-construction pass; no implicit defaults.
15#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
16#[serde(deny_unknown_fields)]
17pub struct TargetConstructionLimits {
18    matching: EntailmentLimits,
19    propositions: PropositionLimits,
20    slots: NonZeroU32,
21    work: NonZeroU64,
22}
23impl TargetConstructionLimits {
24    /// Bound partial matching, total constructor work/output, pending grounding
25    /// slots and total grounding steps. Backend dictionary/depth/I/O caps also apply.
26    pub const fn new(
27        matching: EntailmentLimits,
28        propositions: PropositionLimits,
29        slots: NonZeroU32,
30        work: NonZeroU64,
31    ) -> Self {
32        Self {
33            matching,
34            propositions,
35            slots,
36            work,
37        }
38    }
39}
40
41/// Completed finite selection pass, never a full RDFS entailment verdict.
42#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
43#[serde(deny_unknown_fields)]
44pub struct TargetConstructionReport {
45    /// Consistent partial target substitutions examined, including the empty one.
46    pub substitutions: u64,
47    /// Source/target reads and unification/probe steps in partial matching.
48    pub matching_steps: u64,
49    /// Target inspection, recursive grounding and substitution work.
50    pub grounding_steps: u64,
51    /// Shared Grdfs14 candidate/premise work and novel proposition memberships.
52    pub propositions: PropositionReport,
53}
54
55/// Failed construction invalidates the complete scratch attempt.
56#[derive(Debug)]
57pub enum TargetConstructionError<E> {
58    /// Matching, target reads or structural access failed.
59    Matching(EntailmentError<E>),
60    /// Shared constructor, dictionary or premise access failed.
61    Reasoning(ReasoningError<E>),
62    /// Pending grounding tasks/values exceeded the configured cap.
63    SlotLimit,
64    /// The total target-inspection/grounding budget was exhausted.
65    WorkLimit,
66    /// A constructed key did not decompose to the exact requested tuple.
67    Identity,
68    /// A report counter could not be represented.
69    CounterOverflow,
70}
71impl<E: fmt::Display> fmt::Display for TargetConstructionError<E> {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            Self::Matching(e) => write!(f, "target construction matching failed: {e}"),
75            Self::Reasoning(e) => write!(f, "target construction failed: {e}"),
76            Self::SlotLimit => f.write_str("target grounding slot limit exceeded"),
77            Self::WorkLimit => f.write_str("target grounding work limit exceeded"),
78            Self::Identity => f.write_str("target construction dictionary identity mismatch"),
79            Self::CounterOverflow => f.write_str("target construction counter overflow"),
80        }
81    }
82}
83impl<E: Error + 'static> Error for TargetConstructionError<E> {
84    fn source(&self) -> Option<&(dyn Error + 'static)> {
85        match self {
86            Self::Matching(e) => Some(e),
87            Self::Reasoning(e) => Some(e),
88            _ => None,
89        }
90    }
91}
92impl<E> From<EntailmentError<E>> for TargetConstructionError<E> {
93    fn from(e: EntailmentError<E>) -> Self {
94        Self::Matching(e)
95    }
96}
97impl<E> From<ReasoningError<E>> for TargetConstructionError<E> {
98    fn from(e: ReasoningError<E>) -> Self {
99        Self::Reasoning(e)
100    }
101}