ifc-template-catalog 0.4.0

Versioned IFC PSD/QTO catalog definitions and correction overlays
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Writing a container: distinct records by content, the string table,
//! then the bytes.

use std::collections::HashMap;

use super::{FORMAT_VERSION, MAGIC, SLOT_ABSENT, SLOT_HEX, SLOT_TABLE, SLOT_TEXT};
use crate::definition::{
    CatalogEdition, LocalizedText, PropertyDataType, PropertyKind, PropertySetType,
    PropertyTemplate, QuantityKind, QuantitySetType, QuantityTemplate, SetTemplate,
    SetTemplateKind, SourceManifest,
};

/// Encode official editions into one container. Editions keep the order
/// given; the caller sorts them for a canonical container.
pub(crate) fn encode(editions: &[(&SourceManifest, &[SetTemplate])]) -> Vec<u8> {
    // 1. Each distinct record once, keyed by its content (every string
    //    inline), in first-use order.
    let mut unique = Unique::default();
    let entries: Vec<Vec<u64>> = editions
        .iter()
        .map(|(_, templates)| templates.iter().map(|set| unique.set(set)).collect())
        .collect();

    // 2. The table: every string a distinct record or a manifest uses more
    //    than once, most used first (ties by byte order), so the commonest
    //    take a one-byte slot. A string used once stays inline, next to
    //    the text around it.
    let mut counts: HashMap<&str, u64> = HashMap::new();
    let mut tally = |s| *counts.entry(s).or_default() += 1;
    for (manifest, _) in editions {
        manifest_strings(manifest, &mut tally);
    }
    for (property, _) in &unique.properties {
        property_strings(property, &mut tally);
    }
    for quantity in &unique.quantities {
        quantity_strings(quantity, &mut tally);
    }
    for (set, _) in &unique.sets {
        set_strings(set, &mut tally);
    }
    let mut table: Vec<(&str, u64)> = counts.into_iter().filter(|(_, n)| *n > 1).collect();
    table.sort_unstable_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(b.0)));
    let ids: HashMap<&str, u64> = table
        .iter()
        .enumerate()
        .map(|(index, (s, _))| (*s, index as u64))
        .collect();
    let writer = Writer { table: Some(&ids) };

    // 3. Header, table, records, editions.
    let mut out = Vec::new();
    out.extend_from_slice(&MAGIC);
    uvar(&mut out, FORMAT_VERSION);
    // Every header, then every payload, which compresses better than
    // interleaving them.
    uvar(&mut out, table.len() as u64);
    let mut payload = Vec::new();
    for (s, _) in &table {
        match hex_bytes(s) {
            Some(bytes) => {
                uvar(&mut out, ((bytes.len() as u64) << 1) | 1);
                payload.extend_from_slice(&bytes);
            }
            None => {
                uvar(&mut out, (s.len() as u64) << 1);
                payload.extend_from_slice(s.as_bytes());
            }
        }
    }
    out.extend_from_slice(&payload);
    let mut record = Vec::new();
    uvar(&mut out, unique.properties.len() as u64);
    for (property, children) in &unique.properties {
        record.clear();
        writer.property(&mut record, property, children);
        uvar(&mut out, record.len() as u64);
        out.extend_from_slice(&record);
    }
    uvar(&mut out, unique.quantities.len() as u64);
    for quantity in &unique.quantities {
        record.clear();
        writer.quantity(&mut record, quantity);
        uvar(&mut out, record.len() as u64);
        out.extend_from_slice(&record);
    }
    uvar(&mut out, unique.sets.len() as u64);
    for (set, members) in &unique.sets {
        record.clear();
        writer.set(&mut record, set, members);
        uvar(&mut out, record.len() as u64);
        out.extend_from_slice(&record);
    }
    uvar(&mut out, editions.len() as u64);
    for ((manifest, _), sets) in editions.iter().zip(&entries) {
        out.push(edition_tag(manifest.edition));
        writer.string(&mut out, &manifest.source_label);
        writer.string(&mut out, &manifest.source_url);
        writer.string(&mut out, &manifest.sha256);
        uvar(&mut out, manifest.property_set_count as u64);
        uvar(&mut out, manifest.quantity_set_count as u64);
        uvar(&mut out, sets.len() as u64);
        for id in sets {
            uvar(&mut out, *id);
        }
    }
    out
}

/// The distinct records of a container, each with the ids of the records
/// it refers to, and the content keys that make them distinct.
#[derive(Default)]
struct Unique<'a> {
    properties: Vec<(&'a PropertyTemplate, Vec<u64>)>,
    quantities: Vec<&'a QuantityTemplate>,
    sets: Vec<(&'a SetTemplate, Vec<u64>)>,
    keys: HashMap<(u8, Vec<u8>), u64>,
}

impl<'a> Unique<'a> {
    const CONTENT: Writer<'static> = Writer { table: None };

    fn intern(&mut self, kind: u8, key: Vec<u8>, len: usize) -> Option<u64> {
        match self.keys.entry((kind, key)) {
            std::collections::hash_map::Entry::Occupied(found) => Some(*found.get()),
            std::collections::hash_map::Entry::Vacant(slot) => {
                slot.insert(len as u64);
                None
            }
        }
    }

    fn property(&mut self, property: &'a PropertyTemplate) -> u64 {
        // Children first, so a complex property refers only backwards.
        let children: Vec<u64> = match &property.kind {
            PropertyKind::Complex { properties, .. } => properties
                .iter()
                .map(|child| self.property(child))
                .collect(),
            _ => Vec::new(),
        };
        let mut key = Vec::new();
        Self::CONTENT.property(&mut key, property, &children);
        let id = self.properties.len();
        self.intern(0, key, id).unwrap_or_else(|| {
            self.properties.push((property, children));
            id as u64
        })
    }

    fn quantity(&mut self, quantity: &'a QuantityTemplate) -> u64 {
        let mut key = Vec::new();
        Self::CONTENT.quantity(&mut key, quantity);
        let id = self.quantities.len();
        self.intern(1, key, id).unwrap_or_else(|| {
            self.quantities.push(quantity);
            id as u64
        })
    }

    fn set(&mut self, set: &'a SetTemplate) -> u64 {
        let members: Vec<u64> = match &set.kind {
            SetTemplateKind::Property { properties, .. } => properties
                .iter()
                .map(|property| self.property(property))
                .collect(),
            SetTemplateKind::Quantity { quantities, .. } => quantities
                .iter()
                .map(|quantity| self.quantity(quantity))
                .collect(),
        };
        let mut key = Vec::new();
        Self::CONTENT.set(&mut key, set, &members);
        let id = self.sets.len();
        self.intern(2, key, id).unwrap_or_else(|| {
            self.sets.push((set, members));
            id as u64
        })
    }
}

/// Writes records; without a table every string is inline, which makes the
/// bytes a content key.
struct Writer<'a> {
    table: Option<&'a HashMap<&'a str, u64>>,
}

impl Writer<'_> {
    fn string(&self, out: &mut Vec<u8>, s: &str) {
        if let Some(index) = self.table.and_then(|table| table.get(s)) {
            uvar(out, SLOT_TABLE + index);
            return;
        }
        match hex_bytes(s) {
            Some(bytes) => {
                uvar(out, SLOT_HEX);
                uvar(out, bytes.len() as u64);
                out.extend_from_slice(&bytes);
            }
            None => {
                uvar(out, SLOT_TEXT);
                uvar(out, s.len() as u64);
                out.extend_from_slice(s.as_bytes());
            }
        }
    }

    fn optional(&self, out: &mut Vec<u8>, s: Option<&String>) {
        match s {
            None => uvar(out, SLOT_ABSENT),
            Some(s) => self.string(out, s),
        }
    }

    fn aliases(&self, out: &mut Vec<u8>, aliases: &[LocalizedText]) {
        uvar(out, aliases.len() as u64);
        for alias in aliases {
            self.optional(out, alias.language.as_ref());
            self.string(out, &alias.text);
        }
    }

    fn data_type(&self, out: &mut Vec<u8>, data_type: &PropertyDataType) {
        self.optional(out, data_type.type_name.as_ref());
        self.optional(out, data_type.unit_type.as_ref());
    }

    fn property(&self, out: &mut Vec<u8>, property: &PropertyTemplate, children: &[u64]) {
        self.string(out, &property.name);
        self.optional(out, property.guid.as_ref());
        self.optional(out, property.definition.as_ref());
        self.aliases(out, &property.name_aliases);
        self.aliases(out, &property.definition_aliases);
        match &property.kind {
            PropertyKind::SingleValue { data_type } => {
                out.push(0);
                self.data_type(out, data_type);
            }
            PropertyKind::BoundedValue { data_type } => {
                out.push(1);
                self.data_type(out, data_type);
            }
            PropertyKind::EnumeratedValue {
                enumeration_name,
                data_type,
                values,
                constants,
            } => {
                out.push(2);
                self.optional(out, enumeration_name.as_ref());
                match data_type {
                    None => out.push(0),
                    Some(data_type) => {
                        out.push(1);
                        self.data_type(out, data_type);
                    }
                }
                uvar(out, values.len() as u64);
                for value in values {
                    self.string(out, value);
                }
                uvar(out, constants.len() as u64);
                for constant in constants {
                    self.string(out, &constant.name);
                    self.optional(out, constant.definition.as_ref());
                    self.aliases(out, &constant.name_aliases);
                    self.aliases(out, &constant.definition_aliases);
                }
            }
            PropertyKind::ListValue { data_type } => {
                out.push(3);
                self.data_type(out, data_type);
            }
            PropertyKind::ReferenceValue { reference_type } => {
                out.push(4);
                self.string(out, reference_type);
            }
            PropertyKind::TableValue {
                defining_type,
                defined_type,
                expression,
            } => {
                out.push(5);
                self.data_type(out, defining_type);
                self.data_type(out, defined_type);
                self.optional(out, expression.as_ref());
            }
            PropertyKind::Complex { usage_name, .. } => {
                out.push(6);
                self.string(out, usage_name);
                uvar(out, children.len() as u64);
                for child in children {
                    uvar(out, *child);
                }
            }
        }
    }

    fn quantity(&self, out: &mut Vec<u8>, quantity: &QuantityTemplate) {
        self.string(out, &quantity.name);
        self.optional(out, quantity.definition.as_ref());
        self.aliases(out, &quantity.name_aliases);
        self.aliases(out, &quantity.definition_aliases);
        out.push(match quantity.kind {
            QuantityKind::Length => 0,
            QuantityKind::Area => 1,
            QuantityKind::Volume => 2,
            QuantityKind::Weight => 3,
            QuantityKind::Time => 4,
            QuantityKind::Count => 5,
            QuantityKind::Number => 6,
        });
    }

    fn set(&self, out: &mut Vec<u8>, set: &SetTemplate, members: &[u64]) {
        self.string(out, &set.name);
        self.optional(out, set.guid.as_ref());
        self.optional(out, set.definition.as_ref());
        self.aliases(out, &set.name_aliases);
        self.aliases(out, &set.definition_aliases);
        match &set.source {
            None => out.push(0),
            Some(source) => {
                out.push(1);
                self.string(out, &source.relative_path);
                self.string(out, &source.sha256);
            }
        }
        self.optional(out, set.raw_applicability.as_ref());
        uvar(out, set.applicability.len() as u64);
        for applicability in &set.applicability {
            self.string(out, &applicability.raw);
            self.string(out, &applicability.entity);
            self.optional(out, applicability.predefined_type.as_ref());
        }
        match &set.kind {
            SetTemplateKind::Property { set_type, .. } => {
                out.push(0);
                out.push(match set_type {
                    PropertySetType::TypeDrivenOverride => 0,
                    PropertySetType::TypeDrivenOnly => 1,
                    PropertySetType::OccurrenceDriven => 2,
                    PropertySetType::PerformanceDriven => 3,
                    PropertySetType::Unspecified => 4,
                });
            }
            SetTemplateKind::Quantity {
                set_type,
                method_of_measurement,
                ..
            } => {
                out.push(1);
                out.push(match set_type {
                    QuantitySetType::TypeDrivenOverride => 0,
                    QuantitySetType::TypeDrivenOnly => 1,
                    QuantitySetType::OccurrenceDriven => 2,
                    QuantitySetType::Unspecified => 3,
                });
                self.optional(out, method_of_measurement.as_ref());
            }
        }
        uvar(out, members.len() as u64);
        for member in members {
            uvar(out, *member);
        }
    }
}

// The strings each record (not the records it refers to) writes, for the
// table's use counts.

fn manifest_strings<'a>(manifest: &'a SourceManifest, f: &mut impl FnMut(&'a str)) {
    f(&manifest.source_label);
    f(&manifest.source_url);
    f(&manifest.sha256);
}

fn alias_strings<'a>(aliases: &'a [LocalizedText], f: &mut impl FnMut(&'a str)) {
    for alias in aliases {
        if let Some(language) = &alias.language {
            f(language);
        }
        f(&alias.text);
    }
}

fn data_type_strings<'a>(data_type: &'a PropertyDataType, f: &mut impl FnMut(&'a str)) {
    data_type.type_name.as_deref().map(&mut *f);
    data_type.unit_type.as_deref().map(&mut *f);
}

fn property_strings<'a>(property: &'a PropertyTemplate, f: &mut impl FnMut(&'a str)) {
    f(&property.name);
    property.guid.as_deref().map(&mut *f);
    property.definition.as_deref().map(&mut *f);
    alias_strings(&property.name_aliases, f);
    alias_strings(&property.definition_aliases, f);
    match &property.kind {
        PropertyKind::SingleValue { data_type }
        | PropertyKind::BoundedValue { data_type }
        | PropertyKind::ListValue { data_type } => data_type_strings(data_type, f),
        PropertyKind::EnumeratedValue {
            enumeration_name,
            data_type,
            values,
            constants,
        } => {
            enumeration_name.as_deref().map(&mut *f);
            if let Some(data_type) = data_type {
                data_type_strings(data_type, f);
            }
            for value in values {
                f(value);
            }
            for constant in constants {
                f(&constant.name);
                constant.definition.as_deref().map(&mut *f);
                alias_strings(&constant.name_aliases, f);
                alias_strings(&constant.definition_aliases, f);
            }
        }
        PropertyKind::ReferenceValue { reference_type } => f(reference_type),
        PropertyKind::TableValue {
            defining_type,
            defined_type,
            expression,
        } => {
            data_type_strings(defining_type, f);
            data_type_strings(defined_type, f);
            expression.as_deref().map(&mut *f);
        }
        // Its children are records of their own.
        PropertyKind::Complex { usage_name, .. } => f(usage_name),
    }
}

fn quantity_strings<'a>(quantity: &'a QuantityTemplate, f: &mut impl FnMut(&'a str)) {
    f(&quantity.name);
    quantity.definition.as_deref().map(&mut *f);
    alias_strings(&quantity.name_aliases, f);
    alias_strings(&quantity.definition_aliases, f);
}

fn set_strings<'a>(set: &'a SetTemplate, f: &mut impl FnMut(&'a str)) {
    f(&set.name);
    set.guid.as_deref().map(&mut *f);
    set.definition.as_deref().map(&mut *f);
    alias_strings(&set.name_aliases, f);
    alias_strings(&set.definition_aliases, f);
    if let Some(source) = &set.source {
        f(&source.relative_path);
        f(&source.sha256);
    }
    set.raw_applicability.as_deref().map(&mut *f);
    for applicability in &set.applicability {
        f(&applicability.raw);
        f(&applicability.entity);
        applicability.predefined_type.as_deref().map(&mut *f);
    }
    // Its members are records of their own.
    if let SetTemplateKind::Quantity {
        method_of_measurement: Some(method),
        ..
    } = &set.kind
    {
        f(method);
    }
}

/// The bytes of a nonempty, even-length, lowercase hexadecimal string; it
/// decodes back to the same text, so storing it as bytes is lossless.
fn hex_bytes(s: &str) -> Option<Vec<u8>> {
    let digits = s.as_bytes();
    if digits.is_empty() || digits.len() % 2 != 0 {
        return None;
    }
    let nibble = |digit: u8| match digit {
        b'0'..=b'9' => Some(digit - b'0'),
        b'a'..=b'f' => Some(digit - b'a' + 10),
        _ => None,
    };
    digits
        .chunks_exact(2)
        .map(|pair| Some((nibble(pair[0])? << 4) | nibble(pair[1])?))
        .collect()
}

fn uvar(out: &mut Vec<u8>, mut value: u64) {
    loop {
        let byte = (value & 0x7f) as u8;
        value >>= 7;
        if value == 0 {
            out.push(byte);
            return;
        }
        out.push(byte | 0x80);
    }
}

fn edition_tag(edition: CatalogEdition) -> u8 {
    match edition {
        CatalogEdition::Ifc2x3Tc1 => 0,
        CatalogEdition::Ifc4Add2Tc1 => 1,
        CatalogEdition::Ifc4x3Add2 => 2,
    }
}