Skip to main content

shacl_ir/compiled/
reifier_info.rs

1use crate::{
2    compiled::compile_shape, compiled_shacl_error::CompiledShaclError, schema_ir::SchemaIR,
3    shape_label_idx::ShapeLabelIdx,
4};
5use iri_s::IriS;
6use rudof_rdf::rdf_core::{Rdf, SHACLPath};
7use shacl_ast::{ShaclSchema, property_shape::PropertyShape};
8use std::fmt::Display;
9
10#[derive(Debug, Clone, Default)]
11pub struct ReifierInfo {
12    reification_required: bool,
13    reifier_shape: Vec<ShapeLabelIdx>,
14    predicate: IriS,
15}
16
17impl ReifierInfo {
18    pub fn reification_required(&self) -> bool {
19        self.reification_required
20    }
21
22    pub fn reifier_shape(&self) -> &Vec<ShapeLabelIdx> {
23        &self.reifier_shape
24    }
25
26    pub fn predicate(&self) -> &IriS {
27        &self.predicate
28    }
29
30    pub fn get_reifier_info_property_shape<R: Rdf>(
31        shape: &PropertyShape<R>,
32        schema: &ShaclSchema<R>,
33        schema_ir: &mut SchemaIR,
34    ) -> Result<Option<Self>, Box<CompiledShaclError>> {
35        if let Some(reifier_info) = shape.reifier_info() {
36            let mut compiled_shapes = Vec::new();
37            for shape_node in reifier_info.reifier_shape() {
38                let compiled_shape = compile_shape(shape_node, schema, schema_ir)?;
39                compiled_shapes.push(compiled_shape);
40            }
41            let path = shape.path();
42            let predicate = match path {
43                SHACLPath::Predicate { pred } => pred.clone(),
44                other => {
45                    return Err(Box::new(CompiledShaclError::InvalidReifierShapePath {
46                        shape_id: Box::new(shape.id().clone()),
47                        path: other.to_string(),
48                    }));
49                },
50            };
51            Ok(Some(Self {
52                reification_required: reifier_info.reification_required(),
53                reifier_shape: compiled_shapes,
54                predicate,
55            }))
56        } else {
57            Ok(None)
58        }
59    }
60}
61
62impl Display for ReifierInfo {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        write!(
65            f,
66            "ReifierInfo(reification_required: {}, reifier_shape count: {}, predicate: {})",
67            self.reification_required,
68            self.reifier_shape.len(),
69            self.predicate
70        )
71    }
72}