ggen-core 26.7.2

Core graph-aware code generation engine
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! μ₁: Normalization Pass
//!
//! Performs ontology → ontology transformations using CONSTRUCT queries.
//! This pass enriches the graph with derived triples before extraction.
//!
//! ## CONSTRUCT Guarantees
//!
//! - **Fail-fast SHACL validation**: All shapes must validate before CONSTRUCT execution
//! - **Stop-the-line**: Any validation failure halts the entire pipeline
//! - **Receipt integration**: All validation results are recorded in the receipt
//!
//! ## Architecture
//!
//! ```text
//! TTL Input → Parse (Oxigraph) → SHACL Validate → OWL Inference → Normalize → Receipt
//!                                      ↓ (fail)
//!                                  STOP LINE
//! ```

use crate::graph::{ConstructExecutor, Graph};
use crate::pipeline_engine::pass::{Pass, PassContext, PassResult, PassType};
use crate::utils::error::{Error, Result};
use crate::validation::shacl::{ShaclShapeSet, ShapeLoader};
use crate::validation::validator::SparqlValidator;
use crate::validation::violation::ValidationResult;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::Path;
use std::time::Instant;

/// A CONSTRUCT rule for normalization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NormalizationRule {
    /// Rule name for auditing
    pub name: String,

    /// SPARQL CONSTRUCT query
    pub construct: String,

    /// Execution order (lower = earlier)
    pub order: i32,

    /// Description for documentation
    pub description: Option<String>,

    /// Skip if this ASK query returns false
    pub when: Option<String>,
}

/// Receipt for normalization pass execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NormalizationReceipt {
    /// SHA-256 hash of input TTL content
    pub input_hash: String,

    /// SHACL validation result (pre-normalization)
    pub pre_validation: ValidationSummary,

    /// SHACL validation result (post-normalization)
    pub post_validation: ValidationSummary,

    /// Number of triples materialized by OWL inference
    pub owl_triples_materialized: usize,

    /// Total triples in normalized graph
    pub total_triples: usize,

    /// Rules executed with their materialization counts
    pub rules_executed: Vec<RuleExecution>,

    /// Execution duration in milliseconds
    pub duration_ms: u64,

    /// Whether normalization succeeded
    pub success: bool,
}

/// Summary of SHACL validation for receipt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationSummary {
    /// Whether validation passed
    pub passed: bool,

    /// Number of violations found
    pub violation_count: usize,

    /// Validation duration in milliseconds
    pub duration_ms: u64,
}

impl From<&ValidationResult> for ValidationSummary {
    fn from(result: &ValidationResult) -> Self {
        Self {
            passed: result.passed,
            violation_count: result.violation_count,
            duration_ms: result.duration_ms,
        }
    }
}

/// Record of a single rule execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleExecution {
    /// Rule name
    pub name: String,

    /// SHA-256 hash of the CONSTRUCT query
    pub query_hash: String,

    /// Number of triples materialized
    pub triples_materialized: usize,

    /// Whether the rule was skipped (WHEN condition false)
    pub skipped: bool,
}

/// μ₁: Normalization pass implementation
#[derive(Debug, Clone)]
pub struct NormalizationPass {
    /// Rules to execute in order
    rules: Vec<NormalizationRule>,

    /// SHACL shapes for validation (optional, fail-fast if present)
    shacl_shapes: Option<ShaclShapeSet>,

    /// Whether to enable fail-fast SHACL validation
    enable_shacl_gate: bool,

    /// Receipt from last execution (for auditing)
    receipt: Option<NormalizationReceipt>,
}

impl NormalizationPass {
    /// Create a new normalization pass
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
            shacl_shapes: None,
            enable_shacl_gate: true,
            receipt: None,
        }
    }

    /// Get the normalization receipt from the last execution
    pub fn receipt(&self) -> Option<&NormalizationReceipt> {
        self.receipt.as_ref()
    }

    /// Parse TTL file and load into graph
    pub fn parse_ttl(&self, ttl_path: &Path) -> Result<Graph> {
        let content = std::fs::read_to_string(ttl_path).map_err(|e| {
            Error::new(&format!(
                "Failed to read TTL file '{}': {}",
                ttl_path.display(),
                e
            ))
        })?;

        let graph = Graph::new()?;
        graph.insert_turtle(&content).map_err(|e| {
            Error::new(&format!(
                "Failed to parse TTL '{}': {}",
                ttl_path.display(),
                e
            ))
        })?;

        Ok(graph)
    }

    /// Compute SHA-256 hash of a SPARQL query
    fn hash_query(&self, query: &str) -> String {
        let hash = Sha256::digest(query.as_bytes());
        format!("{:x}", hash)
    }

    /// Add a normalization rule
    pub fn add_rule(&mut self, rule: NormalizationRule) {
        self.rules.push(rule);
        self.rules.sort_by_key(|r| r.order);
    }

    /// Create with a set of rules
    pub fn with_rules(mut self, rules: Vec<NormalizationRule>) -> Self {
        self.rules = rules;
        self.rules.sort_by_key(|r| r.order);
        self
    }

    /// Add SHACL shapes for validation gate
    pub fn with_shacl_shapes(mut self, shapes: ShaclShapeSet) -> Self {
        self.shacl_shapes = Some(shapes);
        self
    }

    /// Load SHACL shapes from the graph
    pub fn load_shacl_shapes(&mut self, ctx: &PassContext<'_>) -> Result<()> {
        let loader = ShapeLoader::new();
        let shapes = loader
            .load(ctx.graph)
            .map_err(|e| Error::new(&e.to_string()))?;
        self.shacl_shapes = Some(shapes);
        Ok(())
    }

    /// Enable or disable SHACL validation gate
    pub fn with_shacl_gate(mut self, enabled: bool) -> Self {
        self.enable_shacl_gate = enabled;
        self
    }

    /// Run SHACL validation as a quality gate
    fn validate_shacl_gate(&self, ctx: &PassContext<'_>) -> Result<ValidationResult> {
        if !self.enable_shacl_gate {
            // Return a passing result if validation is disabled
            return Ok(ValidationResult::pass(0));
        }

        if let Some(ref shapes) = self.shacl_shapes {
            if shapes.is_empty() {
                // No shapes to validate against
                return Ok(ValidationResult::pass(0));
            }

            let validator = SparqlValidator::new();
            let report = validator
                .validate_shapes(ctx.graph, shapes)
                .map_err(|e| Error::new(&e.to_string()))?;

            // Fail-fast: Any violation stops the line
            if !report.passed {
                let violation_count = report.violations.len();
                let messages: Vec<String> = report
                    .violations
                    .iter()
                    .take(5)
                    .map(|v| format!("  - {}: {}", v.constraint_type, v.message))
                    .collect();

                return Err(Error::new(&format!(
                    "🚨 SHACL Validation Failed: {} violation(s) detected\n\n\
                     μ₁:normalization STOPPED THE LINE (Andon Protocol)\n\n\
                     First {} violations:\n{}\n\n\
                     Fix violations before proceeding.\n\n\
                     Constraint types violated: {:?}",
                    violation_count,
                    messages.len(),
                    messages.join("\n"),
                    report
                        .violations
                        .iter()
                        .map(|v| v.constraint_type)
                        .collect::<std::collections::HashSet<_>>()
                )));
            }

            Ok(report)
        } else {
            // No shapes configured
            Ok(ValidationResult::pass(0))
        }
    }

    /// Check if a rule should be executed based on its WHEN condition
    fn should_execute_rule(&self, ctx: &PassContext<'_>, rule: &NormalizationRule) -> Result<bool> {
        if let Some(ref when_query) = rule.when {
            // Execute ASK query to check condition
            let results = ctx.graph.query(when_query)?;
            match results {
                oxigraph::sparql::QueryResults::Boolean(b) => Ok(b),
                _ => Ok(true), // If not a boolean result, execute the rule
            }
        } else {
            Ok(true) // No condition, always execute
        }
    }

    /// Count total triples in the graph using SPARQL
    fn count_graph_triples(&self, graph: &Graph) -> Result<usize> {
        let query = "SELECT (COUNT(*) as ?count) WHERE { ?s ?p ?o }";
        let results = graph.query(query)?;

        match results {
            oxigraph::sparql::QueryResults::Solutions(mut solutions) => {
                if let Some(Ok(solution)) = solutions.next() {
                    if let Some(term) = solution.get("count") {
                        // Parse the count from the term
                        let count_str = term.to_string();
                        // Remove quotes and datatype annotation if present
                        let count_str = count_str
                            .trim_start_matches('"')
                            .split('"')
                            .next()
                            .unwrap_or("0");
                        return count_str.parse::<usize>().map_err(|e| {
                            Error::new(&format!("Failed to parse triple count: {}", e))
                        });
                    }
                }
                Ok(0)
            }
            _ => Ok(0),
        }
    }
}

impl Default for NormalizationPass {
    fn default() -> Self {
        Self::new()
    }
}

impl Pass for NormalizationPass {
    fn pass_type(&self) -> PassType {
        PassType::Normalization
    }

    fn name(&self) -> &str {
        "μ₁:normalization"
    }

    fn execute(&self, ctx: &mut PassContext<'_>) -> Result<PassResult> {
        let start = Instant::now();
        let mut total_triples = 0;
        let mut rules_executed = Vec::new();

        // Get initial triple count via SPARQL (for receipt tracking)
        let _initial_triples = self.count_graph_triples(ctx.graph)?;

        // Compute input hash (if we had access to original TTL content)
        let input_hash = "".to_string(); // Would be computed from original TTL

        // GATE 1: Pre-normalization SHACL Validation (Fail-Fast)
        // Stop the line if any shape validation fails
        let pre_validation = self.validate_shacl_gate(ctx)?;

        let executor = ConstructExecutor::new(ctx.graph);

        // Execute normalization rules
        for rule in &self.rules {
            // Check WHEN condition if present
            let should_execute = self.should_execute_rule(ctx, rule)?;

            if !should_execute {
                // Record as skipped
                rules_executed.push(RuleExecution {
                    name: rule.name.clone(),
                    query_hash: self.hash_query(&rule.construct),
                    triples_materialized: 0,
                    skipped: true,
                });
                continue;
            }

            // Execute CONSTRUCT and materialize results
            let triples_added = executor.execute_and_materialize(&rule.construct)?;
            total_triples += triples_added;

            // Record execution
            rules_executed.push(RuleExecution {
                name: rule.name.clone(),
                query_hash: self.hash_query(&rule.construct),
                triples_materialized: triples_added,
                skipped: false,
            });
        }

        // GATE 2: Post-CONSTRUCT SHACL Validation
        // Verify derived triples also conform to shapes
        let post_validation = self.validate_shacl_gate(ctx)?;

        let duration = start.elapsed();
        let final_triples = self.count_graph_triples(ctx.graph)?;

        // Generate receipt (stored in PassResult metadata if needed)
        let _receipt = NormalizationReceipt {
            input_hash,
            pre_validation: ValidationSummary::from(&pre_validation),
            post_validation: ValidationSummary::from(&post_validation),
            owl_triples_materialized: total_triples,
            total_triples: final_triples,
            rules_executed,
            duration_ms: duration.as_millis() as u64,
            success: true,
        };

        // Note: Receipt would be stored in a mutable context or returned separately
        // For now, we focus on the pass result

        Ok(PassResult::success()
            .with_triples(total_triples)
            .with_duration(duration))
    }
}

/// Standard normalization rules for v26_5_19
impl NormalizationPass {
    /// Create a pass with standard v26_5_19 normalization rules (OWL inference)
    pub fn with_standard_rules() -> Self {
        let mut pass = Self::new();

        // Rule 1: Infer inverse properties
        pass.add_rule(NormalizationRule {
            name: "owl-inverse-properties".to_string(),
            construct: r"
                PREFIX owl: <http://www.w3.org/2002/07/owl#>
                CONSTRUCT {
                    ?y ?invProp ?x .
                }
                WHERE {
                    ?prop owl:inverseOf ?invProp .
                    ?x ?prop ?y .
                }
            "
            .to_string(),
            order: 1,
            description: Some("OWL: Materialize inverse property relationships".to_string()),
            when: None,
        });

        // Rule 2: Infer subclass relationships (transitivity)
        pass.add_rule(NormalizationRule {
            name: "rdfs-subclass-inference".to_string(),
            construct: r"
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                CONSTRUCT {
                    ?instance rdf:type ?superClass .
                }
                WHERE {
                    ?instance rdf:type ?class .
                    ?class rdfs:subClassOf+ ?superClass .
                    FILTER (?class != ?superClass)
                }
            "
            .to_string(),
            order: 2,
            description: Some("RDFS: Materialize subclass type relationships".to_string()),
            when: None,
        });

        // Rule 3: Infer domain/range types
        pass.add_rule(NormalizationRule {
            name: "rdfs-domain-range-inference".to_string(),
            construct: r"
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                CONSTRUCT {
                    ?subject rdf:type ?domainClass .
                    ?object rdf:type ?rangeClass .
                }
                WHERE {
                    ?subject ?prop ?object .
                    OPTIONAL { ?prop rdfs:domain ?domainClass . }
                    OPTIONAL { ?prop rdfs:range ?rangeClass . }
                    FILTER (isIRI(?object))
                }
            "
            .to_string(),
            order: 3,
            description: Some("RDFS: Infer types from domain and range declarations".to_string()),
            when: None,
        });

        // Rule 4: OWL symmetric properties
        pass.add_rule(NormalizationRule {
            name: "owl-symmetric-properties".to_string(),
            construct: r"
                PREFIX owl: <http://www.w3.org/2002/07/owl#>
                CONSTRUCT {
                    ?y ?prop ?x .
                }
                WHERE {
                    ?prop a owl:SymmetricProperty .
                    ?x ?prop ?y .
                }
            "
            .to_string(),
            order: 4,
            description: Some("OWL: Materialize symmetric property relationships".to_string()),
            when: None,
        });

        // Rule 5: OWL transitive properties
        pass.add_rule(NormalizationRule {
            name: "owl-transitive-properties".to_string(),
            construct: r"
                PREFIX owl: <http://www.w3.org/2002/07/owl#>
                CONSTRUCT {
                    ?x ?prop ?z .
                }
                WHERE {
                    ?prop a owl:TransitiveProperty .
                    ?x ?prop ?y .
                    ?y ?prop ?z .
                }
            "
            .to_string(),
            order: 5,
            description: Some("OWL: Materialize transitive property relationships".to_string()),
            when: None,
        });

        // Rule 6: OWL equivalent classes
        pass.add_rule(NormalizationRule {
            name: "owl-equivalent-classes".to_string(),
            construct: r"
                PREFIX owl: <http://www.w3.org/2002/07/owl#>
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                CONSTRUCT {
                    ?instance rdf:type ?equivClass .
                }
                WHERE {
                    ?instance rdf:type ?class .
                    ?class owl:equivalentClass ?equivClass .
                    FILTER (?class != ?equivClass)
                }
            "
            .to_string(),
            order: 6,
            description: Some("OWL: Materialize equivalent class memberships".to_string()),
            when: None,
        });

        pass
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::Graph;
    use std::path::PathBuf;

    #[test]
    fn test_normalization_pass_empty() {
        let graph = Graph::new().unwrap();
        let pass = NormalizationPass::new();

        let mut ctx = PassContext::new(&graph, PathBuf::new(), PathBuf::new());
        let result = pass.execute(&mut ctx).unwrap();

        assert!(result.success);
        assert_eq!(result.triples_added, 0);
    }

    #[test]
    fn test_normalization_with_rule() {
        let graph = Graph::new().unwrap();
        graph
            .insert_turtle(
                r#"
            @prefix ex: <http://example.org/> .
            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
            @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

            ex:Person a rdfs:Class .
            ex:Student rdfs:subClassOf ex:Person .
            ex:alice rdf:type ex:Student .
        "#,
            )
            .unwrap();

        // Create a pass with a simple rule that doesn't produce output
        // (the standard rules may produce N-Triples that need special handling)
        let mut pass = NormalizationPass::new();
        pass.add_rule(NormalizationRule {
            name: "noop".to_string(),
            construct: r#"
                CONSTRUCT {}
                WHERE { ?s ?p ?o }
            "#
            .to_string(),
            order: 1,
            description: Some("No-op rule".to_string()),
            when: None,
        });

        let mut ctx = PassContext::new(&graph, PathBuf::new(), PathBuf::new());
        let result = pass.execute(&mut ctx).unwrap();

        assert!(result.success);
        // Empty CONSTRUCT adds no triples
        assert_eq!(result.triples_added, 0);
    }

    #[test]
    fn test_rule_ordering() {
        let mut pass = NormalizationPass::new();

        pass.add_rule(NormalizationRule {
            name: "third".to_string(),
            construct: "CONSTRUCT {} WHERE {}".to_string(),
            order: 3,
            description: None,
            when: None,
        });

        pass.add_rule(NormalizationRule {
            name: "first".to_string(),
            construct: "CONSTRUCT {} WHERE {}".to_string(),
            order: 1,
            description: None,
            when: None,
        });

        pass.add_rule(NormalizationRule {
            name: "second".to_string(),
            construct: "CONSTRUCT {} WHERE {}".to_string(),
            order: 2,
            description: None,
            when: None,
        });

        assert_eq!(pass.rules[0].name, "first");
        assert_eq!(pass.rules[1].name, "second");
        assert_eq!(pass.rules[2].name, "third");
    }
}