Skip to main content

ifc_geometry/select/
subtype.rs

1//! Subtype resolution without loading the EXPRESS schema at runtime.
2//!
3//! The tables encode two releases, listed in [`VERIFIED_SCHEMA_VERSIONS`]:
4//! IFC4 ADD2 TC1, the base ([`TABLE_SCHEMA_VERSION`]), and IFC4X3 ADD2 as a
5//! delta over it. [`is_a_in`] and [`supertypes_of_in`] answer for one named
6//! release; ask [`tables_are_verified_for`] before trusting them for another.
7//!
8//! # Why this table exists
9//!
10//! EXPRESS `SELECT` types name *abstract* supertypes. `IfcBooleanOperand`
11//! permits `IfcSolidModel`, but no file contains one -- files contain
12//! `IfcExtrudedAreaSolid`, four levels below it. Answering "may this entity
13//! stand in for that select member" therefore needs the inheritance chain.
14//!
15//! `ifc-schema` can parse the official `.exp` files and answer exactly this,
16//! but requiring it would mean a geometry consumer must ship a 3 MB schema
17//! file to interpret a wall. The chains for the geometry-reachable entities
18//! are small and change only when the IFC schema does, so they are compiled in
19//! as data.
20//!
21//! # Why the releases are separate rows, not one merged table
22//!
23//! IFC4X3 does not only add entities. It also inserts abstract supertypes
24//! above four IFC4 entities: `IfcOffsetCurve2D` and `IfcOffsetCurve3D` gain
25//! `IfcOffsetCurve`, `IfcFixedReferenceSweptAreaSolid` and
26//! `IfcSurfaceCurveSweptAreaSolid` gain `IfcDirectrixCurveSweptAreaSolid`.
27//! One merged chain for those names would be wrong for one release or the
28//! other, so the IFC4X3 rows that differ live apart and are used only when a
29//! caller names IFC4X3.
30//!
31//! The unversioned [`is_a`] and [`supertypes_of`] answer the IFC4 chain for
32//! every entity IFC4 declares, exactly as before IFC4X3 rows existed, and the
33//! IFC4X3 chain for entities only IFC4X3 declares. A name IFC4 lacks cannot
34//! occur in an IFC4 file, so that fallback changes no IFC4 answer. The only
35//! IFC4X3 answers it misses are the inserted abstract supertypes above: no
36//! select or rule in this crate asks for them, and [`is_a_in`] answers them.
37//!
38//! # Keeping it honest
39//!
40//! Generated from `IFC4.exp` and `IFC4X3_ADD2.exp`. `tests/schema_coverage.rs`
41//! compares every chain, per release, with the same normative sources, so
42//! drift fails the build rather than silently misclassifying a solid.
43
44use ifc_schema::SchemaVersion;
45
46use super::subtype_ifc4::SUPERTYPES;
47use super::subtype_ifc4x3;
48
49/// The base release of the compiled tables.
50///
51/// The unversioned [`is_a`] and [`supertypes_of`] answer this release's chain
52/// for every entity it declares. [`VERIFIED_SCHEMA_VERSIONS`] lists every
53/// release [`is_a_in`] answers verbatim.
54pub const TABLE_SCHEMA_VERSION: SchemaVersion = SchemaVersion::Ifc4;
55
56/// Every release whose chains the compiled tables carry verbatim.
57pub const VERIFIED_SCHEMA_VERSIONS: &[SchemaVersion] =
58    &[SchemaVersion::Ifc4, SchemaVersion::Ifc4x3];
59
60/// Does [`is_a_in`] answer subtype questions for `version` verbatim?
61///
62/// True for IFC4 and IFC4X3. False does not mean refusal: IFC4X1 and IFC4X2
63/// share most geometry chains with these, so the unversioned answers
64/// [`is_a_in`] falls back to are usually right. It means they are not
65/// *verified* against that schema, and a caller that needs certainty must say
66/// so.
67pub fn tables_are_verified_for(version: SchemaVersion) -> bool {
68    VERIFIED_SCHEMA_VERSIONS.contains(&version)
69}
70
71/// Is `entity` the named type, or any subtype of it?
72///
73/// The question every EXPRESS `SELECT` resolution reduces to. Comparing type
74/// names directly instead of calling this rejects every real file, because
75/// select members are usually abstract.
76///
77/// Release-neutral: answers the IFC4 chain where IFC4 declares `entity`, and
78/// the IFC4X3 chain where only IFC4X3 does (see the module docs). Use
79/// [`is_a_in`] to ask about one release exactly.
80///
81/// ```
82/// use ifc_geometry::select::is_a;
83/// // An extruded area solid IS a solid model, four levels up.
84/// assert!(is_a("IFCEXTRUDEDAREASOLID", "IFCSOLIDMODEL"));
85/// assert!(!is_a("IFCCARTESIANPOINT", "IFCSOLIDMODEL"));
86/// // IFC4X3-only entities resolve too.
87/// assert!(is_a("IFCCLOTHOID", "IFCCURVE"));
88/// ```
89pub fn is_a(entity: &str, ancestor: &str) -> bool {
90    chain_contains(entity, supertypes_of(entity), ancestor)
91}
92
93/// Is `entity` the named type, or any subtype of it, in `version`?
94///
95/// Exact for every release [`tables_are_verified_for`] accepts; other
96/// releases get the release-neutral [`is_a`] answer.
97///
98/// ```
99/// use ifc_geometry::select::is_a_in;
100/// use ifc_schema::SchemaVersion;
101/// // IFC4X3 inserts IfcOffsetCurve above IfcOffsetCurve2D; IFC4 has none.
102/// assert!(is_a_in(SchemaVersion::Ifc4x3, "IFCOFFSETCURVE2D", "IFCOFFSETCURVE"));
103/// assert!(!is_a_in(SchemaVersion::Ifc4, "IFCOFFSETCURVE2D", "IFCOFFSETCURVE"));
104/// ```
105pub fn is_a_in(version: SchemaVersion, entity: &str, ancestor: &str) -> bool {
106    chain_contains(entity, supertypes_of_in(version, entity), ancestor)
107}
108
109fn chain_contains(entity: &str, chain: &[&str], ancestor: &str) -> bool {
110    entity.eq_ignore_ascii_case(ancestor) || chain.iter().any(|s| s.eq_ignore_ascii_case(ancestor))
111}
112
113/// The supertype chain of an entity, immediate parent first.
114///
115/// Release-neutral, as [`is_a`]. Empty for an unknown entity: a type from a
116/// newer schema is not an error, it simply matches no select, which is the
117/// correct conservative answer.
118pub fn supertypes_of(entity: &str) -> &'static [&'static str] {
119    find(ifc4_rows(), entity)
120        .or_else(|| find(subtype_ifc4x3::ADDED.iter(), entity))
121        .unwrap_or(&[])
122}
123
124/// The supertype chain of an entity in `version`, immediate parent first.
125///
126/// Exact for every release [`tables_are_verified_for`] accepts, and empty for
127/// an entity that release does not declare. Other releases get the
128/// release-neutral [`supertypes_of`] answer.
129pub fn supertypes_of_in(version: SchemaVersion, entity: &str) -> &'static [&'static str] {
130    match version {
131        SchemaVersion::Ifc4 => find(ifc4_rows(), entity).unwrap_or(&[]),
132        SchemaVersion::Ifc4x3 => find(subtype_ifc4x3::REDECLARED.iter(), entity)
133            .or_else(|| find(ifc4_rows(), entity))
134            .or_else(|| find(subtype_ifc4x3::ADDED.iter(), entity))
135            .unwrap_or(&[]),
136        _ => supertypes_of(entity),
137    }
138}
139
140/// Every entity this table knows, for cross-checking against the schema.
141///
142/// The union over [`VERIFIED_SCHEMA_VERSIONS`], each name once.
143pub fn known_entities() -> impl Iterator<Item = &'static str> {
144    ifc4_rows()
145        .chain(subtype_ifc4x3::ADDED.iter())
146        .map(|(name, _)| *name)
147}
148
149/// Every entity `version` declares in these tables; empty for a release
150/// [`tables_are_verified_for`] rejects.
151pub fn known_entities_in(version: SchemaVersion) -> impl Iterator<Item = &'static str> {
152    let added: &'static [Row] = match version {
153        SchemaVersion::Ifc4x3 => subtype_ifc4x3::ADDED,
154        _ => &[],
155    };
156    let base = tables_are_verified_for(version);
157    ifc4_rows()
158        .filter(move |_| base)
159        .chain(added.iter())
160        .map(|(name, _)| *name)
161}
162
163type Row = (&'static str, &'static [&'static str]);
164
165fn ifc4_rows() -> impl Iterator<Item = &'static Row> {
166    SUPERTYPES
167        .iter()
168        .chain(super::subtype_profile::PROFILE_SUPERTYPES.iter())
169}
170
171fn find<'a>(
172    mut rows: impl Iterator<Item = &'a Row>,
173    entity: &str,
174) -> Option<&'static [&'static str]> {
175    rows.find(|(name, _)| name.eq_ignore_ascii_case(entity))
176        .map(|(_, chain)| *chain)
177}
178
179#[cfg(test)]
180mod tests;