inference_engine/reasoning/
seeding.rs1use super::{
2 AxiomFamily, Evidence, Inference, Limit, Rdf12WitnessStore, ReasoningError, ReasoningStore,
3};
4use datalog_structural_model::{Rule, Term};
5use rdf_reasoning::{
6 Rdf12AxiomId, Rdf12ContainerProperty, Rdf12ExistentialAxiomId, TRIPLE_RELATION,
7};
8use std::num::NonZeroU64;
9
10pub struct Rdf12AxiomSeeder {
21 maximum: NonZeroU64,
22 inferred: u64,
23}
24
25impl Rdf12AxiomSeeder {
26 pub const fn new(maximum: NonZeroU64) -> Self {
28 Self {
29 maximum,
30 inferred: 0,
31 }
32 }
33
34 pub const fn inferred(&self) -> u64 {
36 self.inferred
37 }
38
39 pub async fn seed_family<S: ReasoningStore>(
45 &mut self,
46 store: &mut S,
47 family: Rdf12AxiomId,
48 ) -> Result<(), ReasoningError<S::Error>> {
49 Self::check_cancelled(store)?;
50 for rule in family.definitions() {
52 self.seed_rule(store, &rule, AxiomFamily::Rdf12(family))
53 .await?;
54 }
55 Self::check_cancelled(store)
56 }
57
58 pub async fn seed_container<S: ReasoningStore, I: AsRef<str>>(
68 &mut self,
69 store: &mut S,
70 container: &Rdf12ContainerProperty<I>,
71 ) -> Result<(), ReasoningError<S::Error>> {
72 Self::check_cancelled(store)?;
73 self.seed_rule(
74 store,
75 &container.rdf_definition(),
76 AxiomFamily::Rdf12Container,
77 )
78 .await?;
79 for rule in container.rdfs_definitions() {
80 self.seed_rule(store, &rule, AxiomFamily::Rdf12Container)
81 .await?;
82 }
83 Self::check_cancelled(store)
84 }
85
86 pub async fn seed_existential<S: Rdf12WitnessStore>(
97 &mut self,
98 store: &mut S,
99 id: Rdf12ExistentialAxiomId,
100 ) -> Result<(), ReasoningError<S::Error>> {
101 Self::check_cancelled(store)?;
102 let blank = store
103 .resolve_existential_blank(id)
104 .await
105 .map_err(ReasoningError::Storage)?;
106 let predicate = store
107 .resolve_iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
108 .await
109 .map_err(ReasoningError::Storage)?;
110 let class = store
111 .resolve_iri(id.class_iri())
112 .await
113 .map_err(ReasoningError::Storage)?;
114 self.stage(
115 store,
116 [blank, predicate, class],
117 AxiomFamily::Rdf12Existential(id),
118 )
119 .await
120 }
121
122 async fn seed_rule<S: ReasoningStore>(
123 &mut self,
124 store: &mut S,
125 rule: &Rule,
126 family: AxiomFamily,
127 ) -> Result<(), ReasoningError<S::Error>> {
128 Self::check_cancelled(store)?;
129 let head = rule
130 .head
131 .as_atom()
132 .ok_or(ReasoningError::UnsupportedAxiom)?;
133 let [Term::Constant(s), Term::Constant(p), Term::Constant(o)] = head.terms.as_slice()
134 else {
135 return Err(ReasoningError::UnsupportedAxiom);
136 };
137 if !rule.body.is_empty() || head.predicate.as_str() != TRIPLE_RELATION {
138 return Err(ReasoningError::UnsupportedAxiom);
139 }
140 let triple = [
141 store
142 .resolve_iri(s.as_str())
143 .await
144 .map_err(ReasoningError::Storage)?,
145 store
146 .resolve_iri(p.as_str())
147 .await
148 .map_err(ReasoningError::Storage)?,
149 store
150 .resolve_iri(o.as_str())
151 .await
152 .map_err(ReasoningError::Storage)?,
153 ];
154 self.stage(store, triple, family).await
155 }
156
157 async fn stage<S: ReasoningStore>(
158 &mut self,
159 store: &mut S,
160 triple: [S::Term; 3],
161 family: AxiomFamily,
162 ) -> Result<(), ReasoningError<S::Error>> {
163 Self::check_cancelled(store)?;
164 if store
165 .contains(&triple)
166 .await
167 .map_err(ReasoningError::Storage)?
168 {
169 return Self::check_cancelled(store);
170 }
171 Self::check_cancelled(store)?;
172 if self.inferred == self.maximum.get() {
173 return Err(ReasoningError::LimitExceeded(Limit::Inferred));
174 }
175 if store
176 .insert_inferred(Inference {
177 triple,
178 evidence: Evidence::Axiom(family),
179 })
180 .await
181 .map_err(ReasoningError::Storage)?
182 {
183 self.inferred += 1;
184 }
185 Self::check_cancelled(store)
186 }
187
188 fn check_cancelled<S: ReasoningStore>(store: &S) -> Result<(), ReasoningError<S::Error>> {
189 if store.is_cancelled() {
190 Err(ReasoningError::Cancelled)
191 } else {
192 Ok(())
193 }
194 }
195}