ifc_lite_processing/data_model/
property.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! The property-set decoder every consumer of the data model reads through
6//! (C6-7, #7783): which property kinds exist, which of them a caller asks
7//! for, and the one per-model complex-property budget (#7675).
8//!
9//! The server's Parquet tables and the attribute export (CSV, JSON, JSON-LD,
10//! IFCX, USD, BOS, Python) used to decode a property set twice, with two
11//! readers that disagreed on what a set holds. Both now take a
12//! [`PropertySetRow`] from here. Two encoders stay, because each is a pinned
13//! contract: the server renders [`PropertyRow::display`], the columnar wire
14//! the viewer decodes, and the export renders [`PropertyRow::nominal`], the
15//! raw value, in its own flat form.
16//!
17//! There is no budget-less constructor: [`PropertyDecoder`] owns the model's
18//! [`ComplexPropertyBudget`] and its per-id memo, so no caller can walk an
19//! `IfcComplexProperty` without them (the k^8 fan-out #7675 closed).
20
21use super::properties::{extract_property, resolve_complex_into, ComplexMemo};
22use super::types::Property;
23use ifc_lite_core::limits::ComplexPropertyBudget;
24use ifc_lite_core::{slot, AttributeValue, DecodedEntity, EntityDecoder, IfcType};
25
26/// The seven `IfcProperty` subtypes a property set lists.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum PropertyKind {
30    Single,
31    Enumerated,
32    List,
33    Bounded,
34    Table,
35    Reference,
36    Complex,
37}
38
39impl PropertyKind {
40    /// The kind of a property entity, `None` for any other entity.
41    pub fn of(ty: &IfcType) -> Option<Self> {
42        if *ty == IfcType::IfcPropertySingleValue {
43            return Some(Self::Single);
44        }
45        Some(match ty.as_str().to_ascii_uppercase().as_str() {
46            "IFCPROPERTYENUMERATEDVALUE" => Self::Enumerated,
47            "IFCPROPERTYLISTVALUE" => Self::List,
48            "IFCPROPERTYBOUNDEDVALUE" => Self::Bounded,
49            "IFCPROPERTYTABLEVALUE" => Self::Table,
50            "IFCPROPERTYREFERENCEVALUE" => Self::Reference,
51            "IFCCOMPLEXPROPERTY" => Self::Complex,
52            _ => return None,
53        })
54    }
55
56    /// The STEP class keyword (the flat exports' `value_type` for a kind that
57    /// carries no member type tag, D47).
58    pub fn class_keyword(self) -> &'static str {
59        match self {
60            Self::Single => "IFCPROPERTYSINGLEVALUE",
61            Self::Enumerated => "IFCPROPERTYENUMERATEDVALUE",
62            Self::List => "IFCPROPERTYLISTVALUE",
63            Self::Bounded => "IFCPROPERTYBOUNDEDVALUE",
64            Self::Table => "IFCPROPERTYTABLEVALUE",
65            Self::Reference => "IFCPROPERTYREFERENCEVALUE",
66            Self::Complex => "IFCCOMPLEXPROPERTY",
67        }
68    }
69}
70
71/// Which property kinds a caller reads. A kind outside the scope is not
72/// decoded at all: it costs no work and spends none of the complex budget.
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74#[non_exhaustive]
75pub enum PropertyScope {
76    /// `IfcPropertySingleValue` only, with its raw nominal value and no
77    /// display form.
78    SingleValueOnly,
79    /// Every kind, as the flat exports write it (EXPORT-DATA-2, D47): a single
80    /// value carries only its raw nominal value (the flat encoder renders that
81    /// and never reads a display form, so none is built), every other kind
82    /// carries its display form.
83    Flat,
84    /// Every kind, each with its display form; a single value also carries
85    /// its raw nominal value. What the server's tables read.
86    AllKinds,
87}
88
89/// One decoded property.
90#[derive(Debug, Clone)]
91pub struct PropertyRow {
92    /// The property's `Name` (attribute 0). May be empty.
93    pub name: String,
94    pub kind: PropertyKind,
95    /// The raw `NominalValue` of an `IfcPropertySingleValue`: `None` for every
96    /// other kind, and for a single value that has no such attribute.
97    pub nominal: Option<AttributeValue>,
98    /// The columnar display form: `Some` for every row under
99    /// [`PropertyScope::AllKinds`] and for every non-single row under
100    /// [`PropertyScope::Flat`]; `None` otherwise.
101    pub display: Option<Property>,
102}
103
104/// One decoded `IfcPropertySet`.
105#[derive(Debug, Clone)]
106pub struct PropertySetRow {
107    pub id: u32,
108    /// The set's `Name`, `None` when it is not a string.
109    pub name: Option<String>,
110    pub properties: Vec<PropertyRow>,
111}
112
113/// Decodes property sets under one model's complex-property budget.
114pub struct PropertyDecoder {
115    scope: PropertyScope,
116    budget: ComplexPropertyBudget,
117    memo: ComplexMemo,
118}
119
120impl PropertyDecoder {
121    /// A decoder for a model of `input_len` bytes. The budget is per model,
122    /// so build one per export or extraction, not one per set.
123    pub fn for_input(input_len: usize, scope: PropertyScope) -> Self {
124        Self { scope, budget: ComplexPropertyBudget::for_input(input_len), memo: ComplexMemo::new() }
125    }
126
127    pub fn scope(&self) -> PropertyScope {
128        self.scope
129    }
130
131    /// Complex values the budget has cut so far.
132    pub fn cuts(&self) -> usize {
133        self.budget.cuts()
134    }
135
136    /// Decode one `IfcPropertySet`. `None` when `HasProperties` (attribute 4)
137    /// is absent or a member list does not resolve; a member that is not in
138    /// scope, or does not decode, is left out and the set keeps the rest.
139    ///
140    /// Complex properties are charged to the budget in the order the sets
141    /// are decoded, so the property a budget cuts is the same on every run
142    /// for one decoding order.
143    pub fn decode_set(&mut self, decoder: &mut EntityDecoder, def: &DecodedEntity) -> Option<PropertySetRow> {
144        let name = def.string_at(slot::IfcPropertySet::NAME).map(str::to_string);
145        let members = decoder.resolve_ref_list(def.attr(slot::IfcPropertySet::HAS_PROPERTIES)?).ok()?;
146        let mut properties = Vec::new();
147        for member in &members {
148            let Some(kind) = PropertyKind::of(&member.ifc_type) else { continue };
149            if self.scope == PropertyScope::SingleValueOnly && kind != PropertyKind::Single {
150                continue;
151            }
152            let Some(row) = self.decode_property(decoder, member, kind) else { continue };
153            properties.push(row);
154        }
155        Some(PropertySetRow { id: def.id, name, properties })
156    }
157
158    fn decode_property(
159        &mut self, decoder: &mut EntityDecoder, entity: &DecodedEntity, kind: PropertyKind,
160    ) -> Option<PropertyRow> {
161        if kind == PropertyKind::Single && self.scope != PropertyScope::AllKinds {
162            let name = entity.string_at(slot::IfcPropertySingleValue::NAME)?.to_string();
163            return Some(PropertyRow { name, kind, nominal: entity.attr(slot::IfcPropertySingleValue::NOMINAL_VALUE).cloned(), display: None });
164        }
165        let mut display = extract_property(entity, decoder)?;
166        if kind == PropertyKind::Complex {
167            resolve_complex_into(&mut display, entity.id, decoder, &mut self.budget, &mut self.memo);
168        }
169        let nominal = if kind == PropertyKind::Single { entity.attr(slot::IfcPropertySingleValue::NOMINAL_VALUE).cloned() } else { None };
170        Some(PropertyRow { name: display.property_name.clone(), kind, nominal, display: Some(display) })
171    }
172}
173
174#[cfg(test)]
175#[path = "property_tests.rs"]
176mod tests;