Skip to main content

swls_core/systems/
links.rs

1use bevy_ecs::prelude::*;
2use tracing::instrument;
3
4use crate::{lsp_types::Url, prelude::*, util::ns::owl};
5
6pub fn derive_prefix_links(
7    mut query: Query<(Entity, &Prefixes, Option<&mut DocumentLinks>), Changed<Prefixes>>,
8    mut commands: Commands,
9    // helper: Res<LovHelper>,
10    fs: Res<Fs>,
11) {
12    const SOURCE: &'static str = "prefix import";
13    for (e, prefixes, mut links) in &mut query {
14        let mut new_links = Vec::new();
15        for u in prefixes.0.iter() {
16            let url: Url =
17                fs.0.lov_url(u.url.as_str(), &u.prefix)
18                    .unwrap_or(u.url.clone());
19
20            new_links.push((url.clone(), SOURCE));
21        }
22        if let Some(links) = links.as_mut() {
23            links.retain(|e| e.1 != SOURCE);
24        }
25        // let new_links: Vec<_> = turtle.0.iter().map(|u| (u.url.clone(), SOURCE)).collect();
26        match (new_links.is_empty(), links) {
27            (false, None) => {
28                commands.entity(e).insert(DocumentLinks(new_links));
29            }
30            (false, Some(mut links)) => {
31                links.extend(new_links);
32            }
33            _ => {}
34        }
35    }
36}
37
38#[instrument(skip(query, commands))]
39pub fn derive_owl_imports_links(
40    mut query: Query<(Entity, &Label, &Triples, Option<&mut DocumentLinks>), Changed<Triples>>,
41    mut commands: Commands,
42) {
43    const SOURCE: &'static str = "owl:imports";
44    for (e, label, triples, mut links) in &mut query {
45        if let Some(links) = links.as_mut() {
46            links.retain(|e| e.1 != SOURCE);
47        }
48
49        let new_links: Vec<_> = triples
50            .0
51            .iter()
52            .filter(|t| t.predicate.as_str() == owl::imports.iriref().as_str())
53            .flat_map(|t| Url::parse(t.object.as_str()))
54            .map(|obj| (obj, SOURCE))
55            .collect();
56
57        for (u, _) in &new_links {
58            tracing::debug!("owl:imports {} to {}", label.as_str(), u);
59        }
60
61        if !new_links.is_empty() {
62            match links {
63                Some(mut links) => {
64                    links.extend(new_links);
65                }
66                None => {
67                    commands.entity(e).insert(DocumentLinks(new_links));
68                }
69            }
70        }
71    }
72}