inference_engine/reasoning/literal_typing/
cursor.rs1use super::{
2 LiteralOccurrence, LiteralOccurrenceCursor, LiteralOccurrenceRead, LiteralReader, LiteralRole,
3};
4use crate::{
5 entailment::EntailmentTerms,
6 reasoning::{
7 OccurrenceError, OccurrenceLimits, Rdf12OccurrenceCursor, TermOccurrence, TripleCursor,
8 },
9};
10use std::{error::Error, fmt, future::Future};
11
12pub trait LiteralTermAccess: EntailmentTerms {
19 type Reader<'a>: LiteralReader<Error = Self::Error>
21 where
22 Self: 'a;
23 fn open_literal<'a>(
27 &'a self,
28 term: &Self::Term,
29 ) -> impl Future<Output = Result<Option<Self::Reader<'a>>, Self::Error>> + Send;
30}
31
32#[derive(Debug)]
34pub enum LiteralCursorError<T, E> {
35 Storage(E),
37 Traversal(OccurrenceError<E>),
39 Open {
41 occurrence: TermOccurrence<T>,
43 error: E,
45 },
46}
47impl<T, E> From<E> for LiteralCursorError<T, E> {
48 fn from(error: E) -> Self {
49 Self::Storage(error)
50 }
51}
52impl<T, E: fmt::Display> fmt::Display for LiteralCursorError<T, E> {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 Self::Storage(error) => write!(f, "literal occurrence access failed: {error}"),
56 Self::Traversal(error) => error.fmt(f),
57 Self::Open { error, .. } => write!(f, "literal opening failed: {error}"),
58 }
59 }
60}
61impl<T: fmt::Debug, E: Error + 'static> Error for LiteralCursorError<T, E> {
62 fn source(&self) -> Option<&(dyn Error + 'static)> {
63 match self {
64 Self::Storage(error) | Self::Open { error, .. } => Some(error),
65 Self::Traversal(error) => Some(error),
66 }
67 }
68}
69
70pub struct Rdf12LiteralOccurrences<'a, A: LiteralTermAccess, C: TripleCursor> {
78 cursor: Rdf12OccurrenceCursor<'a, A, C>,
79 access: &'a A,
80}
81impl<'a, A, C> Rdf12LiteralOccurrences<'a, A, C>
82where
83 A: LiteralTermAccess,
84 C: TripleCursor<Term = A::Term, Error = A::Error>,
85{
86 pub fn new(roots: C, access: &'a A, role: LiteralRole, limits: OccurrenceLimits) -> Self {
89 Self {
90 cursor: Rdf12OccurrenceCursor::new(roots, access, role, limits),
91 access,
92 }
93 }
94}
95impl<'a, A, C> LiteralOccurrenceCursor for Rdf12LiteralOccurrences<'a, A, C>
96where
97 A: LiteralTermAccess,
98 C: TripleCursor<Term = A::Term, Error = A::Error>,
99{
100 type Term = A::Term;
101 type Error = A::Error;
102 type Reader = A::Reader<'a>;
103 async fn next(&mut self) -> LiteralOccurrenceRead<Self::Term, Self::Reader, Self::Error> {
104 loop {
105 let Some(appearance) = self
106 .cursor
107 .next()
108 .await
109 .map_err(LiteralCursorError::Traversal)?
110 else {
111 return Ok(None);
112 };
113 let reader = self
114 .access
115 .open_literal(&appearance.term)
116 .await
117 .map_err(|error| LiteralCursorError::Open {
118 occurrence: appearance.clone(),
119 error,
120 })?;
121 let Some(reader) = reader else {
122 continue;
123 };
124 return Ok(Some((
125 LiteralOccurrence {
126 literal: appearance.term,
127 root: appearance.root,
128 role: appearance.role,
129 path: appearance.path,
130 },
131 reader,
132 )));
133 }
134 }
135}