shapes_converter 0.2.9

RDF data shapes implementation in Rust
Documentation
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use super::Name;
use super::NodeId;
use super::ShEx2UmlConfig;
use super::UmlCardinality;
use super::UmlComponent;
use super::UmlEntry;
use super::UmlError;
use super::UmlLink;
use super::ValueConstraint;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::hash_map::*;
use std::hash::Hash;
use std::io::Write;

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum UmlLabelType {
    Class,
    Or,
    Not,
    And,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, Default)]
pub enum LineType {
    Orthogonal,
    Polyline,
    #[default]
    Default,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, Default)]
pub enum Direction {
    LeftToRight,
    #[default]
    TopToBottom,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum UmlLabel {
    Class(String),
    Or(usize),
    Not(usize),
    And(usize),
}

impl UmlLabel {
    pub fn label_type(&self) -> UmlLabelType {
        match self {
            UmlLabel::Class(_) => UmlLabelType::Class,
            UmlLabel::Or(_) => UmlLabelType::Or,
            UmlLabel::Not(_) => UmlLabelType::Not,
            UmlLabel::And(_) => UmlLabelType::And,
        }
    }

    pub fn mk_logical_label(label_type: &UmlLabelType, idx: usize) -> UmlLabel {
        match label_type {
            UmlLabelType::Class => panic!("Cannot create a logical label with type Class for idx {idx}"),
            UmlLabelType::Or => UmlLabel::Or(idx),
            UmlLabelType::Not => UmlLabel::Not(idx),
            UmlLabelType::And => UmlLabel::And(idx),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
struct LogicalComponent {
    label_type: UmlLabelType,
    members: BTreeSet<NodeId>,
}

#[derive(Debug, PartialEq, Default)]
pub struct Uml {
    /// Counter to generate new node ids
    labels_counter: usize,

    /// Counter to generate new node ids for logical components
    logical_components_counter: usize,

    /// Logical components store
    logical_components: HashMap<LogicalComponent, usize>,

    /// Associates a label with a node
    labels: HashMap<UmlLabel, NodeId>,

    /// Associates a node with an UmlComponent
    components: HashMap<NodeId, UmlComponent>,

    /// List of links
    links: Vec<UmlLink>,

    /// Contains a map that keeps track of all the parents of a node
    extends: HashMap<NodeId, HashSet<NodeId>>,

    /// Outgoing arcs
    outgoing: HashMap<NodeId, HashSet<NodeId>>,

    /// Incoming arcs
    incoming: HashMap<NodeId, HashSet<NodeId>>,
}

impl Uml {
    pub fn new() -> Uml {
        Default::default()
    }

    pub fn get_logical_component_idx(&mut self, nodes: &BTreeSet<NodeId>, label_type: &UmlLabelType) -> usize {
        let logical_component = LogicalComponent {
            label_type: label_type.clone(),
            members: nodes.clone(),
        };
        match self.logical_components.entry(logical_component.clone()) {
            Entry::Occupied(c) => *c.get(),
            Entry::Vacant(v) => {
                self.logical_components_counter += 1;
                v.insert(self.logical_components_counter);
                self.logical_components_counter
            },
        }
    }

    /// Tries to get a node from a label. If it exists returns the node and true, otherwise, adds the node and returns false
    pub fn get_node_adding_label(&mut self, label: &UmlLabel) -> (NodeId, bool) {
        match self.labels.entry(label.clone()) {
            Entry::Occupied(c) => (*c.get(), true),
            Entry::Vacant(v) => {
                self.labels_counter += 1;
                let n = NodeId::new(self.labels_counter);
                v.insert(n);
                (n, false)
            },
        }
    }

    /// Search a node from a label. If it does not exist, returno `None``
    pub fn get_node(&self, label: &UmlLabel) -> Option<NodeId> {
        self.labels.get(label).copied()
    }

    pub fn add_component(&mut self, node: NodeId, component: UmlComponent) -> Result<(), UmlError> {
        match self.components.entry(node) {
            Entry::Occupied(_c) => Err(UmlError::NodeIdHasComponent { node_id: node }),
            Entry::Vacant(v) => {
                v.insert(component);
                Ok(())
            },
        }
    }

    pub fn get_component(&self, node: &NodeId) -> Option<&UmlComponent> {
        self.components.get(node)
    }

    pub fn update_component(&mut self, node: NodeId, component: UmlComponent) -> Result<(), UmlError> {
        if let Some(r) = self.components.get_mut(&node) {
            *r = component
        } else {
            self.components.insert(node, component);
        }
        Ok(())
    }

    pub fn children<'a>(&'a self, node: &'a NodeId) -> impl Iterator<Item = (&'a NodeId, &'a UmlComponent)> {
        self.components.iter().filter(|(node_id, _component)| {
            if let Some(es) = self.extends.get(node_id) {
                es.contains(node)
            } else {
                false
            }
        })
    }

    pub fn add_link(
        &mut self,
        source: NodeId,
        target: UmlLabel,
        link_name: Name,
        card: UmlCardinality,
    ) -> Result<(), UmlError> {
        match self.labels.entry(target) {
            Entry::Occupied(entry) => {
                let target = *entry.get();
                self.make_link(source, target, link_name, card);
                Ok(())
            },
            Entry::Vacant(v) => {
                self.labels_counter += 1;
                let target_node_id = NodeId::new(self.labels_counter);
                v.insert(target_node_id);
                self.make_link(source, target_node_id, link_name, card);
                Ok(())
            },
        }
    }

    pub fn make_link(&mut self, source: NodeId, target: NodeId, name: Name, card: UmlCardinality) {
        let link = UmlLink::new(source, target, name, card);
        self.links.push(link);
        insert_map(&mut self.outgoing, source, target);
        insert_map(&mut self.incoming, target, source);
    }

    pub fn add_extends(&mut self, source: &NodeId, target: &NodeId) {
        match self.extends.entry(*source) {
            Entry::Occupied(mut v) => {
                v.get_mut().insert(*target);
            },
            Entry::Vacant(vacant) => {
                vacant.insert(HashSet::from([*target]));
            },
        }
    }

    pub fn extends(&self) -> impl Iterator<Item = (&NodeId, &NodeId)> {
        self.extends
            .iter()
            .flat_map(|(n1, vs)| vs.iter().map(move |n2| (n1, n2)))
    }

    pub fn as_plantuml_all<W: Write>(&self, config: &ShEx2UmlConfig, writer: &mut W) -> Result<(), UmlError> {
        writeln!(writer, "@startuml")?;
        self.preamble(writer, config)?;
        for (node_id, component) in self.components.iter() {
            component2plantuml(node_id, component, config, writer)?;
        }
        for link in self.links.iter() {
            link2plantuml(link, config, writer)?;
        }
        for (n1, n2) in self.extends() {
            writeln!(writer, "{n1} -|> {n2}")?;
        }
        writeln!(writer, "@enduml")?;
        Ok(())
    }

    pub fn as_plantuml_neighs<W: Write>(
        &self,
        config: &ShEx2UmlConfig,
        writer: &mut W,
        target_node: &NodeId,
    ) -> Result<(), UmlError> {
        writeln!(writer, "@startuml")?;
        self.preamble(writer, config)?;

        // Keep track of serialized components to avoid serializing them twice
        let mut serialized_components = HashSet::new();

        // For all components in schema, check if they are neighbours with target_node
        for (node_id, component) in self.components.iter() {
            if node_id == target_node
                || is_in_extends(&self.extends, node_id, target_node)
                || is_in_extends(&self.extends, target_node, node_id)
                || is_in_map(&self.outgoing, target_node, node_id)
                || is_in_map(&self.incoming, target_node, node_id) && !serialized_components.contains(node_id)
            {
                serialized_components.insert(node_id);
                component2plantuml(node_id, component, config, writer)?;
            }
        }
        for link in self.links.iter() {
            if link.source == *target_node || link.target == *target_node {
                link2plantuml(link, config, writer)?;
            }
        }
        for (n1, n2) in self.extends() {
            if n1 == target_node || n2 == target_node {
                writeln!(writer, "{n1} -|> {n2}")?;
            }
        }
        writeln!(writer, "@enduml")?;
        Ok(())
    }

    fn preamble(&self, writer: &mut impl Write, config: &ShEx2UmlConfig) -> Result<(), UmlError> {
        writeln!(writer, "hide empty members")?;

        match config.direction.clone().unwrap_or_default() {
            Direction::LeftToRight => {
                writeln!(writer, "left to right direction")?;
            },
            Direction::TopToBottom => {
                writeln!(writer, "top to bottom direction")?;
            },
        }

        match config.line_type.clone().unwrap_or_default() {
            LineType::Orthogonal => {
                writeln!(writer, "skinparam linetype ortho")?;
            },
            LineType::Polyline => {
                writeln!(writer, "skinparam linetype polyline")?;
            },
            LineType::Default => {},
        }

        // Hide the class attribute icon
        writeln!(writer, "hide circles")?;

        writeln!(writer, "skinparam shadowing {}", config.shadowing.unwrap_or_default())?;

        // The following parameters should be taken from the ocnfig file...
        writeln!(writer, "skinparam class {{")?;
        writeln!(writer, " BorderColor Black")?;
        writeln!(writer, " ArrowColor Black")?;
        writeln!(writer, "}}")?;
        Ok(())
    }
}

fn component2plantuml<W: Write>(
    node_id: &NodeId,
    component: &UmlComponent,
    config: &ShEx2UmlConfig,
    writer: &mut W,
) -> Result<(), UmlError> {
    match component {
        UmlComponent::UmlClass(class) => {
            let name = if config.replace_iri_by_label() {
                if let Some(label) = class.label() {
                    label
                } else {
                    class.name()
                }
            } else {
                class.name()
            };
            let href = if let Some(href) = class.href() {
                format!("[[{href} {name}]]")
            } else {
                "".to_string()
            };
            writeln!(writer, "class \"{name}\" as {node_id} <<(S,#FF7700)>> {href} {{ ")?;
            for entry in class.entries() {
                entry2plantuml(entry, config, writer)?;
            }
            writeln!(writer, "}}")?;
        },
        UmlComponent::Or { exprs: _ } => {
            writeln!(writer, "class \"OR\" as {node_id} {{}}")?;
        },
        UmlComponent::Not { expr: _ } => {
            writeln!(writer, "class \"NOT\" as {node_id} {{}}")?;
        },
        UmlComponent::And { exprs: _ } => {
            writeln!(writer, "class \"AND\" as {node_id} {{}}")?;
        },
    }
    Ok(())
}

fn link2plantuml<W: Write>(link: &UmlLink, config: &ShEx2UmlConfig, writer: &mut W) -> Result<(), UmlError> {
    let source = format!("{}", link.source);
    let card = card2plantuml(&link.card);
    let target = format!("{}", link.target);
    let name = name2plantuml(&link.name, config);
    writeln!(writer, "{source} --> \"{card}\" {target} : {name}")?;
    Ok(())
}

fn entry2plantuml<W: Write>(entry: &UmlEntry, config: &ShEx2UmlConfig, writer: &mut W) -> Result<(), UmlError> {
    let property = name2plantuml(&entry.name, config);
    let value_constraint = value_constraint2plantuml(&entry.value_constraint, config);
    let card = card2plantuml(&entry.card);
    writeln!(writer, "{property} : {value_constraint} {card}")?;
    writeln!(writer, "--")?;
    Ok(())
}

fn name2plantuml(name: &Name, config: &ShEx2UmlConfig) -> String {
    let str = if config.replace_iri_by_label() {
        if let Some(label) = name.label() {
            label
        } else {
            name.name()
        }
    } else {
        name.name()
    };
    if let Some(href) = name.href() {
        format!("[[{href} {str}]]")
    } else {
        name.name()
    }
}

fn value_constraint2plantuml(vc: &ValueConstraint, config: &ShEx2UmlConfig) -> String {
    match vc {
        ValueConstraint::Any => ".".to_string(),
        ValueConstraint::Datatype(dt) => name2plantuml(dt, config),
        ValueConstraint::Ref(r) => format!("@{}", name2plantuml(r, config)),
        ValueConstraint::None => "".to_string(),
        ValueConstraint::ValueSet(values) => {
            let mut str = String::new();
            str.push_str("[ ");
            for name in values {
                let name_puml = name2plantuml(name, config);
                if !str.is_empty() {
                    str.push(' ');
                }
                str.push_str(name_puml.as_str());
            }
            str.push_str(" ]");
            str.to_string()
        },
        ValueConstraint::Facet(names) => {
            let mut str = String::new();
            for name in names {
                let name_puml = name2plantuml(name, config);
                if !str.is_empty() {
                    str.push(' ');
                }
                str.push_str(name_puml.as_str());
            }
            str.to_string()
        },
        ValueConstraint::Kind(name) => name2plantuml(name, config),
        ValueConstraint::And { values } => values.iter().fold(String::new(), |mut acc, vc| {
            let vc_str = value_constraint2plantuml(vc, config);
            if !acc.is_empty() {
                acc.push_str(" AND ");
            }
            acc.push_str(vc_str.as_str());
            acc
        }),
        ValueConstraint::Or { values } => values.iter().fold(String::new(), |mut acc, vc| {
            let vc_str = value_constraint2plantuml(vc, config);
            if !acc.is_empty() {
                acc.push_str(" OR ");
            }
            acc.push_str(vc_str.as_str());
            acc
        }),
        ValueConstraint::Not { value } => {
            let vc_str = value_constraint2plantuml(value, config);
            format!("NOT {vc_str}")
        },
    }
}

fn card2plantuml(card: &UmlCardinality) -> String {
    match card {
        UmlCardinality::OneOne => " ".to_string(),
        UmlCardinality::Star => "*".to_string(),
        UmlCardinality::Plus => "+".to_string(),
        UmlCardinality::Optional => "?".to_string(),
        UmlCardinality::Range(m, n) => format!("{m}-{n}"),
        UmlCardinality::Fixed(m) => format!("{{{m}}}"),
    }
}

fn is_in_extends(extends: &HashMap<NodeId, HashSet<NodeId>>, node: &NodeId, target: &NodeId) -> bool {
    if let Some(es) = extends.get(node) {
        es.contains(target)
    } else {
        false
    }
}

fn insert_map<A, B>(map: &mut HashMap<A, HashSet<B>>, source: A, target: B)
where
    A: Eq + Hash,
    B: Eq + Hash,
{
    match map.entry(source) {
        Entry::Occupied(mut entry) => {
            let set = entry.get_mut();
            set.insert(target);
        },
        Entry::Vacant(v) => {
            v.insert(HashSet::from([target]));
        },
    }
}

fn is_in_map<A, B>(map: &HashMap<A, HashSet<B>>, source: &A, target: &B) -> bool
where
    A: Eq + Hash,
    B: Eq + Hash,
{
    if let Some(es) = map.get(source) {
        es.contains(target)
    } else {
        false
    }
}