shex_ast/ast/
simple_repr_schema.rs

1use crate::{Shape, ShapeExprLabel};
2use iri_s::IriS;
3use prefixmap::IriRef;
4use serde::{Deserialize, Serialize};
5
6use super::{Schema, ShapeDecl, ShapeExpr, TripleExpr, ValueSetValue};
7
8#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
9pub struct SimpleReprSchema {
10    shapes: Vec<SimpleReprShape>,
11}
12
13impl Default for SimpleReprSchema {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl SimpleReprSchema {
20    pub fn new() -> SimpleReprSchema {
21        SimpleReprSchema { shapes: Vec::new() }
22    }
23
24    pub fn from_schema(&mut self, schema: &Schema) {
25        if let Some(shapes) = schema.shapes() {
26            for shape in shapes {
27                let simple_shape = self.convert_shape_decl(&shape, schema);
28                self.shapes.push(simple_shape)
29            }
30        }
31    }
32
33    pub fn convert_shape_decl(
34        &mut self,
35        shape_decl: &ShapeDecl,
36        schema: &Schema,
37    ) -> SimpleReprShape {
38        self.convert_shape_expr(&shape_decl.id, &shape_decl.shape_expr, schema)
39    }
40
41    pub fn convert_shape_expr(
42        &mut self,
43        name: &ShapeExprLabel,
44        shape: &ShapeExpr,
45        schema: &Schema,
46    ) -> SimpleReprShape {
47        match shape {
48            ShapeExpr::ShapeOr { shape_exprs: _ } => todo!(),
49            ShapeExpr::ShapeAnd { shape_exprs: _ } => todo!(),
50            ShapeExpr::ShapeNot { shape_expr: _ } => todo!(),
51            ShapeExpr::NodeConstraint(_) => todo!(),
52            ShapeExpr::Shape(shape) => self.convert_shape(name, shape, schema),
53            ShapeExpr::External => todo!(),
54            ShapeExpr::Ref(_) => todo!(),
55        }
56    }
57
58    pub fn convert_shape(
59        &mut self,
60        name: &ShapeExprLabel,
61        shape: &Shape,
62        schema: &Schema,
63    ) -> SimpleReprShape {
64        let mut simple = SimpleReprShape::new(name);
65        if let Some(triple_expr) = &shape.expression {
66            self.convert_triple_expr(&mut simple, &triple_expr.te, schema);
67        }
68        simple
69    }
70
71    pub fn convert_triple_expr(
72        &mut self,
73        shape: &mut SimpleReprShape,
74        te: &TripleExpr,
75        schema: &Schema,
76    ) {
77        match te {
78            TripleExpr::EachOf {
79                id: _,
80                expressions,
81                min: _,
82                max: _,
83                sem_acts: _,
84                annotations: _,
85            } => {
86                for te in expressions {
87                    self.convert_triple_expr(shape, &(te.te), schema);
88                }
89            }
90            TripleExpr::OneOf {
91                id: _,
92                expressions,
93                min: _,
94                max: _,
95                sem_acts: _,
96                annotations: _,
97            } => {
98                for te in expressions {
99                    self.convert_triple_expr(shape, &te.te, schema);
100                }
101            }
102            TripleExpr::TripleConstraint {
103                id: _,
104                negated: _,
105                inverse: _,
106                predicate,
107                value_expr,
108                min: _,
109                max: _,
110                sem_acts: _,
111                annotations: _,
112            } => {
113                let iri = schema.resolve_iriref(predicate);
114                if iri == IriS::rdf_type() {
115                    if let Some(se) = value_expr {
116                        self.extract_class_values(se, shape);
117                    }
118                } else {
119                    shape.add_predicate(predicate)
120                }
121            }
122            TripleExpr::TripleExprRef(_) => todo!(),
123        }
124    }
125
126    fn extract_class_values(&mut self, se: &ShapeExpr, shape: &mut SimpleReprShape) {
127        match se {
128            ShapeExpr::NodeConstraint(nc) => {
129                if let Some(values) = nc.values() {
130                    for value in values {
131                        match value {
132                            ValueSetValue::ObjectValue(ov) => match ov {
133                                super::ObjectValue::IriRef(iri_ref) => shape.add_class(&iri_ref),
134                                super::ObjectValue::Literal(_) => todo!(),
135                            },
136                            _ => todo!(),
137                        }
138                    }
139                }
140            }
141            _ => todo!(),
142        }
143    }
144}
145
146#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
147pub struct SimpleReprShape {
148    #[serde(skip_serializing)]
149    id: ShapeExprLabel,
150
151    #[serde(skip_serializing_if = "Vec::is_empty")]
152    class: Vec<IriRef>,
153
154    predicates: Vec<IriRef>,
155}
156
157impl SimpleReprShape {
158    pub fn new(label: &ShapeExprLabel) -> SimpleReprShape {
159        SimpleReprShape {
160            id: label.clone(),
161            class: Vec::new(),
162            predicates: Vec::new(),
163        }
164    }
165
166    pub fn add_predicate(&mut self, pred: &IriRef) {
167        self.predicates.push(pred.clone())
168    }
169
170    pub fn add_class(&mut self, cls: &IriRef) {
171        self.class.push(cls.clone())
172    }
173}