Skip to main content

ifc_validate/where_rule/
engine.rs

1//! Running the registered rules under a budget.
2
3use ifc_model::Model;
4use ifc_schema::Schema;
5
6use super::budget::Budget;
7use super::builtin;
8use super::registry::{self, Support};
9use crate::report::{Finding, Path, Report};
10
11/// Evaluate every implemented rule, and record every unimplemented one.
12///
13/// Only rules the schema's release declares are considered, in registry
14/// order. The second half is the point: a report from this function
15/// distinguishes "conformant" from "conformant as far as we can tell", and
16/// says which rules fall in the gap.
17pub fn evaluate(model: &Model, schema: &Schema, budget: Budget, report: &mut Report) {
18    evaluate_except(model, schema, budget, report, &|_| false, false);
19}
20
21/// [`evaluate`], leaving out every registered rule `superseded` accepts.
22///
23/// With the release's EXPRESS rules supplied, those are evaluated instead
24/// of the hand-written rules carrying the same id, and `express` says so:
25/// the release then has WHERE rules evaluated even when the registry
26/// scopes none to it.
27pub(crate) fn evaluate_except(
28    model: &Model,
29    schema: &Schema,
30    budget: Budget,
31    report: &mut Report,
32    superseded: &dyn Fn(&str) -> bool,
33    express: bool,
34) {
35    // A recognised release no registered rule is scoped to (IFC4X1, IFC4X2)
36    // gets no WHERE-rule verdict at all. Say so, rather than let the report
37    // read as if its rules had passed.
38    if let Some(version) = schema.version().filter(|_| !express) {
39        if !registry::RULES
40            .iter()
41            .any(|entry| entry.applies_to(schema) && entry.id != registry::UNIQUE_GLOBAL_ID)
42        {
43            report.push(Finding::unsupported(
44                "where.release",
45                Path::File,
46                format!(
47                    "no WHERE rule is registered for {}; structural and type checks \
48                     ran against its own tables, WHERE rules were not evaluated",
49                    version.release_id()
50                ),
51            ));
52            return;
53        }
54    }
55    for entry in registry::implemented() {
56        if entry.applies_to(schema) && !superseded(entry.id) {
57            let dispatched = builtin::run(entry, model, schema, report);
58            debug_assert!(dispatched, "{} has no native implementation", entry.id);
59        }
60    }
61
62    for entry in registry::unsupported() {
63        if report.findings().len() >= budget.max_findings {
64            report.mark_truncated();
65            return;
66        }
67        let Support::Unsupported(reason) = entry.support else {
68            continue;
69        };
70        if !entry.applies_to(schema) || superseded(entry.id) {
71            continue;
72        }
73        // Only mention a rule the file could actually trip: an unsupported
74        // rule for an entity type the file never uses is noise.
75        if let Some(entity) = entry.entity {
76            if !model_contains(model, schema, entity) {
77                continue;
78            }
79        }
80        report.push(Finding::unsupported(entry.id, Path::File, reason));
81    }
82}
83
84/// Whether the model holds any instance of `entity` or a subtype of it.
85fn model_contains(model: &Model, schema: &Schema, entity: &str) -> bool {
86    model
87        .type_histogram()
88        .iter()
89        .any(|(name, _)| schema.is_a(name, entity))
90}
91
92#[cfg(all(test, feature = "ifc4"))]
93mod intermediate_release_tests {
94    use super::*;
95    use crate::report::Severity;
96
97    /// IFC4X1 and IFC4X2 validate against their own tables, but no WHERE
98    /// rule is scoped to them yet: the report says so instead of reading as
99    /// if every rule passed.
100    #[test]
101    fn an_unscoped_release_reports_that_where_rules_did_not_run() {
102        for schema in [ifc_schema::ifc4x1(), ifc_schema::ifc4x2()] {
103            let mut report = Report::new();
104            evaluate(&Model::new(), schema, Budget::default(), &mut report);
105            let findings = report.findings();
106            assert_eq!(findings.len(), 1, "{}", schema.name());
107            assert_eq!(findings[0].rule, "where.release");
108            assert_eq!(findings[0].severity, Severity::Unsupported);
109        }
110        let mut report = Report::new();
111        evaluate(
112            &Model::new(),
113            ifc_schema::ifc4(),
114            Budget::default(),
115            &mut report,
116        );
117        assert!(report.findings().iter().all(|f| f.rule != "where.release"));
118    }
119}