ifc-validate 0.4.0

Schema conformance: WHERE rules, cardinality, GUID and reference integrity.
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
//! Native rules.

use ifc_model::{Entity, EntityId, Model, Value};
use ifc_schema::Schema;
use ifc_validate::{validate, Report};

fn entity(schema: &Schema, type_name: &str, values: &[(&str, Value)]) -> Entity {
    let names = schema.attribute_names(type_name);
    let mut attributes = vec![Value::Null; names.len()];
    for (name, value) in values {
        let index = names
            .iter()
            .position(|candidate| candidate.eq_ignore_ascii_case(name))
            .unwrap_or_else(|| panic!("{type_name} has no {name} in {}", schema.name()));
        attributes[index] = value.clone();
    }
    Entity::new(type_name, attributes)
}

fn has(report: &Report, rule: &str) -> bool {
    report.findings().iter().any(|finding| finding.rule == rule)
}

#[test]
fn external_references_require_identity_in_every_bundled_schema() {
    for schema in [
        ifc_schema::ifc2x3(),
        ifc_schema::ifc4(),
        ifc_schema::ifc4x3(),
    ] {
        let mut invalid = Model::new();
        invalid.push(entity(schema, "IFCCLASSIFICATIONREFERENCE", &[]));
        assert!(has(&validate(&invalid, schema), "IfcExternalReference.WR1"));

        let mut valid = Model::new();
        valid.push(entity(
            schema,
            "IFCCLASSIFICATIONREFERENCE",
            &[("Location", Value::Text("urn:classification".into()))],
        ));
        assert!(!has(&validate(&valid, schema), "IfcExternalReference.WR1"));
    }
}

#[test]
fn sequence_endpoints_must_differ_in_every_bundled_schema() {
    for schema in [
        ifc_schema::ifc2x3(),
        ifc_schema::ifc4(),
        ifc_schema::ifc4x3(),
    ] {
        let rule = if schema.version() == Some(ifc_schema::SchemaVersion::Ifc2x3) {
            "IfcRelSequence.WR1"
        } else {
            "IfcRelSequence.AvoidInconsistentSequence"
        };
        let mut invalid = Model::new();
        let first = invalid.push(entity(schema, "IFCTASK", &[]));
        invalid.push(entity(
            schema,
            "IFCRELSEQUENCE",
            &[
                ("RelatingProcess", Value::Ref(first)),
                ("RelatedProcess", Value::Ref(first)),
            ],
        ));
        assert!(has(&validate(&invalid, schema), rule));

        let mut valid = Model::new();
        let first = valid.push(entity(schema, "IFCTASK", &[]));
        let second = valid.push(entity(schema, "IFCTASK", &[]));
        valid.push(entity(
            schema,
            "IFCRELSEQUENCE",
            &[
                ("RelatingProcess", Value::Ref(first)),
                ("RelatedProcess", Value::Ref(second)),
            ],
        ));
        assert!(!has(&validate(&valid, schema), rule));
    }
}

#[test]
fn decomposition_self_references_are_rejected_only_where_declared() {
    for schema in [ifc_schema::ifc4(), ifc_schema::ifc4x3()] {
        for (relation, rule) in [
            ("IFCRELAGGREGATES", "IfcRelAggregates.NoSelfReference"),
            ("IFCRELNESTS", "IfcRelNests.NoSelfReference"),
        ] {
            let mut model = Model::new();
            let object = model.push(entity(schema, "IFCPROJECT", &[]));
            model.push(entity(
                schema,
                relation,
                &[
                    ("RelatingObject", Value::Ref(object)),
                    ("RelatedObjects", Value::List(vec![Value::Ref(object)])),
                ],
            ));
            assert!(has(&validate(&model, schema), rule));

            let mut model = Model::new();
            let parent = model.push(entity(schema, "IFCPROJECT", &[]));
            let child = model.push(entity(schema, "IFCPROJECT", &[]));
            model.push(entity(
                schema,
                relation,
                &[
                    ("RelatingObject", Value::Ref(parent)),
                    ("RelatedObjects", Value::List(vec![Value::Ref(child)])),
                ],
            ));
            assert!(!has(&validate(&model, schema), rule));
        }
    }

    let schema = ifc_schema::ifc2x3();
    let mut model = Model::new();
    let object = model.push(entity(schema, "IFCPROJECT", &[]));
    model.push(entity(
        schema,
        "IFCRELAGGREGATES",
        &[
            ("RelatingObject", Value::Ref(object)),
            ("RelatedObjects", Value::List(vec![Value::Ref(object)])),
        ],
    ));
    assert!(!has(
        &validate(&model, schema),
        "IfcRelAggregates.NoSelfReference"
    ));
}

#[test]
fn material_layer_priority_is_bounded_in_ifc4_and_ifc4x3() {
    for schema in [ifc_schema::ifc4(), ifc_schema::ifc4x3()] {
        for priority in [-1, 101] {
            let mut model = Model::new();
            model.push(entity(
                schema,
                "IFCMATERIALLAYER",
                &[
                    ("LayerThickness", Value::Real(1.0)),
                    ("Priority", Value::Integer(priority)),
                ],
            ));
            assert!(has(
                &validate(&model, schema),
                "IfcMaterialLayer.NormalizedPriority"
            ));
        }
        for priority in [0, 100] {
            let mut model = Model::new();
            model.push(entity(
                schema,
                "IFCMATERIALLAYER",
                &[
                    ("LayerThickness", Value::Real(1.0)),
                    ("Priority", Value::Integer(priority)),
                ],
            ));
            assert!(!has(
                &validate(&model, schema),
                "IfcMaterialLayer.NormalizedPriority"
            ));
        }
    }
}

/// `NoSelfReference` on each assignment subtype: an object cannot be
/// assigned to itself. Each subtype names its relating end differently,
/// so all four are exercised.
#[test]
fn assignment_relations_reject_assigning_an_object_to_itself() {
    let schema = ifc_schema::ifc4();
    let cases = [
        (
            "IFCRELASSIGNSTOACTOR",
            "RelatingActor",
            "IfcRelAssignsToActor.NoSelfReference",
        ),
        (
            "IFCRELASSIGNSTOPROCESS",
            "RelatingProcess",
            "IfcRelAssignsToProcess.NoSelfReference",
        ),
        (
            "IFCRELASSIGNSTOPRODUCT",
            "RelatingProduct",
            "IfcRelAssignsToProduct.NoSelfReference",
        ),
        (
            "IFCRELASSIGNSTOGROUPBYFACTOR",
            "RelatingGroup",
            "IfcRelAssignsToGroup.NoSelfReference",
        ),
    ];
    for (relation, relating, rule) in cases {
        // The relating entity also appears in RelatedObjects: self-assignment.
        let mut invalid = Model::new();
        let subject = EntityId(1);
        invalid.insert(subject, entity(schema, "IFCWALL", &[]));
        invalid.push(entity(
            schema,
            relation,
            &[
                (relating, Value::Ref(subject)),
                ("RelatedObjects", Value::List(vec![Value::Ref(subject)])),
            ],
        ));
        assert!(has(&validate(&invalid, schema), rule), "{rule} must fire");

        // A distinct target is conformant.
        let mut valid = Model::new();
        let other = EntityId(2);
        valid.insert(subject, entity(schema, "IFCWALL", &[]));
        valid.insert(other, entity(schema, "IFCWALL", &[]));
        valid.push(entity(
            schema,
            relation,
            &[
                (relating, Value::Ref(subject)),
                ("RelatedObjects", Value::List(vec![Value::Ref(other)])),
            ],
        ));
        assert!(
            !has(&validate(&valid, schema), rule),
            "{rule} false positive"
        );
    }
}

/// Path-connection priorities are bounded 0..=100 inclusive, and an empty
/// list is explicitly conformant per the schema's OR clause.
#[test]
fn path_connection_priorities_are_bounded() {
    let schema = ifc_schema::ifc4();
    let rule = "IfcRelConnectsPathElements.NormalizedRelatingPriorities";

    let mut invalid = Model::new();
    invalid.push(entity(
        schema,
        "IFCRELCONNECTSPATHELEMENTS",
        &[("RelatingPriorities", Value::List(vec![Value::Integer(101)]))],
    ));
    assert!(has(&validate(&invalid, schema), rule));

    // 0 and 100 are inside the inclusive range.
    let mut edges = Model::new();
    edges.push(entity(
        schema,
        "IFCRELCONNECTSPATHELEMENTS",
        &[(
            "RelatingPriorities",
            Value::List(vec![Value::Integer(0), Value::Integer(100)]),
        )],
    ));
    assert!(!has(&validate(&edges, schema), rule), "0 and 100 are legal");

    // An empty list satisfies the rule's first disjunct.
    let mut empty = Model::new();
    empty.push(entity(
        schema,
        "IFCRELCONNECTSPATHELEMENTS",
        &[("RelatingPriorities", Value::List(Vec::new()))],
    ));
    assert!(!has(&validate(&empty, schema), rule), "empty is conformant");
}

/// `CorrectPhysOrVirt` across all three concrete boundary subtypes.
///
/// The 2nd-level form is the one real BEM exports write, and it is
/// invisible to a query for the supertype, so each is checked explicitly.
#[test]
fn space_boundary_physicality_matches_the_related_element() {
    let schema = ifc_schema::ifc4();
    let rule = "IfcRelSpaceBoundary.CorrectPhysOrVirt";
    let types = [
        "IFCRELSPACEBOUNDARY",
        "IFCRELSPACEBOUNDARY1STLEVEL",
        "IFCRELSPACEBOUNDARY2NDLEVEL",
    ];
    for boundary in types {
        // PHYSICAL against a virtual element contradicts the rule.
        let mut invalid = Model::new();
        let element = EntityId(1);
        invalid.insert(element, entity(schema, "IFCVIRTUALELEMENT", &[]));
        invalid.push(entity(
            schema,
            boundary,
            &[
                ("RelatedBuildingElement", Value::Ref(element)),
                ("PhysicalOrVirtualBoundary", Value::Enum("PHYSICAL".into())),
            ],
        ));
        assert!(
            has(&validate(&invalid, schema), rule),
            "{boundary} must fire"
        );
    }
}

/// The rule's three conformant shapes, including the one that is easy
/// to get wrong: VIRTUAL is legal against an opening, not only against an
/// IfcVirtualElement.
#[test]
fn space_boundary_physicality_accepts_the_legal_combinations() {
    let schema = ifc_schema::ifc4();
    let rule = "IfcRelSpaceBoundary.CorrectPhysOrVirt";
    let cases = [
        ("IFCWALL", "PHYSICAL"),
        ("IFCVIRTUALELEMENT", "VIRTUAL"),
        ("IFCOPENINGELEMENT", "VIRTUAL"),
        ("IFCVIRTUALELEMENT", "NOTDEFINED"),
    ];
    for (element_type, declared) in cases {
        let mut model = Model::new();
        let element = EntityId(1);
        model.insert(element, entity(schema, element_type, &[]));
        model.push(entity(
            schema,
            "IFCRELSPACEBOUNDARY2NDLEVEL",
            &[
                ("RelatedBuildingElement", Value::Ref(element)),
                ("PhysicalOrVirtualBoundary", Value::Enum(declared.into())),
            ],
        ));
        assert!(
            !has(&validate(&model, schema), rule),
            "{declared} against {element_type} is conformant"
        );
    }
}

/// `NoSelfReference` is declared on `IfcRelAssignsToGroup`, so it binds the
/// plain relation and its `ByFactor` subtype alike, under the declaring
/// entity's id, in both releases that declare it.
#[test]
fn group_assignment_self_reference_is_checked_on_the_declaring_entity_and_its_subtype() {
    let rule = "IfcRelAssignsToGroup.NoSelfReference";
    for schema in [ifc_schema::ifc4(), ifc_schema::ifc4x3()] {
        for relation in ["IFCRELASSIGNSTOGROUP", "IFCRELASSIGNSTOGROUPBYFACTOR"] {
            let mut model = Model::new();
            let group = model.push(entity(schema, "IFCGROUP", &[]));
            model.push(entity(
                schema,
                relation,
                &[
                    ("RelatingGroup", Value::Ref(group)),
                    ("RelatedObjects", Value::List(vec![Value::Ref(group)])),
                ],
            ));
            let report = validate(&model, schema);
            assert!(has(&report, rule), "{relation} in {}", schema.name());
            assert!(
                !has(&report, "IfcRelAssignsToGroupByFactor.NoSelfReference"),
                "the subtype declares no rule of its own"
            );
        }
    }
}

/// `IfcMaterialLayerWithOffsets` inherits `NormalizedPriority`.
#[test]
fn material_layer_with_offsets_priority_is_bounded() {
    for schema in [ifc_schema::ifc4(), ifc_schema::ifc4x3()] {
        for (priority, fires) in [(101, true), (-1, true), (50, false)] {
            let mut model = Model::new();
            model.push(entity(
                schema,
                "IFCMATERIALLAYERWITHOFFSETS",
                &[
                    ("LayerThickness", Value::Real(1.0)),
                    ("Priority", Value::Integer(priority)),
                ],
            ));
            assert_eq!(
                has(
                    &validate(&model, schema),
                    "IfcMaterialLayer.NormalizedPriority"
                ),
                fires,
                "priority {priority} in {}",
                schema.name()
            );
        }
    }
}

/// IFC2X3 declares neither rule, so a model that breaks both is silent
/// under IFC2X3 tables.
#[test]
fn ifc4_only_rules_do_not_run_under_ifc2x3() {
    let schema = ifc_schema::ifc2x3();
    let mut model = Model::new();
    let wall_type = model.push(entity(schema, "IFCWALLTYPE", &[]));
    model.push(entity(
        schema,
        "IFCRELDEFINESBYPROPERTIES",
        &[("RelatedObjects", Value::List(vec![Value::Ref(wall_type)]))],
    ));
    let group = model.push(entity(schema, "IFCGROUP", &[]));
    model.push(entity(
        schema,
        "IFCRELASSIGNSTOGROUP",
        &[
            ("RelatingGroup", Value::Ref(group)),
            ("RelatedObjects", Value::List(vec![Value::Ref(group)])),
        ],
    ));
    let report = validate(&model, schema);
    for rule in [
        "IfcRelDefinesByProperties.NoRelatedTypeObject",
        "IfcRelAssignsToGroup.NoSelfReference",
    ] {
        assert!(!has(&report, rule), "{rule} is not declared by IFC2X3");
    }
}