ifc-systems 0.2.3

Distribution systems, ports, and connectivity between elements.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Release-bound variants of the systems writers, with a caller-supplied
//! `IfcOwnerHistory` (#202).
//!
//! Each binds the model's declared release, runs the plain writer's draft
//! checks, lays the record out by attribute name from the release's table
//! and sets `OwnerHistory`, which IFC2X3 requires. The attribute names are
//! the ones the readers' slot constants stand for; the unit test below pins
//! each name to its constant in all three bundled releases.

use ifc_model::{EntityId, Model, Transaction, Value};

use super::release::bind;
use super::{check_ports, check_related, guid, refs, SystemAuthoringResult};

fn text(value: Option<&str>) -> Value {
    value.map_or(Value::Null, |t| Value::Text(t.into()))
}

/// [`create_system`](super::create_system) in `model`'s declared release,
/// with a caller-supplied `IfcOwnerHistory`, which IFC2X3 requires.
///
/// The release is bound from `FILE_SCHEMA` (none binds IFC4). The owner
/// history is never invented: build it with `ifc-author`.
///
/// # Errors
///
/// Those of [`create_system`](super::create_system), and:
/// [`MultipleSchemas`](super::SystemAuthoringError::MultipleSchemas) or
/// [`UnsupportedSchema`](super::SystemAuthoringError::UnsupportedSchema) if
/// the model binds no single known release;
/// [`MissingReference`](super::SystemAuthoringError::MissingReference) if
/// `owner_history` is neither in the model nor staged;
/// [`WrongReferenceType`](super::SystemAuthoringError::WrongReferenceType)
/// if it is not an `IfcOwnerHistory`. Nothing is staged on an error.
pub fn create_system_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    name: Option<&str>,
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCSYSTEM";
    let release = bind(model)?;
    guid(ENTITY, global_id)?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("Name", text(name)),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

/// [`create_port`](super::create_port) in `model`'s declared release, with
/// a caller-supplied `IfcOwnerHistory`.
///
/// IFC2X3 declares eight attributes for `IfcDistributionPort`, IFC4 and
/// IFC4X3 ten; `FlowDirection` is the eighth in all three.
///
/// # Errors
///
/// Those of [`create_port`](super::create_port), the release and
/// owner-history refusals of [`create_system_with_owner_history`], and
/// [`AuthoringValueType`](super::SystemAuthoringError::AuthoringValueType)
/// for a token the release's `IfcFlowDirectionEnum` does not declare.
pub fn create_port_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    name: Option<&str>,
    flow_direction: Option<&str>,
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCDISTRIBUTIONPORT";
    let release = bind(model)?;
    guid(ENTITY, global_id)?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("Name", text(name)),
        (
            "FlowDirection",
            flow_direction.map_or(Value::Null, |t| Value::Enum(t.into())),
        ),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

/// [`assign_to_group`](super::assign_to_group) in `model`'s declared
/// release, with a caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of [`assign_to_group`](super::assign_to_group), and the release
/// and owner-history refusals of [`create_system_with_owner_history`].
pub fn assign_to_group_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    group: EntityId,
    members: &[EntityId],
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCRELASSIGNSTOGROUP";
    let release = bind(model)?;
    check_related(ENTITY, "RelatedObjects", global_id, group, members, "group")?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("RelatedObjects", refs(members)),
        ("RelatingGroup", Value::Ref(group)),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

/// [`nest_ports`](super::nest_ports) in `model`'s declared release, with a
/// caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of [`nest_ports`](super::nest_ports), and the release and
/// owner-history refusals of [`create_system_with_owner_history`].
pub fn nest_ports_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    parent: EntityId,
    children: &[EntityId],
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCRELNESTS";
    let release = bind(model)?;
    check_related(
        ENTITY,
        "RelatedObjects",
        global_id,
        parent,
        children,
        "parent",
    )?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("RelatingObject", Value::Ref(parent)),
        ("RelatedObjects", refs(children)),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

/// [`connect_port_to_element`](super::connect_port_to_element) in
/// `model`'s declared release, with a caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of [`connect_port_to_element`](super::connect_port_to_element),
/// and the release and owner-history refusals of
/// [`create_system_with_owner_history`].
pub fn connect_port_to_element_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    port: EntityId,
    element: EntityId,
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCRELCONNECTSPORTTOELEMENT";
    let release = bind(model)?;
    guid(ENTITY, global_id)?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("RelatingPort", Value::Ref(port)),
        ("RelatedElement", Value::Ref(element)),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

/// [`connect_ports`](super::connect_ports) in `model`'s declared release,
/// with a caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of [`connect_ports`](super::connect_ports), and the release and
/// owner-history refusals of [`create_system_with_owner_history`].
pub fn connect_ports_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    relating: EntityId,
    related: EntityId,
    realizing: Option<EntityId>,
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCRELCONNECTSPORTS";
    let release = bind(model)?;
    check_ports(global_id, relating, related)?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("RelatingPort", Value::Ref(relating)),
        ("RelatedPort", Value::Ref(related)),
        (
            "RealizingElement",
            realizing.map_or(Value::Null, Value::Ref),
        ),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

/// [`contain_in_spatial_structure`](super::contain_in_spatial_structure) in
/// `model`'s declared release, with a caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of
/// [`contain_in_spatial_structure`](super::contain_in_spatial_structure),
/// and the release and owner-history refusals of
/// [`create_system_with_owner_history`].
pub fn contain_in_spatial_structure_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    structure: EntityId,
    elements: &[EntityId],
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    place(
        tx,
        model,
        "IFCRELCONTAINEDINSPATIALSTRUCTURE",
        global_id,
        structure,
        elements,
        owner_history,
    )
}

/// [`reference_in_spatial_structure`](super::reference_in_spatial_structure)
/// in `model`'s declared release, with a caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of
/// [`reference_in_spatial_structure`](super::reference_in_spatial_structure),
/// and the release and owner-history refusals of
/// [`create_system_with_owner_history`].
pub fn reference_in_spatial_structure_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    structure: EntityId,
    elements: &[EntityId],
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    place(
        tx,
        model,
        "IFCRELREFERENCEDINSPATIALSTRUCTURE",
        global_id,
        structure,
        elements,
        owner_history,
    )
}

fn place(
    tx: &mut Transaction,
    model: &Model,
    entity: &'static str,
    global_id: &str,
    structure: EntityId,
    elements: &[EntityId],
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    let release = bind(model)?;
    check_related(
        entity,
        "RelatedElements",
        global_id,
        structure,
        elements,
        "structure",
    )?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("RelatedElements", refs(elements)),
        ("RelatingStructure", Value::Ref(structure)),
    ];
    release.stage(tx, model, entity, values, owner_history)
}

/// [`create_group`](super::create_group) in `model`'s declared release,
/// with a caller-supplied `IfcOwnerHistory`.
///
/// # Errors
///
/// Those of [`create_group`](super::create_group), and the release and
/// owner-history refusals of [`create_system_with_owner_history`].
pub fn create_group_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    name: Option<&str>,
    description: Option<&str>,
    owner_history: EntityId,
) -> SystemAuthoringResult<EntityId> {
    const ENTITY: &str = "IFCGROUP";
    let release = bind(model)?;
    guid(ENTITY, global_id)?;
    let values = vec![
        ("GlobalId", Value::Text(global_id.into())),
        ("Name", text(name)),
        ("Description", text(description)),
    ];
    release.stage(tx, model, ENTITY, values, owner_history)
}

#[cfg(test)]
mod tests {
    use ifc_schema::{for_version, SchemaVersion};

    use crate::authoring::distribution::{distribution_slot, spatial_zone_slot, zone_slot};
    use crate::connectivity::relation::slot as connects_slot;
    use crate::port::definition::slot as port_slot;
    use crate::system::group::slot as group_slot;
    use crate::zone::spatial_group::slot as placement_slot;

    /// Every attribute name the variants write is the one the reader's (and
    /// the plain writer's) slot constant stands for, in all three releases
    /// that declare the entity.
    #[test]
    fn names_agree_with_the_slot_constants() {
        let pinned: &[(&str, &str, usize)] = &[
            (
                "IFCRELASSIGNSTOGROUP",
                "RelatedObjects",
                group_slot::ASSIGNS_RELATED,
            ),
            (
                "IFCRELASSIGNSTOGROUP",
                "RelatingGroup",
                group_slot::ASSIGNS_GROUP,
            ),
            ("IFCRELNESTS", "RelatingObject", port_slot::NESTS_PARENT),
            ("IFCRELNESTS", "RelatedObjects", port_slot::NESTS_CHILDREN),
            (
                "IFCRELCONNECTSPORTTOELEMENT",
                "RelatingPort",
                port_slot::PORT_TO_ELEMENT_PORT,
            ),
            (
                "IFCRELCONNECTSPORTTOELEMENT",
                "RelatedElement",
                port_slot::PORT_TO_ELEMENT_ELEMENT,
            ),
            (
                "IFCDISTRIBUTIONPORT",
                "FlowDirection",
                port_slot::FLOW_DIRECTION,
            ),
            (
                "IFCRELCONNECTSPORTS",
                "RelatingPort",
                connects_slot::RELATING,
            ),
            ("IFCRELCONNECTSPORTS", "RelatedPort", connects_slot::RELATED),
            (
                "IFCRELCONNECTSPORTS",
                "RealizingElement",
                connects_slot::REALIZING,
            ),
            (
                "IFCRELCONTAINEDINSPATIALSTRUCTURE",
                "RelatedElements",
                placement_slot::RELATED_ELEMENTS,
            ),
            (
                "IFCRELCONTAINEDINSPATIALSTRUCTURE",
                "RelatingStructure",
                placement_slot::RELATING_STRUCTURE,
            ),
            (
                "IFCRELREFERENCEDINSPATIALSTRUCTURE",
                "RelatedElements",
                placement_slot::RELATED_ELEMENTS,
            ),
            (
                "IFCRELREFERENCEDINSPATIALSTRUCTURE",
                "RelatingStructure",
                placement_slot::RELATING_STRUCTURE,
            ),
            ("IFCFLOWSEGMENT", "Name", distribution_slot::NAME),
            (
                "IFCFLOWSEGMENT",
                "Description",
                distribution_slot::DESCRIPTION,
            ),
            (
                "IFCFLOWSEGMENT",
                "ObjectPlacement",
                distribution_slot::OBJECT_PLACEMENT,
            ),
            (
                "IFCFLOWSEGMENT",
                "Representation",
                distribution_slot::REPRESENTATION,
            ),
            ("IFCFLOWSEGMENT", "Tag", distribution_slot::TAG),
            ("IFCZONE", "Name", zone_slot::NAME),
            ("IFCZONE", "LongName", zone_slot::LONG_NAME),
            ("IFCSPATIALZONE", "LongName", spatial_zone_slot::LONG_NAME),
            (
                "IFCSPATIALZONE",
                "PredefinedType",
                spatial_zone_slot::PREDEFINED_TYPE,
            ),
        ];
        let mut checked = 0;
        for version in [
            SchemaVersion::Ifc2x3,
            SchemaVersion::Ifc4,
            SchemaVersion::Ifc4x3,
        ] {
            let schema = for_version(version).unwrap();
            for (entity, name, slot) in pinned {
                let names = schema.attribute_names(entity);
                if schema.entity(entity).is_none() {
                    continue;
                }
                if let Some(found) = names.iter().position(|n| n == name) {
                    assert_eq!(found, *slot, "{version:?} {entity}.{name}");
                    checked += 1;
                } else {
                    // Only IFC2X3 may lack one: IfcZone.LongName.
                    assert_eq!(
                        (version, *entity, *name),
                        (SchemaVersion::Ifc2x3, "IFCZONE", "LongName")
                    );
                }
            }
        }
        assert!(checked >= 60, "the pin table matched {checked} names");
    }
}