ifc_properties/exact/enumerate.rs
1//! Exact enumeration of an object's properties (#78).
2//!
3//! A buildingSMART IDS property facet may name its set and property by
4//! pattern (`Pset_.*Common`), and then every matching property must satisfy
5//! the requirement. Deciding that needs every match, and an empty result
6//! must be as much a proof as [`ExactResolution::Absent`]. So enumeration
7//! runs the traversal of [`exact_property`] with two selectors in place of
8//! its two names, and for one set name and one property name it answers
9//! exactly what [`exact_property`] answers:
10//!
11//! - Model and assignment validation are the same.
12//! - A set whose name the set selector rejects is skipped unread, as
13//! [`exact_property`] skips a set of another name. Every member of a
14//! selected set is validated before anything is matched.
15//! - A selected predefined-set attribute that cannot be read exactly (an
16//! aggregate), and an unnamed predefined set outside the set selection
17//! one of whose own attributes is selected, are refused. A complex
18//! property or quantity resolves as `ExactValue::Complex` (#208). An
19//! unselected member must be well formed but need not have a supported
20//! value form.
21//!
22//! [`ExactResolution::Absent`]: super::ExactResolution::Absent
23//! [`exact_property`]: super::exact_property
24
25use std::{collections::BTreeMap, sync::Arc};
26
27use ifc_model::{EntityId, Model};
28
29use super::assignment::{assigned_sets, Relations};
30use super::release::{validate_model, Release};
31use super::set::load_source_set;
32use super::{ExactProperty, ExactPropertyError, ExactSource};
33
34/// One property of an enumeration, with the name it was selected by.
35#[derive(Debug, Clone, PartialEq)]
36#[non_exhaustive]
37pub struct ExactPropertyEntry {
38 /// `IfcProperty.Name` or `IfcPhysicalQuantity.Name`.
39 pub name: Arc<str>,
40 /// The resolved property with its provenance, as [`exact_property`]
41 /// reports it.
42 ///
43 /// [`exact_property`]: super::exact_property
44 pub property: ExactProperty,
45}
46
47/// Every property, simple quantity and predefined-set attribute of
48/// `object`, resolved exactly.
49///
50/// Equivalent to [`exact_properties_where`] selecting every set and every
51/// property, so any predefined set with an aggregate attribute is refused.
52/// Every property and quantity kind resolves, complex ones included, as for
53/// [`exact_property`](super::exact_property). Callers that need
54/// only some properties (an IDS pattern facet) select them with
55/// [`exact_properties_where`], so an unsupported member they do not ask
56/// about cannot refuse their answer.
57///
58/// # Errors
59///
60/// Any [`ExactPropertyError`], as for [`exact_property`](super::exact_property).
61pub fn exact_properties(
62 model: &Model,
63 object: EntityId,
64) -> Result<Vec<ExactPropertyEntry>, ExactPropertyError> {
65 exact_properties_where(model, object, |_| true, |_| true)
66}
67
68/// The properties and simple quantities of `object` in the sets
69/// `select_set` picks, whose names `select_property` picks, resolved
70/// exactly.
71///
72/// Both selectors see names only: `select_set` the `Name` of each assigned
73/// property set and quantity set, `select_property` the `Name` of each
74/// member of a selected set. With `|s| s == set` and `|p| p == name` the
75/// result is `[x]` exactly when [`exact_property`] with that set name
76/// answers `Present(x)`, empty exactly when it answers `Absent`, and the
77/// same error otherwise.
78///
79/// A predefined property set (`IfcDoorLiningProperties` and the like) holds
80/// its values in attributes of its own entity (#149). Its members are the
81/// attributes it declares below `IfcPropertySetDefinition`, by schema name
82/// (`LiningDepth`), and `select_set` sees its `Name`, or its entity name
83/// (`IfcDoorLiningProperties`) when it states none. Because a set without a
84/// `Name` cannot be ruled out by a name, such a set that `select_set` rejects
85/// is still refused with [`ExactPropertyError::UnsupportedDefinition`] when
86/// `select_property` picks one of its attributes (#66).
87///
88/// The result is in assignment order: occurrence sets first, then the sets
89/// inherited from the object's `IfcTypeObject`, each set's members in file
90/// order. An inherited property is left out when an occurrence set of the
91/// same name has a selected property of the same name: the occurrence
92/// value overrides it at property level. An empty result is a proven
93/// absence of every selected property. For a queried `IfcTypeObject` the
94/// result is its own `HasPropertySets`, each entry with
95/// [`ExactSource::Type`] of `object` (#193), as for
96/// [`exact_property`].
97///
98/// # Errors
99///
100/// Any [`ExactPropertyError`], as for [`exact_property`]. Ambiguity is
101/// refused per source: two selected sets of the same name, a property set
102/// and a quantity set included
103/// ([`ExactPropertyError::DuplicateMatchingSets`]), and two selected members
104/// of one set with the same name
105/// ([`ExactPropertyError::DuplicateMatchingProperties`]).
106///
107/// [`exact_property`]: super::exact_property
108pub fn exact_properties_where<S, P>(
109 model: &Model,
110 object: EntityId,
111 mut select_set: S,
112 mut select_property: P,
113) -> Result<Vec<ExactPropertyEntry>, ExactPropertyError>
114where
115 S: FnMut(&str) -> bool,
116 P: FnMut(&str) -> bool,
117{
118 let release = validate_model(model)?;
119 properties_where_in(
120 model,
121 release,
122 None,
123 object,
124 &mut select_set,
125 &mut select_property,
126 )
127}
128
129/// [`exact_properties_where`] once the model is bound to `release`, from a
130/// scan of every relationship when one is given.
131pub(super) fn properties_where_in(
132 model: &Model,
133 release: Release,
134 relations: Option<&Relations>,
135 object: EntityId,
136 select_set: &mut dyn FnMut(&str) -> bool,
137 select_property: &mut dyn FnMut(&str) -> bool,
138) -> Result<Vec<ExactPropertyEntry>, ExactPropertyError> {
139 let assigned = assigned_sets(model, release, relations, object)?;
140 let mut selector = Selector {
141 set: select_set,
142 property: select_property,
143 };
144 let mut entries = collect(
145 model,
146 release,
147 &assigned.occurrence_sets,
148 ExactSource::Occurrence,
149 &mut selector,
150 )?;
151 if let Some((type_id, sets)) = &assigned.type_sets {
152 let inherited = collect(
153 model,
154 release,
155 sets,
156 ExactSource::Type(*type_id),
157 &mut selector,
158 )?;
159 let overridden: Vec<(Arc<str>, Arc<str>)> = entries
160 .iter()
161 .map(|entry| (entry.property.property_set.clone(), entry.name.clone()))
162 .collect();
163 entries.extend(inherited.into_iter().filter(|entry| {
164 !overridden
165 .iter()
166 .any(|(set, name)| *set == entry.property.property_set && *name == entry.name)
167 }));
168 }
169 Ok(entries)
170}
171
172/// The caller's two selectors.
173pub(super) struct Selector<'a> {
174 pub(super) set: &'a mut dyn FnMut(&str) -> bool,
175 pub(super) property: &'a mut dyn FnMut(&str) -> bool,
176}
177
178/// The selected properties of the selected sets of one source.
179pub(super) fn collect(
180 model: &Model,
181 release: Release,
182 sets: &[EntityId],
183 source: ExactSource,
184 select: &mut Selector<'_>,
185) -> Result<Vec<ExactPropertyEntry>, ExactPropertyError> {
186 let mut entries = Vec::new();
187 let mut selected_sets: BTreeMap<&str, EntityId> = BTreeMap::new();
188 for &set_id in sets {
189 let set = load_source_set(model, release, source, set_id)?;
190 if !(select.set)(set.name) {
191 set.refuse_unselected(release, &mut *select.property)?;
192 continue;
193 }
194 if let Some(first) = set.shares_name(&mut selected_sets) {
195 return Err(ExactPropertyError::DuplicateMatchingSets {
196 source,
197 first,
198 second: set_id,
199 });
200 }
201 let mut chosen: BTreeMap<&str, EntityId> = BTreeMap::new();
202 let mut in_order = Vec::new();
203 for (member, name) in set.members(model, release)? {
204 if !(select.property)(name) {
205 continue;
206 }
207 if let Some(first) = chosen.insert(name, set.member_id(member)) {
208 return Err(ExactPropertyError::DuplicateMatchingProperties {
209 set: set_id,
210 first,
211 second: set.member_id(member),
212 });
213 }
214 in_order.push((member, name));
215 }
216 for (member, name) in in_order {
217 let resolved = set.value(model, release, member)?;
218 // A predefined set without a `Name` shares its entity name with
219 // any other such set: a member both hold is ambiguous, as
220 // `exact_property` finds it.
221 let earlier = entries.iter().find(|entry: &&ExactPropertyEntry| {
222 entry.property.property_set.as_ref() == set.name && entry.name.as_ref() == name
223 });
224 if let Some(earlier) = earlier {
225 return Err(ExactPropertyError::DuplicateMatchingSets {
226 source,
227 first: earlier.property.set_id,
228 second: set_id,
229 });
230 }
231 entries.push(ExactPropertyEntry {
232 name: Arc::from(name),
233 property: set.exact(source, member, resolved),
234 });
235 }
236 }
237 Ok(entries)
238}
239
240/// One assigned property set or quantity set that a set selector picked.
241#[derive(Debug, Clone, PartialEq)]
242#[non_exhaustive]
243pub struct ExactPropertySetEntry {
244 /// The name the selector saw: `Name`, or for a predefined set that states
245 /// none, its entity name (`IfcDoorLiningProperties`).
246 pub name: Arc<str>,
247 /// The set entity.
248 pub set_id: EntityId,
249 /// Whether the object carries the set itself or inherits it from its
250 /// type; a queried type object's own sets are `Type` of that object.
251 pub source: ExactSource,
252 /// How many members the set holds, each validated; `0` for an empty set.
253 pub members: usize,
254}
255
256/// The property sets and quantity sets of `object` whose names `select_set`
257/// picks, empty ones included (#186).
258///
259/// [`exact_properties_where`] lists properties, so a selected set with no
260/// members leaves no trace in its answer. This lists the sets themselves:
261/// an IDS property facet must fail on a matching set that is empty, and it
262/// can tell that case from "no such set" only here. An empty result is a
263/// proven absence of every selected set.
264///
265/// The traversal, model and assignment validation and refusals are those of
266/// [`exact_properties_where`]; every member of a selected set is validated,
267/// though none is resolved. One difference: a set whose `HasProperties` or
268/// `Quantities` is `()` or `$` is listed with `members == 0`. The schema
269/// declares both `SET [1:?]`, so [`exact_properties_where`] refuses such a
270/// set as [`ExactPropertyError::MalformedAggregate`]; here the question is
271/// only whether the set exists, and it does. Occurrence sets come first, then the sets
272/// inherited from the object's `IfcTypeObject`, each in assignment order. A
273/// type set is listed even when an occurrence set has the same name:
274/// overriding works per property, so both sets exist for the object. A
275/// queried `IfcTypeObject` lists its own `HasPropertySets` with
276/// [`ExactSource::Type`] of `object` (#193).
277///
278/// # Errors
279///
280/// Any [`ExactPropertyError`], as for [`exact_properties_where`]; in
281/// particular two selected sets of one name on one source are
282/// [`ExactPropertyError::DuplicateMatchingSets`].
283pub fn exact_property_sets_where<S>(
284 model: &Model,
285 object: EntityId,
286 mut select_set: S,
287) -> Result<Vec<ExactPropertySetEntry>, ExactPropertyError>
288where
289 S: FnMut(&str) -> bool,
290{
291 let release = validate_model(model)?;
292 property_sets_where_in(model, release, None, object, &mut select_set)
293}
294
295/// [`exact_property_sets_where`] once the model is bound to `release`, from
296/// a scan of every relationship when one is given.
297pub(super) fn property_sets_where_in(
298 model: &Model,
299 release: Release,
300 relations: Option<&Relations>,
301 object: EntityId,
302 select_set: &mut dyn FnMut(&str) -> bool,
303) -> Result<Vec<ExactPropertySetEntry>, ExactPropertyError> {
304 let assigned = assigned_sets(model, release, relations, object)?;
305 let mut entries = list_sets(
306 model,
307 release,
308 &assigned.occurrence_sets,
309 ExactSource::Occurrence,
310 select_set,
311 )?;
312 if let Some((type_id, sets)) = &assigned.type_sets {
313 entries.extend(list_sets(
314 model,
315 release,
316 sets,
317 ExactSource::Type(*type_id),
318 select_set,
319 )?);
320 }
321 Ok(entries)
322}
323
324/// The selected sets of one source.
325pub(super) fn list_sets(
326 model: &Model,
327 release: Release,
328 sets: &[EntityId],
329 source: ExactSource,
330 select_set: &mut dyn FnMut(&str) -> bool,
331) -> Result<Vec<ExactPropertySetEntry>, ExactPropertyError> {
332 let mut entries = Vec::new();
333 let mut selected: BTreeMap<&str, EntityId> = BTreeMap::new();
334 for &set_id in sets {
335 let set = load_source_set(model, release, source, set_id)?;
336 if !select_set(set.name) {
337 continue;
338 }
339 if let Some(first) = set.shares_name(&mut selected) {
340 return Err(ExactPropertyError::DuplicateMatchingSets {
341 source,
342 first,
343 second: set_id,
344 });
345 }
346 // An empty or unset member list violates `SET [1:?]`, but the set
347 // exists and holds nothing, which is what an IDS facet must see.
348 let members = if set.member_list_is_empty(release) {
349 0
350 } else {
351 set.members(model, release)?.len()
352 };
353 entries.push(ExactPropertySetEntry {
354 name: Arc::from(set.name),
355 set_id,
356 source,
357 members,
358 });
359 }
360 Ok(entries)
361}