ifc-material 0.3.0

Material definitions: layer sets, profile sets, constituents, usage.
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
413
414
415
416
417
418
419
420
421
422
423
424
//! The IFC release a model's material records are read and written against.
//!
//! Every slot position comes from the bundled table of one release, so an
//! IFC2X3 record is never decoded or authored with IFC4 positions.
//!
//! Binding, from `FILE_SCHEMA`:
//! - one recognised declaration (`IFC2X3`, `IFC4`, `IFC4X3`/`IFC4X3_ADD2`)
//!   binds that release's own table;
//! - one unrecognised declaration fails closed with
//!   [`MaterialError::UnsupportedSchema`];
//! - several declarations fail closed with [`MaterialError::MultipleSchemas`];
//! - no declaration at all (an in-memory [`Model::new`]) binds IFC4, the
//!   0.2.0 behaviour, as `ifc-classification` does since #51.

use ifc_model::{Entity, EntityId, Model, Value};
use ifc_schema::{for_version, Attribute, Schema, SchemaVersion};

use crate::{MaterialError, MaterialResult};

/// Every attribute this crate reads or writes, per entity, under its IFC4
/// ADD2 TC1 name and in IFC4 positional order (inherited attributes first).
///
/// This is the IFC4 layout the crate was written against. Accessors never
/// index it: they ask the bound release for the attribute's position by
/// name. `release_layout` tests pin it against the three bundled tables.
pub(crate) const IFC4_LAYOUT: &[(&str, &[&str])] = &[
    ("IFCMATERIAL", &["Name", "Description", "Category"]),
    (
        "IFCMATERIALCLASSIFICATIONRELATIONSHIP",
        &["MaterialClassifications", "ClassifiedMaterial"],
    ),
    (
        "IFCMATERIALCONSTITUENT",
        &["Name", "Description", "Material", "Fraction", "Category"],
    ),
    (
        "IFCMATERIALCONSTITUENTSET",
        &["Name", "Description", "MaterialConstituents"],
    ),
    (
        "IFCMATERIALDEFINITIONREPRESENTATION",
        &[
            "Name",
            "Description",
            "Representations",
            "RepresentedMaterial",
        ],
    ),
    ("IFCMATERIALLAYER", LAYER),
    (
        "IFCMATERIALLAYERSET",
        &["MaterialLayers", "LayerSetName", "Description"],
    ),
    (
        "IFCMATERIALLAYERSETUSAGE",
        &[
            "ForLayerSet",
            "LayerSetDirection",
            "DirectionSense",
            "OffsetFromReferenceLine",
            "ReferenceExtent",
        ],
    ),
    (
        "IFCMATERIALLAYERWITHOFFSETS",
        &[
            "Material",
            "LayerThickness",
            "IsVentilated",
            "Name",
            "Description",
            "Category",
            "Priority",
            "OffsetDirection",
            "OffsetValues",
        ],
    ),
    ("IFCMATERIALLIST", &["Materials"]),
    ("IFCMATERIALPROFILE", PROFILE),
    (
        "IFCMATERIALPROFILESET",
        &[
            "Name",
            "Description",
            "MaterialProfiles",
            "CompositeProfile",
        ],
    ),
    ("IFCMATERIALPROFILESETUSAGE", PROFILE_USAGE),
    (
        "IFCMATERIALPROFILESETUSAGETAPERING",
        &[
            "ForProfileSet",
            "CardinalPoint",
            "ReferenceExtent",
            "ForProfileEndSet",
            "CardinalEndPoint",
        ],
    ),
    (
        "IFCMATERIALPROFILEWITHOFFSETS",
        &[
            "Name",
            "Description",
            "Material",
            "Profile",
            "Priority",
            "Category",
            "OffsetValues",
        ],
    ),
    (
        "IFCMATERIALPROPERTIES",
        &["Name", "Description", "Properties", "Material"],
    ),
    (
        "IFCMATERIALRELATIONSHIP",
        &[
            "Name",
            "Description",
            "RelatingMaterial",
            "RelatedMaterials",
            "Expression",
        ],
    ),
    ("IFCRELASSOCIATESMATERIAL", ROOT_RELATION_MATERIAL),
    ("IFCRELDEFINESBYTYPE", ROOT_RELATION_TYPE),
];

const LAYER: &[&str] = &[
    "Material",
    "LayerThickness",
    "IsVentilated",
    "Name",
    "Description",
    "Category",
    "Priority",
];
const PROFILE: &[&str] = &[
    "Name",
    "Description",
    "Material",
    "Profile",
    "Priority",
    "Category",
];
const PROFILE_USAGE: &[&str] = &["ForProfileSet", "CardinalPoint", "ReferenceExtent"];
const ROOT_RELATION_MATERIAL: &[&str] = &[
    "GlobalId",
    "OwnerHistory",
    "Name",
    "Description",
    "RelatedObjects",
    "RelatingMaterial",
];
const ROOT_RELATION_TYPE: &[&str] = &[
    "GlobalId",
    "OwnerHistory",
    "Name",
    "Description",
    "RelatedObjects",
    "RelatingType",
];

/// The release a projection or authoring call binds to, or why none could be.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Release<'m> {
    /// Reads and writes resolve against this release's bundled table.
    Bound(SchemaVersion),
    /// The header declares this many schemas.
    Multiple(usize),
    /// The header declares one schema this crate has no table for.
    Unsupported(&'m str),
}

impl<'m> Release<'m> {
    /// The binding of projections built without a model (`try_new`) and of
    /// a model whose header declares no schema.
    pub(crate) const LEGACY: Release<'static> = Release::Bound(SchemaVersion::Ifc4);

    /// The release `model`'s header binds.
    pub(crate) fn of(model: &'m Model) -> Self {
        match model.header().schema.as_slice() {
            [] => Release::LEGACY,
            [token] => SchemaVersion::from_header_token(token)
                .map_or(Self::Unsupported(token.as_str()), Self::Bound),
            tokens => Self::Multiple(tokens.len()),
        }
    }

    /// The bound version and its bundled table.
    pub(crate) fn bound(self) -> MaterialResult<(SchemaVersion, &'static Schema)> {
        match self {
            Self::Bound(version) => Ok((
                version,
                for_version(version).expect("every SchemaVersion has a bundled table"),
            )),
            Self::Multiple(schemas) => Err(MaterialError::MultipleSchemas { schemas }),
            Self::Unsupported(schema) => Err(MaterialError::UnsupportedSchema {
                schema: schema.to_owned(),
            }),
        }
    }

    /// Whether the bound release declares `entity` as an instantiable
    /// (non-abstract) entity. `false` when no release is bound.
    pub(crate) fn instantiates(self, entity: &str) -> bool {
        self.bound()
            .is_ok_and(|(_, schema)| instantiable(schema, entity))
    }

    /// Fail with [`MaterialError::EntityNotInSchema`] unless the bound
    /// release can instantiate `entity`.
    pub(crate) fn require_entity(
        self,
        entity: &'static str,
        id: Option<EntityId>,
    ) -> MaterialResult<(SchemaVersion, &'static Schema)> {
        let (version, schema) = self.bound()?;
        if instantiable(schema, entity) {
            Ok((version, schema))
        } else {
            Err(MaterialError::EntityNotInSchema {
                entity,
                id,
                schema: version,
            })
        }
    }

    /// Position and declaration of `attribute` (IFC4 name) on `entity` in
    /// the bound release.
    ///
    /// A record type the release cannot instantiate is `EntityNotInSchema`;
    /// an attribute it does not declare is `NotInSchema`. Neither is ever a
    /// silent `None` read from a slot the release gives another meaning.
    pub(crate) fn attribute(
        self,
        entity: &'static str,
        id: EntityId,
        attribute: &'static str,
    ) -> MaterialResult<(usize, &'static Attribute)> {
        debug_assert!(
            in_ifc4_layout(entity, attribute),
            "{entity}.{attribute} is read but missing from IFC4_LAYOUT"
        );
        let (version, schema) = self.require_entity(entity, Some(id))?;
        position(schema, version, entity, attribute).ok_or(MaterialError::NotInSchema {
            entity,
            id,
            attribute,
            schema: version,
        })
    }

    /// Position of `attribute` (IFC4 name) on `entity` in the bound release.
    pub(crate) fn slot(
        self,
        entity: &'static str,
        id: EntityId,
        attribute: &'static str,
    ) -> MaterialResult<usize> {
        self.attribute(entity, id, attribute).map(|(slot, _)| slot)
    }

    /// Whether `candidate` is a legal value type of `attribute` on `entity`
    /// in the bound release (entity inheritance and SELECTs included), and
    /// instantiable there.
    pub(crate) fn accepts(
        self,
        entity: &'static str,
        attribute: &'static str,
        candidate: &str,
    ) -> MaterialResult<(bool, &'static str)> {
        let (_, schema) = self.bound()?;
        let declared = self.declared(entity, attribute)?.type_name.as_str();
        Ok((
            instantiable(schema, candidate) && schema.accepts_type(declared, candidate),
            declared,
        ))
    }

    /// The bound release's declaration of `attribute` (IFC4 name) on
    /// `entity`, for authoring: `EntityNotInSchema` or
    /// `AuthoringNotInSchema` when the release lacks either.
    pub(crate) fn declared(
        self,
        entity: &'static str,
        attribute: &'static str,
    ) -> MaterialResult<&'static Attribute> {
        let (version, schema) = self.require_entity(entity, None)?;
        position(schema, version, entity, attribute)
            .map(|(_, declared)| declared)
            .ok_or(MaterialError::AuthoringNotInSchema {
                entity,
                attribute,
                schema: version,
            })
    }

    /// Build `entity`'s record in the bound release's layout from values
    /// named by their IFC4 attribute names.
    ///
    /// A non-null value for an attribute the release does not declare is
    /// refused with `AuthoringNotInSchema` rather than dropped; a null for
    /// an attribute the release requires is refused with
    /// `AuthoringRequired`.
    pub(crate) fn record(
        self,
        entity: &'static str,
        values: Vec<(&'static str, Value)>,
    ) -> MaterialResult<Entity> {
        let (version, schema) = self.require_entity(entity, None)?;
        let declared = schema.attributes(entity);
        let mut slots = vec![None; declared.len()];
        for (attribute, value) in values {
            debug_assert!(
                in_ifc4_layout(entity, attribute),
                "{entity}.{attribute} is written but missing from IFC4_LAYOUT"
            );
            match position(schema, version, entity, attribute) {
                Some((slot, _)) => slots[slot] = Some(value),
                None if value == Value::Null => {}
                None => {
                    return Err(MaterialError::AuthoringNotInSchema {
                        entity,
                        attribute,
                        schema: version,
                    })
                }
            }
        }
        let mut attributes = Vec::with_capacity(slots.len());
        for (slot, value) in slots.into_iter().enumerate() {
            let declaration = declared[slot];
            match value.unwrap_or(Value::Null) {
                Value::Null if !declaration.optional => {
                    return Err(MaterialError::AuthoringRequired {
                        entity,
                        attribute: declaration.name.as_str(),
                        schema: version,
                    })
                }
                value => attributes.push(value),
            }
        }
        Ok(Entity::new(entity, attributes))
    }
}

fn instantiable(schema: &Schema, entity: &str) -> bool {
    schema
        .entity(entity)
        .is_some_and(|declaration| !declaration.abstract_)
}

fn position(
    schema: &'static Schema,
    version: SchemaVersion,
    entity: &str,
    attribute: &'static str,
) -> Option<(usize, &'static Attribute)> {
    let name = release_name(version, entity, attribute);
    schema
        .attributes(entity)
        .into_iter()
        .enumerate()
        .find(|(_, declared)| declared.name.eq_ignore_ascii_case(name))
}

fn in_ifc4_layout(entity: &str, attribute: &str) -> bool {
    IFC4_LAYOUT.iter().any(|(declared, attributes)| {
        declared.eq_ignore_ascii_case(entity) && attributes.contains(&attribute)
    })
}

/// The name `release` gives the attribute this crate knows by its IFC4 name.
///
/// IFC4X3 ADD2 renamed `IfcMaterialRelationship.Expression` to
/// `MaterialExpression`; it keeps its position (5th, after the two inherited
/// `IfcResourceLevelRelationship` attributes) and meaning. No other material
/// attribute is renamed between IFC2X3 TC1, IFC4 ADD2 TC1 and IFC4X3 ADD2.
pub(crate) fn release_name(
    release: SchemaVersion,
    entity: &str,
    attribute: &'static str,
) -> &'static str {
    match (release, attribute) {
        (SchemaVersion::Ifc4x3, "Expression")
            if entity.eq_ignore_ascii_case("IFCMATERIALRELATIONSHIP") =>
        {
            "MaterialExpression"
        }
        _ => attribute,
    }
}

/// Whether `entity` is an instantiable `IfcTypeObject` in the bound
/// release, the only legal `IfcRelDefinesByType.RelatingType`.
///
/// Read from the release's table: IFC4X3 removed `IfcDoorStyle` and
/// `IfcWindowStyle`, and IFC2X3 has no `IfcTypeProcess` branch.
pub(crate) fn is_type_object(release: Release<'_>, entity: &str) -> MaterialResult<bool> {
    let (_, schema) = release.bound()?;
    Ok(instantiable(schema, entity) && schema.is_a(entity, "IFCTYPEOBJECT"))
}

/// The IFC release `model`'s material records are read and written against.
///
/// A consumer binds its own vocabulary with this answer instead of
/// re-parsing the header. A header with no declaration binds IFC4 (an
/// in-memory model; the 0.2.0 behaviour).
///
/// # Errors
///
/// [`MaterialError::MultipleSchemas`] when the header declares several
/// schemas, and [`MaterialError::UnsupportedSchema`] when it declares one
/// with no bundled table.
pub fn material_schema(model: &Model) -> MaterialResult<SchemaVersion> {
    Release::of(model).bound().map(|(version, _)| version)
}

#[cfg(test)]
mod tests;