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
//! Standard Ontology Validation and Screening
//!
//! Enforces BIG BANG 80/20 principle: Reject custom ontologies, force users
//! to use proven standard bases (schema.org, FOAF, Dublin Core, SKOS, Big Five).
//!
//! **Design Philosophy**: Like Seth's failure to find "one ontology link", users
//! often build custom ontologies when standard solutions exist. This module forces
//! the discipline: provide real data first, validate immediately, no custom schemas.
//!
//! ## Supported Standard Ontologies
//!
//! - **schema.org** (v3.13+) - Largest vocabulary for structured data on the web
//! - **FOAF** (Friend of a Friend) - Social networks, personal profiles
//! - **Dublin Core** (v1.1) - Metadata and document properties
//! - **SKOS** (Simple Knowledge Organization System) - Thesauri, controlled vocabularies
//! - **Big Five Traits** - Personality modeling (OCEAN framework)
//!
//! All others are rejected in screening mode.
//!
//! ## Constitution Compliance
//!
//! - ✓ Principle V: Type-first (enum-based standard list)
//! - ✓ Principle VII: Result<T,E> (no panics)
//! - ✓ Principle IX: Poka-yoke (reject bad choices at gate)

use crate::validation::error::{Result, ValidationError};
use std::collections::HashSet;

/// Standard ontologies approved for use in ggen projects
///
/// Using a standard ontology is non-negotiable in BIG BANG 80/20 mode.
/// This enum enforces the discipline: no "custom" or "special" schemas.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StandardOntology {
    /// schema.org (v3.13.0+) - Web structured data vocabulary
    ///
    /// Largest vocabulary with 500+ types for real-world domains:
    /// Person, Organization, Place, Event, Product, etc.
    /// **Canonical source**: <https://schema.org/>
    SchemaOrg,

    /// FOAF (Friend of a Friend) - Social networks and profiles
    ///
    /// RDF vocabulary for describing people, their interests, connections.
    /// **Canonical source**: <http://xmlns.com/foaf/0.1/>
    Foaf,

    /// Dublin Core (v1.1) - Metadata and document properties
    ///
    /// 15 core metadata properties: title, creator, date, subject, etc.
    /// **Canonical source**: <http://purl.org/dc/elements/1.1/>
    DublinCore,

    /// SKOS (Simple Knowledge Organization System) - Thesauri
    ///
    /// Controlled vocabularies, concept schemes, hierarchies.
    /// **Canonical source**: <http://www.w3.org/2004/02/skos/core#>
    Skos,

    /// Big Five Personality Traits (OCEAN) - Behavioral modeling
    ///
    /// Openness, Conscientiousness, Extraversion, Agreeableness, Neuroticism
    /// **Canonical source**: Custom namespace (ggen-approved)
    BigFive,
}

impl StandardOntology {
    /// Get the canonical namespace/prefix for this ontology
    pub fn namespace(&self) -> &'static str {
        match self {
            StandardOntology::SchemaOrg => "https://schema.org/",
            StandardOntology::Foaf => "http://xmlns.com/foaf/0.1/",
            StandardOntology::DublinCore => "http://purl.org/dc/elements/1.1/",
            StandardOntology::Skos => "http://www.w3.org/2004/02/skos/core#",
            StandardOntology::BigFive => "https://ggen.io/ontologies/big-five#",
        }
    }

    /// Get human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            StandardOntology::SchemaOrg => "schema.org",
            StandardOntology::Foaf => "FOAF",
            StandardOntology::DublinCore => "Dublin Core",
            StandardOntology::Skos => "SKOS",
            StandardOntology::BigFive => "Big Five Traits",
        }
    }

    /// Get recommended version constraint
    pub fn version_constraint(&self) -> &'static str {
        match self {
            StandardOntology::SchemaOrg => "^3.13.0",
            StandardOntology::Foaf => "~0.1.0",
            StandardOntology::DublinCore => "^1.1.0",
            StandardOntology::Skos => "~0.3.0",
            StandardOntology::BigFive => "^0.1.0",
        }
    }

    /// Get link to canonical specification
    pub fn spec_url(&self) -> &'static str {
        match self {
            StandardOntology::SchemaOrg => "https://schema.org/",
            StandardOntology::Foaf => "http://xmlns.com/foaf/spec/",
            StandardOntology::DublinCore => "http://dublincore.org/documents/dces/",
            StandardOntology::Skos => "https://www.w3.org/2009/08/skos-reference/skos.html",
            StandardOntology::BigFive => "https://ggen.io/docs/big-five",
        }
    }

    /// All approved standard ontologies
    pub fn all() -> &'static [StandardOntology] {
        &[
            StandardOntology::SchemaOrg,
            StandardOntology::Foaf,
            StandardOntology::DublinCore,
            StandardOntology::Skos,
            StandardOntology::BigFive,
        ]
    }

    /// All approved namespace URIs
    pub fn approved_namespaces() -> HashSet<&'static str> {
        Self::all().iter().map(|o| o.namespace()).collect()
    }

    /// Resolve a namespace to a standard ontology
    pub fn from_namespace(namespace: &str) -> Option<Self> {
        match namespace {
            "https://schema.org/" | "http://schema.org/" => Some(StandardOntology::SchemaOrg),
            "http://xmlns.com/foaf/0.1/" | "http://xmlns.com/foaf/" => Some(StandardOntology::Foaf),
            "http://purl.org/dc/elements/1.1/" | "http://purl.org/dc/terms/" => {
                Some(StandardOntology::DublinCore)
            }
            "http://www.w3.org/2004/02/skos/core#" => Some(StandardOntology::Skos),
            "https://ggen.io/ontologies/big-five#" | "https://ggen.io/big-five#" => {
                Some(StandardOntology::BigFive)
            }
            _ => None,
        }
    }
}

/// Validator for standard ontology compliance
pub struct StandardOntologyValidator;

impl StandardOntologyValidator {
    /// Check if an ontology namespace is standard-approved
    ///
    /// # Arguments
    /// - `namespace`: The ontology namespace URI to validate
    /// - `strict`: If true, rejects custom prefixes even if base is standard
    ///
    /// # Returns
    /// - `Ok(StandardOntology)` if namespace is approved
    /// - `Err(ValidationError)` if custom/non-standard
    pub fn validate_namespace(namespace: &str, _strict: bool) -> Result<StandardOntology> {
        StandardOntology::from_namespace(namespace).ok_or_else(|| {
            ValidationError::OxigraphError(format!(
                "Custom ontology namespace '{}' rejected. Use one of: {}",
                namespace,
                Self::approved_list()
            ))
        })
    }

    /// Get formatted list of approved ontologies for error messages
    fn approved_list() -> String {
        StandardOntology::all()
            .iter()
            .map(|o| format!("{} ({})", o.name(), o.namespace()))
            .collect::<Vec<_>>()
            .join(", ")
    }

    /// Check if configuration uses only standard ontologies
    ///
    /// **BIG BANG 80/20 Gate**: Rejects custom ontologies and forces users
    /// to either:
    /// 1. Use standard ontologies with real data
    /// 2. Prove why custom ontology is necessary (not supported)
    pub fn validate_config_ontologies(
        ontology_namespaces: &[String],
    ) -> Result<Vec<StandardOntology>> {
        if ontology_namespaces.is_empty() {
            return Err(ValidationError::OxigraphError(
                "No ontology namespaces configured. Required: one or more of: \
                 schema.org, FOAF, Dublin Core, SKOS, Big Five"
                    .to_string(),
            ));
        }

        let mut validated = Vec::new();
        for namespace in ontology_namespaces {
            match StandardOntology::from_namespace(namespace) {
                Some(ontology) => validated.push(ontology),
                None => {
                    return Err(ValidationError::OxigraphError(
                        format!(
                            "Ontology '{}' is not standard-approved.\n\
                             Approved options:\n{}\n\
                             \n\
                             Why? Custom ontologies delay execution. Seth built a 100-page custom \
                             ontology instead of using schema.org in 5 minutes. Use standards first, \
                             prove market fit, then extend if needed.",
                            namespace,
                            StandardOntology::all()
                                .iter()
                                .map(|o| format!("{} ({})", o.name(), o.namespace()))
                                .collect::<Vec<_>>()
                                .join("\n")
                        ),
                    ))
                }
            }
        }

        Ok(validated)
    }

    /// Screening questions to determine if user is ready for ggen
    ///
    /// These are the "litmus test" questions Sean asked Seth:
    /// - Can you find an existing ontology link?
    /// - Do you have real user data to validate with?
    /// - Can you articulate the problem in one sentence?
    pub fn screening_questions() -> Vec<&'static str> {
        vec![
            "Do you have real user data (CSV, JSON) to validate with? (Not promised, but actual)",
            "Can you find one existing standard ontology that fits your domain? \
             (schema.org, FOAF, Dublin Core, SKOS - 5 minute task, not 3 months)",
            "Can you explain your product in one sentence without a 100-page ontology?",
            "Has anyone (not a friend, not a co-founder) committed time/money to this? \
             (Email, contract, payment - proof, not enthusiasm)",
            "If we gave you 48 hours, could you validate your idea with 10 real users?",
        ]
    }
}

/// Configuration for ontology screening and validation
#[derive(Debug, Clone)]
pub struct OntologyScreeningConfig {
    /// Enforce standard ontologies (BIG BANG 80/20 mode)
    pub strict_standard_only: bool,

    /// Require user data upload before allowing sync
    pub require_user_data: bool,

    /// Require market validation (email list, beta users)
    pub require_market_signal: bool,

    /// Maximum custom namespace URIs allowed (0 in strict mode)
    pub max_custom_namespaces: usize,
}

impl Default for OntologyScreeningConfig {
    fn default() -> Self {
        Self {
            strict_standard_only: true,
            require_user_data: true,
            require_market_signal: true,
            max_custom_namespaces: 0,
        }
    }
}

impl OntologyScreeningConfig {
    /// BIG BANG 80/20 mode: maximum strictness
    pub fn big_bang_80_20() -> Self {
        Self {
            strict_standard_only: true,
            require_user_data: true,
            require_market_signal: true,
            max_custom_namespaces: 0,
        }
    }

    /// Relaxed mode: allow custom if user has proven execution capability
    pub fn permissive() -> Self {
        Self {
            strict_standard_only: false,
            require_user_data: false,
            require_market_signal: false,
            max_custom_namespaces: 3,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_standard_ontology_namespaces() {
        assert_eq!(
            StandardOntology::SchemaOrg.namespace(),
            "https://schema.org/"
        );
        assert_eq!(
            StandardOntology::Foaf.namespace(),
            "http://xmlns.com/foaf/0.1/"
        );
        assert_eq!(
            StandardOntology::DublinCore.namespace(),
            "http://purl.org/dc/elements/1.1/"
        );
        assert_eq!(
            StandardOntology::Skos.namespace(),
            "http://www.w3.org/2004/02/skos/core#"
        );
    }

    #[test]
    fn test_resolve_namespace_schema_org() {
        let result = StandardOntology::from_namespace("https://schema.org/");
        assert_eq!(result, Some(StandardOntology::SchemaOrg));
    }

    #[test]
    fn test_resolve_namespace_foaf() {
        let result = StandardOntology::from_namespace("http://xmlns.com/foaf/0.1/");
        assert_eq!(result, Some(StandardOntology::Foaf));
    }

    #[test]
    fn test_reject_custom_namespace() {
        let result = StandardOntology::from_namespace("https://custom.example.com/ontology#");
        assert_eq!(result, None);
    }

    #[test]
    fn test_validate_config_ontologies_success() {
        let namespaces = vec!["https://schema.org/".to_string()];
        let result = StandardOntologyValidator::validate_config_ontologies(&namespaces);
        assert!(result.is_ok());
        let validated = result.unwrap();
        assert_eq!(validated.len(), 1);
        assert_eq!(validated[0], StandardOntology::SchemaOrg);
    }

    #[test]
    fn test_validate_config_ontologies_multiple() {
        let namespaces = vec![
            "https://schema.org/".to_string(),
            "http://xmlns.com/foaf/0.1/".to_string(),
        ];
        let result = StandardOntologyValidator::validate_config_ontologies(&namespaces);
        assert!(result.is_ok());
        let validated = result.unwrap();
        assert_eq!(validated.len(), 2);
    }

    #[test]
    fn test_validate_config_ontologies_custom_rejected() {
        let namespaces = vec!["https://custom.example.com/ontology#".to_string()];
        let result = StandardOntologyValidator::validate_config_ontologies(&namespaces);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("not standard-approved"));
    }

    #[test]
    fn test_screening_config_big_bang_80_20() {
        let config = OntologyScreeningConfig::big_bang_80_20();
        assert!(config.strict_standard_only);
        assert!(config.require_user_data);
        assert!(config.require_market_signal);
        assert_eq!(config.max_custom_namespaces, 0);
    }

    #[test]
    fn test_screening_config_permissive() {
        let config = OntologyScreeningConfig::permissive();
        assert!(!config.strict_standard_only);
        assert!(!config.require_user_data);
        assert!(!config.require_market_signal);
        assert_eq!(config.max_custom_namespaces, 3);
    }

    #[test]
    fn test_screening_questions_non_empty() {
        let questions = StandardOntologyValidator::screening_questions();
        assert!(!questions.is_empty());
        assert!(questions[0].contains("real user data"));
    }

    #[test]
    fn test_approved_namespaces_set() {
        let namespaces = StandardOntology::approved_namespaces();
        assert_eq!(namespaces.len(), 5); // 5 standard ontologies
        assert!(namespaces.contains("https://schema.org/"));
        assert!(namespaces.contains("http://xmlns.com/foaf/0.1/"));
    }

    #[test]
    fn test_standard_ontology_all() {
        let all = StandardOntology::all();
        assert_eq!(all.len(), 5);
    }

    #[test]
    fn test_standard_ontology_names() {
        assert_eq!(StandardOntology::SchemaOrg.name(), "schema.org");
        assert_eq!(StandardOntology::Foaf.name(), "FOAF");
        assert_eq!(StandardOntology::DublinCore.name(), "Dublin Core");
        assert_eq!(StandardOntology::Skos.name(), "SKOS");
        assert_eq!(StandardOntology::BigFive.name(), "Big Five Traits");
    }

    #[test]
    fn test_empty_ontology_namespaces_rejected() {
        let namespaces: Vec<String> = vec![];
        let result = StandardOntologyValidator::validate_config_ontologies(&namespaces);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("No ontology namespaces"));
    }
}