shex_validation/
validator.rs

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
use crate::atom;
use crate::validator_error::*;
use crate::validator_runner::Engine;
use crate::PosAtom;
use crate::Reason;
use crate::ResultValue;
use crate::ValidatorConfig;
use either::Either;
use prefixmap::IriRef;
use prefixmap::PrefixMap;
use serde_json::Value;
use shapemap::query_shape_map::QueryShapeMap;
use shapemap::ResultShapeMap;
use shapemap::ValidationStatus;
use shex_ast::compiled::compiled_schema::CompiledSchema;
use shex_ast::compiled::shape_expr::ShapeExpr;
use shex_ast::compiled::shape_label::ShapeLabel;
use shex_ast::object_value::ObjectValue;
use shex_ast::Node;
use shex_ast::ShapeExprLabel;
use shex_ast::ShapeLabelIdx;
use srdf::SRDF;
use tracing::debug;

type Result<T> = std::result::Result<T, ValidatorError>;
type Atom = atom::Atom<(Node, ShapeLabelIdx)>;

#[derive(Debug)]
pub struct Validator {
    schema: CompiledSchema,
    runner: Engine,
}

impl Validator {
    pub fn new(schema: CompiledSchema, config: &ValidatorConfig) -> Validator {
        Validator {
            schema,
            runner: Engine::new(config),
        }
    }

    pub fn reset_result_map(&mut self) {
        self.runner.reset()
    }

    /// validate a node against a shape label
    pub fn validate_node_shape<S>(&mut self, node: &Node, shape: &ShapeLabel, rdf: &S) -> Result<()>
    where
        S: SRDF,
    {
        let idx = self.get_idx(shape)?;
        self.runner.add_pending(node.clone(), idx);
        debug!("Before while loop: ${}@{}", node, idx);
        self.loop_validating(rdf)?;
        Ok(())
    }

    fn get_shape_expr_label(&mut self, label: &ShapeExprLabel) -> Result<ShapeLabelIdx> {
        self.schema
            .find_ref(label)
            .map_err(|error| ValidatorError::ShapeLabelNotFoundError {
                shape_label: label.clone(),
                error: format!("{error}"),
            })
    }

    pub fn validate_shapemap<S>(&mut self, shapemap: &QueryShapeMap, rdf: &S) -> Result<()>
    where
        S: SRDF,
    {
        self.fill_pending(shapemap, rdf)?;
        self.loop_validating(rdf)?;
        Ok(())
    }

    fn fill_pending<S>(&mut self, shapemap: &QueryShapeMap, rdf: &S) -> Result<()>
    where
        S: SRDF,
    {
        for (node_value, label) in shapemap.iter_node_shape(rdf) {
            let idx = self.get_shape_expr_label(label)?;
            let node = self.node_from_object_value(node_value, rdf)?;
            self.runner.add_pending(node.clone(), idx);
        }
        Ok(())
    }

    fn node_from_object_value<S>(&mut self, value: &ObjectValue, rdf: &S) -> Result<Node>
    where
        S: SRDF,
    {
        match value {
            ObjectValue::IriRef(IriRef::Iri(iri)) => Ok(Node::iri(iri.clone())),
            ObjectValue::IriRef(IriRef::Prefixed { prefix, local }) => {
                let iri = rdf.resolve_prefix_local(prefix, local)?;
                Ok(Node::iri(iri.clone()))
            }
            ObjectValue::Literal(_lit) => todo!(),
        }
    }

    fn loop_validating<S>(&mut self, rdf: &S) -> Result<()>
    where
        S: SRDF,
    {
        while self.runner.no_end_steps() && self.runner.more_pending() {
            self.runner.new_step();
            let atom = self.runner.pop_pending().unwrap();
            debug!("Processing atom: ${atom:?}");
            self.runner.add_processing(&atom);
            let passed = self.check_node_atom(&atom, rdf)?;
            self.runner.remove_processing(&atom);
            match passed {
                Either::Right(reasons) => {
                    self.runner.add_checked_pos(atom, reasons);
                }
                Either::Left(errors) => {
                    self.runner.add_checked_neg(atom.negated(), errors);
                }
            }
        }
        Ok(())
    }

    pub fn check_node_atom<S>(
        &mut self,
        atom: &Atom,
        rdf: &S,
    ) -> Result<Either<Vec<ValidatorError>, Vec<Reason>>>
    where
        S: SRDF,
    {
        let (node, idx) = atom.get_value();
        let se = find_shape_idx(idx, &self.schema);
        match atom {
            Atom::Pos { .. } => self.runner.check_node_shape_expr(node, se, rdf),
            Atom::Neg { .. } => {
                // Check if a node doesn't conform to a shape expr
                todo!()
            }
        }
    }

    pub fn get_result(&self, node: &Node, shape: &ShapeLabel) -> Result<ResultValue> {
        if let Some(idx) = self.schema.find_shape_label_idx(shape) {
            let pos_atom = PosAtom::new((node.clone(), *idx));
            let atom = Atom::pos(&pos_atom);
            Ok(self.runner.get_result(&atom))
        } else {
            Err(ValidatorError::NotFoundShapeLabel {
                shape: shape.clone(),
            })
        }
    }

    pub fn with_max_steps(mut self, max_steps: usize) -> Self {
        self.runner.set_max_steps(max_steps);
        self
    }

    fn get_idx(&self, shape: &ShapeLabel) -> Result<ShapeLabelIdx> {
        match self.schema.find_label(shape) {
            Some((idx, _se)) => Ok(*idx),
            None => Err(ValidatorError::NotFoundShapeLabel {
                shape: (*shape).clone(),
            }),
        }
    }

    fn get_shape_label(&self, idx: &ShapeLabelIdx) -> Result<&ShapeLabel> {
        let (label, _se) = self.schema.find_shape_idx(idx).unwrap();
        Ok(label)
    }

    pub fn result_map(&self, maybe_nodes_prefixmap: Option<PrefixMap>) -> Result<ResultShapeMap> {
        let mut result = match maybe_nodes_prefixmap {
            None => ResultShapeMap::new(),
            Some(pm) => ResultShapeMap::new().with_nodes_prefixmap(&pm),
        };
        for atom in &self.runner.checked() {
            let (node, idx) = atom.get_value();
            let label = self.get_shape_label(idx)?;
            match atom {
                Atom::Pos(pa) => {
                    let reasons = self.runner.find_reasons(pa);
                    let status = ValidationStatus::conformant(
                        show_reasons(&reasons),
                        json_reasons(&reasons),
                    );
                    // result.add_ok()
                    result
                        .add_result((*node).clone(), label.clone(), status)
                        .map_err(|e| ValidatorError::AddingConformantError {
                            node: node.to_string(),
                            label: label.to_string(),
                            error: format!("{e}"),
                        })?;
                }
                Atom::Neg(na) => {
                    let errors = self.runner.find_errors(na);
                    let status = ValidationStatus::non_conformant(
                        show_errors(&errors),
                        json_errors(&errors),
                    );
                    result
                        .add_result((*node).clone(), label.clone(), status)
                        .map_err(|e| ValidatorError::AddingNonConformantError {
                            node: node.to_string(),
                            label: label.to_string(),
                            error: format!("{e}"),
                        })?;
                }
            }
        }
        for atom in &self.runner.pending() {
            let (node, idx) = atom.get_value();
            let label = self.get_shape_label(idx)?;
            let status = ValidationStatus::pending();
            result
                .add_result((*node).clone(), label.clone(), status)
                .map_err(|e| ValidatorError::AddingPendingError {
                    node: node.to_string(),
                    label: label.to_string(),
                    error: format!("{e}"),
                })?;
        }
        Ok(result)
    }

    pub fn shapes_prefixmap(&self) -> PrefixMap {
        self.schema.prefixmap()
    }
}

fn find_shape_idx<'a>(idx: &'a ShapeLabelIdx, schema: &'a CompiledSchema) -> &'a ShapeExpr {
    let (_label, se) = schema.find_shape_idx(idx).unwrap();
    se
}

fn show_errors(errors: &[ValidatorError]) -> String {
    let mut result = String::new();
    for (err, idx) in errors.iter().enumerate() {
        result.push_str(format!("Error #{idx}: {err}\n").as_str());
    }
    result
}

fn json_errors(_errors: &[ValidatorError]) -> Value {
    let vs = vec!["todo", "errors"];
    vs.into()
}

fn json_reasons(_reasons: &[Reason]) -> Value {
    let vs = vec!["todo", "reasons"];
    vs.into()
}

fn show_reasons(reasons: &[Reason]) -> String {
    let mut result = String::new();
    for (reason, idx) in reasons.iter().enumerate() {
        result.push_str(format!("Reason #{idx}: {reason}\n").as_str());
    }
    result
}

#[cfg(test)]
mod tests {}