Skip to main content

ifc_properties/query/
quantity.rs

1//! Resolving which quantity sets apply to an object.
2//!
3//! The quantity counterpart of `properties_of` / `resolved_properties`, with
4//! the same precedence rule (`assignment.rs`): an occurrence inherits the
5//! `IfcElementQuantity` sets of its type, and a same-named set on the
6//! occurrence wins, the type's being kept as shadowed.
7//!
8//! # Schema facts
9//!
10//! `IfcTypeObject.HasPropertySets` holds `IfcPropertySetDefinition`s, and
11//! `IfcElementQuantity` is one in every release, so a type states quantity
12//! sets in the same attribute as property sets. An occurrence takes them
13//! through `IfcRelDefinesByProperties`. No release forbids two same-named
14//! quantity sets on one owner (`UniquePropertySetNames` counts only
15//! `IfcPropertySet`), so the view is keyed by name and reports the repeat as
16//! [`PropertyAnomaly::DuplicateSetName`].
17//!
18//! # Precedence is per set
19//!
20//! The winner is chosen per set by name, not per quantity. An occurrence set
21//! `Qto_WallBaseQuantities` hides the type's set of that name completely: a
22//! quantity only the type states is not inherited through it. The exact path
23//! (`exact_properties_where`) resolves per property instead. Property sets
24//! and quantity sets are separate namespaces here: a property set and a
25//! quantity set of one name never shadow each other (#546).
26//!
27//! # Units
28//!
29//! The view does not resolve units. A [`Quantity`] carries the unit it
30//! states, if any; the effective unit is
31//! `exact_unit(model, kind.measure_type(), unit)`, which falls back to the
32//! project unit when none is stated. [`stated_unit`](crate::stated_unit)
33//! answers only for what the quantity states.
34//!
35//! # Cost
36//!
37//! [`quantities_of`] scans the whole model for one object. Use
38//! [`resolved_quantities`] when asking about many.
39
40use std::collections::BTreeMap;
41
42use ifc_model::{EntityId, Model};
43
44use crate::error::PropertyAnomaly;
45use crate::quantity::{quantity_sets_by_object, Quantity, QuantitySet};
46use crate::query::assignment::{resolve, NamedSet, Source};
47
48impl NamedSet for QuantitySet {
49    fn set_id(&self) -> EntityId {
50        self.id
51    }
52
53    fn set_name(&self) -> Option<&str> {
54        self.name.as_deref()
55    }
56}
57
58/// A quantity set that applies to an object, with its provenance.
59#[derive(Debug, Clone, PartialEq)]
60#[non_exhaustive]
61pub struct ResolvedQuantitySet {
62    /// Where it came from.
63    pub source: Source,
64    /// The set itself.
65    pub set: QuantitySet,
66    /// A same-named set this one overrode, if any.
67    ///
68    /// Populated when an occurrence set shadows a type set, so a caller can
69    /// explain why a quantity differs from the type default.
70    pub shadowed: Option<Box<ResolvedQuantitySet>>,
71}
72
73/// Resolved quantity sets per object, after precedence is applied.
74pub type ResolvedQuantities = BTreeMap<EntityId, Vec<ResolvedQuantitySet>>;
75
76/// Every quantity set applying to every object, precedence applied.
77///
78/// Sets are ordered by name (an unnamed one by `#<id>`), so output is
79/// deterministic. An object appears only with at least one set. A type
80/// object gets only the sets attached to it by the forbidden relationship
81/// (reported as [`PropertyAnomaly::TypeAttachedByRelationship`]); its own
82/// sets are in [`quantity_sets_by_object`](crate::quantity_sets_by_object).
83///
84/// Anomalies are those of `quantity_sets_by_object`, those of resolving the
85/// type assignments, and [`PropertyAnomaly::DuplicateSetName`] for two
86/// same-named quantity sets on one owner. `MissingDefinition`, `TypedTwice`
87/// and the `IfcRelDefinesByType` anomalies appear in the lists of both this
88/// view and the property view.
89pub fn resolved_quantities(model: &Model) -> (ResolvedQuantities, Vec<PropertyAnomaly>) {
90    let (direct, mut anomalies) = quantity_sets_by_object(model);
91    let out = resolve(model, &direct, &mut anomalies)
92        .into_iter()
93        .map(|(object, sets)| {
94            let sets = sets
95                .into_iter()
96                .map(|p| ResolvedQuantitySet {
97                    source: p.source,
98                    set: p.set,
99                    shadowed: p.shadowed.map(|(source, set)| {
100                        Box::new(ResolvedQuantitySet {
101                            source,
102                            set,
103                            shadowed: None,
104                        })
105                    }),
106                })
107                .collect();
108            (object, sets)
109        })
110        .collect();
111    (out, anomalies)
112}
113
114/// Quantity sets applying to one object.
115///
116/// Scans the whole model; prefer [`resolved_quantities`] for many objects.
117pub fn quantities_of(model: &Model, object: EntityId) -> Vec<ResolvedQuantitySet> {
118    resolved_quantities(model)
119        .0
120        .remove(&object)
121        .unwrap_or_default()
122}
123
124/// Find a single quantity by set name and quantity name.
125///
126/// Returns the quantity from the occurrence's set of that name when the
127/// occurrence states one, else from the type's. A quantity absent from the
128/// winning set is `None` even if the shadowed type set has it.
129pub fn quantity_value<'a>(
130    resolved: &'a [ResolvedQuantitySet],
131    set_name: &str,
132    quantity_name: &str,
133) -> Option<&'a Quantity> {
134    resolved
135        .iter()
136        .find(|r| r.set.name.as_deref() == Some(set_name))
137        .and_then(|r| r.set.quantity(quantity_name))
138}