inference_engine/reasoning/
types.rs1use super::{LiteralOccurrence, TermOccurrence};
2use rdf_reasoning::{
3 Exclusion, Preset, Rdf12AxiomId, Rdf12DatatypeConflictId, Rdf12ExistentialAxiomId,
4 Rdf12LiteralRuleId, Rdf12OccurrenceRuleId, Rdf12PropositionRuleId, RuleId,
5};
6use serde::{Deserialize, Serialize};
7use std::{
8 error::Error,
9 fmt,
10 num::{NonZeroU32, NonZeroU64},
11 str::FromStr,
12};
13
14#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
16pub enum ExecutionProfile {
17 #[serde(rename = "rdfs-subclass")]
19 RdfsSubclass,
20 #[serde(rename = "rdfs-core")]
22 RdfsCore,
23}
24
25impl ExecutionProfile {
26 pub const fn name(self) -> &'static str {
28 self.preset().name()
29 }
30 pub const fn revision(self) -> u32 {
32 1
33 }
34 pub const fn preset(self) -> Preset {
36 match self {
37 Self::RdfsSubclass => Preset::RdfsSubclass,
38 Self::RdfsCore => Preset::RdfsCore,
39 }
40 }
41 pub const fn rules(self) -> &'static [RuleId] {
43 self.preset().rules()
44 }
45 pub const fn exclusions(self) -> &'static [Exclusion] {
47 self.preset().exclusions()
48 }
49}
50
51impl FromStr for ExecutionProfile {
52 type Err = ProfileError;
53 fn from_str(value: &str) -> Result<Self, Self::Err> {
54 match value {
55 "rdfs-subclass" => Ok(Self::RdfsSubclass),
56 "rdfs-core" => Ok(Self::RdfsCore),
57 _ => Err(ProfileError(value.to_owned())),
58 }
59 }
60}
61
62#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
64pub struct ProfileError(String);
65impl fmt::Display for ProfileError {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 write!(f, "unsupported executable profile: {}", self.0)
68 }
69}
70impl Error for ProfileError {}
71
72#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
74#[serde(deny_unknown_fields)]
75pub struct RunLimits {
76 pub(super) rounds: NonZeroU32,
77 pub(super) inferred: NonZeroU64,
78 pub(super) matches: NonZeroU64,
79}
80impl RunLimits {
81 pub const fn new(rounds: NonZeroU32, inferred: NonZeroU64, matches: NonZeroU64) -> Self {
84 Self {
85 rounds,
86 inferred,
87 matches,
88 }
89 }
90}
91
92#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
94pub enum Premises<T> {
95 One([T; 3]),
97 Two([[T; 3]; 2]),
99}
100impl<T> Premises<T> {
101 pub fn iter(&self) -> std::slice::Iter<'_, [T; 3]> {
103 match self {
104 Self::One(tuple) => std::slice::from_ref(tuple).iter(),
105 Self::Two(tuples) => tuples.iter(),
106 }
107 }
108}
109impl<T> From<[[T; 3]; 2]> for Premises<T> {
110 fn from(tuples: [[T; 3]; 2]) -> Self {
111 Self::Two(tuples)
112 }
113}
114
115#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
117pub enum AxiomFamily {
118 Rdf12(Rdf12AxiomId),
120 Rdf12Container,
122 Rdf12Existential(Rdf12ExistentialAxiomId),
124}
125
126#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
128#[serde(deny_unknown_fields)]
129pub enum Evidence<T> {
130 Rule {
132 rule: RuleId,
134 premises: Premises<T>,
136 },
137 Axiom(AxiomFamily),
139 Literal {
141 rule: Rdf12LiteralRuleId,
143 occurrence: LiteralOccurrence<T>,
145 lexical_bytes: u64,
147 },
148 Occurrence {
150 rule: Rdf12OccurrenceRuleId,
152 occurrence: TermOccurrence<T>,
154 },
155 Proposition {
157 rule: Rdf12PropositionRuleId,
159 premises: [[T; 3]; 3],
161 },
162}
163
164#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
166#[serde(deny_unknown_fields)]
167pub struct Inference<T> {
168 pub triple: [T; 3],
170 pub evidence: Evidence<T>,
172}
173
174#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
176pub struct RunReport {
177 pub rounds: u32,
179 pub matches: u64,
181 pub inferred: u64,
183}
184
185#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
187pub enum Limit {
188 Rounds,
190 Inferred,
192 Matches,
194}
195
196#[derive(Debug)]
198pub enum ReasoningError<E> {
199 Storage(E),
201 InvalidProbe,
203 Cancelled,
205 LimitExceeded(Limit),
207 UnsupportedRule(RuleId),
209 UnsupportedAxiom,
211 UnsupportedLiteralRule(Rdf12LiteralRuleId),
213 UnsupportedPropositionRule(Rdf12PropositionRuleId),
215 UnsupportedDatatypeConflict(Rdf12DatatypeConflictId),
217 UnsupportedOccurrenceRule(Rdf12OccurrenceRuleId),
219}
220impl<E: fmt::Display> fmt::Display for ReasoningError<E> {
221 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222 match self {
223 Self::Storage(error) => write!(f, "reasoning storage failed: {error}"),
224 Self::InvalidProbe => {
225 f.write_str("reasoning snapshot returned a row outside an exact probe")
226 }
227 Self::Cancelled => f.write_str("reasoning cancelled"),
228 Self::LimitExceeded(limit) => write!(
229 f,
230 "reasoning {} limit exceeded",
231 match limit {
232 Limit::Rounds => "rounds",
233 Limit::Inferred => "inferred",
234 Limit::Matches => "matches",
235 }
236 ),
237 Self::UnsupportedRule(rule) => {
238 write!(f, "unsupported shared rule definition: {rule:?}")
239 }
240 Self::UnsupportedAxiom => f.write_str("unsupported shared ground axiom definition"),
241 Self::UnsupportedLiteralRule(rule) => {
242 write!(f, "unsupported shared literal rule definition: {rule:?}")
243 }
244 Self::UnsupportedOccurrenceRule(id) => {
245 write!(f, "unsupported occurrence rule definition: {id:?}")
246 }
247 Self::UnsupportedDatatypeConflict(id) => {
248 write!(f, "unsupported datatype conflict definition: {id:?}")
249 }
250 Self::UnsupportedPropositionRule(rule) => {
251 write!(f, "unsupported proposition rule definition: {rule:?}")
252 }
253 }
254 }
255}
256impl<E: Error + 'static> Error for ReasoningError<E> {
257 fn source(&self) -> Option<&(dyn Error + 'static)> {
258 match self {
259 Self::Storage(error) => Some(error),
260 _ => None,
261 }
262 }
263}