openehr-lang 0.0.128

openEHR LANG BMM/P_BMM object model, generated from BMM: generations v1_0 (LANG 1.0.0) and v1_1 (LANG 1.1.0, current; the stable BMM v2.x unit beside the paused BMM3 unit), plus hand-written ODIN/BEL/EL readers
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
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
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-FileCopyrightText: openEHR Foundation
// SPDX-License-Identifier: Apache-2.0

//! Schema inclusion resolution: one `P_BMM_SCHEMA` plus the schemas it includes
//! (transitively) merged into a single self-contained schema.
//!
//! "`.bmm` files function as schemas that support schema inclusion and therefore
//! re-use, in a similar manner to the XML schema languages. Thus, a single
//! logical BMM model can be expressed as a _number_ of `.bmm` schema files which
//! are actually `P_BMM_*` object serialisations of parts of the BMM model. A
//! schema reading component has to resolve the schema inclusions and ultimately
//! `BMM_*` object instantiations to obtain the in-memory form of the model"
//! (`LANG/docs/bmm_persistence/master02-overview.adoc` §Conceptual Approach).
//!
//! The merge itself is `P_BMM_SCHEMA.merge (other)` with precondition
//! `includes_to_process.has (included_schema.schema_id)`, and
//! `P_BMM_PACKAGE.merge (other)` — "Merge packages and classes from other (from
//! an included `P_BMM_SCHEMA`) into this package"
//! (`LANG/docs/UML/classes/org.openehr.lang.bmm_persistence.p_bmm_schema.adoc`
//! + `…p_bmm_package.adoc` §Functions).
//!
//! **Precedence: the includer wins.** That is exactly what
//! `P_BMM_CLASS.is_override` records — "True if this class definition overrides
//! one found in an included schema" (`…p_bmm_class.adoc` §Attributes) — so a
//! class name defined by both the including and an included schema keeps the
//! including schema's definition, and that definition is marked
//! `is_override = True`.

use std::collections::BTreeMap;

use crate::v1_1::bmm_persistence::error::PBmmReadError;
use crate::v1_1::bmm_persistence::p_bmm_class::PBmmClass;
use crate::v1_1::bmm_persistence::p_bmm_package::PBmmPackage;
use crate::v1_1::bmm_persistence::p_bmm_schema::PBmmSchema;

/// Resolve `root`'s inclusions against `loaded`, returning one self-contained
/// schema.
///
/// `loaded` supplies every schema that may be included. Its map KEY is only a
/// caller-side label: resolution indexes each value by its own
/// [`PBmmSchema::schema_id`], which renders lower-cased, and matches a
/// `BMM_INCLUDE_SPEC.id` against it lower-cased too — the vendored schemas write
/// include ids in either case. Resolution is transitive (an included schema's own
/// includes are resolved first) and cycle-safe.
///
/// The returned schema keeps `root`'s header attributes and its `includes`
/// record verbatim: `includes` is a persisted attribute stating what the root
/// schema declares, not a work list to be consumed.
///
/// # Errors
/// Returns [`PBmmReadError::MissingInclude`] when a declared include is not in
/// `loaded`, [`PBmmReadError::IncludeCycle`] when the inclusion graph is cyclic,
/// and [`PBmmReadError::DuplicateSchemaId`] when two entries of `loaded` render
/// the same schema id.
pub fn resolve_includes(
    root: PBmmSchema,
    loaded: &BTreeMap<String, PBmmSchema>,
) -> Result<PBmmSchema, PBmmReadError> {
    let mut by_id: BTreeMap<String, &PBmmSchema> = BTreeMap::new();
    for schema in loaded.values() {
        let id = schema.schema_id();
        if by_id.insert(id.clone(), schema).is_some() {
            return Err(PBmmReadError::DuplicateSchemaId { id });
        }
    }
    let mut chain: Vec<String> = Vec::new();
    resolve(root, &by_id, &mut chain)
}

/// Resolves one schema's inclusions, with `chain` as the active inclusion stack.
fn resolve(
    schema: PBmmSchema,
    by_id: &BTreeMap<String, &PBmmSchema>,
    chain: &mut Vec<String>,
) -> Result<PBmmSchema, PBmmReadError> {
    let id = schema.schema_id();
    if chain.contains(&id) {
        let mut cycle = chain.clone();
        cycle.push(id);
        return Err(PBmmReadError::IncludeCycle {
            chain: cycle.join(" -> "),
        });
    }
    chain.push(id.clone());
    let mut merged = schema;
    // BTreeMap key order — deterministic, so a name collision between two
    // included schemas always resolves the same way.
    let include_ids: Vec<String> = merged
        .includes
        .iter()
        .flat_map(BTreeMap::values)
        .map(|spec| spec.id.clone())
        .collect();
    for include_id in include_ids {
        let key = include_id.to_lowercase();
        let Some(included) = by_id.get(&key) else {
            return Err(PBmmReadError::MissingInclude {
                requester: id.clone(),
                id: include_id,
            });
        };
        let included = resolve((*included).clone(), by_id, chain)?;
        absorb(&mut merged, included);
    }
    chain.pop();
    Ok(merged)
}

/// Merges `included` into `includer`, with `includer` winning every collision.
fn absorb(target: &mut PBmmSchema, source: PBmmSchema) {
    absorb_classes(
        target,
        source.primitive_types.unwrap_or_default(),
        ClassList::Primitive,
    );
    absorb_classes(
        target,
        source.class_definitions.unwrap_or_default(),
        ClassList::Definitions,
    );
    merge_packages(&mut target.packages, source.packages);
}

/// Which of the schema's two class lists an included class joins.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ClassList {
    /// `P_BMM_SCHEMA.primitive_types`.
    Primitive,
    /// `P_BMM_SCHEMA.class_definitions`.
    Definitions,
}

/// Adds every class of `incoming` the includer does not already define, under
/// its key.
///
/// A class name is looked up across BOTH of the includer's lists, because
/// `master04-syntax.adoc` §Classes for Primitive Types says primitive types
/// "are just normal class definitions within a `primitive_types` block …
/// otherwise are processed in the same way as types defined in the main
/// `class_definitions` group" — so one name means one class regardless of which
/// list holds it. The surviving (including) definition is marked
/// `is_override = True`.
///
/// A persisted `P_BMM_INTERFACE` is one of these list members
/// (`master02-overview.adoc` §Conceptual Approach; see
/// [`crate::v1_1::bmm_persistence::reader::read_schema`]) and takes part in exactly
/// the same name-collision rule: its `name` is the collision key, and the
/// includer's definition wins whether either side is an interface or an
/// ordinary class. The only thing an interface cannot do is RECORD the
/// override flag — it declares no `is_override` attribute, so
/// [`PBmmClass::set_is_override`] is a no-op for it (documented there).
fn absorb_classes(
    includer: &mut PBmmSchema,
    incoming: BTreeMap<String, PBmmClass>,
    list: ClassList,
) {
    for (key, class) in incoming {
        if let Some(existing) = find_class_mut(includer, class.name()) {
            existing.set_is_override(true);
            continue;
        }
        let target = match list {
            ClassList::Primitive => &mut includer.primitive_types,
            ClassList::Definitions => &mut includer.class_definitions,
        };
        target.get_or_insert_default().insert(key, class);
    }
}

/// The schema's own definition of `name`, in either class list.
fn find_class_mut<'a>(schema: &'a mut PBmmSchema, name: &str) -> Option<&'a mut PBmmClass> {
    schema
        .primitive_types
        .iter_mut()
        .flat_map(BTreeMap::values_mut)
        .chain(
            schema
                .class_definitions
                .iter_mut()
                .flat_map(BTreeMap::values_mut),
        )
        .find(|class| class.name() == name)
}

/// Merges an included package tree into the includer's, recursively.
///
/// `P_BMM_PACKAGE.merge`: "Merge packages and classes from other (from an
/// included `P_BMM_SCHEMA`) into this package" (class doc §Functions). A package
/// the includer does not declare is taken whole; a package both declare keeps the
/// includer's `name`/`documentation` and gains the included package's classes
/// (in included order, after the includer's own) and child packages.
fn merge_packages(
    target: &mut BTreeMap<String, PBmmPackage>,
    source: BTreeMap<String, PBmmPackage>,
) {
    for (key, package) in source {
        match target.get_mut(&key) {
            None => {
                target.insert(key, package);
            }
            Some(existing) => {
                for class in package.classes.into_iter().flatten() {
                    let existing_classes = existing.classes.get_or_insert_default();
                    if !existing_classes.contains(&class) {
                        existing_classes.push(class);
                    }
                }
                merge_packages(&mut existing.packages, package.packages);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #![expect(
        clippy::panic_in_result_fn,
        reason = "the Book ch11 test shape: `?` propagates the read/resolve/model plumbing while the assertions ARE the test — an assertion panic is how these tests fail"
    )]
    use std::collections::BTreeMap;
    use std::fmt::Write;

    use crate::v1_1::bmm_persistence::error::PBmmReadError;
    use crate::v1_1::bmm_persistence::include_resolution::resolve_includes;
    use crate::v1_1::bmm_persistence::p_bmm_class::PBmmClass;
    use crate::v1_1::bmm_persistence::p_bmm_schema::PBmmSchema;
    use crate::v1_1::bmm_persistence::reader::read_schema;

    /// Reads one schema fixture.
    fn read(src: &str) -> PBmmSchema {
        read_schema(src).expect("the fixture reads")
    }

    /// The supplied-schemas map, keyed by each schema's own id.
    fn loaded(schemas: Vec<PBmmSchema>) -> BTreeMap<String, PBmmSchema> {
        schemas
            .into_iter()
            .map(|schema| (schema.schema_id(), schema))
            .collect()
    }

    /// A schema named `name` defining `classes`, including `includes`.
    fn schema_src(name: &str, includes: &[&str], classes: &[(&str, &str)]) -> String {
        let mut src = format!(
            r#"
            bmm_version = <"2.4">
            rm_publisher = <"openehr">
            schema_name = <"{name}">
            rm_release = <"1.0.2">
        "#
        );
        if !includes.is_empty() {
            src.push_str("includes = <\n");
            for (index, id) in includes.iter().enumerate() {
                let key = index + 1;
                let _ = writeln!(src, "[\"{key}\"] = < id = <\"{id}\"> >");
            }
            src.push_str(">\n");
        }
        let names: Vec<String> = classes
            .iter()
            .map(|(class, _)| format!("\"{class}\""))
            .collect();
        let _ = writeln!(
            src,
            "packages = <\n[\"org.openehr.{name}\"] = <\nname = <\"org.openehr.{name}\">\nclasses = <{}>\n>\n>",
            names.join(", ")
        );
        src.push_str("class_definitions = <\n");
        for (class, documentation) in classes {
            let _ = writeln!(
                src,
                "[\"{class}\"] = < name = <\"{class}\"> documentation = <\"{documentation}\"> >"
            );
        }
        src.push_str(">\n");
        src
    }

    #[test]
    fn inclusion_is_transitive() -> Result<(), PBmmReadError> {
        let leaf = read(&schema_src("primitive_types", &[], &[("Any", "leaf")]));
        let middle = read(&schema_src(
            "basic_types",
            &["openehr_primitive_types_1.0.2"],
            &[("DV_TEXT", "middle")],
        ));
        let root = read(&schema_src(
            "structures",
            &["openehr_basic_types_1.0.2"],
            &[("ELEMENT", "root")],
        ));
        let merged = resolve_includes(root, &loaded(vec![leaf, middle]))?;
        let mut names: Vec<&str> = merged
            .class_definitions
            .iter()
            .flat_map(BTreeMap::values)
            .map(PBmmClass::name)
            .collect();
        names.sort_unstable();
        assert_eq!(names, ["Any", "DV_TEXT", "ELEMENT"]);
        // The merged schema keeps the root's own identity and include record.
        assert_eq!(merged.schema_id(), "openehr_structures_1.0.2");
        assert_eq!(merged.includes.as_ref().map(BTreeMap::len), Some(1));
        Ok(())
    }

    #[test]
    fn the_includer_wins_a_name_collision_and_is_marked_as_an_override() -> Result<(), PBmmReadError>
    {
        let included = read(&schema_src(
            "primitive_types",
            &[],
            &[("Any", "from the included schema")],
        ));
        let root = read(&schema_src(
            "structures",
            &["openehr_primitive_types_1.0.2"],
            &[("Any", "from the including schema")],
        ));
        let merged = resolve_includes(root, &loaded(vec![included]))?;
        assert_eq!(
            merged.class_definitions.as_ref().map_or(0, BTreeMap::len),
            1
        );
        let class = merged
            .class_definitions
            .iter()
            .flat_map(BTreeMap::values)
            .next()
            .expect("the surviving definition");
        assert_eq!(class.documentation(), Some("from the including schema"));
        assert!(class.is_override());
        Ok(())
    }

    #[test]
    fn every_class_keeps_the_schema_id_that_defined_it() -> Result<(), PBmmReadError> {
        let included = read(&schema_src("primitive_types", &[], &[("Any", "leaf")]));
        let root = read(&schema_src(
            "structures",
            &["openehr_primitive_types_1.0.2"],
            &[("ELEMENT", "root")],
        ));
        let merged = resolve_includes(root, &loaded(vec![included]))?;
        let sources: BTreeMap<&str, Option<&str>> = merged
            .class_definitions
            .iter()
            .flat_map(BTreeMap::values)
            .map(|class| (class.name(), class.source_schema_id()))
            .collect();
        assert_eq!(
            sources.get("Any").copied().flatten(),
            Some("openehr_primitive_types_1.0.2")
        );
        assert_eq!(
            sources.get("ELEMENT").copied().flatten(),
            Some("openehr_structures_1.0.2")
        );
        Ok(())
    }

    #[test]
    fn an_included_interface_joins_the_class_list_and_loses_to_the_includer()
    -> Result<(), PBmmReadError> {
        // A `(P_BMM_INTERFACE)` entry is a member of `class_definitions`
        // (master02-overview.adoc §Conceptual Approach), so it merges under the
        // same includer-wins rule as any other definition.
        let included = read(
            r#"
            bmm_version = <"2.4">
            rm_publisher = <"openehr">
            schema_name = <"leaf">
            rm_release = <"1.0.2">
            packages = <
                ["org.openehr.leaf"] = <
                    name = <"org.openehr.leaf">
                    classes = <"Math", "TERMINOLOGY_ACCESS">
                >
            >
            class_definitions = <
                ["Math"] = (P_BMM_INTERFACE) < name = <"Math"> >
                ["TERMINOLOGY_ACCESS"] = (P_BMM_INTERFACE) <
                    name = <"TERMINOLOGY_ACCESS">
                    documentation = <"from the included schema">
                >
            >
        "#,
        );
        let root = read(
            r#"
            bmm_version = <"2.4">
            rm_publisher = <"openehr">
            schema_name = <"root">
            rm_release = <"1.0.2">
            includes = <
                ["1"] = < id = <"openehr_leaf_1.0.2"> >
            >
            packages = <
                ["org.openehr.root"] = <
                    name = <"org.openehr.root">
                    classes = <"TERMINOLOGY_ACCESS">
                >
            >
            class_definitions = <
                ["TERMINOLOGY_ACCESS"] = (P_BMM_INTERFACE) <
                    name = <"TERMINOLOGY_ACCESS">
                    documentation = <"from the including schema">
                >
            >
        "#,
        );
        let merged = resolve_includes(root, &loaded(vec![included]))?;
        let mut names: Vec<&str> = merged
            .class_definitions
            .iter()
            .flat_map(BTreeMap::values)
            .map(PBmmClass::name)
            .collect();
        names.sort_unstable();
        assert_eq!(names, ["Math", "TERMINOLOGY_ACCESS"]);
        let surviving = merged
            .class_definitions
            .iter()
            .flat_map(BTreeMap::values)
            .find(|class| class.name() == "TERMINOLOGY_ACCESS")
            .expect("the surviving definition");
        assert_eq!(surviving.documentation(), Some("from the including schema"));
        // An interface has no `is_override` slot, so the flag stays unrecorded.
        assert!(!surviving.is_override());
        Ok(())
    }

    #[test]
    fn packages_merge_recursively() -> Result<(), PBmmReadError> {
        let included = read(
            r#"
            bmm_version = <"2.4">
            rm_publisher = <"openehr">
            schema_name = <"leaf">
            rm_release = <"1.0.2">
            packages = <
                ["org.openehr.rm"] = <
                    name = <"org.openehr.rm">
                    classes = <"Any">
                    packages = <
                        ["support"] = <
                            name = <"support">
                            classes = <"TERMINOLOGY_ID">
                        >
                    >
                >
            >
            class_definitions = <
                ["Any"] = < name = <"Any"> >
                ["TERMINOLOGY_ID"] = < name = <"TERMINOLOGY_ID"> >
            >
        "#,
        );
        let root = read(
            r#"
            bmm_version = <"2.4">
            rm_publisher = <"openehr">
            schema_name = <"root">
            rm_release = <"1.0.2">
            includes = <
                ["1"] = < id = <"openehr_leaf_1.0.2"> >
            >
            packages = <
                ["org.openehr.rm"] = <
                    name = <"org.openehr.rm">
                    classes = <"ELEMENT">
                    packages = <
                        ["composition"] = <
                            name = <"composition">
                            classes = <"COMPOSITION">
                        >
                    >
                >
            >
            class_definitions = <
                ["ELEMENT"] = < name = <"ELEMENT"> >
                ["COMPOSITION"] = < name = <"COMPOSITION"> >
            >
        "#,
        );
        let merged = resolve_includes(root, &loaded(vec![included]))?;
        let rm = merged
            .packages
            .get("org.openehr.rm")
            .expect("the shared top-level package");
        assert_eq!(
            rm.classes,
            Some(["ELEMENT".to_owned(), "Any".to_owned()].to_vec())
        );
        assert_eq!(rm.packages.len(), 2);
        assert!(rm.packages.contains_key("composition"));
        assert!(rm.packages.contains_key("support"));
        Ok(())
    }

    #[test]
    fn a_missing_include_is_refused() {
        let root = read(&schema_src("structures", &["openehr_absent_9.9.9"], &[]));
        let error = resolve_includes(root, &BTreeMap::new()).expect_err("the include is absent");
        assert_eq!(
            error,
            PBmmReadError::MissingInclude {
                requester: "openehr_structures_1.0.2".to_owned(),
                id: "openehr_absent_9.9.9".to_owned(),
            }
        );
    }

    #[test]
    fn an_inclusion_cycle_is_refused() {
        let first = read(&schema_src("first", &["openehr_second_1.0.2"], &[]));
        let second = read(&schema_src("second", &["openehr_first_1.0.2"], &[]));
        let error = resolve_includes(first.clone(), &loaded(vec![first, second]))
            .expect_err("the graph is cyclic");
        assert!(
            matches!(error, PBmmReadError::IncludeCycle { .. }),
            "expected a cycle refusal, got {error:?}"
        );
    }
}