inference_engine/reasoning/literal_typing/
engine.rs1use super::{
2 LiteralOccurrenceCursor, LiteralRole, LiteralTypingError, LiteralTypingLimits,
3 LiteralTypingReport,
4};
5use crate::reasoning::{
6 Evidence, Inference, Limit, Rdf12LiteralValidator, ReasoningError, ReasoningStore,
7 plan::LiteralClause,
8};
9use rdf_reasoning::Rdf12LiteralRuleId;
10
11pub struct Rdf12LiteralTyper {
16 limits: LiteralTypingLimits,
17 role: LiteralRole,
18}
19impl Rdf12LiteralTyper {
20 pub const fn new(limits: LiteralTypingLimits, role: LiteralRole) -> Self {
22 Self { limits, role }
23 }
24
25 pub async fn run<S, C>(
41 &self,
42 store: &mut S,
43 cursor: &mut C,
44 ) -> Result<LiteralTypingReport, LiteralTypingError<S::Term, S::Error>>
45 where
46 S: ReasoningStore,
47 C: LiteralOccurrenceCursor<Term = S::Term, Error = S::Error>,
48 {
49 Self::check_cancelled(store)?;
50 let mut clauses = Vec::with_capacity(3);
52 for &id in Rdf12LiteralRuleId::ALL {
53 clauses.push(
54 LiteralClause::compile(id, store)
55 .await
56 .map_err(LiteralTypingError::Reasoning)?,
57 );
58 }
59 let mut report = LiteralTypingReport::default();
60 loop {
61 Self::check_cancelled(store)?;
62 let next = cursor.next().await.map_err(LiteralTypingError::Cursor)?;
63 Self::check_cancelled(store)?;
64 let Some((occurrence, mut reader)) = next else {
65 return Ok(report);
66 };
67 if occurrence.role != self.role {
68 return Err(LiteralTypingError::RoleMismatch {
69 expected: self.role,
70 occurrence,
71 });
72 }
73 if report.occurrences == self.limits.occurrences.get() {
74 return Err(LiteralTypingError::OccurrenceLimit);
75 }
76 report.occurrences += 1;
77 crate::cooperative::checkpoint(report.occurrences).await;
78 Self::check_cancelled(store)?;
79 let inspection = Rdf12LiteralValidator::new(self.limits.bytes - report.bytes)
80 .validate(&mut reader)
81 .await
82 .map_err(|error| LiteralTypingError::Literal {
83 occurrence: occurrence.clone(),
84 error,
85 })?;
86 Self::check_cancelled(store)?;
87 report.bytes += inspection.bytes;
88 let Some(datatype) = inspection.datatype else {
89 report.opaque += 1;
90 continue;
91 };
92 let id = Rdf12LiteralRuleId::GrdfD1(datatype);
93 let clause = clauses
94 .iter()
95 .find(|clause| clause.id == id)
96 .ok_or_else(|| {
97 LiteralTypingError::Reasoning(ReasoningError::UnsupportedLiteralRule(id))
98 })?;
99 let metadata = [occurrence.literal.clone(), clause.datatype.clone()];
100 let bindings = clause
101 .body
102 .bind(&metadata, std::array::from_fn(|_| None))
103 .ok_or_else(|| {
104 LiteralTypingError::Reasoning(ReasoningError::UnsupportedLiteralRule(id))
105 })?;
106 let triple = clause.head.ground(&bindings).ok_or_else(|| {
107 LiteralTypingError::Reasoning(ReasoningError::UnsupportedLiteralRule(id))
108 })?;
109 if store
110 .contains(&triple)
111 .await
112 .map_err(|error| LiteralTypingError::Reasoning(ReasoningError::Storage(error)))?
113 {
114 continue;
115 }
116 Self::check_cancelled(store)?;
117 if report.inferred == self.limits.inferred.get() {
118 return Err(LiteralTypingError::Reasoning(
119 ReasoningError::LimitExceeded(Limit::Inferred),
120 ));
121 }
122 let inference = Inference {
123 triple,
124 evidence: Evidence::Literal {
125 rule: id,
126 occurrence,
127 lexical_bytes: inspection.bytes,
128 },
129 };
130 if store
131 .insert_inferred(inference)
132 .await
133 .map_err(|error| LiteralTypingError::Reasoning(ReasoningError::Storage(error)))?
134 {
135 report.inferred += 1;
136 }
137 }
138 }
139 fn check_cancelled<S: ReasoningStore>(
140 store: &S,
141 ) -> Result<(), LiteralTypingError<S::Term, S::Error>> {
142 if store.is_cancelled() {
143 Err(LiteralTypingError::Reasoning(ReasoningError::Cancelled))
144 } else {
145 Ok(())
146 }
147 }
148}