shacl_ast/compiled/
property_shape.rs

1use std::collections::HashSet;
2
3use srdf::Rdf;
4use srdf::SHACLPath;
5
6use crate::property_shape::PropertyShape;
7use crate::Schema;
8
9use super::compile_shape;
10use super::compiled_shacl_error::CompiledShaclError;
11use super::component::CompiledComponent;
12use super::severity::CompiledSeverity;
13use super::shape::CompiledShape;
14use super::target::CompiledTarget;
15
16#[derive(Debug)]
17pub struct CompiledPropertyShape<S: Rdf> {
18    id: S::Term,
19    path: SHACLPath,
20    components: Vec<CompiledComponent<S>>,
21    targets: Vec<CompiledTarget<S>>,
22    property_shapes: Vec<CompiledShape<S>>,
23    closed: bool,
24    // ignored_properties: Vec<S::IRI>,
25    deactivated: bool,
26    // message: MessageMap,
27    severity: Option<CompiledSeverity<S>>,
28    // name: MessageMap,
29    // description: MessageMap,
30    // order: Option<NumericLiteral>,
31    // group: Option<S::Term>,
32    // source_iri: Option<S::IRI>,
33    // annotations: Vec<(S::IRI, S::Term)>,
34}
35
36impl<S: Rdf> CompiledPropertyShape<S> {
37    #[allow(clippy::too_many_arguments)]
38    pub fn new(
39        id: S::Term,
40        path: SHACLPath,
41        components: Vec<CompiledComponent<S>>,
42        targets: Vec<CompiledTarget<S>>,
43        property_shapes: Vec<CompiledShape<S>>,
44        closed: bool,
45        deactivated: bool,
46        severity: Option<CompiledSeverity<S>>,
47    ) -> Self {
48        CompiledPropertyShape {
49            id,
50            path,
51            components,
52            targets,
53            property_shapes,
54            closed,
55            deactivated,
56            severity,
57        }
58    }
59
60    pub fn id(&self) -> &S::Term {
61        &self.id
62    }
63
64    pub fn is_closed(&self) -> &bool {
65        &self.closed
66    }
67
68    pub fn path(&self) -> &SHACLPath {
69        &self.path
70    }
71
72    pub fn is_deactivated(&self) -> &bool {
73        &self.deactivated
74    }
75
76    pub fn severity(&self) -> &CompiledSeverity<S> {
77        match &self.severity {
78            Some(severity) => severity,
79            None => &CompiledSeverity::Violation,
80        }
81    }
82
83    pub fn components(&self) -> &Vec<CompiledComponent<S>> {
84        &self.components
85    }
86
87    pub fn targets(&self) -> &Vec<CompiledTarget<S>> {
88        &self.targets
89    }
90
91    pub fn property_shapes(&self) -> &Vec<CompiledShape<S>> {
92        &self.property_shapes
93    }
94}
95
96impl<S: Rdf> CompiledPropertyShape<S> {
97    pub fn compile(shape: PropertyShape, schema: &Schema) -> Result<Self, CompiledShaclError> {
98        let id = shape.id().clone().into();
99        let path = shape.path().to_owned();
100        let closed = shape.is_closed().to_owned();
101        let deactivated = shape.is_deactivated().to_owned();
102        let severity = CompiledSeverity::compile(shape.severity())?;
103
104        let components = shape.components().iter().collect::<HashSet<_>>();
105        let mut compiled_components = Vec::new();
106        for component in components {
107            let component = CompiledComponent::compile(component.to_owned(), schema)?;
108            compiled_components.push(component);
109        }
110
111        let mut targets = Vec::new();
112        for target in shape.targets() {
113            let ans = CompiledTarget::compile(target.to_owned())?;
114            targets.push(ans);
115        }
116
117        let mut property_shapes = Vec::new();
118        for property_shape in shape.property_shapes() {
119            let shape = compile_shape(property_shape.to_owned(), schema)?;
120            property_shapes.push(shape);
121        }
122
123        let compiled_property_shape = CompiledPropertyShape::new(
124            id,
125            path,
126            compiled_components,
127            targets,
128            property_shapes,
129            closed,
130            deactivated,
131            severity,
132        );
133
134        Ok(compiled_property_shape)
135    }
136}