data_generator 0.1.119

RDF data shapes implementation in Rust
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
use crate::Result;
use crate::field_generators::{FieldGenerator, GenerationContext};
use rand::Rng;
use rand::seq::SliceRandom;

// XSD datatypes constants
pub const XSD_STRING: &str = "http://www.w3.org/2001/XMLSchema#string";
pub const XSD_INTEGER: &str = "http://www.w3.org/2001/XMLSchema#integer";
pub const XSD_DECIMAL: &str = "http://www.w3.org/2001/XMLSchema#decimal";
pub const XSD_BOOLEAN: &str = "http://www.w3.org/2001/XMLSchema#boolean";
pub const XSD_DATE: &str = "http://www.w3.org/2001/XMLSchema#date";
pub const XSD_DATETIME: &str = "http://www.w3.org/2001/XMLSchema#dateTime";
pub const XSD_ANYURI: &str = "http://www.w3.org/2001/XMLSchema#anyURI";

/// Basic string generator
pub struct StringGenerator;

impl FieldGenerator for StringGenerator {
    fn generate(&self, context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        // Generate based on property context
        let value = if context.property.contains("name") || context.property.contains("Name") {
            generate_name(&mut rng, &context.locale)
        } else if context.property.contains("title") || context.property.contains("Title") {
            generate_title(&mut rng, &context.locale)
        } else if context.property.contains("description")
            || context.property.contains("Description")
        {
            generate_description(&mut rng, &context.locale)
        } else if context.property.contains("email") || context.property.contains("Email") {
            generate_email(&mut rng)
        } else {
            generate_generic_string(&mut rng, &context.locale)
        };

        Ok(value)
    }

    fn name(&self) -> &str {
        "string"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_STRING.to_string()]
    }
}

/// Basic integer generator
pub struct IntegerGenerator;

impl FieldGenerator for IntegerGenerator {
    fn generate(&self, context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        // Get range from parameters or use defaults
        let min = context
            .parameters
            .get("min")
            .and_then(|v| v.as_i64())
            .unwrap_or(-1000) as i32;

        let max = context
            .parameters
            .get("max")
            .and_then(|v| v.as_i64())
            .unwrap_or(10000) as i32;

        let value = rng.gen_range(min..=max);
        Ok(value.to_string())
    }

    fn name(&self) -> &str {
        "integer"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_INTEGER.to_string()]
    }
}

/// Basic decimal generator
pub struct DecimalGenerator;

impl FieldGenerator for DecimalGenerator {
    fn generate(&self, context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        let min = context
            .parameters
            .get("min")
            .and_then(|v| v.as_f64())
            .unwrap_or(-500.0);

        let max = context
            .parameters
            .get("max")
            .and_then(|v| v.as_f64())
            .unwrap_or(500.0);

        let precision = context
            .parameters
            .get("precision")
            .and_then(|v| v.as_u64())
            .unwrap_or(2) as usize;

        let value = rng.gen_range(min..=max);
        Ok(format!("{value:.precision$}"))
    }

    fn name(&self) -> &str {
        "decimal"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_DECIMAL.to_string()]
    }
}

/// Basic boolean generator
pub struct BooleanGenerator;

impl FieldGenerator for BooleanGenerator {
    fn generate(&self, context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        let true_probability = context
            .parameters
            .get("true_probability")
            .and_then(|v| v.as_f64())
            .unwrap_or(0.5);

        let value = rng.gen_bool(true_probability);
        Ok(value.to_string())
    }

    fn name(&self) -> &str {
        "boolean"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_BOOLEAN.to_string()]
    }
}

/// Basic date generator
pub struct DateGenerator;

impl FieldGenerator for DateGenerator {
    fn generate(&self, context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        let start_year = context
            .parameters
            .get("start_year")
            .and_then(|v| v.as_u64())
            .unwrap_or(1950) as i32;

        let end_year = context
            .parameters
            .get("end_year")
            .and_then(|v| v.as_u64())
            .unwrap_or(2024) as i32;

        let year = rng.gen_range(start_year..=end_year);
        let month = rng.gen_range(1..=12);
        let day = rng.gen_range(1..=28); // Simplified to avoid month-specific validation

        Ok(format!("{year:04}-{month:02}-{day:02}"))
    }

    fn name(&self) -> &str {
        "date"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_DATE.to_string()]
    }
}

/// Basic dateTime generator
pub struct DateTimeGenerator;

impl FieldGenerator for DateTimeGenerator {
    fn generate(&self, context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        let start_year = context
            .parameters
            .get("start_year")
            .and_then(|v| v.as_u64())
            .unwrap_or(1950) as i32;

        let end_year = context
            .parameters
            .get("end_year")
            .and_then(|v| v.as_u64())
            .unwrap_or(2024) as i32;

        let year = rng.gen_range(start_year..=end_year);
        let month = rng.gen_range(1..=12);
        let day = rng.gen_range(1..=28); // Simplified to avoid month-specific validation
        let hour = rng.gen_range(0..=23);
        let minute = rng.gen_range(0..=59);
        let second = rng.gen_range(0..=59);

        Ok(format!(
            "{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z"
        ))
    }

    fn name(&self) -> &str {
        "datetime"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_DATETIME.to_string()]
    }
}

/// Basic URI generator
pub struct UriGenerator;

impl FieldGenerator for UriGenerator {
    fn generate(&self, _context: &GenerationContext) -> Result<String> {
        let mut rng = rand::thread_rng();

        let domains = [
            "example.com",
            "example.org",
            "example.net",
            "company.xyz",
            "research.edu",
        ];
        let paths = ["resource", "item", "entity", "object", "data"];

        let domain = domains.choose(&mut rng).unwrap();
        let path = paths.choose(&mut rng).unwrap();
        let id = rng.gen_range(1..10000);

        Ok(format!("http://{domain}/{path}/{id}"))
    }

    fn name(&self) -> &str {
        "uri"
    }

    fn supported_datatypes(&self) -> Vec<String> {
        vec![XSD_ANYURI.to_string()]
    }
}

// Helper functions for generating realistic data

fn generate_name(rng: &mut impl Rng, locale: &str) -> String {
    let first_names = match locale {
        "es" => &[
            "Ana", "Carlos", "María", "Diego", "Elena", "Fernando", "Isabel", "Jorge", "Laura",
            "Miguel",
        ],
        "fr" => &[
            "Alice",
            "Bernard",
            "Claire",
            "David",
            "Élise",
            "François",
            "Gabrielle",
            "Henri",
            "Isabelle",
            "Jacques",
        ],
        _ => &[
            "Alice", "Bob", "Charlie", "Diana", "Edward", "Fiona", "George", "Hannah", "Ian",
            "Julia",
        ],
    };

    let last_names = match locale {
        "es" => &[
            "García",
            "Rodríguez",
            "González",
            "Fernández",
            "López",
            "Martínez",
            "Sánchez",
            "Pérez",
            "Gómez",
            "Ruiz",
        ],
        "fr" => &[
            "Martin", "Bernard", "Dubois", "Thomas", "Robert", "Richard", "Petit", "Durand",
            "Leroy", "Moreau",
        ],
        _ => &[
            "Smith",
            "Johnson",
            "Williams",
            "Brown",
            "Jones",
            "Garcia",
            "Miller",
            "Davis",
            "Rodriguez",
            "Martinez",
        ],
    };

    let first = first_names.choose(rng).unwrap();
    let last = last_names.choose(rng).unwrap();

    format!("{first} {last}")
}

fn generate_title(rng: &mut impl Rng, locale: &str) -> String {
    let adjectives = match locale {
        "es" => &[
            "Moderno",
            "Avanzado",
            "Eficiente",
            "Innovador",
            "Práctico",
            "Teórico",
            "Aplicado",
        ],
        "fr" => &[
            "Moderne",
            "Avancé",
            "Efficace",
            "Innovant",
            "Pratique",
            "Théorique",
            "Appliqué",
        ],
        _ => &[
            "Modern",
            "Advanced",
            "Efficient",
            "Innovative",
            "Practical",
            "Theoretical",
            "Applied",
        ],
    };

    let nouns = match locale {
        "es" => &[
            "Sistema",
            "Proyecto",
            "Análisis",
            "Investigación",
            "Desarrollo",
            "Plataforma",
        ],
        "fr" => &[
            "Système",
            "Projet",
            "Analyse",
            "Recherche",
            "Développement",
            "Plateforme",
        ],
        _ => &[
            "System",
            "Project",
            "Analysis",
            "Research",
            "Development",
            "Platform",
        ],
    };

    let adj = adjectives.choose(rng).unwrap();
    let noun = nouns.choose(rng).unwrap();

    format!("{adj} {noun}")
}

fn generate_description(rng: &mut impl Rng, _locale: &str) -> String {
    let templates = [
        "This is a comprehensive overview of the subject matter.",
        "An innovative approach to solving complex problems.",
        "A detailed analysis of current methodologies and practices.",
        "Research findings and their practical applications.",
        "Advanced techniques for modern challenges.",
    ];

    templates.choose(rng).unwrap().to_string()
}

fn generate_email(rng: &mut impl Rng) -> String {
    let prefixes = [
        "user",
        "admin",
        "contact",
        "info",
        "support",
        "sales",
        "john.doe",
        "jane.smith",
    ];
    let domains = ["example.com", "company.org", "research.edu", "business.net"];

    let prefix = prefixes.choose(rng).unwrap();
    let domain = domains.choose(rng).unwrap();
    let number = rng.gen_range(1..1000);

    if rng.gen_bool(0.3) {
        format!("{prefix}{number}@{domain}")
    } else {
        format!("{prefix}@{domain}")
    }
}

fn generate_generic_string(rng: &mut impl Rng, _locale: &str) -> String {
    let words = [
        "Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta", "Theta",
    ];
    let word = words.choose(rng).unwrap();
    let number = rng.gen_range(1..1000);

    format!("{word}{number:03}")
}