use super::{LiteralOccurrence, TermOccurrence};
use rdf_reasoning::{
Exclusion, Preset, Rdf12AxiomId, Rdf12DatatypeConflictId, Rdf12ExistentialAxiomId,
Rdf12LiteralRuleId, Rdf12OccurrenceRuleId, Rdf12PropositionRuleId, 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,
#[serde(rename = "rdfs-core")]
RdfsCore,
}
impl ExecutionProfile {
pub const fn name(self) -> &'static str {
self.preset().name()
}
pub const fn revision(self) -> u32 {
1
}
pub const fn preset(self) -> Preset {
match self {
Self::RdfsSubclass => Preset::RdfsSubclass,
Self::RdfsCore => Preset::RdfsCore,
}
}
pub const fn rules(self) -> &'static [RuleId] {
self.preset().rules()
}
pub const fn exclusions(self) -> &'static [Exclusion] {
self.preset().exclusions()
}
}
impl FromStr for ExecutionProfile {
type Err = ProfileError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"rdfs-subclass" => Ok(Self::RdfsSubclass),
"rdfs-core" => Ok(Self::RdfsCore),
_ => 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)]
pub enum Premises<T> {
One([T; 3]),
Two([[T; 3]; 2]),
}
impl<T> Premises<T> {
pub fn iter(&self) -> std::slice::Iter<'_, [T; 3]> {
match self {
Self::One(tuple) => std::slice::from_ref(tuple).iter(),
Self::Two(tuples) => tuples.iter(),
}
}
}
impl<T> From<[[T; 3]; 2]> for Premises<T> {
fn from(tuples: [[T; 3]; 2]) -> Self {
Self::Two(tuples)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum AxiomFamily {
Rdf12(Rdf12AxiomId),
Rdf12Container,
Rdf12Existential(Rdf12ExistentialAxiomId),
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub enum Evidence<T> {
Rule {
rule: RuleId,
premises: Premises<T>,
},
Axiom(AxiomFamily),
Literal {
rule: Rdf12LiteralRuleId,
occurrence: LiteralOccurrence<T>,
lexical_bytes: u64,
},
Occurrence {
rule: Rdf12OccurrenceRuleId,
occurrence: TermOccurrence<T>,
},
Proposition {
rule: Rdf12PropositionRuleId,
premises: [[T; 3]; 3],
},
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Inference<T> {
pub triple: [T; 3],
pub evidence: Evidence<T>,
}
#[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),
InvalidProbe,
Cancelled,
LimitExceeded(Limit),
UnsupportedRule(RuleId),
UnsupportedAxiom,
UnsupportedLiteralRule(Rdf12LiteralRuleId),
UnsupportedPropositionRule(Rdf12PropositionRuleId),
UnsupportedDatatypeConflict(Rdf12DatatypeConflictId),
UnsupportedOccurrenceRule(Rdf12OccurrenceRuleId),
}
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::InvalidProbe => {
f.write_str("reasoning snapshot returned a row outside an exact probe")
}
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:?}")
}
Self::UnsupportedAxiom => f.write_str("unsupported shared ground axiom definition"),
Self::UnsupportedLiteralRule(rule) => {
write!(f, "unsupported shared literal rule definition: {rule:?}")
}
Self::UnsupportedOccurrenceRule(id) => {
write!(f, "unsupported occurrence rule definition: {id:?}")
}
Self::UnsupportedDatatypeConflict(id) => {
write!(f, "unsupported datatype conflict definition: {id:?}")
}
Self::UnsupportedPropositionRule(rule) => {
write!(f, "unsupported proposition 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,
}
}
}