shacl_ast/ast/
node_shape.rs1use crate::{component::Component, message_map::MessageMap, severity::Severity, target::Target};
2use iri_s::IriS;
3use rudof_rdf::rdf_core::vocabs::ShaclVocab;
4use rudof_rdf::rdf_core::{BuildRDF, Rdf, term::Object};
5use std::collections::HashSet;
6use std::fmt::Display;
7
8#[derive(Debug)]
9pub struct NodeShape<RDF: Rdf>
10where
11 RDF::Term: Clone,
12{
13 id: Object,
14 components: Vec<Component>,
15 targets: Vec<Target<RDF>>,
16 property_shapes: Vec<Object>,
17 severity: Option<Severity>,
21 name: MessageMap,
22 description: MessageMap,
23 group: Option<Object>,
24 }
26
27impl<RDF: Rdf> NodeShape<RDF> {
28 pub fn new(id: Object) -> Self {
29 NodeShape {
30 id,
31 components: Vec::new(),
32 targets: Vec::new(),
33 property_shapes: Vec::new(),
34 severity: None,
38 name: MessageMap::new(),
39 description: MessageMap::new(),
40 group: None,
41 }
43 }
44
45 pub fn with_targets(mut self, targets: Vec<Target<RDF>>) -> Self {
46 self.targets = targets;
47 self
48 }
49
50 pub fn set_targets(&mut self, targets: Vec<Target<RDF>>) {
51 self.targets = targets;
52 }
53
54 pub fn with_severity(mut self, severity: Option<Severity>) -> Self {
55 self.severity = severity;
56 self
57 }
58
59 pub fn with_property_shapes(mut self, property_shapes: Vec<Object>) -> Self {
60 self.property_shapes = property_shapes;
61 self
62 }
63
64 pub fn with_components(mut self, components: Vec<Component>) -> Self {
65 self.components = components;
66 self
67 }
68
69 pub fn id(&self) -> &Object {
70 &self.id
71 }
72
73 pub fn is_deactivated(&self) -> bool {
74 for component in &self.components {
75 if let Component::Deactivated(true) = component {
78 return true;
79 }
80 }
81 false
82 }
83
84 pub fn closed_component(&self) -> (bool, HashSet<IriS>) {
85 for component in &self.components {
86 if let Component::Closed {
87 is_closed,
88 ignored_properties,
89 } = component
90 {
91 return (*is_closed, ignored_properties.clone());
92 }
93 }
94 (false, HashSet::new())
95 }
96
97 pub fn severity(&self) -> Option<Severity> {
98 self.severity.to_owned()
99 }
100
101 pub fn components(&self) -> &Vec<Component> {
102 &self.components
103 }
104
105 pub fn targets(&self) -> &Vec<Target<RDF>> {
106 &self.targets
107 }
108
109 pub fn property_shapes(&self) -> &Vec<Object> {
110 &self.property_shapes
111 }
112
113 pub fn write<B: BuildRDF>(&self, rdf: &mut B) -> Result<(), B::Err> {
115 let id: B::Subject = self.id.clone().try_into().map_err(|_| unreachable!())?;
116 rdf.add_type(id.clone(), ShaclVocab::sh_node_shape().clone())?;
117
118 self.name.iter().try_for_each(|(lang, value)| {
119 let literal: B::Literal = match lang {
120 Some(_) => todo!(),
121 None => value.clone().into(),
122 };
123 rdf.add_triple(id.clone(), ShaclVocab::sh_name().clone(), literal)
124 })?;
125
126 self.description.iter().try_for_each(|(lang, value)| {
127 let literal: B::Literal = match lang {
128 Some(_) => todo!(),
129 None => value.clone().into(),
130 };
131 rdf.add_triple(id.clone(), ShaclVocab::sh_description().clone(), literal)
132 })?;
133
134 self.components
135 .iter()
136 .try_for_each(|component| component.write(&self.id, rdf))?;
137
138 self.targets.iter().try_for_each(|target| target.write(&self.id, rdf))?;
139
140 self.property_shapes.iter().try_for_each(|property_shape| {
141 rdf.add_triple(id.clone(), ShaclVocab::sh_property().clone(), property_shape.clone())
142 })?;
143
144 if let Some(group) = &self.group {
145 rdf.add_triple(id.clone(), ShaclVocab::sh_group().clone(), group.clone())?;
146 }
147
148 if let Some(severity) = &self.severity {
149 let pred = match severity {
150 Severity::Trace => ShaclVocab::sh_trace(),
151 Severity::Debug => ShaclVocab::sh_debug(),
152 Severity::Violation => ShaclVocab::sh_violation(),
153 Severity::Info => ShaclVocab::sh_info(),
154 Severity::Warning => ShaclVocab::sh_warning(),
155 Severity::Generic(iri) => iri.get_iri().unwrap(),
156 };
157
158 rdf.add_triple(id.clone(), ShaclVocab::sh_severity().clone(), pred.clone())?;
159 }
160
161 Ok(())
162 }
163}
164
165impl<RDF: Rdf> Display for NodeShape<RDF> {
166 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167 if let Some(severity) = self.severity() {
168 write!(f, "{severity} ")?;
169 }
170 writeln!(f, "{{")?;
171 for target in self.targets.iter() {
172 writeln!(f, " {target}")?
173 }
174 for property in self.property_shapes.iter() {
175 writeln!(f, " Property {property}")?
176 }
177 for component in self.components.iter() {
178 writeln!(f, " {component}")?
179 }
180 write!(f, "}}")?;
181 Ok(())
182 }
183}
184
185impl<RDF: Rdf> Clone for NodeShape<RDF> {
186 fn clone(&self) -> Self {
187 Self {
188 id: self.id.clone(),
189 components: self.components.clone(),
190 targets: self.targets.clone(),
191 property_shapes: self.property_shapes.clone(),
192 severity: self.severity.clone(),
193 name: self.name.clone(),
194 description: self.description.clone(),
195 group: self.group.clone(),
196 }
197 }
198}
199
200impl<RDF: Rdf> PartialEq for NodeShape<RDF> {
201 fn eq(&self, other: &Self) -> bool {
202 self.id == other.id
203 && self.components == other.components
204 && self.targets == other.targets
205 && self.property_shapes == other.property_shapes
206 && self.severity == other.severity
207 && self.name == other.name
208 && self.description == other.description
209 && self.group == other.group
210 }
211}