shacl_ir/compiled/
closed_info.rs1use std::collections::HashSet;
2
3use iri_s::IriS;
4use rudof_rdf::rdf_core::{Rdf, term::Object};
5use shacl_ast::{ShaclError, ShaclSchema, node_shape::NodeShape, property_shape::PropertyShape, shape::Shape};
6
7#[derive(Debug, Clone, Default)]
8pub enum ClosedInfo {
9 Yes {
10 ignored_properties: HashSet<IriS>,
12
13 defined_properties: HashSet<IriS>,
15
16 allowed_properties: HashSet<IriS>,
18 },
19
20 #[default]
21 No,
22}
23
24impl ClosedInfo {
25 pub fn is_closed(&self) -> bool {
26 matches!(self, ClosedInfo::Yes { .. })
27 }
28
29 pub fn ignored_properties(&self) -> Option<&HashSet<IriS>> {
30 match self {
31 ClosedInfo::Yes { ignored_properties, .. } => Some(ignored_properties),
32 ClosedInfo::No => None,
33 }
34 }
35
36 pub fn allowed_properties(&self) -> Option<&HashSet<IriS>> {
38 match self {
39 ClosedInfo::Yes { allowed_properties, .. } => Some(allowed_properties),
40 ClosedInfo::No => None,
41 }
42 }
43
44 pub fn get_closed_info_node_shape<R: Rdf>(
45 shape: &NodeShape<R>,
46 schema: &ShaclSchema<R>,
47 ) -> Result<Self, ShaclError> {
48 let (is_closed, ignored_properties) = shape.closed_component();
49 if is_closed {
50 let ignored_properties: HashSet<IriS> = ignored_properties.into_iter().collect();
51 let defined_properties = defined_properties_for(shape.property_shapes(), schema)?;
52 let all_properties = defined_properties
53 .union(&ignored_properties)
54 .cloned()
55 .collect::<HashSet<IriS>>();
56 Ok(ClosedInfo::Yes {
57 ignored_properties,
58 defined_properties,
59 allowed_properties: all_properties,
60 })
61 } else {
62 Ok(ClosedInfo::No)
63 }
64 }
65
66 pub fn get_closed_info_property_shape<R: Rdf>(
67 shape: &PropertyShape<R>,
68 schema: &ShaclSchema<R>,
69 ) -> Result<Self, ShaclError> {
70 let (is_closed, ignored_properties) = shape.closed_component();
71 if is_closed {
72 let ignored_properties: HashSet<IriS> = ignored_properties.into_iter().collect();
73 let defined_properties = defined_properties_for(shape.property_shapes(), schema)?;
74 let all_properties = defined_properties
75 .union(&ignored_properties)
76 .cloned()
77 .collect::<HashSet<IriS>>();
78 Ok(ClosedInfo::Yes {
79 ignored_properties,
80 defined_properties,
81 allowed_properties: all_properties,
82 })
83 } else {
84 Ok(ClosedInfo::No)
85 }
86 }
87}
88
89fn defined_properties_for<R: Rdf>(properties: &[Object], schema: &ShaclSchema<R>) -> Result<HashSet<IriS>, ShaclError> {
90 let mut defined_properties: HashSet<IriS> = HashSet::new();
91 for property_shape_ref in properties {
92 let property_shape = schema
93 .get_shape(property_shape_ref)
94 .ok_or_else(|| ShaclError::ShapeNotFound {
95 shape: Box::new(property_shape_ref.clone()),
96 })?;
97 match property_shape {
98 Shape::PropertyShape(ps) => {
99 if let Some(pred) = ps.path().pred() {
103 defined_properties.insert(pred.clone());
104 }
105 },
106 _ => {
107 return Err(ShaclError::ShapeNotFound {
108 shape: Box::new(property_shape_ref.clone()),
109 });
110 },
111 }
112 }
113
114 Ok(defined_properties)
115}