datasynth-core 2.4.0

Core domain models, traits, and distributions for synthetic enterprise data generation
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
//! Template provider trait and implementations.
//!
//! This module defines the `TemplateProvider` trait for accessing template data,
//! along with implementations that combine embedded and file-based templates.

use rand::seq::IndexedRandom;
use rand::Rng;
use std::sync::Arc;

use super::loader::{MergeStrategy, TemplateData, TemplateLoader};
use super::names::NameCulture;
use crate::models::BusinessProcess;

/// Trait for providing template data to generators.
///
/// This trait abstracts the source of template data, allowing generators
/// to work with either embedded templates, file-based templates, or a
/// combination of both.
///
/// Methods use `&mut dyn Rng` to allow the trait to be dyn-compatible.
pub trait TemplateProvider: Send + Sync {
    /// Get a random person first name for the given culture and gender.
    fn get_person_first_name(
        &self,
        culture: NameCulture,
        is_male: bool,
        rng: &mut dyn Rng,
    ) -> String;

    /// Get a random person last name for the given culture.
    fn get_person_last_name(&self, culture: NameCulture, rng: &mut dyn Rng) -> String;

    /// Get a random vendor name for the given category.
    fn get_vendor_name(&self, category: &str, rng: &mut dyn Rng) -> String;

    /// Get a random customer name for the given industry.
    fn get_customer_name(&self, industry: &str, rng: &mut dyn Rng) -> String;

    /// Get a random material description for the given type.
    fn get_material_description(&self, material_type: &str, rng: &mut dyn Rng) -> String;

    /// Get a random asset description for the given category.
    fn get_asset_description(&self, category: &str, rng: &mut dyn Rng) -> String;

    /// Get a random line text for the given process and account type.
    fn get_line_text(
        &self,
        process: BusinessProcess,
        account_type: &str,
        rng: &mut dyn Rng,
    ) -> String;

    /// Get a random header text template for the given process.
    fn get_header_template(&self, process: BusinessProcess, rng: &mut dyn Rng) -> String;
}

/// Default template provider using embedded templates with optional file overrides.
pub struct DefaultTemplateProvider {
    /// Loaded template data (file-based)
    template_data: Option<TemplateData>,
    /// Merge strategy for combining embedded and file templates
    merge_strategy: MergeStrategy,
}

impl DefaultTemplateProvider {
    /// Create a new provider with embedded templates only.
    pub fn new() -> Self {
        Self {
            template_data: None,
            merge_strategy: MergeStrategy::Extend,
        }
    }

    /// Create a provider with file-based templates.
    pub fn with_templates(template_data: TemplateData, strategy: MergeStrategy) -> Self {
        Self {
            template_data: Some(template_data),
            merge_strategy: strategy,
        }
    }

    /// Load templates from a file path.
    pub fn from_file(path: &std::path::Path) -> Result<Self, super::loader::TemplateError> {
        let data = TemplateLoader::load_from_file(path)?;
        Ok(Self::with_templates(data, MergeStrategy::Extend))
    }

    /// Load templates from a directory.
    pub fn from_directory(path: &std::path::Path) -> Result<Self, super::loader::TemplateError> {
        let data = TemplateLoader::load_from_directory(path)?;
        Ok(Self::with_templates(data, MergeStrategy::Extend))
    }

    /// Set the merge strategy.
    pub fn with_merge_strategy(mut self, strategy: MergeStrategy) -> Self {
        self.merge_strategy = strategy;
        self
    }

    /// Get embedded German first names (sample).
    fn embedded_german_first_names_male() -> Vec<&'static str> {
        vec![
            "Hans", "Klaus", "Wolfgang", "Dieter", "Michael", "Stefan", "Thomas", "Andreas",
            "Peter", "Jürgen", "Matthias", "Frank", "Martin", "Bernd",
        ]
    }

    fn embedded_german_first_names_female() -> Vec<&'static str> {
        vec![
            "Anna",
            "Maria",
            "Elisabeth",
            "Ursula",
            "Monika",
            "Petra",
            "Karin",
            "Sabine",
            "Andrea",
            "Christine",
            "Gabriele",
            "Heike",
            "Birgit",
        ]
    }

    fn embedded_german_last_names() -> Vec<&'static str> {
        vec![
            "Müller",
            "Schmidt",
            "Schneider",
            "Fischer",
            "Weber",
            "Meyer",
            "Wagner",
            "Becker",
            "Schulz",
            "Hoffmann",
            "Schäfer",
            "Koch",
            "Bauer",
            "Richter",
        ]
    }

    fn embedded_us_first_names_male() -> Vec<&'static str> {
        vec![
            "James",
            "John",
            "Robert",
            "Michael",
            "William",
            "David",
            "Richard",
            "Joseph",
            "Thomas",
            "Charles",
            "Christopher",
            "Daniel",
            "Matthew",
        ]
    }

    fn embedded_us_first_names_female() -> Vec<&'static str> {
        vec![
            "Mary",
            "Patricia",
            "Jennifer",
            "Linda",
            "Barbara",
            "Elizabeth",
            "Susan",
            "Jessica",
            "Sarah",
            "Karen",
            "Lisa",
            "Nancy",
            "Betty",
            "Margaret",
        ]
    }

    fn embedded_us_last_names() -> Vec<&'static str> {
        vec![
            "Smith",
            "Johnson",
            "Williams",
            "Brown",
            "Jones",
            "Garcia",
            "Miller",
            "Davis",
            "Rodriguez",
            "Martinez",
            "Hernandez",
            "Lopez",
            "Gonzalez",
        ]
    }

    fn embedded_vendor_names_manufacturing() -> Vec<&'static str> {
        vec![
            "Precision Parts Inc.",
            "Industrial Components LLC",
            "Advanced Materials Corp.",
            "Steel Solutions GmbH",
            "Quality Fasteners Ltd.",
            "Machining Excellence Inc.",
        ]
    }

    fn embedded_vendor_names_services() -> Vec<&'static str> {
        vec![
            "Consulting Partners LLP",
            "Technical Services Inc.",
            "Professional Solutions LLC",
            "Business Advisory Group",
            "Strategic Consulting Co.",
            "Expert Services Ltd.",
        ]
    }

    fn embedded_customer_names_automotive() -> Vec<&'static str> {
        vec![
            "AutoWerke Industries",
            "Vehicle Tech Solutions",
            "Motor Parts Direct",
            "Automotive Excellence Corp.",
            "Drive Systems Inc.",
            "Engine Components Ltd.",
        ]
    }

    fn embedded_customer_names_retail() -> Vec<&'static str> {
        vec![
            "Retail Solutions Corp.",
            "Consumer Goods Direct",
            "Shop Smart Inc.",
            "Merchandise Holdings LLC",
            "Retail Distribution Co.",
            "Store Systems Ltd.",
        ]
    }

    fn culture_to_key(culture: NameCulture) -> &'static str {
        match culture {
            NameCulture::WesternUs => "us",
            NameCulture::German => "german",
            NameCulture::Hispanic => "hispanic",
            NameCulture::French => "french",
            NameCulture::Chinese => "chinese",
            NameCulture::Japanese => "japanese",
            NameCulture::Indian => "indian",
        }
    }

    fn process_to_key(process: BusinessProcess) -> &'static str {
        match process {
            BusinessProcess::P2P => "p2p",
            BusinessProcess::O2C => "o2c",
            BusinessProcess::H2R => "h2r",
            BusinessProcess::R2R => "r2r",
            _ => "other",
        }
    }
}

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

impl TemplateProvider for DefaultTemplateProvider {
    fn get_person_first_name(
        &self,
        culture: NameCulture,
        is_male: bool,
        rng: &mut dyn Rng,
    ) -> String {
        let key = Self::culture_to_key(culture);

        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(culture_names) = data.person_names.cultures.get(key) {
                let names = if is_male {
                    &culture_names.male_first_names
                } else {
                    &culture_names.female_first_names
                };
                if !names.is_empty() {
                    if let Some(name) = names.choose(rng) {
                        return name.clone();
                    }
                }
            }
        }

        // Fall back to embedded templates
        let embedded = match culture {
            NameCulture::German => {
                if is_male {
                    Self::embedded_german_first_names_male()
                } else {
                    Self::embedded_german_first_names_female()
                }
            }
            _ => {
                if is_male {
                    Self::embedded_us_first_names_male()
                } else {
                    Self::embedded_us_first_names_female()
                }
            }
        };

        embedded.choose(rng).unwrap_or(&"Unknown").to_string()
    }

    fn get_person_last_name(&self, culture: NameCulture, rng: &mut dyn Rng) -> String {
        let key = Self::culture_to_key(culture);

        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(culture_names) = data.person_names.cultures.get(key) {
                if !culture_names.last_names.is_empty() {
                    if let Some(name) = culture_names.last_names.choose(rng) {
                        return name.clone();
                    }
                }
            }
        }

        // Fall back to embedded templates
        let embedded = match culture {
            NameCulture::German => Self::embedded_german_last_names(),
            _ => Self::embedded_us_last_names(),
        };

        embedded.choose(rng).unwrap_or(&"Unknown").to_string()
    }

    fn get_vendor_name(&self, category: &str, rng: &mut dyn Rng) -> String {
        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(names) = data.vendor_names.categories.get(category) {
                if !names.is_empty() {
                    if let Some(name) = names.choose(rng) {
                        return name.clone();
                    }
                }
            }
        }

        // Fall back to embedded templates
        let embedded = match category {
            "manufacturing" => Self::embedded_vendor_names_manufacturing(),
            "services" => Self::embedded_vendor_names_services(),
            _ => {
                tracing::debug!(
                    "Unknown vendor name category '{}', falling back to manufacturing",
                    category
                );
                Self::embedded_vendor_names_manufacturing()
            }
        };

        embedded
            .choose(rng)
            .unwrap_or(&"Unknown Vendor")
            .to_string()
    }

    fn get_customer_name(&self, industry: &str, rng: &mut dyn Rng) -> String {
        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(names) = data.customer_names.industries.get(industry) {
                if !names.is_empty() {
                    if let Some(name) = names.choose(rng) {
                        return name.clone();
                    }
                }
            }
        }

        // Fall back to embedded templates
        let embedded = match industry {
            "automotive" => Self::embedded_customer_names_automotive(),
            "retail" => Self::embedded_customer_names_retail(),
            _ => {
                tracing::debug!(
                    "Unknown customer name industry '{}', falling back to retail",
                    industry
                );
                Self::embedded_customer_names_retail()
            }
        };

        embedded
            .choose(rng)
            .unwrap_or(&"Unknown Customer")
            .to_string()
    }

    fn get_material_description(&self, material_type: &str, rng: &mut dyn Rng) -> String {
        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(descs) = data.material_descriptions.by_type.get(material_type) {
                if !descs.is_empty() {
                    if let Some(desc) = descs.choose(rng) {
                        return desc.clone();
                    }
                }
            }
        }

        // Fall back to generic
        format!("{material_type} material")
    }

    fn get_asset_description(&self, category: &str, rng: &mut dyn Rng) -> String {
        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(descs) = data.asset_descriptions.by_category.get(category) {
                if !descs.is_empty() {
                    if let Some(desc) = descs.choose(rng) {
                        return desc.clone();
                    }
                }
            }
        }

        // Fall back to generic
        format!("{category} asset")
    }

    fn get_line_text(
        &self,
        process: BusinessProcess,
        account_type: &str,
        rng: &mut dyn Rng,
    ) -> String {
        let key = Self::process_to_key(process);

        // Try file templates first
        if let Some(ref data) = self.template_data {
            let descs_map = match process {
                BusinessProcess::P2P => &data.line_item_descriptions.p2p,
                BusinessProcess::O2C => &data.line_item_descriptions.o2c,
                BusinessProcess::H2R => &data.line_item_descriptions.h2r,
                BusinessProcess::R2R => &data.line_item_descriptions.r2r,
                _ => &data.line_item_descriptions.p2p,
            };

            if let Some(descs) = descs_map.get(account_type) {
                if !descs.is_empty() {
                    if let Some(desc) = descs.choose(rng) {
                        return desc.clone();
                    }
                }
            }
        }

        // Fall back to generic
        format!("{} posting", key.to_uppercase())
    }

    fn get_header_template(&self, process: BusinessProcess, rng: &mut dyn Rng) -> String {
        let key = Self::process_to_key(process);

        // Try file templates first
        if let Some(ref data) = self.template_data {
            if let Some(templates) = data.header_text_templates.by_process.get(key) {
                if !templates.is_empty() {
                    if let Some(template) = templates.choose(rng) {
                        return template.clone();
                    }
                }
            }
        }

        // Fall back to generic
        format!("{} Transaction", key.to_uppercase())
    }
}

/// A thread-safe wrapper around a template provider.
pub type SharedTemplateProvider = Arc<dyn TemplateProvider>;

/// Create a default shared template provider.
pub fn default_provider() -> SharedTemplateProvider {
    Arc::new(DefaultTemplateProvider::new())
}

/// Create a shared template provider from a file.
pub fn provider_from_file(
    path: &std::path::Path,
) -> Result<SharedTemplateProvider, super::loader::TemplateError> {
    Ok(Arc::new(DefaultTemplateProvider::from_file(path)?))
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use rand::SeedableRng;
    use rand_chacha::ChaCha8Rng;

    #[test]
    fn test_default_provider() {
        let provider = DefaultTemplateProvider::new();
        let mut rng = ChaCha8Rng::seed_from_u64(12345);

        let name = provider.get_person_first_name(NameCulture::German, true, &mut rng);
        assert!(!name.is_empty());

        let last_name = provider.get_person_last_name(NameCulture::German, &mut rng);
        assert!(!last_name.is_empty());
    }

    #[test]
    fn test_vendor_names() {
        let provider = DefaultTemplateProvider::new();
        let mut rng = ChaCha8Rng::seed_from_u64(12345);

        let name = provider.get_vendor_name("manufacturing", &mut rng);
        assert!(!name.is_empty());
        assert!(!name.contains("Unknown"));
    }

    #[test]
    fn test_shared_provider() {
        let provider = default_provider();
        let mut rng = ChaCha8Rng::seed_from_u64(12345);

        let name = provider.get_customer_name("retail", &mut rng);
        assert!(!name.is_empty());
    }
}