1use crate::{node_kind::NodeKind, value::Value};
2use iri_s::{IriS, iri};
3use itertools::Itertools;
4use prefixmap::IriRef;
5use rudof_rdf::rdf_core::vocabs::ShaclVocab;
6use rudof_rdf::rdf_core::{
7 BuildRDF,
8 term::{
9 Object,
10 literal::{ConcreteLiteral, Lang},
11 },
12};
13use std::collections::HashSet;
14use std::fmt::Display;
15
16#[derive(Debug, Clone, Eq, PartialEq)]
18pub enum Component {
19 Class(Object),
20 Datatype(IriRef),
21 NodeKind(NodeKind),
22 MinCount(isize),
23 MaxCount(isize),
24 MinExclusive(ConcreteLiteral),
25 MaxExclusive(ConcreteLiteral),
26 MinInclusive(ConcreteLiteral),
27 MaxInclusive(ConcreteLiteral),
28 MinLength(isize),
29 MaxLength(isize),
30 Pattern {
31 pattern: String,
32 flags: Option<String>,
33 },
34 UniqueLang(bool),
35 LanguageIn(Vec<Lang>),
36 Equals(IriRef),
37 Disjoint(IriRef),
38 LessThan(IriRef),
39 LessThanOrEquals(IriRef),
40 Or(Vec<Object>),
41 And(Vec<Object>),
42 Not(Object),
43 Xone(Vec<Object>),
44 Closed {
45 is_closed: bool,
46 ignored_properties: HashSet<IriS>,
47 },
48 Node(Object),
49 HasValue(Value),
50 In(Vec<Value>),
51 QualifiedValueShape {
52 shape: Object,
53 q_min_count: Option<isize>,
54 q_max_count: Option<isize>,
55 disjoint: Option<bool>,
56 siblings: Vec<Object>,
57 },
58 Deactivated(bool), }
60
61impl Component {
62 pub fn write<B: BuildRDF>(&self, rdf_node: &Object, rdf: &mut B) -> Result<(), B::Err> {
63 match self {
64 Self::Class(rdf_node) => {
65 Self::write_term(&rdf_node.clone().into(), ShaclVocab::SH_CLASS, rdf_node, rdf)?;
66 },
67 Self::Datatype(iri) => {
68 Self::write_iri(iri, ShaclVocab::SH_DATATYPE, rdf_node, rdf)?;
69 },
70 Self::NodeKind(node_kind) => {
71 let iri = match &node_kind {
72 NodeKind::Iri => ShaclVocab::SH_IRI,
73
74 _ => unimplemented!(),
75 };
76
77 Self::write_iri(&IriRef::Iri(iri!(iri)), ShaclVocab::SH_DATATYPE, rdf_node, rdf)?;
78 },
79 Self::MinCount(value) => {
80 Self::write_integer(*value, ShaclVocab::SH_MIN_COUNT, rdf_node, rdf)?;
81 },
82 Self::MaxCount(value) => {
83 Self::write_integer(*value, ShaclVocab::SH_MAX_COUNT, rdf_node, rdf)?;
84 },
85 Self::MinExclusive(value) => {
86 Self::write_literal(value, ShaclVocab::SH_MIN_EXCLUSIVE, rdf_node, rdf)?;
87 },
88 Self::MaxExclusive(value) => {
89 Self::write_literal(value, ShaclVocab::SH_MAX_EXCLUSIVE, rdf_node, rdf)?;
90 },
91 Self::MinInclusive(value) => {
92 Self::write_literal(value, ShaclVocab::SH_MIN_INCLUSIVE, rdf_node, rdf)?;
93 },
94 Self::MaxInclusive(value) => {
95 Self::write_literal(value, ShaclVocab::SH_MAX_INCLUSIVE, rdf_node, rdf)?;
96 },
97 Self::MinLength(value) => {
98 Self::write_integer(*value, ShaclVocab::SH_MIN_LENGTH, rdf_node, rdf)?;
99 },
100 Self::MaxLength(value) => {
101 Self::write_integer(*value, ShaclVocab::SH_MAX_LENGTH, rdf_node, rdf)?;
102 },
103 Self::Pattern { pattern, flags } => {
104 Self::write_literal(&ConcreteLiteral::str(pattern), ShaclVocab::SH_PATTERN, rdf_node, rdf)?;
105 if let Some(flags) = flags {
106 Self::write_literal(&ConcreteLiteral::str(flags), ShaclVocab::SH_FLAGS, rdf_node, rdf)?;
107 }
108 },
109 Self::UniqueLang(value) => {
110 Self::write_boolean(*value, ShaclVocab::SH_UNIQUE_LANG, rdf_node, rdf)?;
111 },
112 Self::LanguageIn(langs) => {
113 langs.iter().try_for_each(|lang| {
114 Self::write_literal(
115 &ConcreteLiteral::str(&lang.to_string()),
116 ShaclVocab::SH_LANGUAGE_IN,
117 rdf_node,
118 rdf,
119 )
120 })?;
121 },
122 Self::Equals(iri) => {
123 Self::write_iri(iri, ShaclVocab::SH_EQUALS, rdf_node, rdf)?;
124 },
125 Self::Disjoint(iri) => {
126 Self::write_iri(iri, ShaclVocab::SH_DISJOINT, rdf_node, rdf)?;
127 },
128 Self::LessThan(iri) => {
129 Self::write_iri(iri, ShaclVocab::SH_LESS_THAN, rdf_node, rdf)?;
130 },
131 Self::LessThanOrEquals(iri) => {
132 Self::write_iri(iri, ShaclVocab::SH_LESS_THAN_OR_EQUALS, rdf_node, rdf)?;
133 },
134 Self::Or(shapes) => {
135 shapes
136 .iter()
137 .try_for_each(|shape| Self::write_term(&shape.clone().into(), ShaclVocab::SH_OR, rdf_node, rdf))?;
138 },
139 Self::And(shapes) => {
140 shapes
141 .iter()
142 .try_for_each(|shape| Self::write_term(&shape.clone().into(), ShaclVocab::SH_AND, rdf_node, rdf))?;
143 },
144 Self::Not(shape) => {
145 Self::write_term(&shape.clone().into(), ShaclVocab::SH_PATTERN, rdf_node, rdf)?;
146 },
147 Self::Xone(shapes) => {
148 shapes.iter().try_for_each(|shape| {
149 Self::write_term(&shape.clone().into(), ShaclVocab::SH_XONE, rdf_node, rdf)
150 })?;
151 },
152 Self::Closed {
153 is_closed,
154 ignored_properties,
155 } => {
156 Self::write_boolean(*is_closed, ShaclVocab::SH_CLOSED, rdf_node, rdf)?;
157
158 ignored_properties.iter().try_for_each(|iri| {
159 let iri_ref = IriRef::Iri(iri.clone());
160 Self::write_iri(&iri_ref, ShaclVocab::SH_IGNORED_PROPERTIES, rdf_node, rdf)
161 })?;
162 },
163 Self::Node(shape) => {
164 Self::write_term(&shape.clone().into(), ShaclVocab::SH_NODE, rdf_node, rdf)?;
165 },
166 Self::HasValue(value) => match value {
167 Value::Iri(iri) => {
168 Self::write_iri(iri, ShaclVocab::SH_HAS_VALUE, rdf_node, rdf)?;
169 },
170 Value::Literal(literal) => {
171 Self::write_literal(
172 &ConcreteLiteral::str(&literal.to_string()),
173 ShaclVocab::SH_HAS_VALUE,
174 rdf_node,
175 rdf,
176 )?;
177 },
178 },
179 Self::In(values) => {
180 values.iter().try_for_each(|value| match value {
182 Value::Iri(iri) => Self::write_iri(iri, ShaclVocab::SH_IN, rdf_node, rdf),
183 Value::Literal(literal) => Self::write_literal(
184 &ConcreteLiteral::str(&literal.to_string()),
185 ShaclVocab::SH_IN,
186 rdf_node,
187 rdf,
188 ),
189 })?;
190 },
191 Self::Deactivated(value) => {
192 Self::write_boolean(*value, ShaclVocab::SH_DEACTIVATED, rdf_node, rdf)?;
193 },
200 Self::QualifiedValueShape {
201 shape,
202 q_min_count,
203 q_max_count,
204 disjoint,
205 ..
206 } => {
207 Self::write_term(
208 &shape.clone().into(),
209 ShaclVocab::SH_QUALIFIED_VALUE_SHAPE,
210 rdf_node,
211 rdf,
212 )?;
213
214 if let Some(value) = q_min_count {
215 Self::write_integer(*value, ShaclVocab::SH_QUALIFIED_MIN_COUNT, rdf_node, rdf)?;
216 }
217
218 if let Some(value) = q_max_count {
219 Self::write_integer(*value, ShaclVocab::SH_QUALIFIED_MAX_COUNT, rdf_node, rdf)?;
220 }
221
222 if let Some(value) = disjoint {
223 Self::write_boolean(*value, ShaclVocab::SH_QUALIFIED_MAX_COUNT, rdf_node, rdf)?;
224 }
225 },
226 }
227 Ok(())
228 }
229
230 fn write_integer<B: BuildRDF>(value: isize, predicate: &str, rdf_node: &Object, rdf: &mut B) -> Result<(), B::Err> {
231 let value: i128 = value.try_into().unwrap();
232 let literal: B::Literal = value.into();
233 Self::write_term(&literal.into(), predicate, rdf_node, rdf)
234 }
235
236 fn write_boolean<B: BuildRDF>(value: bool, predicate: &str, rdf_node: &Object, rdf: &mut B) -> Result<(), B::Err> {
237 let literal: B::Literal = value.into();
238 Self::write_term(&literal.into(), predicate, rdf_node, rdf)
239 }
240
241 fn write_literal<B: BuildRDF>(
242 value: &ConcreteLiteral,
243 predicate: &str,
244 rdf_node: &Object,
245 rdf: &mut B,
246 ) -> Result<(), B::Err> {
247 let literal: B::Literal = value.lexical_form().into();
248 Self::write_term(&literal.into(), predicate, rdf_node, rdf)
249 }
250
251 fn write_iri<B: BuildRDF>(value: &IriRef, predicate: &str, rdf_node: &Object, rdf: &mut B) -> Result<(), B::Err> {
252 Self::write_term(&value.get_iri().unwrap().clone().into(), predicate, rdf_node, rdf)
253 }
254
255 fn write_term<B: BuildRDF>(value: &B::Term, predicate: &str, rdf_node: &Object, rdf: &mut B) -> Result<(), B::Err> {
256 let node: B::Subject = rdf_node.clone().try_into().map_err(|_| unreachable!())?;
257 rdf.add_triple(node, iri!(predicate), value.clone())
258 }
259
260 pub fn closed(is_closed: bool, ignored_properties: HashSet<IriS>) -> Self {
261 Component::Closed {
262 is_closed,
263 ignored_properties,
264 }
265 }
266}
267
268impl Display for Component {
269 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
270 match self {
271 Component::Class(cls) => write!(f, "class({cls})"),
272 Component::Datatype(dt) => write!(f, "datatype({dt})"),
273 Component::NodeKind(nk) => write!(f, "nodeKind({nk})"),
274 Component::MinCount(mc) => write!(f, "minCount({mc})"),
275 Component::MaxCount(mc) => write!(f, "maxCount({mc})"),
276 Component::MinExclusive(me) => write!(f, "minExclusive({me})"),
277 Component::MaxExclusive(me) => write!(f, "maxExclusive({me})"),
278 Component::MinInclusive(mi) => write!(f, "minInclusive({mi})"),
279 Component::MaxInclusive(mi) => write!(f, "maxInclusive({mi})"),
280 Component::MinLength(ml) => write!(f, "minLength({ml})"),
281 Component::MaxLength(ml) => write!(f, "maxLength({ml})"),
282 Component::Pattern { pattern, flags } => match flags {
283 Some(flags) => write!(f, "pattern({pattern}, {flags})"),
284 None => write!(f, "pattern({pattern})"),
285 },
286 Component::UniqueLang(ul) => write!(f, "uniqueLang({ul})"),
287 Component::LanguageIn { .. } => todo!(),
288 Component::Equals(e) => write!(f, "equals({e})"),
289 Component::Disjoint(d) => write!(f, "disjoint({d})"),
290 Component::LessThan(lt) => write!(f, "lessThan({lt})"),
291 Component::LessThanOrEquals(lte) => write!(f, "lessThanOrEquals({lte})"),
292 Component::Or(shapes) => {
293 let str = shapes.iter().map(|s| s.to_string()).join(" ");
294 write!(f, "or [{str}]")
295 },
296 Component::And(shapes) => {
297 let str = shapes.iter().map(|s| s.to_string()).join(" ");
298 write!(f, "and [{str}]")
299 },
300 Component::Not(shape) => {
301 write!(f, "not [{shape}]")
302 },
303 Component::Xone(shapes) => {
304 let str = shapes.iter().map(|s| s.to_string()).join(" ");
305 write!(f, "xone [{str}]")
306 },
307 Component::Closed {
308 is_closed,
309 ignored_properties,
310 } => {
311 write!(
312 f,
313 "closed({is_closed}{})",
314 if ignored_properties.is_empty() {
315 "".to_string()
316 } else {
317 format!(
318 ", Ignored props: [{}]",
319 ignored_properties.iter().map(|p| p.to_string()).join(", ")
320 )
321 }
322 )
323 },
324 Component::Node(shape) => write!(f, "node({shape})"),
325 Component::HasValue(value) => write!(f, "hasValue({value})"),
326 Component::In(values) => {
327 let str = values.iter().map(|v| v.to_string()).join(" ");
328 write!(f, "In [{str}]")
329 },
330 Component::QualifiedValueShape {
331 shape,
332 q_max_count,
333 q_min_count,
334 disjoint,
335 siblings,
336 } => write!(
337 f,
338 "QualifiedValueShape(shape: {shape}, qualified_min_count: {q_min_count:?}, qualified_max_count: {q_max_count:?}, qualified_value_shapes_disjoint: {disjoint:?}{})",
339 if siblings.is_empty() {
340 "".to_string()
341 } else {
342 format!(", siblings: [{}]", siblings.iter().map(|s| s.to_string()).join(", "))
343 }
344 ),
345 Component::Deactivated(b) => write!(f, "deactivated({b})"),
346 }
347 }
348}
349
350impl From<Component> for IriS {
351 fn from(value: Component) -> Self {
352 match value {
353 Component::Class(_) => ShaclVocab::sh_class().clone(),
354 Component::Datatype(_) => ShaclVocab::sh_datatype().clone(),
355 Component::NodeKind(_) => ShaclVocab::sh_iri().clone(),
356 Component::MinCount(_) => ShaclVocab::sh_min_count().clone(),
357 Component::MaxCount(_) => ShaclVocab::sh_max_count().clone(),
358 Component::MinExclusive(_) => ShaclVocab::sh_min_exclusive().clone(),
359 Component::MaxExclusive(_) => ShaclVocab::sh_max_exclusive().clone(),
360 Component::MinInclusive(_) => ShaclVocab::sh_min_inclusive().clone(),
361 Component::MaxInclusive(_) => ShaclVocab::sh_max_inclusive().clone(),
362 Component::MinLength(_) => ShaclVocab::sh_min_length().clone(),
363 Component::MaxLength(_) => ShaclVocab::sh_max_length().clone(),
364 Component::Pattern { .. } => ShaclVocab::sh_pattern().clone(),
365 Component::UniqueLang(_) => ShaclVocab::sh_unique_lang().clone(),
366 Component::LanguageIn { .. } => ShaclVocab::sh_language_in().clone(),
367 Component::Equals(_) => ShaclVocab::sh_equals().clone(),
368 Component::Disjoint(_) => ShaclVocab::sh_disjoint().clone(),
369 Component::LessThan(_) => ShaclVocab::sh_less_than().clone(),
370 Component::LessThanOrEquals(_) => ShaclVocab::sh_less_than_or_equals().clone(),
371 Component::Or { .. } => ShaclVocab::sh_or().clone(),
372 Component::And { .. } => ShaclVocab::sh_and().clone(),
373 Component::Not { .. } => ShaclVocab::sh_not().clone(),
374 Component::Xone { .. } => ShaclVocab::sh_xone().clone(),
375 Component::Closed { .. } => ShaclVocab::sh_closed().clone(),
376 Component::Node { .. } => ShaclVocab::sh_node().clone(),
377 Component::HasValue { .. } => ShaclVocab::sh_has_value().clone(),
378 Component::In { .. } => ShaclVocab::sh_in().clone(),
379 Component::QualifiedValueShape { .. } => ShaclVocab::sh_qualified_value_shape().clone(),
380 Component::Deactivated(_) => ShaclVocab::sh_deactivated().clone(),
381 }
382 }
383}
384
385