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
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-FileCopyrightText: openEHR Foundation
// SPDX-License-Identifier: Apache-2.0

//! The composed P_BMM load: ODIN schema text (plus the text of every schema it
//! includes) → an in-memory `BMM_MODEL`.
//!
//! This is the whole of what `master02-overview.adoc` §Conceptual Approach asks a
//! schema reader to do — "A schema reading component has to resolve the schema
//! inclusions and ultimately `BMM_*` object instantiations to obtain the
//! in-memory form of the model" — as one call over three stages:
//! [`crate::v1_1::bmm_persistence::reader::read_schema`] →
//! [`crate::v1_1::bmm_persistence::include_resolution::resolve_includes`] →
//! [`crate::v1_1::bmm_persistence::create_model::create_bmm_model`].
//!
//! It takes SOURCE TEXT, never paths: locating `.bmm` files on disk is a
//! repository concern, not a schema-reading one, and keeping it out of this
//! module keeps the whole pipeline testable from string literals.

use std::collections::BTreeMap;

use crate::v1_1::bmm::core::bmm_model::BmmModel;
use crate::v1_1::bmm_persistence::create_model::create_bmm_model;
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_schema::PBmmSchema;
use crate::v1_1::bmm_persistence::reader::read_schema;

/// Load the `BMM_MODEL` of `root_src`, resolving its inclusions against
/// `includes`.
///
/// `includes` maps a caller-side label (conventionally the schema id) to that
/// schema's ODIN text. Resolution keys on each parsed schema's OWN
/// [`PBmmSchema::schema_id`], so a mislabelled entry still resolves correctly
/// and a duplicate id is refused rather than silently shadowed. Supplying more
/// schemas than `root_src` needs is harmless; the unused ones are read (and so
/// validated) but not merged.
///
/// # Errors
/// Returns any [`PBmmReadError`] the three stages raise: an ODIN or schema-shape
/// failure while reading `root_src` or any entry of `includes`, an unresolvable
/// or cyclic inclusion, or an unresolvable symbolic reference while
/// materialising the model.
pub fn load_model(
    root_src: &str,
    includes: &BTreeMap<String, String>,
) -> Result<BmmModel, PBmmReadError> {
    let root = read_schema(root_src)?;
    let mut loaded: BTreeMap<String, PBmmSchema> = BTreeMap::new();
    for (label, src) in includes {
        loaded.insert(label.clone(), read_schema(src)?);
    }
    let resolved = resolve_includes(root, &loaded)?;
    create_bmm_model(&resolved)
}

#[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 crate::v1_1::bmm::core::bmm_class::BmmClass;
    use crate::v1_1::bmm::core::bmm_enumeration::BmmEnumeration;
    use crate::v1_1::bmm::core::bmm_model::BmmModel;
    use crate::v1_1::bmm::core::bmm_property::BmmProperty;
    use crate::v1_1::bmm_persistence::error::PBmmReadError;
    use crate::v1_1::bmm_persistence::loader::load_model;

    /// A two-file schema set: `primitive_types` defining `Any`/`Ordered`/`String`
    /// plus the generic `Interval`, and `rm` including it and defining
    /// `DV_ORDERED` / `DV_QUANTITY` / `ITEM_TREE`.
    const PRIMITIVE_TYPES: &str = r#"
        bmm_version = <"2.4">
        rm_publisher = <"openehr">
        schema_name = <"primitive_types">
        rm_release = <"1.0.2">
        packages = <
            ["org.openehr.rm.support.assumed_types"] = <
                name = <"org.openehr.rm.support.assumed_types">
                classes = <"Any", "Ordered", "String", "Real", "List", "Interval">
            >
        >
        primitive_types = <
            ["Any"] = <
                name = <"Any">
                is_abstract = <True>
            >
            ["Ordered"] = <
                name = <"Ordered">
                is_abstract = <True>
                ancestors = <"Any", ...>
            >
            ["String"] = <
                name = <"String">
                ancestors = <"Any", ...>
            >
            ["Real"] = <
                name = <"Real">
                ancestors = <"Ordered", ...>
            >
            ["List"] = <
                name = <"List">
                ancestors = <"Any", ...>
                generic_parameter_defs = <
                    ["T"] = <
                        name = <"T">
                    >
                >
            >
            ["Interval"] = <
                name = <"Interval">
                ancestors = <"Any", ...>
                generic_parameter_defs = <
                    ["T"] = <
                        name = <"T">
                        conforms_to_type = <"Ordered">
                    >
                >
                properties = <
                    ["lower"] = (P_BMM_SINGLE_PROPERTY_OPEN) <
                        name = <"lower">
                        type = <"T">
                    >
                >
            >
        >
    "#;

    const RM: &str = r#"
        bmm_version = <"2.4">
        rm_publisher = <"openehr">
        schema_name = <"rm">
        rm_release = <"1.0.2">
        includes = <
            ["1"] = <
                id = <"openehr_primitive_types_1.0.2">
            >
        >
        packages = <
            ["org.openehr.rm.data_types"] = <
                name = <"org.openehr.rm.data_types">
                classes = <"DV_ORDERED", "DV_QUANTITY", "MAGNITUDE_STATUS">
                packages = <
                    ["item_structure"] = <
                        name = <"item_structure">
                        classes = <"ITEM_TREE">
                    >
                >
            >
        >
        class_definitions = <
            ["DV_ORDERED"] = <
                name = <"DV_ORDERED">
                ancestors = <"Any">
                is_abstract = <True>
                properties = <
                    ["magnitude_status"] = (P_BMM_SINGLE_PROPERTY) <
                        name = <"magnitude_status">
                        type = <"MAGNITUDE_STATUS">
                    >
                >
            >
            ["DV_QUANTITY"] = <
                name = <"DV_QUANTITY">
                ancestors = <"DV_ORDERED">
                properties = <
                    ["magnitude"] = (P_BMM_SINGLE_PROPERTY) <
                        name = <"magnitude">
                        type = <"Real">
                        is_mandatory = <True>
                    >
                    ["normal_range"] = (P_BMM_GENERIC_PROPERTY) <
                        name = <"normal_range">
                        type_def = <
                            root_type = <"Interval">
                            generic_parameters = <"DV_QUANTITY">
                        >
                    >
                >
            >
            ["MAGNITUDE_STATUS"] = (P_BMM_ENUMERATION_STRING) <
                name = <"MAGNITUDE_STATUS">
                ancestors = <"String", ...>
                item_names = <"le", "ge", "eq">
                item_values = <"<=", ">=", "=">
            >
            ["ITEM_TREE"] = <
                name = <"ITEM_TREE">
                ancestors = <"Any">
                properties = <
                    ["items"] = (P_BMM_CONTAINER_PROPERTY) <
                        name = <"items">
                        type_def = <
                            container_type = <"List">
                            type = <"DV_QUANTITY">
                        >
                        cardinality = <|>=1|>
                    >
                >
            >
        >
    "#;

    /// The `RM` schema loaded over its include.
    fn model() -> Result<BmmModel, PBmmReadError> {
        let includes: BTreeMap<String, String> = [(
            "openehr_primitive_types_1.0.2".to_owned(),
            PRIMITIVE_TYPES.to_owned(),
        )]
        .into_iter()
        .collect();
        load_model(RM, &includes)
    }

    #[test]
    fn the_loaded_model_carries_the_root_header_and_every_included_class()
    -> Result<(), PBmmReadError> {
        let model = model()?;
        assert_eq!(model.schema_id(), "openehr_rm_1.0.2");
        let mut names: Vec<&str> = model
            .class_definitions
            .iter()
            .flatten()
            .map(|(name, _)| name.as_str())
            .collect();
        names.sort_unstable();
        assert_eq!(
            names,
            [
                "Any",
                "DV_ORDERED",
                "DV_QUANTITY",
                "ITEM_TREE",
                "Interval",
                "List",
                "MAGNITUDE_STATUS",
                "Ordered",
                "Real",
                "String",
            ]
        );
        // The primitive_types list is carried into is_primitive_type.
        let mut primitives = model.primitive_types();
        primitives.sort_unstable();
        assert_eq!(
            primitives,
            ["Any", "Interval", "List", "Ordered", "Real", "String"]
        );
        assert_eq!(model.enumeration_types(), ["MAGNITUDE_STATUS"]);
        Ok(())
    }

    #[test]
    fn a_generic_class_reports_its_type_signature() -> Result<(), PBmmReadError> {
        let model = model()?;
        let interval = model
            .class_definition("Interval")
            .expect("Interval is defined by the included schema");
        assert_eq!(interval.type_name(), "Interval<T>");
        assert_eq!(interval.type_signature(), "Interval<T:Ordered>");
        assert!(matches!(interval, BmmClass::BmmGenericClass(_)));
        Ok(())
    }

    #[test]
    fn immediate_descendants_invert_the_ancestor_graph() -> Result<(), PBmmReadError> {
        let model = model()?;
        let ordered = model
            .class_definition("DV_ORDERED")
            .expect("DV_ORDERED is defined");
        assert_eq!(ordered.immediate_descendants(), ["DV_QUANTITY"]);
        let any = model.class_definition("Any").expect("Any is defined");
        let mut descendants = any.immediate_descendants().to_vec();
        descendants.sort_unstable();
        assert_eq!(
            descendants,
            [
                "DV_ORDERED".to_owned(),
                "ITEM_TREE".to_owned(),
                "Interval".to_owned(),
                "List".to_owned(),
                "Ordered".to_owned(),
                "String".to_owned(),
            ]
        );
        Ok(())
    }

    #[test]
    fn property_definition_at_path_navigates_the_loaded_model() -> Result<(), PBmmReadError> {
        let model = model()?;
        // `ITEM_TREE.items` is List<DV_QUANTITY>; the container step navigates
        // into the contained type, whose `magnitude` is inherited-free and whose
        // `magnitude_status` comes from DV_ORDERED.
        let magnitude = model
            .property_definition_at_path("ITEM_TREE", "items/magnitude")
            .expect("the nested property resolves through the contained type");
        assert_eq!(magnitude.type_name(), "Real");
        let status = model
            .property_definition_at_path("ITEM_TREE", "items/magnitude_status")
            .expect("the inherited property resolves too");
        assert_eq!(status.type_name(), "MAGNITUDE_STATUS");
        assert!(
            model
                .property_definition_at_path("ITEM_TREE", "items/absent")
                .is_none()
        );
        Ok(())
    }

    #[test]
    fn container_cardinality_and_type_map_onto_the_bmm_shapes() -> Result<(), PBmmReadError> {
        let model = model()?;
        let items = model
            .property_definition("ITEM_TREE", "items")
            .expect("ITEM_TREE declares items");
        assert_eq!(items.type_name(), "List<DV_QUANTITY>");
        assert_eq!(items.conformance_type_name(), "DV_QUANTITY");
        // `|>=1|` maps onto a lower-bounded, upper-unbounded
        // Multiplicity_interval (BMM_CONTAINER_PROPERTY.cardinality).
        let BmmProperty::BmmContainerProperty(container) = items else {
            panic!("a (P_BMM_CONTAINER_PROPERTY) maps onto BMM_CONTAINER_PROPERTY");
        };
        let cardinality = container
            .cardinality
            .as_ref()
            .expect("the property states a cardinality");
        assert_eq!(cardinality.lower, Some(1));
        assert!(cardinality.lower_included);
        assert_eq!(cardinality.upper, None);
        assert!(cardinality.upper_unbounded);
        Ok(())
    }

    #[test]
    fn a_generic_property_binds_its_actual_parameter() -> Result<(), PBmmReadError> {
        let model = model()?;
        let range = model
            .property_definition("DV_QUANTITY", "normal_range")
            .expect("DV_QUANTITY declares normal_range");
        assert_eq!(range.type_name(), "Interval<DV_QUANTITY>");
        Ok(())
    }

    #[test]
    fn an_enumeration_maps_onto_the_string_enumeration_form() -> Result<(), PBmmReadError> {
        let model = model()?;
        let enumeration = model
            .enumeration_definition("MAGNITUDE_STATUS")
            .expect("MAGNITUDE_STATUS is an enumeration");
        let BmmEnumeration::BmmEnumerationString(string) = enumeration else {
            panic!("a (P_BMM_ENUMERATION_STRING) maps onto BMM_ENUMERATION_STRING");
        };
        assert_eq!(
            string.item_names,
            Some(["le", "ge", "eq"].to_vec()).map(|v| v.into_iter().map(str::to_owned).collect())
        );
        assert_eq!(string.underlying_type_name, "STRING");
        assert_eq!(string.item_values.as_ref().map_or(0, Vec::len), 3);
        Ok(())
    }

    #[test]
    fn a_class_carries_the_fully_qualified_path_of_its_package() -> Result<(), PBmmReadError> {
        let model = model()?;
        let item_tree = model
            .class_definition("ITEM_TREE")
            .expect("ITEM_TREE is defined");
        assert_eq!(
            item_tree.package_path(),
            "org.openehr.rm.data_types.item_structure"
        );
        assert_eq!(
            item_tree.class_path(),
            "org.openehr.rm.data_types.item_structure.ITEM_TREE"
        );
        // The package TREE keeps each node's own name as written.
        let nested = model
            .package_at_path("org.openehr.rm.data_types.item_structure")
            .expect("the nested package resolves from the model root");
        assert_eq!(nested.name, "item_structure");
        assert_eq!(nested.classes.as_ref().map_or(0, Vec::len), 1);
        Ok(())
    }

    #[test]
    fn an_open_property_resolves_its_formal_parameter() -> Result<(), PBmmReadError> {
        let model = model()?;
        let lower = model
            .property_definition("Interval", "lower")
            .expect("Interval declares lower");
        assert_eq!(lower.type_name(), "T");
        // BMM_OPEN_TYPE.conformance_type_name is the constrainer.
        assert_eq!(lower.conformance_type_name(), "Ordered");
        Ok(())
    }

    #[test]
    fn a_missing_include_is_reported_from_the_composed_entry_point() {
        let error = load_model(RM, &BTreeMap::new()).expect_err("the include is not supplied");
        assert_eq!(
            error,
            PBmmReadError::MissingInclude {
                requester: "openehr_rm_1.0.2".to_owned(),
                id: "openehr_primitive_types_1.0.2".to_owned(),
            }
        );
    }
}