Skip to main content

shacl_ir/compiled/
mod.rs

1pub mod closed_info;
2pub mod compiled_shacl_error;
3pub mod component_ir;
4pub mod dependency_graph;
5pub mod node_shape;
6pub mod property_shape;
7pub mod reifier_info;
8pub mod schema_ir;
9pub mod severity;
10pub mod shape;
11pub mod shape_label_idx;
12pub mod target;
13
14use compiled_shacl_error::CompiledShaclError;
15use either::Either;
16use iri_s::IriS;
17use prefixmap::IriRef;
18use rudof_rdf::rdf_core::{Rdf, term::Object};
19use shacl_ast::ShaclSchema;
20use shacl_ast::value::Value;
21use shape::ShapeIR;
22use tracing::trace;
23
24use crate::schema_ir::SchemaIR;
25use crate::shape_label_idx::ShapeLabelIdx;
26
27fn convert_iri_ref(iri_ref: IriRef) -> Result<IriS, Box<CompiledShaclError>> {
28    let iri = iri_ref.get_iri().map_err(|err| {
29        Box::new(CompiledShaclError::IriRefConversion {
30            iri_ref: iri_ref.to_string(),
31            err: err.to_string(),
32        })
33    })?;
34    Ok(iri.clone())
35}
36
37fn compile_shape<S: Rdf>(
38    node: &Object,
39    schema: &ShaclSchema<S>,
40    schema_ir: &mut SchemaIR,
41) -> Result<ShapeLabelIdx, Box<CompiledShaclError>> {
42    let shape = schema.get_shape(node).ok_or(CompiledShaclError::ShapeNotFound {
43        shape: Box::new(node.clone()),
44    })?;
45    match schema_ir.add_shape_idx(node.clone())? {
46        Either::Right(idx) => {
47            trace!("Compiling shape {:?} with index {:?}", node, idx);
48            ShapeIR::compile(shape.to_owned(), schema, &idx, schema_ir)
49        },
50        Either::Left(idx) => {
51            trace!("Shape {:?} already compiled, skipping recompilation", node);
52            Ok(idx)
53        },
54    }
55}
56
57fn compile_shapes<S: Rdf>(
58    shapes: Vec<Object>,
59    schema: &ShaclSchema<S>,
60    schema_ir: &mut SchemaIR,
61) -> Result<Vec<ShapeLabelIdx>, Box<CompiledShaclError>> {
62    let compiled_shapes = shapes
63        .into_iter()
64        .map(|shape| compile_shape::<S>(&shape, schema, schema_ir))
65        .collect::<Result<Vec<_>, _>>()?;
66    Ok(compiled_shapes)
67}
68
69fn convert_value(value: Value) -> Result<Object, Box<CompiledShaclError>> {
70    let ans = match value {
71        Value::Iri(iri_ref) => {
72            let iri = convert_iri_ref(iri_ref)?;
73
74            Object::iri(iri)
75        },
76        Value::Literal(literal) => Object::literal(literal),
77    };
78    Ok(ans)
79}