qualia_core_db/domains/mod.rs
1//! Domain-specific engines for QualiaDB
2//!
3//! This module organizes domain-specific computation engines by category,
4//! providing specialized capabilities for biological, chemical, physical,
5//! financial, mathematical, and geospatial domains.
6//!
7//! # Decoupling Architecture and Unconditional Callers
8//!
9//! Several core governance and platform paths in `qualia-core-db` interface
10//! directly with domain engines:
11//! - `biological`: Referenced by `governance::webizen::vm` (sequence alignment,
12//! k-mer frequency, FASTA parsing, Tanimoto fingerprint similarity).
13//! - `chemical`: Referenced by `governance::webizen::vm` (SMILES/InChI validation,
14//! descriptors, Lipinski/Veber/Ghose/Egan rules, functional groups, pKa, chiral
15//! centers, circular fingerprints, thermodynamics, and kinetics).
16//! - `financial`: Referenced by `governance::webizen::vm` (Monte Carlo VaR) and
17//! `governance::webizen::mod` (`TaxRuleSchema`).
18//! - `geospatial`: Referenced by `governance::webizen::vm` (`CanvasRightsModel`) and
19//! `lib.rs::tests` (`SpatiotemporalQuadTree`, `embed_h3_context`).
20//! - `mathematical`: Referenced by `lib.rs::tests` (`VectorSectorMap`,
21//! `extract_spatial_projection`) and `platform::npu_ffi` (`LorentzVector`).
22//! - `physical`: Referenced by `governance::webizen::vm` (`ThermodynamicSampler`).
23//!
24//! Under default and monolithic configurations, 100% of these domain modules
25//! remain unconditionally exported to ensure zero broken callers. The
26//! zero-heap capability function [`active_domain_mask`] allows runtimes to inspect
27//! active domain boundaries at compile-time and runtime.
28
29pub mod biological;
30pub mod biosphere;
31pub mod chemical;
32pub mod financial;
33pub mod geospatial;
34pub mod mathematical;
35pub mod physical;
36
37// ── Domain Bitmask Constants (1 << N) ────────────────────────────────────────
38
39/// Biological domain engine (bioinformatics, k-mer analysis, sequence alignment).
40pub const DOMAIN_BIOLOGICAL: u32 = 1 << 0;
41
42/// Chemical domain engine (organic chemistry, cheminformatics, SMILES/InChI).
43pub const DOMAIN_CHEMICAL: u32 = 1 << 1;
44
45/// Financial domain engine (quantitative economics, tax schema, Monte Carlo VaR).
46pub const DOMAIN_FINANCIAL: u32 = 1 << 2;
47
48/// Geospatial domain engine (H3 indexing, quadtree, canvas rights, GIS adapters).
49pub const DOMAIN_GEOSPATIAL: u32 = 1 << 3;
50
51/// Mathematical domain engine (geometric algebra, Lorentz vectors, Voronoi cells).
52pub const DOMAIN_MATHEMATICAL: u32 = 1 << 4;
53
54/// Physical domain engine (thermodynamics, MCMC sampling, reaction kinetics).
55pub const DOMAIN_PHYSICAL: u32 = 1 << 5;
56
57/// Combined bitmask representing all domain capabilities.
58pub const ALL_DOMAINS_MASK: u32 = DOMAIN_BIOLOGICAL
59 | DOMAIN_CHEMICAL
60 | DOMAIN_FINANCIAL
61 | DOMAIN_GEOSPATIAL
62 | DOMAIN_MATHEMATICAL
63 | DOMAIN_PHYSICAL;
64
65// ── Zero-Heap Capability Query Functions ─────────────────────────────────────
66
67/// Returns the 32-bit bitmask of active domain engines compiled into this binary.
68///
69/// Pure `const fn` executing with zero heap allocations (Tier-1 contract).
70#[inline(always)]
71pub const fn active_domain_mask() -> u32 {
72 let mut mask = 0u32;
73 mask |= DOMAIN_BIOLOGICAL;
74 mask |= DOMAIN_CHEMICAL;
75 mask |= DOMAIN_FINANCIAL;
76 mask |= DOMAIN_GEOSPATIAL;
77 mask |= DOMAIN_MATHEMATICAL;
78 mask |= DOMAIN_PHYSICAL;
79 mask
80}
81
82/// Checks whether a specific domain capability bit is active in the current build.
83///
84/// Returns `true` if the corresponding domain flag is set, `false` otherwise.
85#[inline(always)]
86pub const fn is_domain_active(domain_bit: u32) -> bool {
87 (active_domain_mask() & domain_bit) != 0
88}
89
90/// Returns the canonical kebab-case name of a domain capability bit.
91///
92/// Returns `"unknown"` if an unrecognized bit or multiple bits are passed.
93pub const fn domain_name(domain_bit: u32) -> &'static str {
94 match domain_bit {
95 DOMAIN_BIOLOGICAL => "biological",
96 DOMAIN_CHEMICAL => "chemical",
97 DOMAIN_FINANCIAL => "financial",
98 DOMAIN_GEOSPATIAL => "geospatial",
99 DOMAIN_MATHEMATICAL => "mathematical",
100 DOMAIN_PHYSICAL => "physical",
101 _ => "unknown",
102 }
103}
104
105// ── Antigravity Unit Tests ───────────────────────────────────────────────────
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn test_active_domain_mask_all_active() {
113 let mask = active_domain_mask();
114 assert_eq!(
115 mask, ALL_DOMAINS_MASK,
116 "All six core domains must be active in default build"
117 );
118 assert_eq!(mask.count_ones(), 6, "Expected exactly 6 active domains");
119 }
120
121 #[test]
122 fn test_is_domain_active_positive() {
123 assert!(is_domain_active(DOMAIN_BIOLOGICAL));
124 assert!(is_domain_active(DOMAIN_CHEMICAL));
125 assert!(is_domain_active(DOMAIN_FINANCIAL));
126 assert!(is_domain_active(DOMAIN_GEOSPATIAL));
127 assert!(is_domain_active(DOMAIN_MATHEMATICAL));
128 assert!(is_domain_active(DOMAIN_PHYSICAL));
129 }
130
131 #[test]
132 fn test_is_domain_active_negative_invalid_bits() {
133 // High bits not assigned to any domain must report inactive
134 assert!(!is_domain_active(1 << 6));
135 assert!(!is_domain_active(1 << 15));
136 assert!(!is_domain_active(1 << 31));
137 assert!(!is_domain_active(0));
138 }
139
140 #[test]
141 fn test_domain_name_labels() {
142 assert_eq!(domain_name(DOMAIN_BIOLOGICAL), "biological");
143 assert_eq!(domain_name(DOMAIN_CHEMICAL), "chemical");
144 assert_eq!(domain_name(DOMAIN_FINANCIAL), "financial");
145 assert_eq!(domain_name(DOMAIN_GEOSPATIAL), "geospatial");
146 assert_eq!(domain_name(DOMAIN_MATHEMATICAL), "mathematical");
147 assert_eq!(domain_name(DOMAIN_PHYSICAL), "physical");
148 }
149
150 #[test]
151 fn test_domain_name_unknown() {
152 assert_eq!(domain_name(0), "unknown");
153 assert_eq!(domain_name(1 << 6), "unknown");
154 assert_eq!(domain_name(DOMAIN_BIOLOGICAL | DOMAIN_CHEMICAL), "unknown");
155 }
156
157 #[test]
158 fn test_domain_bits_are_disjoint_powers_of_two() {
159 let bits = [
160 DOMAIN_BIOLOGICAL,
161 DOMAIN_CHEMICAL,
162 DOMAIN_FINANCIAL,
163 DOMAIN_GEOSPATIAL,
164 DOMAIN_MATHEMATICAL,
165 DOMAIN_PHYSICAL,
166 ];
167 for (i, &b1) in bits.iter().enumerate() {
168 assert!(b1.is_power_of_two(), "Domain bit {b1} must be a power of two");
169 for (j, &b2) in bits.iter().enumerate() {
170 if i != j {
171 assert_eq!(
172 b1 & b2,
173 0,
174 "Domain bits {b1} and {b2} must be strictly disjoint"
175 );
176 }
177 }
178 }
179 }
180}