Skip to main content

shacl_ast/ast/
property_shape.rs

1use std::collections::HashSet;
2use std::fmt::Display;
3
4use crate::reifier_info::ReifierInfo;
5use crate::{component::Component, message_map::MessageMap, severity::Severity, target::Target};
6use iri_s::IriS;
7use rudof_rdf::rdf_core::vocabs::ShaclVocab;
8use rudof_rdf::rdf_core::{
9    BuildRDF, Rdf, SHACLPath,
10    term::{Object, literal::NumericLiteral},
11};
12
13#[derive(Debug)]
14pub struct PropertyShape<RDF: Rdf> {
15    id: Object,
16    path: SHACLPath,
17    components: Vec<Component>,
18    targets: Vec<Target<RDF>>,
19    property_shapes: Vec<Object>,
20    reifier_info: Option<ReifierInfo>,
21    closed: bool,
22    // ignored_properties: Vec<IriRef>,
23    deactivated: bool,
24    // message: MessageMap,
25    severity: Option<Severity>,
26    name: MessageMap,
27    description: MessageMap,
28    order: Option<NumericLiteral>,
29    group: Option<Object>,
30    // source_iri: Option<IriRef>,
31    // annotations: Vec<(IriRef, RDFNode)>,
32
33    // TODO - For NodeExpr, do not delete
34    // default_value: Option<NodeExpr<RDF>>, // ONLY WHEN PATH IS PREDICATE PATH
35    // values: Option<NodeExpr<RDF>>,        // ONLY WHEN PATH IS PREDICATE PATH
36}
37
38impl<RDF: Rdf> PropertyShape<RDF> {
39    pub fn new(id: Object, path: SHACLPath) -> Self {
40        PropertyShape {
41            id,
42            path,
43            components: Vec::new(),
44            targets: Vec::new(),
45            property_shapes: Vec::new(),
46            closed: false,
47            // ignored_properties: Vec::new(),
48            deactivated: false,
49            // message: MessageMap::new(),
50            severity: None,
51            name: MessageMap::new(),
52            description: MessageMap::new(),
53            order: None,
54            group: None,
55            reifier_info: None, // source_iri: None,
56                                // annotations: Vec::new()
57
58                                // TODO - For NodeExpr, do not delete
59                                // default_value: None,
60                                // values: None,
61        }
62    }
63
64    pub fn with_name(mut self, name: MessageMap) -> Self {
65        self.name = name;
66        self
67    }
68    pub fn with_description(mut self, description: MessageMap) -> Self {
69        self.description = description;
70        self
71    }
72
73    pub fn with_order(mut self, order: Option<NumericLiteral>) -> Self {
74        self.order = order;
75        self
76    }
77
78    pub fn with_group(mut self, group: Option<Object>) -> Self {
79        self.group = group;
80        self
81    }
82
83    pub fn reifier_info(&self) -> &Option<ReifierInfo> {
84        &self.reifier_info
85    }
86
87    pub fn with_reifier_shape(mut self, reifier_info: Option<ReifierInfo>) -> Self {
88        self.reifier_info = reifier_info;
89        self
90    }
91
92    pub fn with_severity_option(mut self, severity: Option<Severity>) -> Self {
93        self.severity = severity;
94        self
95    }
96
97    pub fn closed_component(&self) -> (bool, HashSet<IriS>) {
98        for component in &self.components {
99            if let Component::Closed {
100                is_closed,
101                ignored_properties,
102            } = component
103            {
104                return (*is_closed, ignored_properties.clone());
105            }
106        }
107        (false, HashSet::new())
108    }
109
110    pub fn with_targets(mut self, targets: Vec<Target<RDF>>) -> Self {
111        self.targets = targets;
112        self
113    }
114
115    pub fn with_property_shapes(mut self, property_shapes: Vec<Object>) -> Self {
116        self.property_shapes = property_shapes;
117        self
118    }
119
120    pub fn with_components(mut self, components: Vec<Component>) -> Self {
121        self.components = components;
122        self
123    }
124
125    pub fn with_closed(mut self, closed: bool) -> Self {
126        self.closed = closed;
127        self
128    }
129
130    pub fn with_severity(mut self, severity: Option<Severity>) -> Self {
131        self.severity = severity;
132        self
133    }
134
135    // TODO - For NodeExpr, do not delete
136    // pub fn with_values(mut self, values: Option<NodeExpr<RDF>>) -> Self {
137    //     self.values = values;
138    //     self
139    // }
140    //
141    // pub fn with_default_value(mut self, default_value: Option<NodeExpr<RDF>>) -> Self {
142    //     self.default_value = default_value;
143    //     self
144    // }
145
146    pub fn id(&self) -> &Object {
147        &self.id
148    }
149
150    pub fn path(&self) -> &SHACLPath {
151        &self.path
152    }
153
154    pub fn name(&self) -> &MessageMap {
155        &self.name
156    }
157
158    pub fn description(&self) -> &MessageMap {
159        &self.description
160    }
161
162    pub fn is_closed(&self) -> &bool {
163        &self.closed
164    }
165
166    pub fn is_deactivated(&self) -> &bool {
167        &self.deactivated
168    }
169
170    pub fn severity(&self) -> Option<Severity> {
171        self.severity.to_owned()
172    }
173
174    pub fn components(&self) -> &Vec<Component> {
175        &self.components
176    }
177
178    pub fn targets(&self) -> &Vec<Target<RDF>> {
179        &self.targets
180    }
181
182    pub fn property_shapes(&self) -> &Vec<Object> {
183        &self.property_shapes
184    }
185
186    // TODO - For NodeExpr, do not delete
187    // pub fn values(&self) -> Option<&NodeExpr<RDF>> {
188    //     self.values.as_ref()
189    // }
190    //
191    // pub fn default_value(&self) -> Option<&NodeExpr<RDF>> {
192    //     self.default_value.as_ref()
193    // }
194
195    // TODO: this is a bit ugly
196    pub fn write<B: BuildRDF>(&self, rdf: &mut B) -> Result<(), B::Err> {
197        let id: B::Subject = self.id.clone().try_into().map_err(|_| unreachable!())?;
198        rdf.add_type(id.clone(), ShaclVocab::sh_property_shape().clone())?;
199
200        self.name.iter().try_for_each(|(lang, value)| {
201            let literal: B::Literal = match lang {
202                Some(_) => todo!(),
203                None => value.clone().into(),
204            };
205            rdf.add_triple(id.clone(), ShaclVocab::sh_name().clone(), literal)
206        })?;
207
208        self.description.iter().try_for_each(|(lang, value)| {
209            let literal: B::Literal = match lang {
210                Some(_) => todo!(),
211                None => value.clone().into(),
212            };
213            rdf.add_triple(id.clone(), ShaclVocab::sh_description().clone(), literal)
214        })?;
215
216        if let Some(order) = self.order.clone() {
217            let literal: B::Literal = match order {
218                NumericLiteral::Decimal(_) => todo!(),
219                NumericLiteral::Double(float) => float.into(),
220                NumericLiteral::Float(float) => float.to_string().into(),
221                #[allow(clippy::useless_conversion)]
222                NumericLiteral::Integer(int) => {
223                    let i: i128 = int.try_into().unwrap();
224                    i.into()
225                },
226                NumericLiteral::Long(_) => todo!(),
227                NumericLiteral::Byte(_) => todo!(),
228                NumericLiteral::Short(_) => todo!(),
229                NumericLiteral::NonNegativeInteger(_) => todo!(),
230                NumericLiteral::UnsignedLong(_) => todo!(),
231                NumericLiteral::UnsignedInt(_) => todo!(),
232                NumericLiteral::UnsignedShort(_) => todo!(),
233                NumericLiteral::UnsignedByte(_) => todo!(),
234                NumericLiteral::PositiveInteger(_) => todo!(),
235                NumericLiteral::NegativeInteger(_) => todo!(),
236                NumericLiteral::NonPositiveInteger(_) => todo!(),
237            };
238            rdf.add_triple(id.clone(), ShaclVocab::sh_order().clone(), literal)?;
239        }
240
241        if let Some(group) = &self.group {
242            rdf.add_triple(id.clone(), ShaclVocab::sh_group().clone(), group.clone())?;
243        }
244
245        if let SHACLPath::Predicate { pred } = &self.path {
246            rdf.add_triple(id.clone(), ShaclVocab::sh_path().clone(), pred.clone())?;
247        } else {
248            unimplemented!()
249        }
250
251        self.components
252            .iter()
253            .try_for_each(|component| component.write(&self.id, rdf))?;
254
255        self.targets.iter().try_for_each(|target| target.write(&self.id, rdf))?;
256
257        if self.deactivated {
258            let literal: B::Literal = "true".to_string().into();
259
260            rdf.add_triple(id.clone(), ShaclVocab::sh_deactivated().clone(), literal)?;
261        }
262
263        if let Some(severity) = &self.severity {
264            let pred = match severity {
265                Severity::Trace => ShaclVocab::sh_trace(),
266                Severity::Debug => ShaclVocab::sh_debug(),
267                Severity::Violation => ShaclVocab::sh_violation(),
268                Severity::Info => ShaclVocab::sh_info(),
269                Severity::Warning => ShaclVocab::sh_warning(),
270                Severity::Generic(iri) => iri.get_iri().unwrap(),
271            };
272
273            rdf.add_triple(id.clone(), ShaclVocab::sh_severity().clone(), pred.clone())?;
274        }
275
276        Ok(())
277    }
278}
279
280impl<RDF: Rdf> Display for PropertyShape<RDF> {
281    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
282        if let Some(severity) = self.severity() {
283            write!(f, "{severity} ")?;
284        }
285        writeln!(f, "{{")?;
286        writeln!(f, "       PropertyShape")?;
287        writeln!(f, "       path: {}", self.path)?;
288        for target in self.targets.iter() {
289            writeln!(f, "       {target}")?
290        }
291        if self.closed {
292            writeln!(f, "       closed: {}", self.closed)?
293        }
294        for property in self.property_shapes.iter() {
295            writeln!(f, "       Property {property}")?
296        }
297        for reifier in self.reifier_info.iter() {
298            writeln!(f, "       ReifierInfo {reifier}")?
299        }
300        for component in self.components.iter() {
301            writeln!(f, "       {component}")?
302        }
303        // TODO - For NodeExpr, do not delete
304        // if let Some(v) = &self.default_value {
305        //     writeln!(f, "       default_value: {}", v)?;
306        // }
307        // if let Some(v) = &self.values {
308        //     writeln!(f, "       values: {}", v)?;
309        // }
310        write!(f, "}}")
311    }
312}
313
314impl<RDF: Rdf> Clone for PropertyShape<RDF> {
315    fn clone(&self) -> Self {
316        Self {
317            id: self.id.clone(),
318            path: self.path.clone(),
319            components: self.components.clone(),
320            targets: self.targets.clone(),
321            property_shapes: self.property_shapes.clone(),
322            closed: self.closed,
323            deactivated: self.deactivated,
324            severity: self.severity.clone(),
325            name: self.name.clone(),
326            description: self.description.clone(),
327            order: self.order.clone(),
328            group: self.group.clone(),
329            reifier_info: self.reifier_info.clone(),
330            // TODO - For NodeExpr, do not delete
331            // default_value: self.default_value.clone(),
332            // values: self.values.clone(),
333        }
334    }
335}
336
337impl<RDF: Rdf> PartialEq for PropertyShape<RDF> {
338    fn eq(&self, other: &Self) -> bool {
339        self.id == other.id
340            && self.path == other.path
341            && self.components == other.components
342            && self.targets == other.targets
343            && self.property_shapes == other.property_shapes
344            && self.closed == other.closed
345            && self.deactivated == other.deactivated
346            && self.severity == other.severity
347            && self.name == other.name
348            && self.description == other.description
349            && self.order == other.order
350            && self.group == other.group
351
352        // TODO - For NodeExpr, do not delete
353        // && self.default_value == other.default_value
354        // && self.values == other.values
355    }
356}