Skip to main content

inference_engine/shacl12/
types.rs

1use std::{error::Error, fmt, num::NonZeroU64};
2
3/// Explicit attempt-wide controller limits. Backend-specific query/I/O limits
4/// must also be enforced; these counters do not replace query cancellation.
5#[derive(Clone, Copy, Debug)]
6pub struct Limits {
7    pub(super) iterations: u32,
8    pub(super) work: NonZeroU64,
9    pub(super) inferred: NonZeroU64,
10}
11impl Limits {
12    /// Limit iterations per layer (including the final empty iteration), total
13    /// charged work and total novel inferred triples, including temporary output.
14    pub const fn new(iterations: u32, work: NonZeroU64, inferred: NonZeroU64) -> Self {
15        Self {
16            iterations,
17            work,
18            inferred,
19        }
20    }
21}
22/// Successfully completed counters; no report is returned after partial failure.
23#[derive(Default, Debug, Eq, PartialEq)]
24pub struct Report {
25    /// Completed fixed-point iterations across layers, including empty rounds.
26    pub iterations: u64,
27    /// Executed same-order phases, including run-once phases.
28    pub phases: u64,
29    /// Completed rule invocations across all phases.
30    pub executions: u64,
31    /// Novel inferred triples before derived/temporary cleanup.
32    pub inferred: u64,
33    /// Total charged execution work, including backend reads and candidates.
34    pub work: u64,
35}
36/// Attempt-wide charged work. An ignored exhaustion error remains sticky.
37#[derive(Debug)]
38pub struct Work {
39    used: u64,
40    maximum: u64,
41    exceeded: bool,
42}
43impl Work {
44    pub(super) const fn new(maximum: NonZeroU64) -> Self {
45        Self {
46            used: 0,
47            maximum: maximum.get(),
48            exceeded: false,
49        }
50    }
51    /// Charge before performing a bounded unit of work.
52    /// # Errors
53    /// Exhaustion invalidates the entire attempt, even if a backend ignores it.
54    pub fn charge(&mut self) -> Result<(), WorkExceeded> {
55        if self.exceeded || self.used == self.maximum {
56            self.exceeded = true;
57            return Err(WorkExceeded);
58        }
59        self.used += 1;
60        Ok(())
61    }
62    /// Successfully charged work units.
63    pub const fn used(&self) -> u64 {
64        self.used
65    }
66    pub(super) const fn exceeded(&self) -> bool {
67        self.exceeded
68    }
69}
70/// Work budget exhaustion, suitable for propagation through a backend error.
71#[derive(Debug)]
72pub struct WorkExceeded;
73impl fmt::Display for WorkExceeded {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.write_str("SHACL work limit exceeded")
76    }
77}
78impl Error for WorkExceeded {}
79/// Controller limit preventing successful completion.
80#[derive(Debug, Clone, Copy, Eq, PartialEq)]
81pub enum Limit {
82    /// A layer did not reach a fixed point within the allowed iterations.
83    Iterations,
84    /// Total charged work exhausted.
85    Work,
86    /// Novel inferred output exhausted.
87    Inferred,
88}
89/// The entire attempt failed; discard all staged output.
90#[derive(Debug)]
91pub enum ExecutionError<E> {
92    /// A native read, query or staging operation failed.
93    Backend(E),
94    /// Explicit execution limit exhausted.
95    Limit(Limit),
96    /// Attempt cancelled or superseded.
97    Cancelled,
98}
99impl<E: fmt::Display> fmt::Display for ExecutionError<E> {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        match self {
102            Self::Backend(error) => write!(f, "SHACL backend failed: {error}"),
103            Self::Limit(limit) => write!(f, "SHACL {limit:?} limit exceeded"),
104            Self::Cancelled => f.write_str("SHACL attempt cancelled"),
105        }
106    }
107}
108impl<E: Error + 'static> Error for ExecutionError<E> {
109    fn source(&self) -> Option<&(dyn Error + 'static)> {
110        if let Self::Backend(error) = self {
111            Some(error)
112        } else {
113            None
114        }
115    }
116}