inference_engine/reasoning/
types.rs1use rdf_reasoning::RuleId;
2use serde::{Deserialize, Serialize};
3use std::{
4 error::Error,
5 fmt,
6 num::{NonZeroU32, NonZeroU64},
7 str::FromStr,
8};
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
12pub enum ExecutionProfile {
13 #[serde(rename = "rdfs-subclass")]
15 RdfsSubclass,
16}
17
18impl ExecutionProfile {
19 pub const fn name(self) -> &'static str {
21 "rdfs-subclass"
22 }
23 pub const fn revision(self) -> u32 {
25 1
26 }
27 pub const fn rules(self) -> &'static [RuleId; 2] {
29 &[RuleId::Rdfs9, RuleId::Rdfs11]
30 }
31}
32
33impl FromStr for ExecutionProfile {
34 type Err = ProfileError;
35 fn from_str(value: &str) -> Result<Self, Self::Err> {
36 match value {
37 "rdfs-subclass" => Ok(Self::RdfsSubclass),
38 _ => Err(ProfileError(value.to_owned())),
39 }
40 }
41}
42
43#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
45pub struct ProfileError(String);
46impl fmt::Display for ProfileError {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 write!(f, "unsupported executable profile: {}", self.0)
49 }
50}
51impl Error for ProfileError {}
52
53#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
55#[serde(deny_unknown_fields)]
56pub struct RunLimits {
57 pub(super) rounds: NonZeroU32,
58 pub(super) inferred: NonZeroU64,
59 pub(super) matches: NonZeroU64,
60}
61impl RunLimits {
62 pub const fn new(rounds: NonZeroU32, inferred: NonZeroU64, matches: NonZeroU64) -> Self {
65 Self {
66 rounds,
67 inferred,
68 matches,
69 }
70 }
71}
72
73#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
75#[serde(deny_unknown_fields)]
76pub struct Inference<T> {
77 pub triple: [T; 3],
79 pub rule: RuleId,
81 pub premises: [[T; 3]; 2],
83}
84
85#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
87pub struct RunReport {
88 pub rounds: u32,
90 pub matches: u64,
92 pub inferred: u64,
94}
95
96#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
98pub enum Limit {
99 Rounds,
101 Inferred,
103 Matches,
105}
106
107#[derive(Debug)]
109pub enum ReasoningError<E> {
110 Storage(E),
112 Cancelled,
114 LimitExceeded(Limit),
116 UnsupportedRule(RuleId),
118}
119impl<E: fmt::Display> fmt::Display for ReasoningError<E> {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 match self {
122 Self::Storage(error) => write!(f, "reasoning storage failed: {error}"),
123 Self::Cancelled => f.write_str("reasoning cancelled"),
124 Self::LimitExceeded(limit) => write!(
125 f,
126 "reasoning {} limit exceeded",
127 match limit {
128 Limit::Rounds => "rounds",
129 Limit::Inferred => "inferred",
130 Limit::Matches => "matches",
131 }
132 ),
133 Self::UnsupportedRule(rule) => {
134 write!(f, "unsupported shared rule definition: {rule:?}")
135 }
136 }
137 }
138}
139impl<E: Error + 'static> Error for ReasoningError<E> {
140 fn source(&self) -> Option<&(dyn Error + 'static)> {
141 match self {
142 Self::Storage(error) => Some(error),
143 _ => None,
144 }
145 }
146}