Skip to main content

qualia_core_db/modalities/logic/
infrastructure_shacl.rs

1//! SHACL Extensions for Domains, Obfuscation, and Solvers
2//!
3//! This module provides SHACL constraint extensions for:
4//! - Domain-specific engines (biological, chemical, physical, financial, mathematical, geospatial)
5//! - Data obfuscation and semantic stripping
6//! - Zero-allocation solver library
7
8use crate::webizen::SlgOpcode;
9
10// ── Domain-Specific Constraints ───────────────────────────────────────────────
11
12/// `q42:BiologicalDomainConfiguration` — validates biological domain parameters
13#[derive(Debug, Clone)]
14pub struct BiologicalDomainConfiguration {
15    pub max_sequence_length: u32, // Maximum DNA/RNA/protein sequence length
16    pub max_gene_count: u32,      // Maximum number of genes in analysis
17    pub allowed_sequence_types: Vec<String>, // ["dna", "rna", "protein"]
18    pub require_quality_score: bool, // Require sequence quality validation
19}
20
21/// `q42:ChemicalDomainConfiguration` — validates chemical domain parameters
22#[derive(Debug, Clone)]
23pub struct ChemicalDomainConfiguration {
24    pub max_molecular_weight: f64,          // Maximum molecular weight (Da)
25    pub max_atom_count: u32,                // Maximum atoms per molecule
26    pub allowed_element_types: Vec<String>, // Periodic table symbols
27    pub require_valence_validation: bool,   // Require chemical valence checks
28}
29
30/// `q42:PhysicalDomainConfiguration` — validates physical domain parameters
31#[derive(Debug, Clone)]
32pub struct PhysicalDomainConfiguration {
33    pub max_temperature_kelvin: f64,           // Maximum temperature (K)
34    pub max_pressure_pascal: f64,              // Maximum pressure (Pa)
35    pub allowed_energy_units: Vec<String>,     // ["joule", "electron_volt", "calorie"]
36    pub require_dimensional_consistency: bool, // Require unit consistency
37}
38
39/// `q42:FinancialDomainConfiguration` — validates financial domain parameters
40#[derive(Debug, Clone)]
41pub struct FinancialDomainConfiguration {
42    pub max_transaction_value: f64,      // Maximum transaction value
43    pub max_portfolio_size: f64,         // Maximum portfolio value
44    pub allowed_currencies: Vec<String>, // Currency codes
45    pub require_compliance_check: bool,  // Require regulatory compliance
46}
47
48/// `q42:MathematicalDomainConfiguration` — validates mathematical domain parameters
49#[derive(Debug, Clone)]
50pub struct MathematicalDomainConfiguration {
51    pub max_precision_bits: u8,            // Maximum precision (bits)
52    pub allowed_number_types: Vec<String>, // ["integer", "float", "complex", "rational"]
53    pub max_expression_depth: u8,          // Maximum expression nesting depth
54    pub require_well_formedness: bool,     // Require well-formed expressions
55}
56
57/// `q42:GeospatialDomainConfiguration` — validates geospatial domain parameters
58#[derive(Debug, Clone)]
59pub struct GeospatialDomainConfiguration {
60    pub max_coordinate_precision: u8, // Decimal places for coordinates
61    pub allowed_coordinate_systems: Vec<String>, // ["wgs84", "utm", "mercator"]
62    pub max_area_sq_km: f64,          // Maximum area in square kilometers
63    pub require_valid_geometry: bool, // Require valid geometric shapes
64}
65
66// ── Obfuscation Constraints ─────────────────────────────────────────────────
67
68/// `q42:ObfuscationConfiguration` — validates obfuscation parameters
69#[derive(Debug, Clone)]
70pub struct ObfuscationConfiguration {
71    pub max_obfuscation_depth: u8, // Maximum obfuscation transformation depth
72    pub allowed_obfuscation_methods: Vec<String>, // ["polynomial", "semantic", "domain"]
73    pub require_reversibility: bool, // Require reversible transformations
74    pub min_entropy_bits: u8,      // Minimum entropy after obfuscation
75}
76
77/// `q42:PolynomialObfuscationConfiguration` — validates polynomial obfuscation parameters
78#[derive(Debug, Clone)]
79pub struct PolynomialObfuscationConfiguration {
80    pub max_polynomial_degree: u8,              // Maximum polynomial degree
81    pub allowed_coefficient_types: Vec<String>, // ["integer", "float", "rational"]
82    pub require_irreducibility: bool,           // Require irreducible polynomials
83}
84
85/// `q42:SemanticStripperConfiguration` — validates semantic stripping parameters
86#[derive(Debug, Clone)]
87pub struct SemanticStripperConfiguration {
88    pub max_context_depth: u8,              // Maximum context stripping depth
89    pub allowed_context_types: Vec<String>, // ["clinical", "financial", "personal"]
90    pub require_anonymity: bool,            // Require complete anonymity
91}
92
93/// `q42:DomainTransformerConfiguration` — validates domain transformation parameters
94#[derive(Debug, Clone)]
95pub struct DomainTransformerConfiguration {
96    pub allowed_target_domains: Vec<String>, // Target transformation domains
97    pub max_transformation_chain_length: u8, // Maximum chain of transformations
98    pub require_preservation: bool,          // Require mathematical structure preservation
99}
100
101/// `q42:HybridStateConfiguration` — validates hybrid state management parameters
102#[derive(Debug, Clone)]
103pub struct HybridStateConfiguration {
104    pub max_state_size_bytes: u32,          // Maximum state size in bytes
105    pub allowed_state_domains: Vec<String>, // ["classical", "quantum", "hybrid"]
106    pub require_synchronization: bool,      // Require state synchronization
107}
108
109// ── Solver Constraints ─────────────────────────────────────────────────────
110
111/// `q42:SolverConfiguration` — validates general solver parameters
112#[derive(Debug, Clone)]
113pub struct SolverConfiguration {
114    pub max_iterations: u32,               // Maximum solver iterations
115    pub convergence_tolerance: f64,        // Convergence threshold
116    pub max_step_size: f64,                // Maximum step size
117    pub min_step_size: f64,                // Minimum step size
118    pub allowed_solver_types: Vec<String>, // ["calculus", "linear_algebra", "optimization"]
119}
120
121/// `q42:CalculusSolverConfiguration` — validates calculus solver parameters
122#[derive(Debug, Clone)]
123pub struct CalculusSolverConfiguration {
124    pub max_ode_order: u8,                // Maximum ODE order
125    pub allowed_integrators: Vec<String>, // ["runge_kutta", "simpsons", "shooting"]
126    pub require_stability_check: bool,    // Require numerical stability check
127}
128
129/// `q42:LinearAlgebraSolverConfiguration` — validates linear algebra solver parameters
130#[derive(Debug, Clone)]
131pub struct LinearAlgebraSolverConfiguration {
132    pub max_matrix_dimension: u16,            // Maximum matrix dimension
133    pub allowed_decompositions: Vec<String>,  // ["lu", "qr", "svd", "eigen"]
134    pub require_condition_number_check: bool, // Require condition number validation
135}
136
137/// `q42:OptimizationSolverConfiguration` — validates optimization solver parameters
138#[derive(Debug, Clone)]
139pub struct OptimizationSolverConfiguration {
140    pub max_variables: u32,              // Maximum number of variables
141    pub allowed_algorithms: Vec<String>, // ["nelder_mead", "newton_raphson", "levenberg_marquardt"]
142    pub require_convexity_check: bool,   // Require convexity validation
143}
144
145/// `q42:QuantumOptimizerConfiguration` — validates quantum optimizer parameters
146#[derive(Debug, Clone)]
147pub struct QuantumOptimizerConfiguration {
148    pub max_qubits: u16,                 // Maximum number of qubits
149    pub allowed_algorithms: Vec<String>, // ["qaoa", "spsa", "variational"]
150    pub require_error_correction: bool,  // Require error correction
151}
152
153/// `q42:SymbolicLogicSolverConfiguration` — validates symbolic logic solver parameters
154#[derive(Debug, Clone)]
155pub struct SymbolicLogicSolverConfiguration {
156    pub max_clause_count: u32,            // Maximum number of clauses
157    pub max_variable_count: u32,          // Maximum number of variables
158    pub allowed_logic_types: Vec<String>, // ["defeasible", "classical", "modal"]
159}
160
161// ── Geometric Algebra Constraints ─────────────────────────────────────────────
162
163/// `q42:GeometricAlgebraConfiguration` — validates geometric algebra parameters
164#[derive(Debug, Clone)]
165pub struct GeometricAlgebraConfiguration {
166    pub max_dimension: u8, // Maximum geometric algebra dimension (3D, 4D, etc.)
167    pub allowed_algebras: Vec<String>, // ["pga", "cga", "conformal"]
168    pub require_normalization: bool, // Require multivector normalization
169}
170
171// ── Opcode Generation Functions ───────────────────────────────────────────────
172
173impl BiologicalDomainConfiguration {
174    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
175        vec![
176            SlgOpcode::CheckMaxInclusive(self.max_sequence_length as f64),
177            SlgOpcode::CheckMaxInclusive(self.max_gene_count as f64),
178        ]
179    }
180}
181
182impl ChemicalDomainConfiguration {
183    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
184        vec![
185            SlgOpcode::CheckMaxInclusive(self.max_molecular_weight),
186            SlgOpcode::CheckMaxInclusive(self.max_atom_count as f64),
187        ]
188    }
189}
190
191impl PhysicalDomainConfiguration {
192    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
193        vec![
194            SlgOpcode::CheckMaxInclusive(self.max_temperature_kelvin),
195            SlgOpcode::CheckMaxInclusive(self.max_pressure_pascal),
196        ]
197    }
198}
199
200impl FinancialDomainConfiguration {
201    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
202        vec![
203            SlgOpcode::CheckMaxInclusive(self.max_transaction_value),
204            SlgOpcode::CheckMaxInclusive(self.max_portfolio_size),
205        ]
206    }
207}
208
209impl MathematicalDomainConfiguration {
210    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
211        vec![
212            SlgOpcode::CheckMaxInclusive(self.max_precision_bits as f64),
213            SlgOpcode::CheckMaxInclusive(self.max_expression_depth as f64),
214        ]
215    }
216}
217
218impl GeospatialDomainConfiguration {
219    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
220        vec![
221            SlgOpcode::CheckMaxInclusive(self.max_coordinate_precision as f64),
222            SlgOpcode::CheckMaxInclusive(self.max_area_sq_km),
223        ]
224    }
225}
226
227impl ObfuscationConfiguration {
228    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
229        vec![
230            SlgOpcode::CheckMaxInclusive(self.max_obfuscation_depth as f64),
231            SlgOpcode::CheckMinInclusive(self.min_entropy_bits as f64),
232        ]
233    }
234}
235
236impl PolynomialObfuscationConfiguration {
237    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
238        vec![SlgOpcode::CheckMaxInclusive(
239            self.max_polynomial_degree as f64,
240        )]
241    }
242}
243
244impl SemanticStripperConfiguration {
245    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
246        vec![SlgOpcode::CheckMaxInclusive(self.max_context_depth as f64)]
247    }
248}
249
250impl DomainTransformerConfiguration {
251    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
252        vec![SlgOpcode::CheckMaxInclusive(
253            self.max_transformation_chain_length as f64,
254        )]
255    }
256}
257
258impl HybridStateConfiguration {
259    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
260        vec![SlgOpcode::CheckMaxInclusive(
261            self.max_state_size_bytes as f64,
262        )]
263    }
264}
265
266impl SolverConfiguration {
267    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
268        vec![
269            SlgOpcode::CheckMaxInclusive(self.max_iterations as f64),
270            SlgOpcode::CheckMaxInclusive(self.convergence_tolerance),
271            SlgOpcode::CheckMaxInclusive(self.max_step_size),
272            SlgOpcode::CheckMinInclusive(self.min_step_size),
273        ]
274    }
275}
276
277impl CalculusSolverConfiguration {
278    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
279        vec![SlgOpcode::CheckMaxInclusive(self.max_ode_order as f64)]
280    }
281}
282
283impl LinearAlgebraSolverConfiguration {
284    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
285        vec![SlgOpcode::CheckMaxInclusive(
286            self.max_matrix_dimension as f64,
287        )]
288    }
289}
290
291impl OptimizationSolverConfiguration {
292    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
293        vec![SlgOpcode::CheckMaxInclusive(self.max_variables as f64)]
294    }
295}
296
297impl QuantumOptimizerConfiguration {
298    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
299        vec![SlgOpcode::CheckMaxInclusive(self.max_qubits as f64)]
300    }
301}
302
303impl SymbolicLogicSolverConfiguration {
304    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
305        vec![
306            SlgOpcode::CheckMaxInclusive(self.max_clause_count as f64),
307            SlgOpcode::CheckMaxInclusive(self.max_variable_count as f64),
308        ]
309    }
310}
311
312impl GeometricAlgebraConfiguration {
313    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
314        vec![SlgOpcode::CheckMaxInclusive(self.max_dimension as f64)]
315    }
316}
317
318// ── SHACL TTL Vocabulary for Domains, Obfuscation, and Solvers ─────────────
319
320/// Returns comprehensive SHACL TTL vocabulary for domains, obfuscation, and solvers
321pub fn get_infrastructure_shacl_ttl() -> &'static str {
322    r#"
323@prefix q42: <https://webizen.org/q42#> .
324@prefix sh: <http://www.w3.org/ns/shacl#> .
325@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
326
327# ── Domain-Specific Constraints ─────────────────────────────────────────────
328
329q42:BiologicalDomainConfigurationShape a sh:NodeShape ;
330    sh:property [
331        sh:path q42:maxSequenceLength ;
332        sh:datatype xsd:integer ;
333        sh:minInclusive 1 ;
334        sh:maxInclusive 1000000 ;
335        sh:message "Sequence length must be between 1 and 1,000,000" ;
336    ] ;
337    sh:property [
338        sh:path q42:allowedSequenceTypes ;
339        sh:in ("dna" "rna" "protein") ;
340        sh:message "Sequence type must be valid" ;
341    ] .
342
343q42:ChemicalDomainConfigurationShape a sh:NodeShape ;
344    sh:property [
345        sh:path q42:maxMolecularWeight ;
346        sh:datatype xsd:float ;
347        sh:minInclusive 1.0 ;
348        sh:maxInclusive 1000000.0 ;
349        sh:message "Molecular weight must be between 1 and 1,000,000 Da" ;
350    ] ;
351    sh:property [
352        sh:path q42:maxAtomCount ;
353        sh:datatype xsd:integer ;
354        sh:minInclusive 1 ;
355        sh:maxInclusive 10000 ;
356        sh:message "Atom count must be between 1 and 10,000" ;
357    ] .
358
359q42:PhysicalDomainConfigurationShape a sh:NodeShape ;
360    sh:property [
361        sh:path q42:maxTemperatureKelvin ;
362        sh:datatype xsd:float ;
363        sh:minInclusive 0.0 ;
364        sh:maxInclusive 1e6 ;
365        sh:message "Temperature must be between 0K and 1,000,000K" ;
366    ] ;
367    sh:property [
368        sh:path q42:maxPressurePascal ;
369        sh:datatype xsd:float ;
370        sh:minInclusive 0.0 ;
371        sh:maxInclusive 1e12 ;
372        sh:message "Pressure must be reasonable" ;
373    ] .
374
375q42:FinancialDomainConfigurationShape a sh:NodeShape ;
376    sh:property [
377        sh:path q42:maxTransactionValue ;
378        sh:datatype xsd:float ;
379        sh:minInclusive 0.0 ;
380        sh:maxInclusive 1e15 ;
381        sh:message "Transaction value must be reasonable" ;
382    ] ;
383    sh:property [
384        sh:path q42:allowedCurrencies ;
385        sh:message "Currency must be valid ISO 4217 code" ;
386    ] .
387
388q42:MathematicalDomainConfigurationShape a sh:NodeShape ;
389    sh:property [
390        sh:path q42:maxPrecisionBits ;
391        sh:datatype xsd:unsignedByte ;
392        sh:maxInclusive 128 ;
393        sh:message "Precision must be ≤ 128 bits" ;
394    ] ;
395    sh:property [
396        sh:path q42:maxExpressionDepth ;
397        sh:datatype xsd:unsignedByte ;
398        sh:maxInclusive 50 ;
399        sh:message "Expression depth must be ≤ 50" ;
400    ] .
401
402q42:GeospatialDomainConfigurationShape a sh:NodeShape ;
403    sh:property [
404        sh:path q42:maxCoordinatePrecision ;
405        sh:datatype xsd:unsignedByte ;
406        sh:maxInclusive 15 ;
407        sh:message "Coordinate precision must be ≤ 15 decimal places" ;
408    ] ;
409    sh:property [
410        sh:path q42:allowedCoordinateSystems ;
411        sh:in ("wgs84" "utm" "mercator" "geographic") ;
412        sh:message "Coordinate system must be valid" ;
413    ] .
414
415# ── Obfuscation Constraints ─────────────────────────────────────────
416
417q42:ObfuscationConfigurationShape a sh:NodeShape ;
418    sh:property [
419        sh:path q42:maxObfuscationDepth ;
420        sh:datatype xsd:unsignedByte ;
421        sh:maxInclusive 10 ;
422        sh:message "Obfuscation depth must be ≤ 10" ;
423    ] ;
424    sh:property [
425        sh:path q42:minEntropyBits ;
426        sh:datatype xsd:unsignedByte ;
427        sh:minInclusive 64 ;
428        sh:message "Entropy must be ≥ 64 bits" ;
429    ] .
430
431q42:PolynomialObfuscationConfigurationShape a sh:NodeShape ;
432    sh:property [
433        sh:path q42:maxPolynomialDegree ;
434        sh:datatype xsd:unsignedByte ;
435        sh:maxInclusive 20 ;
436        sh:message "Polynomial degree must be ≤ 20" ;
437    ] .
438
439q42:SemanticStripperConfigurationShape a sh:NodeShape ;
440    sh:property [
441        sh:path q42:maxContextDepth ;
442        sh:datatype xsd:unsignedByte ;
443        sh:maxInclusive 10 ;
444        sh:message "Context depth must be ≤ 10" ;
445    ] ;
446    sh:property [
447        sh:path q42:allowedContextTypes ;
448        sh:in ("clinical" "financial" "personal" "legal") ;
449        sh:message "Context type must be valid" ;
450    ] .
451
452q42:HybridStateConfigurationShape a sh:NodeShape ;
453    sh:property [
454        sh:path q42:maxStateSizeBytes ;
455        sh:datatype xsd:integer ;
456        sh:minInclusive 1 ;
457        sh:maxInclusive 1048576 ;
458        sh:message "State size must be between 1 and 1MB" ;
459    ] .
460
461# ── Solver Constraints ─────────────────────────────────────────────────
462
463q42:SolverConfigurationShape a sh:NodeShape ;
464    sh:property [
465        sh:path q42:maxIterations ;
466        sh:datatype xsd:integer ;
467        sh:minInclusive 1 ;
468        sh:maxInclusive 1000000 ;
469        sh:message "Iterations must be between 1 and 1,000,000" ;
470    ] ;
471    sh:property [
472        sh:path q42:convergenceTolerance ;
473        sh:datatype xsd:float ;
474        sh:minInclusive 1e-15 ;
475        sh:maxInclusive 1.0 ;
476        sh:message "Tolerance must be reasonable" ;
477    ] ;
478    sh:property [
479        sh:path q42:maxStepSize ;
480        sh:datatype xsd:float ;
481        sh:minInclusive 1e-15 ;
482        sh:maxInclusive 1.0 ;
483        sh:message "Step size must be reasonable" ;
484    ] ;
485    sh:property [
486        sh:path q42:minStepSize ;
487        sh:datatype xsd:float ;
488        sh:minInclusive 1e-15 ;
489        sh:maxInclusive 1.0 ;
490        sh:message "Step size must be reasonable" ;
491    ] .
492
493q42:CalculusSolverConfigurationShape a sh:NodeShape ;
494    sh:property [
495        sh:path q42:maxOdeOrder ;
496        sh:datatype xsd:unsignedByte ;
497        sh:maxInclusive 10 ;
498        sh:message "ODE order must be ≤ 10" ;
499    ] .
500
501q42:LinearAlgebraSolverConfigurationShape a sh:NodeShape ;
502    sh:property [
503        sh:path q42:maxMatrixDimension ;
504        sh:datatype xsd:unsignedShort ;
505        sh:maxInclusive 1000 ;
506        sh:message "Matrix dimension must be ≤ 1000" ;
507    ] .
508
509q42:OptimizationSolverConfigurationShape a sh:NodeShape ;
510    sh:property [
511        sh:path q42:maxVariables ;
512        sh:datatype xsd:integer ;
513        sh:minInclusive 1 ;
514        sh:maxInclusive 100000 ;
515        sh:message "Variables must be between 1 and 100,000" ;
516    ] .
517
518q42:QuantumOptimizerConfigurationShape a sh:NodeShape ;
519    sh:property [
520        sh:path q42:maxQubits ;
521        sh:datatype xsd:unsignedShort ;
522        sh:maxInclusive 1000 ;
523        sh:message "Qubits must be ≤ 1000" ;
524    ] .
525
526q42:SymbolicLogicSolverConfigurationShape a sh:NodeShape ;
527    sh:property [
528        sh:path q42:maxClauseCount ;
529        sh:datatype xsd:integer ;
530        sh:minInclusive 1 ;
531        sh:maxInclusive 1000000 ;
532        sh:message "Clauses must be between 1 and 1,000,000" ;
533    ] ;
534    sh:property [
535        sh:path q42:maxVariableCount ;
536        sh:datatype xsd:integer ;
537        sh:minInclusive 1 ;
538        sh:maxInclusive 100000 ;
539        sh:message "Variables must be between 1 and 100,000" ;
540    ] .
541
542# ── Geometric Algebra Constraints ─────────────────────────────────────────
543
544q42:GeometricAlgebraConfigurationShape a sh:NodeShape ;
545    sh:property [
546        sh:path q42:maxDimension ;
547        sh:datatype xsd:unsignedByte ;
548        sh:minInclusive 2 ;
549        sh:maxInclusive 8 ;
550        sh:message "Dimension must be between 2 and 8" ;
551    ] ;
552    sh:property [
553        sh:path q42:allowedAlgebras ;
554        sh:in ("pga" "cga" "conformal" "projective") ;
555        sh:message "Algebra type must be valid" ;
556    ] .
557"#
558}