oxilite-cli 0.10.0

oxilite command line: load, query, and a SPARQL HTTP endpoint compatible with `oxigraph serve`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! SHACL validation of the Project store as a background job: rudof validates asserted (and,
//! by default, inferred) triples, and each result becomes a diagnostic on the source line of the
//! statement it is about.
//!
// @lat: [[architecture#Studio server#Live validation]]

use super::index::SourceIndex;
use super::scanner::Pos;
use lsp_types::{
    Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Location, Position, Range, Uri,
};
use oxilite::model::GraphName;
use oxilite::sparql::{QueryOptions, SparqlParser};
use oxilite::store::Store;
use oxilite_core::QueryOutput;
use oxilite_validate::{shacl_schema, validate_shacl_graph, ShaclValidationMode, StoreGraph};
use rudof_rdf::rdf_core::term::Object;
use rudof_rdf::rdf_core::{RDFFormat, SHACLPath};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

/// Everything a validation run needs, owned so it can run on another thread.
pub struct Job {
    pub store: Store,
    pub options: QueryOptions,
    pub shapes: String,
    /// The shapes files, where a result's shape is looked for when it is a blank node.
    pub shape_files: Vec<String>,
    /// ShEx schemas with their shape maps: (URI, ShExC, shape map).
    pub shex: Vec<(String, String, String)>,
    pub index: Arc<SourceIndex>,
    /// Validate the entailed graph rather than asserted triples only.
    pub inferred: bool,
    pub generation: u64,
    /// The project's current generation: a run that falls behind stops early.
    pub latest: Arc<AtomicU64>,
}

pub struct Outcome {
    pub generation: u64,
    pub diagnostics: HashMap<String, Vec<Diagnostic>>,
    pub report: Value,
}

impl Job {
    fn stale(&self) -> bool {
        self.latest.load(Ordering::SeqCst) != self.generation
    }

    pub fn run(self) -> Option<Outcome> {
        match self.validate() {
            Ok(Some(o)) => Some(o),
            Ok(None) => None,
            Err(e) => Some(Outcome {
                generation: self.generation,
                diagnostics: HashMap::new(),
                report: json!({"conforms": null, "error": e.to_string(), "results": []}),
            }),
        }
    }

    fn validate(&self) -> Result<Option<Outcome>> {
        let entailing = entailing(&self.options);
        let report = if self.shapes.trim().is_empty() {
            None
        } else {
            let Some(r) = report(
                &self.store,
                &self.options,
                &self.shapes,
                &RDFFormat::NTriples,
                self.inferred,
                &|| self.stale(),
            )?
            else {
                return Ok(None);
            };
            Some(r)
        };
        let mut diagnostics: HashMap<String, Vec<Diagnostic>> = HashMap::new();
        let mut results = Vec::new();
        let owners = owners(&self.shapes);
        let mut conforms = true;
        for r in report.iter().flat_map(|r| r.results()) {
            conforms = false;
            let focus = object_iri(r.focus_node());
            let path = r.path().and_then(path_predicate);
            let shape = shape_of(r.source(), &owners);
            let severity = severity(&r.severity().to_string());
            let component = local(&r.constraint_component().to_string());
            let message = r
                .message()
                .iter()
                .next()
                .map(|(_, m)| m.clone())
                .unwrap_or_else(|| {
                    format!(
                        "{component} violated{}",
                        path.as_ref()
                            .map(|p| format!(" on <{p}>"))
                            .unwrap_or_default()
                    )
                });
            let focus_text = r.focus_node().to_string();
            let at = focus
                .as_ref()
                .and_then(|f| self.index.triple_location(f, path.as_deref()));
            // A named shape is found where it is defined; a blank property shape where its
            // path is mentioned in a shapes file.
            let shape_at = shape
                .as_ref()
                .and_then(|s| self.index.definitions(s).into_iter().next())
                .or_else(|| {
                    let p = path.as_ref()?;
                    self.index
                        .references(p)
                        .into_iter()
                        .find(|l| self.shape_files.contains(&l.uri))
                });
            results.push(json!({
                "focus": focus_text,
                "path": path,
                "value": r.value().map(ToString::to_string),
                "severity": severity_name(severity),
                "component": component,
                "message": message,
                "shape": shape,
                "location": at.as_ref().map(location_json),
                "shapeLocation": shape_at.as_ref().map(location_json),
            }));
            // Report on the data line; without one, on the shape (naming the focus node).
            let (target, text) = match (&at, &shape_at) {
                (Some(l), _) => (l.clone(), message.clone()),
                (None, Some(l)) => (l.clone(), format!("{focus_text}: {message}")),
                (None, None) => continue,
            };
            let related = shape_at.as_ref().filter(|_| at.is_some()).and_then(|l| {
                Some(vec![DiagnosticRelatedInformation {
                    location: Location::new(Uri::from_str(&l.uri).ok()?, range(l.start, l.end)),
                    message: match &shape {
                        Some(s) => format!("shape <{s}>"),
                        None => "the property shape".into(),
                    },
                }])
            });
            diagnostics
                .entry(target.uri.clone())
                .or_default()
                .push(Diagnostic {
                    range: range(target.start, target.end),
                    severity: Some(severity),
                    code: Some(lsp_types::NumberOrString::String(component.clone())),
                    source: Some("shacl".into()),
                    message: text,
                    related_information: related,
                    ..Diagnostic::default()
                });
        }
        // ShEx: each nonconformant node of a shape map is a result on its statement.
        for (uri, schema, map) in &self.shex {
            if self.stale() {
                return Ok(None);
            }
            let graph = data_graph(&self.store, &self.options, self.inferred)?;
            let compiled = oxilite_validate::shex_schema(schema, uri)?;
            let shapes = oxilite_validate::validate_shex_graph(&graph, &compiled, map)?;
            for (node, label, status) in shapes.iter() {
                if status.is_conformant() {
                    continue;
                }
                conforms = false;
                let focus = node
                    .to_string()
                    .trim_matches(|c| c == '<' || c == '>')
                    .to_string();
                let shape = label
                    .to_string()
                    .trim_matches(|c| c == '<' || c == '>')
                    .to_string();
                let message = format!("does not conform to ShEx shape <{shape}>: {status:?}")
                    .chars()
                    .take(600)
                    .collect::<String>();
                let at = self.index.triple_location(&focus, None);
                let shape_at = self
                    .index
                    .definitions(&shape)
                    .into_iter()
                    .find(|l| l.uri == *uri);
                results.push(json!({
                    "focus": focus, "path": null, "value": null, "severity": "violation",
                    "component": "shex", "message": message, "shape": shape,
                    "location": at.as_ref().map(location_json),
                    "shapeLocation": shape_at.as_ref().map(location_json),
                }));
                let Some(target) = at.or(shape_at.clone()) else {
                    continue;
                };
                diagnostics
                    .entry(target.uri.clone())
                    .or_default()
                    .push(Diagnostic {
                        range: range(target.start, target.end),
                        severity: Some(DiagnosticSeverity::ERROR),
                        code: Some(lsp_types::NumberOrString::String("shex".into())),
                        source: Some("shex".into()),
                        message: message.clone(),
                        related_information: shape_at.and_then(|l| {
                            Some(vec![DiagnosticRelatedInformation {
                                location: Location::new(
                                    Uri::from_str(&l.uri).ok()?,
                                    range(l.start, l.end),
                                ),
                                message: format!("shape <{shape}>"),
                            }])
                        }),
                        ..Diagnostic::default()
                    });
            }
        }
        Ok(Some(Outcome {
            generation: self.generation,
            diagnostics,
            report: json!({
                "conforms": conforms,
                "inferred": self.inferred && entailing,
                "results": results,
            }),
        }))
    }
}

fn entailing(options: &QueryOptions) -> bool {
    options.reasoning != oxilite::sparql::Reasoning::None || options.include_inferred
}

/// The graph validators read: the store's union of graphs, or with reasoning on and
/// `inferred`, a copy of the entailed graph.
fn data_graph(
    store: &Store,
    options: &QueryOptions,
    inferred: bool,
) -> Result<StoreGraph<oxilite::rusqlite::RusqliteBackend>> {
    if inferred && entailing(options) {
        let copy = Store::new()?;
        let q = SparqlParser::new().parse_query("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }")?;
        if let QueryOutput::Graph(triples) = store.query_output(q, options)? {
            copy.extend(
                triples
                    .into_iter()
                    .map(|t| t.in_graph(GraphName::DefaultGraph)),
            )?;
        }
        Ok(StoreGraph::new(copy))
    } else {
        Ok(StoreGraph::new(store.clone()).with_union(true))
    }
}

/// Validates a store (all graphs) against shapes. With reasoning on and `inferred`, a copy of
/// the entailed graph is validated, so a `sh:class` shape sees types that follow from the
/// ontology. `None` when `stale` says the result is no longer wanted.
pub fn report(
    store: &Store,
    options: &QueryOptions,
    shapes: &str,
    format: &RDFFormat,
    inferred: bool,
    stale: &dyn Fn() -> bool,
) -> Result<Option<oxilite_validate::ValidationReport>> {
    let schema = shacl_schema(shapes, format, None)?;
    if stale() {
        return Ok(None);
    }
    let graph = data_graph(store, options, inferred)?;
    if stale() {
        return Ok(None);
    }
    let report = validate_shacl_graph(graph, &schema, &ShaclValidationMode::Native)?;
    Ok((!stale()).then_some(report))
}

/// Blank property shapes (by label) to the named shape that owns them through `sh:property`
/// (or `sh:node`, transitively), read from shapes in N-Triples, whose labels rudof keeps.
pub fn owners(shapes_nt: &str) -> HashMap<String, String> {
    use oxilite::model::{NamedOrBlankNode, Term};
    let mut parent: HashMap<String, NamedOrBlankNode> = HashMap::new();
    for q in oxilite::io::RdfParser::from_format(oxilite::io::RdfFormat::NTriples)
        .for_slice(shapes_nt.as_bytes())
        .flatten()
    {
        let p = q.predicate.as_str();
        if p == "http://www.w3.org/ns/shacl#property" || p == "http://www.w3.org/ns/shacl#node" {
            if let Term::BlankNode(b) = &q.object {
                parent.insert(b.as_str().to_string(), q.subject.clone());
            }
        }
    }
    let mut out = HashMap::new();
    for label in parent.keys() {
        let mut at = label.clone();
        for _ in 0..16 {
            match parent.get(&at) {
                Some(NamedOrBlankNode::NamedNode(n)) => {
                    out.insert(label.clone(), n.as_str().to_string());
                    break;
                }
                Some(NamedOrBlankNode::BlankNode(b)) => at = b.as_str().to_string(),
                None => break,
            }
        }
    }
    out
}

/// The named shape a result is about: its source, or the named shape owning a blank one.
pub fn shape_of(source: Option<&Object>, owners: &HashMap<String, String>) -> Option<String> {
    match source? {
        Object::Iri(i) => Some(i.as_str().to_string()),
        Object::BlankNode(b) => owners.get(b.as_str()).cloned(),
        _ => None,
    }
}

/// The named shapes a report has results for.
pub fn failing_shapes(
    report: &oxilite_validate::ValidationReport,
    owners: &HashMap<String, String>,
) -> Vec<String> {
    let mut v: Vec<String> = report
        .results()
        .iter()
        .filter_map(|r| shape_of(r.source(), owners))
        .collect();
    v.sort();
    v.dedup();
    v
}

/// Shapes text in any RDF format as N-Triples, so blank node labels stay stable.
pub fn to_ntriples(text: &str, format: oxilite::io::RdfFormat) -> Result<String> {
    let mut out = String::new();
    for q in oxilite::io::RdfParser::from_format(format).for_slice(text.as_bytes()) {
        let q = q?;
        out.push_str(&format!("{} {} {} .\n", q.subject, q.predicate, q.object));
    }
    Ok(out)
}

fn range(start: Pos, end: Pos) -> Range {
    Range::new(
        Position::new(start.line, start.col),
        Position::new(end.line, end.col),
    )
}

fn location_json(l: &super::index::Location) -> Value {
    json!({
        "uri": l.uri,
        "range": {"start": {"line": l.start.line, "character": l.start.col}, "end": {"line": l.end.line, "character": l.end.col}},
    })
}

fn object_iri(o: &Object) -> Option<String> {
    match o {
        Object::Iri(i) => Some(i.as_str().to_string()),
        _ => None,
    }
}

fn path_predicate(p: &SHACLPath) -> Option<String> {
    match p {
        SHACLPath::Predicate { pred } => Some(pred.as_str().to_string()),
        _ => None,
    }
}

fn severity(s: &str) -> DiagnosticSeverity {
    let s = s.to_ascii_lowercase();
    if s.contains("warning") {
        DiagnosticSeverity::WARNING
    } else if s.contains("info") {
        DiagnosticSeverity::INFORMATION
    } else {
        DiagnosticSeverity::ERROR
    }
}

fn severity_name(s: DiagnosticSeverity) -> &'static str {
    match s {
        DiagnosticSeverity::WARNING => "warning",
        DiagnosticSeverity::INFORMATION => "info",
        _ => "violation",
    }
}

/// `sh:MinCountConstraintComponent` → `sh:minCount`.
fn local(component: &str) -> String {
    let name = component
        .trim_matches(|c| c == '<' || c == '>')
        .rsplit(['#', '/', ':'])
        .next()
        .unwrap_or(component);
    let name = name.strip_suffix("ConstraintComponent").unwrap_or(name);
    let mut chars = name.chars();
    match chars.next() {
        Some(c) => format!("sh:{}{}", c.to_ascii_lowercase(), chars.as_str()),
        None => component.to_string(),
    }
}