Skip to main content

swls_core/systems/shapes/
visible.rs

1//! Which SHACL shape documents apply to a document.
2//!
3//! One rule, used by validation, shape templates, hover and completion:
4//! a shape document applies to a document when it is
5//!
6//! - **global**: configured in `shapes` (marked [`Global`]),
7//! - **the document itself**, or
8//! - **linked**: the document links to it, through a prefix it declares or an
9//!   `owl:imports` ([`DocumentLinks`]).
10
11use std::sync::Arc;
12
13use bevy_ecs::prelude::*;
14
15use crate::{prelude::*, systems::shapes::ShaclShapes};
16
17/// Whether the shape document labelled `shapes` applies to document `doc`.
18pub fn shapes_apply(shapes: &Label, global: bool, doc: &Label, links: &DocumentLinks) -> bool {
19    global || shapes.0 == doc.0 || links.iter().any(|link| link.0 == shapes.0)
20}
21
22/// Shape documents, with what [`visible_shapes`] needs.
23pub type ShapeDocs<'w, 's> = Query<
24    'w,
25    's,
26    (
27        &'static Label,
28        &'static Triples,
29        Option<&'static Global>,
30        Option<&'static Prefixes>,
31    ),
32    With<ShaclShapes>,
33>;
34
35/// Triples and prefixes of the shape documents that apply to `doc`.
36pub fn visible_shapes<'a>(
37    shapes: &'a ShapeDocs,
38    doc: &Label,
39    links: &DocumentLinks,
40) -> (Vec<Arc<Vec<MyQuad<'static>>>>, Vec<&'a Prefixes>) {
41    let mut triples = Vec::new();
42    let mut prefixes = Vec::new();
43    for (label, t, global, p) in shapes.iter() {
44        if shapes_apply(label, global.is_some(), doc, links) {
45            triples.push(t.0.clone());
46            prefixes.extend(p);
47        }
48    }
49    (triples, prefixes)
50}