shacl_ir/compiled/
property_shape.rs1use super::compile_shape;
2use super::compiled_shacl_error::CompiledShaclError;
3use super::component_ir::ComponentIR;
4use super::severity::CompiledSeverity;
5use super::target::CompiledTarget;
6use crate::closed_info::ClosedInfo;
7use crate::dependency_graph::DependencyGraph;
8use crate::dependency_graph::PosNeg;
9use crate::reifier_info;
10use crate::reifier_info::ReifierInfo;
11use crate::schema_ir::SchemaIR;
12use crate::shape_label_idx::ShapeLabelIdx;
13use iri_s::IriS;
14use rudof_rdf::rdf_core::{Rdf, SHACLPath, term::Object};
15use shacl_ast::ShaclSchema;
16use shacl_ast::property_shape::PropertyShape;
17use std::collections::HashSet;
18
19#[derive(Debug, Clone)]
20pub struct PropertyShapeIR {
21 id: Object,
22 path: SHACLPath,
23 components: Vec<ComponentIR>,
24 targets: Vec<CompiledTarget>,
25 property_shapes: Vec<ShapeLabelIdx>,
26 closed_info: ClosedInfo,
27 deactivated: bool,
28 severity: Option<CompiledSeverity>,
30
31 reifier_info: Option<ReifierInfo>,
33 }
40
41impl PropertyShapeIR {
42 #[allow(clippy::too_many_arguments)]
43 pub fn new(
44 id: Object,
45 path: SHACLPath,
46 components: Vec<ComponentIR>,
47 targets: Vec<CompiledTarget>,
48 property_shapes: Vec<ShapeLabelIdx>,
49 closed_info: ClosedInfo,
50 deactivated: bool,
51 severity: Option<CompiledSeverity>,
52 reifier_info: Option<ReifierInfo>,
53 ) -> Self {
54 PropertyShapeIR {
55 id,
56 path,
57 components,
58 targets,
59 property_shapes,
60 closed_info,
61 deactivated,
62 severity,
63 reifier_info,
64 }
65 }
66
67 pub fn id(&self) -> &Object {
68 &self.id
69 }
70
71 pub fn closed(&self) -> bool {
72 self.closed_info.is_closed()
73 }
74
75 pub fn reifier_info(&self) -> Option<ReifierInfo> {
76 self.reifier_info.clone()
77 }
78
79 pub fn allowed_properties(&self) -> HashSet<IriS> {
80 self.closed_info
81 .allowed_properties()
82 .cloned()
83 .unwrap_or_else(HashSet::new)
84 }
85
86 pub fn path(&self) -> &SHACLPath {
87 &self.path
88 }
89
90 pub fn deactivated(&self) -> bool {
91 self.deactivated
92 }
93
94 pub fn severity(&self) -> CompiledSeverity {
95 match &self.severity {
96 Some(severity) => severity.clone(),
97 None => CompiledSeverity::Violation,
98 }
99 }
100
101 pub fn components(&self) -> &Vec<ComponentIR> {
102 &self.components
103 }
104
105 pub fn targets(&self) -> &Vec<CompiledTarget> {
106 &self.targets
107 }
108
109 pub fn property_shapes(&self) -> &Vec<ShapeLabelIdx> {
110 &self.property_shapes
111 }
112}
113
114impl PropertyShapeIR {
115 pub fn compile<S: Rdf>(
116 shape: PropertyShape<S>,
117 schema: &ShaclSchema<S>,
118 schema_ir: &mut SchemaIR,
119 ) -> Result<Self, Box<CompiledShaclError>> {
120 let id = shape.id().clone();
121 let path = shape.path().to_owned();
122 let deactivated = shape.is_deactivated().to_owned();
123 let severity = CompiledSeverity::compile(shape.severity())?;
124
125 let components = shape.components().iter().collect::<Vec<_>>();
126 let mut compiled_components = Vec::new();
127 for component in components {
128 if let Some(component) = ComponentIR::compile(component.to_owned(), schema, schema_ir)? {
129 compiled_components.push(component);
130 }
131 }
132
133 let mut targets = Vec::new();
134 for target in shape.targets() {
135 let ans = CompiledTarget::compile(target.to_owned())?;
136 targets.push(ans);
137 }
138
139 let mut property_shapes = Vec::new();
140 for property_shape in shape.property_shapes() {
141 let shape = compile_shape(property_shape, schema, schema_ir)?;
142 property_shapes.push(shape);
143 }
144
145 let closed_info = ClosedInfo::get_closed_info_property_shape(&shape, schema)
146 .map_err(|e| Box::new(CompiledShaclError::ShaclError { source: e }))?;
147
148 let reifier_info = reifier_info::ReifierInfo::get_reifier_info_property_shape(&shape, schema, schema_ir)?;
149
150 let compiled_property_shape = PropertyShapeIR::new(
151 id,
152 path,
153 compiled_components,
154 targets,
155 property_shapes,
156 closed_info,
157 deactivated,
158 severity,
159 reifier_info,
160 );
161
162 Ok(compiled_property_shape)
163 }
164
165 pub(crate) fn add_edges(
166 &self,
167 shape_idx: ShapeLabelIdx,
168 dg: &mut DependencyGraph,
169 posneg: PosNeg,
170 schema_ir: &SchemaIR,
171 visited: &mut HashSet<ShapeLabelIdx>,
172 ) {
173 for component in &self.components {
174 component.add_edges(shape_idx, dg, posneg, schema_ir, visited);
175 }
176 for property_shape_idx in &self.property_shapes {
177 if let Some(shape) = schema_ir.get_shape_from_idx(property_shape_idx) {
178 dg.add_edge(shape_idx, *property_shape_idx, posneg);
179 if visited.contains(property_shape_idx) {
180 } else {
181 visited.insert(*property_shape_idx);
182 shape.add_edges(*property_shape_idx, dg, posneg, schema_ir, visited);
183 }
184 }
185 }
186 }
187}