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