cimoxide-gen 0.3.2

Code generator turning ENTSO-E CGMES RDF/SHACL schemas into typed Rust structs and SHACL validators
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std::collections::HashMap;
use std::fs;

use quick_xml::events::Event;
use quick_xml::Reader;

use super::model::*;

// Collects all fields from one rdf:Description block during XML parsing.
#[derive(Debug, Default)]
struct Block {
    about: String,
    rdf_types: Vec<String>,
    label: String,
    comment: String,
    sub_class_of: String,
    domain: String,
    range: String,
    stereotype: String,
    // Any of the block's `cims:stereotype` values is `concrete`; `stereotype`
    // keeps only the last one.
    concrete: bool,
    multiplicity: String,
    association_used: String,
    inverse_role: String,
    data_type: String,
    is_fixed: String,
    category: String,
    version_info: String,
    version_iri: String,
    keyword: String,
    title: String,
}

impl Block {
    fn set(&mut self, tag: &str, value: String) {
        match tag {
            "rdf:type" => self.rdf_types.push(value),
            "rdfs:label" => self.label = value,
            "rdfs:comment" => self.comment = value,
            "rdfs:subClassOf" => self.sub_class_of = value,
            "rdfs:domain" => self.domain = value,
            "rdfs:range" => self.range = value,
            "cims:stereotype" => {
                self.concrete |= uri_end(&value) == "concrete";
                self.stereotype = value;
            }
            "cims:multiplicity" => self.multiplicity = value,
            "cims:AssociationUsed" => self.association_used = value,
            "cims:inverseRoleName" => self.inverse_role = value,
            "cims:dataType" => self.data_type = value,
            "cims:isFixed" => self.is_fixed = value,
            "cims:belongsToCategory" => self.category = value,
            "owl:versionInfo" => self.version_info = value,
            "owl:versionIRI" => self.version_iri = value,
            "dcat:keyword" => self.keyword = value,
            "dcterms:title" => self.title = value,
            _ => {}
        }
    }
}

pub fn import_schema_files(
    pattern: &str,
    verbose: bool,
) -> Result<CimSpecification, Box<dyn std::error::Error>> {
    let mut paths: Vec<String> = glob::glob(pattern)?
        .filter_map(Result::ok)
        .map(|p| p.to_string_lossy().into_owned())
        .collect();

    if paths.is_empty() {
        return Err(format!("no schema files matched: {pattern}").into());
    }
    paths.sort();

    let mut spec = CimSpecification::new();
    for path in &paths {
        if verbose {
            eprintln!("parsing {path}");
        }
        parse_file(path, &mut spec)?;
    }

    super::processing::postprocess(&mut spec);
    Ok(spec)
}

fn parse_file(
    path: &str,
    spec: &mut CimSpecification,
) -> Result<(), Box<dyn std::error::Error>> {
    let content = fs::read_to_string(path)
        .map_err(|e| format!("cannot read {path}: {e}"))?;

    let mut reader = Reader::from_str(&content);
    let mut buf = Vec::new();

    let mut ns_map: HashMap<String, String> = HashMap::new();
    let mut blocks: Vec<Block> = Vec::new();
    let mut current: Option<Block> = None;
    let mut current_tag: Option<String> = None;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                let name = bytes_to_str(e.name().as_ref())?;

                if name == "rdf:RDF" {
                    for attr in e.attributes().flatten() {
                        let key = bytes_to_str(attr.key.as_ref())?;
                        let val = std::str::from_utf8(&attr.value)?.to_string();
                        if let Some(prefix) = key.strip_prefix("xmlns:").or_else(|| key.strip_prefix("xml:")) {
                            let ns = ensure_hash(val);
                            ns_map.insert(prefix.to_string(), ns);
                        }
                    }
                } else if name == "rdf:Description" {
                    let mut b = Block::default();
                    for attr in e.attributes().flatten() {
                        let key = bytes_to_str(attr.key.as_ref())?;
                        if key == "rdf:about" {
                            b.about = std::str::from_utf8(&attr.value)?.to_string();
                        }
                    }
                    current = Some(b);
                } else if current.is_some() {
                    let resource = find_resource(e.attributes());
                    if let Some(res) = resource {
                        if let Some(ref mut b) = current {
                            b.set(&name, res);
                        }
                        current_tag = None;
                    } else {
                        current_tag = Some(name.to_string());
                    }
                }
            }

            Ok(Event::Empty(ref e)) => {
                let name = bytes_to_str(e.name().as_ref())?;

                if name == "rdf:Description" {
                    let mut b = Block::default();
                    for attr in e.attributes().flatten() {
                        let key = bytes_to_str(attr.key.as_ref())?;
                        if key == "rdf:about" {
                            b.about = std::str::from_utf8(&attr.value)?.to_string();
                        }
                    }
                    blocks.push(b);
                } else if current.is_some() {
                    if let Some(res) = find_resource(e.attributes()) {
                        if let Some(ref mut b) = current {
                            b.set(&name, res);
                        }
                    }
                }
            }

            Ok(Event::Text(ref e)) => {
                if let Some(tag) = current_tag.take() {
                    let text = e.unescape()?.into_owned();
                    let text = text.trim().to_string();
                    if !text.is_empty() {
                        if let Some(ref mut b) = current {
                            b.set(&tag, text);
                        }
                    }
                }
            }

            Ok(Event::End(ref e)) => {
                let name = bytes_to_str(e.name().as_ref())?;
                if name == "rdf:Description" {
                    if let Some(b) = current.take() {
                        blocks.push(b);
                    }
                }
                current_tag = None;
            }

            Ok(Event::Eof) => break,
            Err(e) => return Err(Box::new(e)),
            _ => {}
        }
        buf.clear();
    }

    // Find ontology keyword first (needed to set origin on all other objects)
    let keyword = blocks
        .iter()
        .find(|b| b.rdf_types.iter().any(|t| t.contains("owl#Ontology")))
        .map(|b| b.keyword.clone())
        .unwrap_or_default();

    // Classify and collect per-file results
    let mut types: HashMap<String, CimType> = HashMap::new();
    let mut enums: HashMap<String, CimEnum> = HashMap::new();
    let mut datatypes: HashMap<String, CimDatatype> = HashMap::new();
    let mut primitives: HashMap<String, CimPrimitive> = HashMap::new();
    let mut attributes: Vec<CimAttribute> = Vec::new();
    let mut enum_values: Vec<CimEnumValue> = Vec::new();
    let mut ontology = CimOntology::default();

    for b in blocks {
        let type_str = b.rdf_types.join(" ");
        if type_str.contains("rdf-schema#Class") {
            let stereo = uri_end(&b.stereotype);
            if stereo == "enumeration" {
                let mut e = make_enum(&b);
                e.origin = keyword.clone();
                e.origins = vec![keyword.clone()];
                enums.insert(e.id.clone(), e);
            } else if stereo == "CIMDatatype" {
                let d = make_datatype(&b);
                datatypes.insert(d.id.clone(), d);
            } else if stereo == "Primitive" {
                let p = make_primitive(&b);
                primitives.insert(p.id.clone(), p);
            } else {
                let mut t = make_type(&b);
                t.origin = keyword.clone();
                t.origins = vec![keyword.clone()];
                if b.concrete {
                    t.concrete_in = vec![keyword.clone()];
                }
                types.insert(t.id.clone(), t);
            }
        } else if type_str.contains("rdf-syntax-ns#Property") {
            let mut attr = make_attribute(&b);
            attr.origin = keyword.clone();
            attr.origins = vec![keyword.clone()];
            attributes.push(attr);
        } else if type_str.contains("owl#Ontology") {
            ontology = make_ontology(&b);
        } else if type_str.contains("ClassCategory") {
            // skip package category nodes
        } else if !type_str.is_empty() {
            // Enum value: rdf:type points to the owning enum class
            enum_values.push(make_enum_value(&b));
        }
    }

    assign_attrs_to_types(&mut types, &attributes);
    assign_attrs_to_datatypes(&mut datatypes, &attributes);
    assign_values_to_enums(&mut enums, &enum_values);

    merge_types(&mut spec.types, types);
    merge_enums(&mut spec.enums, enums);
    merge_datatypes(&mut spec.cim_datatypes, datatypes);
    merge_primitives(&mut spec.primitive_types, primitives);

    if !ontology.keyword.is_empty() {
        spec.ontologies.insert(ontology.keyword.clone(), ontology);
    }

    for (k, v) in ns_map {
        spec.specification_namespaces.entry(k).or_insert(v);
    }

    Ok(())
}

// --- constructors -----------------------------------------------------------

fn make_type(b: &Block) -> CimType {
    CimType {
        id: uri_end(&b.about),
        label: b.label.clone(),
        comment: clean_text(&b.comment),
        namespace: uri_path(&b.about),
        super_type: uri_end(&b.sub_class_of),
        cim_stereotype: uri_end(&b.stereotype),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
        cim_categories: vec![uri_end(&b.category)],
        ..Default::default()
    }
}

fn make_primitive(b: &Block) -> CimPrimitive {
    CimPrimitive {
        id: uri_end(&b.about),
        label: b.label.clone(),
        comment: clean_text(&b.comment),
        namespace: uri_path(&b.about),
        cim_stereotype: uri_end(&b.stereotype),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
        ..Default::default()
    }
}

fn make_datatype(b: &Block) -> CimDatatype {
    CimDatatype {
        id: uri_end(&b.about),
        label: b.label.clone(),
        comment: clean_text(&b.comment),
        namespace: uri_path(&b.about),
        cim_stereotype: uri_end(&b.stereotype),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
        cim_category: uri_end(&b.category),
        ..Default::default()
    }
}

fn make_enum(b: &Block) -> CimEnum {
    CimEnum {
        id: uri_end(&b.about),
        label: b.label.clone(),
        comment: clean_text(&b.comment),
        namespace: uri_path(&b.about),
        cim_stereotype: uri_end(&b.stereotype),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
        ..Default::default()
    }
}

fn make_enum_value(b: &Block) -> CimEnumValue {
    CimEnumValue {
        id: uri_end(&b.about),
        label: b.label.clone(),
        comment: clean_text(&b.comment),
        cim_stereotype: uri_end(&b.stereotype),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
    }
}

fn make_ontology(b: &Block) -> CimOntology {
    CimOntology {
        id: uri_end(&b.about),
        namespace: uri_path(&b.about),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
        owl_version_iri: b.version_iri.clone(),
        owl_version_info: b.version_info.clone(),
        keyword: b.keyword.clone(),
        name: b.title.trim_end_matches(" Vocabulary").to_string(),
        priority: 0,
    }
}

fn make_attribute(b: &Block) -> CimAttribute {
    let assoc = b.association_used.to_lowercase();
    let is_assoc = assoc == "yes" || assoc.is_empty();
    let is_list = is_list_multiplicity(&b.multiplicity);
    CimAttribute {
        id: uri_end(&b.about),
        label: b.label.clone(),
        comment: clean_text(&b.comment),
        namespace: uri_path(&b.about),
        cim_stereotype: uri_end(&b.stereotype),
        rdf_domain: uri_end(&b.domain),
        rdf_range: uri_end(&b.range),
        cim_data_type: uri_end(&b.data_type),
        rdf_type: b.rdf_types.first().map(|s| uri_end(s)).unwrap_or_default(),
        cim_association_used: assoc,
        is_association_used: is_assoc,
        cim_inverse_role: uri_end(&b.inverse_role),
        cim_multiplicity: b.multiplicity.clone(),
        is_list,
        cim_is_fixed: b.is_fixed.clone(),
        ..Default::default()
    }
}

// --- assignment -------------------------------------------------------------

fn assign_attrs_to_types(types: &mut HashMap<String, CimType>, attrs: &[CimAttribute]) {
    for attr in attrs {
        if let Some(t) = types.get_mut(&attr.rdf_domain) {
            let mut a = attr.clone();
            a.cim_categories = t.cim_categories.clone();
            t.attributes.push(a);
        }
    }
}

fn assign_attrs_to_datatypes(dt: &mut HashMap<String, CimDatatype>, attrs: &[CimAttribute]) {
    for attr in attrs {
        if let Some(d) = dt.get_mut(&attr.rdf_domain) {
            d.attributes.push(attr.clone());
        }
    }
}

fn assign_values_to_enums(enums: &mut HashMap<String, CimEnum>, values: &[CimEnumValue]) {
    for val in values {
        if let Some(e) = enums.get_mut(&val.rdf_type) {
            e.values.push(val.clone());
        }
    }
}

// --- merge ------------------------------------------------------------------

fn merge_types(target: &mut HashMap<String, CimType>, source: HashMap<String, CimType>) {
    for (k, src) in source {
        if let Some(existing) = target.get_mut(&k) {
            if !src.super_type.is_empty() {
                existing.super_type = src.super_type;
            }
            if !src.cim_stereotype.is_empty() {
                existing.cim_stereotype = src.cim_stereotype;
            }
            existing.cim_categories.extend(src.cim_categories);
            if !src.origin.is_empty() {
                existing.origins.push(src.origin);
            }
            existing.concrete_in.extend(src.concrete_in);
            for attr in src.attributes {
                if let Some(idx) = existing.attributes.iter().position(|a| a.id == attr.id) {
                    existing.attributes[idx].origins.extend(attr.origins);
                    existing.attributes[idx]
                        .cim_categories
                        .extend(attr.cim_categories);
                } else {
                    existing.attributes.push(attr);
                }
            }
        } else {
            target.insert(k, src);
        }
    }
}

fn merge_enums(target: &mut HashMap<String, CimEnum>, source: HashMap<String, CimEnum>) {
    for (k, src) in source {
        if let Some(existing) = target.get_mut(&k) {
            if !src.cim_stereotype.is_empty() {
                existing.cim_stereotype = src.cim_stereotype;
            }
            if !src.origin.is_empty() {
                existing.origins.push(src.origin);
            }
            for val in src.values {
                if !existing.values.iter().any(|v| v.id == val.id) {
                    existing.values.push(val);
                }
            }
        } else {
            target.insert(k, src);
        }
    }
}

fn merge_datatypes(
    target: &mut HashMap<String, CimDatatype>,
    source: HashMap<String, CimDatatype>,
) {
    for (k, src) in source {
        if let Some(existing) = target.get_mut(&k) {
            if !src.cim_stereotype.is_empty() {
                existing.cim_stereotype = src.cim_stereotype;
            }
        } else {
            target.insert(k, src);
        }
    }
}

fn merge_primitives(
    target: &mut HashMap<String, CimPrimitive>,
    source: HashMap<String, CimPrimitive>,
) {
    for (k, src) in source {
        if let Some(existing) = target.get_mut(&k) {
            if !src.cim_stereotype.is_empty() {
                existing.cim_stereotype = src.cim_stereotype;
            }
        } else {
            target.insert(k, src);
        }
    }
}

// --- helpers ----------------------------------------------------------------

fn bytes_to_str(b: &[u8]) -> Result<String, Box<dyn std::error::Error>> {
    Ok(std::str::from_utf8(b)?.to_string())
}

fn find_resource(
    attrs: quick_xml::events::attributes::Attributes,
) -> Option<String> {
    for attr in attrs.flatten() {
        if let Ok(key) = std::str::from_utf8(attr.key.as_ref()) {
            if key == "rdf:resource" {
                if let Ok(s) = std::str::from_utf8(&attr.value) {
                    return Some(s.to_string());
                }
            }
        }
    }
    None
}

pub fn uri_end(uri: &str) -> String {
    match uri.rfind('#') {
        Some(pos) => uri[pos + 1..].to_string(),
        None => uri.to_string(),
    }
}

pub fn uri_path(uri: &str) -> String {
    match uri.rfind('#') {
        Some(pos) => uri[..pos].to_string(),
        None => String::new(),
    }
}

fn is_list_multiplicity(m: &str) -> bool {
    matches!(
        m,
        "http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#M:0..n"
            | "http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#M:1..n"
            | "http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#M:2..n"
            | "http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#M:0..2"
    )
}

fn ensure_hash(mut ns: String) -> String {
    if !ns.ends_with('#') {
        ns.push('#');
    }
    ns
}

pub fn clean_text(s: &str) -> String {
    // Strip HTML tags with a simple state machine
    let mut out = String::with_capacity(s.len());
    let mut in_tag = false;
    for ch in s.chars() {
        match ch {
            '<' => in_tag = true,
            '>' => in_tag = false,
            _ if !in_tag => out.push(ch),
            _ => {}
        }
    }
    let out = out
        .replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "'")
        .replace("&apos;", "'")
        .replace('"', "'")
        .replace('\u{2013}', "-"); // en dash

    out.split_whitespace().collect::<Vec<_>>().join(" ")
}