use rdf_reasoning::RuleId;
use serde::{Deserialize, Serialize};
use std::{
error::Error,
fmt,
num::{NonZeroU32, NonZeroU64},
str::FromStr,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum ExecutionProfile {
#[serde(rename = "rdfs-subclass")]
RdfsSubclass,
}
impl ExecutionProfile {
pub const fn name(self) -> &'static str {
"rdfs-subclass"
}
pub const fn revision(self) -> u32 {
1
}
pub const fn rules(self) -> &'static [RuleId; 2] {
&[RuleId::Rdfs9, RuleId::Rdfs11]
}
}
impl FromStr for ExecutionProfile {
type Err = ProfileError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"rdfs-subclass" => Ok(Self::RdfsSubclass),
_ => Err(ProfileError(value.to_owned())),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProfileError(String);
impl fmt::Display for ProfileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "unsupported executable profile: {}", self.0)
}
}
impl Error for ProfileError {}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RunLimits {
pub(super) rounds: NonZeroU32,
pub(super) inferred: NonZeroU64,
pub(super) matches: NonZeroU64,
}
impl RunLimits {
pub const fn new(rounds: NonZeroU32, inferred: NonZeroU64, matches: NonZeroU64) -> Self {
Self {
rounds,
inferred,
matches,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Inference<T> {
pub triple: [T; 3],
pub rule: RuleId,
pub premises: [[T; 3]; 2],
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct RunReport {
pub rounds: u32,
pub matches: u64,
pub inferred: u64,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Limit {
Rounds,
Inferred,
Matches,
}
#[derive(Debug)]
pub enum ReasoningError<E> {
Storage(E),
Cancelled,
LimitExceeded(Limit),
UnsupportedRule(RuleId),
}
impl<E: fmt::Display> fmt::Display for ReasoningError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Storage(error) => write!(f, "reasoning storage failed: {error}"),
Self::Cancelled => f.write_str("reasoning cancelled"),
Self::LimitExceeded(limit) => write!(
f,
"reasoning {} limit exceeded",
match limit {
Limit::Rounds => "rounds",
Limit::Inferred => "inferred",
Limit::Matches => "matches",
}
),
Self::UnsupportedRule(rule) => {
write!(f, "unsupported shared rule definition: {rule:?}")
}
}
}
}
impl<E: Error + 'static> Error for ReasoningError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Storage(error) => Some(error),
_ => None,
}
}
}