Skip to main content

inference_engine/reasoning/
mod.rs

1//! Bounded forward execution of the shared `rdfs9` and `rdfs11` clauses.
2//!
3//! Callers own immutable selected input, indexed scratch storage and publication.
4//! This module never collects a graph, chooses graphs, or promotes partial output.
5
6mod engine;
7mod plan;
8mod types;
9
10pub use engine::Reasoner;
11pub use types::{
12    ExecutionProfile, Inference, Limit, ProfileError, ReasoningError, RunLimits, RunReport,
13};
14
15use std::{error::Error, future::Future};
16
17/// A fallible, bounded cursor over an immutable snapshot's matching tuples.
18pub trait TripleCursor: Send {
19    /// Caller-owned exact RDF term identity; scoped blanks must remain distinct.
20    type Term: Clone + Eq + Send + Sync;
21    /// Read failures, including cancellation, must never become end of stream.
22    type Error: Error + Send + Sync + 'static;
23    /// Read at most one tuple. Drop must release/cancel the underlying read.
24    fn next(&mut self)
25    -> impl Future<Output = Result<Option<[Self::Term; 3]>, Self::Error>> + Send;
26}
27
28/// One immutable round view of selected assertions plus staged consequences.
29///
30/// Scans must be bounded and match every supplied term exactly. The view and
31/// its cursors remain valid while the caller inserts into the separate store.
32/// No ordering is required; set closure is independent of scan order. The first
33/// retained proof can differ between orders and is not a canonical explanation.
34pub trait TripleSource: Send + Sync {
35    /// Exact term type, without lexical/value normalization by the executor.
36    type Term: Clone + Eq + Send + Sync;
37    /// Backend read error.
38    type Error: Error + Send + Sync + 'static;
39    /// Owned cursor, independent of subsequent store writes.
40    type Cursor: TripleCursor<Term = Self::Term, Error = Self::Error>;
41    /// Open a scan/probe; `None` is a wildcard in subject/predicate/object order.
42    fn scan(
43        &self,
44        pattern: [Option<Self::Term>; 3],
45    ) -> impl Future<Output = Result<Self::Cursor, Self::Error>> + Send;
46}
47
48/// Backend capabilities for one fresh, isolated inference attempt.
49///
50/// Initially expose only the caller's selected asserted snapshot, never earlier
51/// inferred output. Scratch facts must spill to caller-owned indexed storage;
52/// no method may buffer a graph-sized relation. Each snapshot freezes the union
53/// as of its creation; subsequent insertions are visible in the next snapshot.
54///
55/// On any error the caller must discard/abort staged output. A successful run
56/// grants no publication authority: callers still check input currentness and
57/// atomically promote their owned inference contribution. Recompute from fresh
58/// assertions after deletion. The executor never mutates asserted input.
59pub trait ReasoningStore: Send {
60    /// Exact RDF term identity; database IDs and backend RDF terms both work.
61    type Term: Clone + Eq + Send + Sync;
62    /// Backend failure propagated without substituting an empty relation.
63    type Error: Error + Send + Sync + 'static;
64    /// Owned immutable view supporting concurrent outer/inner read cursors.
65    type Snapshot: TripleSource<Term = Self::Term, Error = Self::Error>;
66    /// Resolve a full rule-constant IRI as an IRI, never as a plain string literal.
67    fn resolve_iri(
68        &mut self,
69        iri: &str,
70    ) -> impl Future<Output = Result<Self::Term, Self::Error>> + Send;
71    /// Freeze selected assertions plus all currently staged inferred tuples.
72    fn snapshot(&mut self) -> impl Future<Output = Result<Self::Snapshot, Self::Error>> + Send;
73    /// Indexed membership in the current asserted-plus-staged union.
74    fn contains(
75        &mut self,
76        triple: &[Self::Term; 3],
77    ) -> impl Future<Output = Result<bool, Self::Error>> + Send;
78    /// Stage a novel inferred tuple with its first proof; assertions win duplicates.
79    ///
80    /// Return `true` only for a new inferred tuple. Preserve generalized tuples
81    /// in scratch; do not silently drop terms or normalize literal lexical forms.
82    fn insert_inferred(
83        &mut self,
84        inference: Inference<Self::Term>,
85    ) -> impl Future<Output = Result<bool, Self::Error>> + Send;
86    /// Whether this attempt is cancelled/superseded. Awaited operations must also
87    /// support cancellation through the backend's own error/control boundary.
88    fn is_cancelled(&self) -> bool;
89}