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
// SPDX-FileCopyrightText: Vernum Projecten B.V.
// SPDX-FileCopyrightText: openEHR Foundation
// SPDX-License-Identifier: Apache-2.0
//! Hand-written AOM2 `C_PRIMITIVE_OBJECT` spec functions.
//!
//! Spec sources (vendored):
//! `AM/docs/UML/classes/org.openehr.am.aom2.c_primitive_object.adoc` §Functions,
//! each concrete descendant's own class page, whose `constraint` attribute
//! names the native type it constrains (e.g. `c_duration.adoc`:
//! `List<Interval<Iso8601_duration>>`), and
//! `AM/docs/AOM2/master04.5-constraint_model-class_definitions.adoc`
//! §Conformance Semantics: C_PRIMITIVE_OBJECT.
use crate::v2_4::aom2::constraint_model::c_attribute::CAttribute;
use crate::v2_4::aom2::constraint_model::c_object_impl::{NodeFacts, conforms};
use crate::v2_4::aom2::constraint_model::c_primitive_object::CPrimitiveObject;
use crate::v2_4::aom2::constraint_model::primitive::c_ordered::COrdered;
use crate::v2_4::aom2::constraint_model::sibling_order::SiblingOrder;
use openehr_base::v1_3::foundation_types::interval::multiplicity_interval::MultiplicityInterval;
impl CPrimitiveObject {
/// Returns true if this constraint carries an assumed value.
///
/// `has_assumed_value` (`org.openehr.am.aom2.c_primitive_object.adoc`
/// §Functions): "True if there is an assumed value", read against the
/// `0..1` `assumed_value` attribute every concrete descendant carries.
#[must_use]
pub fn has_assumed_value(&self) -> bool {
match self {
Self::CBoolean(c) => c.assumed_value.is_some(),
Self::CDate(c) => c.assumed_value.is_some(),
Self::CDateTime(c) => c.assumed_value.is_some(),
Self::CDuration(c) => c.assumed_value.is_some(),
Self::CInteger(c) => c.assumed_value.is_some(),
Self::CReal(c) => c.assumed_value.is_some(),
Self::CString(c) => c.assumed_value.is_some(),
Self::CTerminologyCode(c) => c.assumed_value.is_some(),
Self::CTime(c) => c.assumed_value.is_some(),
}
}
/// Returns the name of the native type this constraint constrains.
///
/// `constrained_typename` (`org.openehr.am.aom2.c_primitive_object.adoc`
/// §Functions): for most types the constrainer typename without its `C_`
/// prefix (`C_INTEGER` → `Integer`), while "for the date/time types the
/// mapping is different" — the different mapping being each temporal
/// descendant's own declared `constraint` element type (`Iso8601_date`,
/// `Iso8601_time`, `Iso8601_date_time`, `Iso8601_duration`) and
/// `C_TERMINOLOGY_CODE`'s `Terminology_code` `assumed_value` type.
#[must_use]
pub fn constrained_typename(&self) -> &'static str {
match self {
Self::CBoolean(_) => "Boolean",
Self::CInteger(_) => "Integer",
Self::CReal(_) => "Real",
Self::CString(_) => "String",
Self::CDate(_) => "Iso8601_date",
Self::CTime(_) => "Iso8601_time",
Self::CDateTime(_) => "Iso8601_date_time",
Self::CDuration(_) => "Iso8601_duration",
Self::CTerminologyCode(_) => "Terminology_code",
}
}
/// The node facts the `master04.5` conformance functions read.
fn facts(&self) -> NodeFacts<'_> {
match self {
Self::CBoolean(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CDate(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CDateTime(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CDuration(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CInteger(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CReal(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CString(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CTerminologyCode(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
Self::CTime(c) => leaf_facts(
&c.node_id,
&c.rm_type_name,
c.occurrences.as_ref(),
c.sibling_order.as_ref(),
),
}
}
/// Returns true if this node on its own expresses the same or narrower
/// constraints than `other`.
///
/// `c_conforms_to` (`master04.5` §Conformance Semantics:
/// C_PRIMITIVE_OBJECT): `precursor (other, rmcc) and c_value_conforms_to
/// (other)`, the precursor being the `C_OBJECT` body. `owning_attribute`
/// carries the Eiffel `parent` as it does on
/// [`crate::v2_4::aom2::constraint_model::c_object::CObject`].
#[must_use]
pub fn c_conforms_to(
&self,
other: &CPrimitiveObject,
rmcc: &dyn Fn(&str, &str) -> bool,
owning_attribute: Option<&CAttribute>,
) -> bool {
conforms(&self.facts(), &other.facts(), rmcc, owning_attribute)
&& self.c_value_conforms_to(other)
}
/// Returns true if this node expresses no constraints beyond `other`'s.
///
/// `c_congruent_to` (`master04.5` §Conformance Semantics:
/// C_PRIMITIVE_OBJECT): `constrained_typename.is_case_insensitive_equal
/// (other.constrained_typename) and c_value_congruent_to (other)`.
#[must_use]
pub fn c_congruent_to(&self, other: &CPrimitiveObject) -> bool {
self.constrained_typename()
.eq_ignore_ascii_case(other.constrained_typename())
&& self.c_value_congruent_to(other)
}
/// Returns true if this node expresses a value constraint that conforms to
/// `other`'s.
///
/// `c_value_conforms_to` (`master04.5` §Conformance Semantics:
/// C_PRIMITIVE_OBJECT) is deferred there and effected on each descendant, so
/// this dispatches to the descendant body. The Eiffel types the parameter
/// `like Current`, so a pair of different primitive types never conforms.
#[must_use]
pub fn c_value_conforms_to(&self, other: &CPrimitiveObject) -> bool {
match (self, other) {
(Self::CBoolean(own), Self::CBoolean(theirs)) => own.c_value_conforms_to(theirs),
(Self::CString(own), Self::CString(theirs)) => own.c_value_conforms_to(theirs),
(Self::CTerminologyCode(own), Self::CTerminologyCode(theirs)) => {
own.c_value_conforms_to(theirs)
}
_ => match (self.as_ordered(), other.as_ordered()) {
(Some(own), Some(theirs)) => own.c_value_conforms_to(&theirs),
_ => false,
},
}
}
/// Returns true if this node's value constraint is the same as `other`'s.
///
/// `c_value_congruent_to` (`master04.5` §Conformance Semantics:
/// C_PRIMITIVE_OBJECT) — deferred there and effected on each descendant, as
/// with [`CPrimitiveObject::c_value_conforms_to`].
#[must_use]
pub fn c_value_congruent_to(&self, other: &CPrimitiveObject) -> bool {
match (self, other) {
(Self::CBoolean(own), Self::CBoolean(theirs)) => own.c_value_congruent_to(theirs),
(Self::CString(own), Self::CString(theirs)) => own.c_value_congruent_to(theirs),
(Self::CTerminologyCode(own), Self::CTerminologyCode(theirs)) => {
own.c_value_congruent_to(theirs)
}
_ => match (self.as_ordered(), other.as_ordered()) {
(Some(own), Some(theirs)) => own.c_value_congruent_to(&theirs),
_ => false,
},
}
}
/// This node as a `C_ORDERED`, when it is one of that subtree's leaves.
fn as_ordered(&self) -> Option<COrdered> {
match self {
Self::CDate(c) => Some(COrdered::CDate(c.clone())),
Self::CDateTime(c) => Some(COrdered::CDateTime(c.clone())),
Self::CDuration(c) => Some(COrdered::CDuration(c.clone())),
Self::CInteger(c) => Some(COrdered::CInteger(c.clone())),
Self::CReal(c) => Some(COrdered::CReal(c.clone())),
Self::CTime(c) => Some(COrdered::CTime(c.clone())),
Self::CBoolean(_) | Self::CString(_) | Self::CTerminologyCode(_) => None,
}
}
}
/// The `NodeFacts` of a primitive leaf.
fn leaf_facts<'a>(
node_id: &'a str,
rm_type_name: &'a str,
occurrences: Option<&'a MultiplicityInterval>,
sibling_order: Option<&'a SiblingOrder>,
) -> NodeFacts<'a> {
NodeFacts {
node_id,
rm_type_name,
occurrences,
sibling_order,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::v2_4::aom2::constraint_model::primitive::c_date::CDate;
use crate::v2_4::aom2::constraint_model::primitive::c_integer::CInteger;
fn integer(assumed_value: Option<f64>) -> CPrimitiveObject {
CPrimitiveObject::CInteger(CInteger {
parent: None,
soc_parent: None,
rm_type_name: "Integer".to_owned(),
occurrences: None,
node_id: "at9999".to_owned(),
alternative_ids: None,
is_deprecated: None,
sibling_order: None,
default_value: None,
assumed_value,
is_enumerated_type_constraint: None,
constraint: None,
})
}
fn date() -> CPrimitiveObject {
CPrimitiveObject::CDate(CDate {
parent: None,
soc_parent: None,
rm_type_name: "DV_DATE".to_owned(),
occurrences: None,
node_id: "at9999".to_owned(),
alternative_ids: None,
is_deprecated: None,
sibling_order: None,
default_value: None,
assumed_value: None,
is_enumerated_type_constraint: None,
constraint: None,
pattern_constraint: None,
})
}
#[test]
fn an_absent_assumed_value_is_no_assumed_value() {
assert!(!integer(None).has_assumed_value());
assert!(integer(Some(0.0)).has_assumed_value());
}
#[test]
fn the_simple_types_drop_the_c_prefix() {
assert_eq!(integer(None).constrained_typename(), "Integer");
}
#[test]
fn the_temporal_types_name_their_iso8601_constraint_type() {
assert_eq!(date().constrained_typename(), "Iso8601_date");
}
}