datasynth-generators 2.2.0

50+ data generators covering GL, P2P, O2C, S2C, HR, manufacturing, audit, tax, treasury, and ESG
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
//! Error cascade modeling.
//!
//! Models how errors propagate through a system, where one error
//! leads to others (e.g., wrong account coding leads to reconciliation
//! differences which lead to correcting entries).

use chrono::NaiveDate;
use rand::Rng;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use datasynth_core::models::AnomalyType;
use datasynth_core::uuid_factory::{DeterministicUuidFactory, GeneratorType};

/// Configuration for cascade generation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CascadeConfig {
    /// Maximum depth of cascade (how many steps).
    pub max_depth: u32,
    /// Probability of cascade continuing at each step.
    pub continuation_probability: f64,
    /// Whether cascades can branch (multiple consequences from one step).
    pub allow_branching: bool,
    /// Maximum branches per step.
    pub max_branches: u32,
}

impl Default for CascadeConfig {
    fn default() -> Self {
        Self {
            max_depth: 4,
            continuation_probability: 0.7,
            allow_branching: true,
            max_branches: 2,
        }
    }
}

/// A step in an error cascade.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CascadeStep {
    /// Step number in cascade.
    pub step: u32,
    /// Anomaly type for this step.
    pub anomaly_type: AnomalyType,
    /// Days after previous step.
    pub lag_days: i32,
    /// Description of why this step occurs.
    pub reason: String,
    /// Whether this step was executed.
    pub executed: bool,
    /// Document ID if executed.
    pub document_id: Option<String>,
    /// Anomaly ID if labeled.
    pub anomaly_id: Option<String>,
}

impl CascadeStep {
    /// Creates a new cascade step.
    pub fn new(step: u32, anomaly_type: AnomalyType, lag_days: i32) -> Self {
        Self {
            step,
            anomaly_type,
            lag_days,
            reason: String::new(),
            executed: false,
            document_id: None,
            anomaly_id: None,
        }
    }

    /// Sets the reason for this step.
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = reason.into();
        self
    }

    /// Marks step as executed.
    pub fn mark_executed(&mut self, document_id: impl Into<String>, anomaly_id: impl Into<String>) {
        self.executed = true;
        self.document_id = Some(document_id.into());
        self.anomaly_id = Some(anomaly_id.into());
    }
}

/// An error cascade instance.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorCascade {
    /// Unique cascade ID.
    pub cascade_id: Uuid,
    /// Trigger anomaly type.
    pub trigger: AnomalyType,
    /// Trigger document ID.
    pub trigger_document_id: String,
    /// Trigger date.
    pub trigger_date: NaiveDate,
    /// Steps in the cascade.
    pub steps: Vec<CascadeStep>,
    /// Current step index.
    pub current_step: usize,
}

impl ErrorCascade {
    /// Creates a new error cascade.
    pub fn new(
        trigger: AnomalyType,
        trigger_document_id: impl Into<String>,
        trigger_date: NaiveDate,
        uuid_factory: &DeterministicUuidFactory,
    ) -> Self {
        Self {
            cascade_id: uuid_factory.next(),
            trigger,
            trigger_document_id: trigger_document_id.into(),
            trigger_date,
            steps: Vec::new(),
            current_step: 0,
        }
    }

    /// Adds a step to the cascade.
    pub fn add_step(&mut self, step: CascadeStep) {
        self.steps.push(step);
    }

    /// Gets the next step to execute.
    pub fn next_step(&self) -> Option<&CascadeStep> {
        self.steps.get(self.current_step)
    }

    /// Gets the next step mutably.
    pub fn next_step_mut(&mut self) -> Option<&mut CascadeStep> {
        self.steps.get_mut(self.current_step)
    }

    /// Advances to the next step.
    pub fn advance(&mut self) {
        if self.current_step < self.steps.len() {
            self.current_step += 1;
        }
    }

    /// Returns whether the cascade is complete.
    pub fn is_complete(&self) -> bool {
        self.current_step >= self.steps.len()
    }

    /// Gets the expected date for the next step.
    pub fn next_step_date(&self) -> Option<NaiveDate> {
        if let Some(step) = self.next_step() {
            // Calculate date based on trigger date plus accumulated lags
            let total_lag: i32 = self.steps[..self.current_step]
                .iter()
                .map(|s| s.lag_days)
                .sum::<i32>()
                + step.lag_days;
            Some(self.trigger_date + chrono::Duration::days(total_lag as i64))
        } else {
            None
        }
    }
}

/// Generator for error cascades.
pub struct CascadeGenerator {
    config: CascadeConfig,
    /// Deterministic UUID factory for cascade IDs.
    uuid_factory: DeterministicUuidFactory,
    /// Active cascades.
    active_cascades: Vec<ErrorCascade>,
    /// Completed cascades.
    completed_cascades: Vec<ErrorCascade>,
    /// Cascade templates by trigger type.
    templates: Vec<CascadeTemplate>,
}

/// Template for a cascade based on trigger type.
#[derive(Debug, Clone)]
pub struct CascadeTemplate {
    /// Trigger anomaly type.
    pub trigger: AnomalyType,
    /// Potential cascade steps.
    pub steps: Vec<CascadeStepTemplate>,
}

/// Template for a cascade step.
#[derive(Debug, Clone)]
pub struct CascadeStepTemplate {
    /// Anomaly type for this step.
    pub anomaly_type: AnomalyType,
    /// Probability this step occurs.
    pub probability: f64,
    /// Minimum lag days.
    pub lag_min: i32,
    /// Maximum lag days.
    pub lag_max: i32,
    /// Reason description.
    pub reason: String,
}

impl CascadeStepTemplate {
    /// Creates a new step template.
    pub fn new(
        anomaly_type: AnomalyType,
        probability: f64,
        lag_range: (i32, i32),
        reason: impl Into<String>,
    ) -> Self {
        Self {
            anomaly_type,
            probability,
            lag_min: lag_range.0,
            lag_max: lag_range.1,
            reason: reason.into(),
        }
    }
}

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

impl CascadeGenerator {
    /// Creates a new cascade generator.
    pub fn new(config: CascadeConfig) -> Self {
        Self {
            config,
            uuid_factory: DeterministicUuidFactory::new(0, GeneratorType::Anomaly),
            active_cascades: Vec::new(),
            completed_cascades: Vec::new(),
            templates: Self::default_templates(),
        }
    }

    /// Creates default cascade templates.
    fn default_templates() -> Vec<CascadeTemplate> {
        use datasynth_core::models::{ErrorType, ProcessIssueType};

        vec![
            // Account misclassification cascade
            CascadeTemplate {
                trigger: AnomalyType::Error(ErrorType::MisclassifiedAccount),
                steps: vec![
                    CascadeStepTemplate::new(
                        AnomalyType::Error(ErrorType::DuplicateEntry),
                        0.40,
                        (5, 15),
                        "Attempt to correct via additional entry",
                    ),
                    CascadeStepTemplate::new(
                        AnomalyType::Error(ErrorType::ReversedAmount),
                        0.30,
                        (10, 30),
                        "Reversal of original entry",
                    ),
                    CascadeStepTemplate::new(
                        AnomalyType::Error(ErrorType::WrongPeriod),
                        0.25,
                        (30, 60),
                        "Correction posted to wrong period",
                    ),
                ],
            },
            // Wrong period cascade
            CascadeTemplate {
                trigger: AnomalyType::Error(ErrorType::WrongPeriod),
                steps: vec![
                    CascadeStepTemplate::new(
                        AnomalyType::ProcessIssue(ProcessIssueType::LatePosting),
                        0.50,
                        (1, 5),
                        "Late correction posting",
                    ),
                    CascadeStepTemplate::new(
                        AnomalyType::Error(ErrorType::CutoffError),
                        0.35,
                        (5, 15),
                        "Additional cutoff issues from correction",
                    ),
                ],
            },
            // Missing field cascade
            CascadeTemplate {
                trigger: AnomalyType::Error(ErrorType::MissingField),
                steps: vec![
                    CascadeStepTemplate::new(
                        AnomalyType::ProcessIssue(ProcessIssueType::MissingDocumentation),
                        0.60,
                        (1, 7),
                        "Request for missing documentation",
                    ),
                    CascadeStepTemplate::new(
                        AnomalyType::ProcessIssue(ProcessIssueType::LatePosting),
                        0.40,
                        (5, 14),
                        "Delayed posting while gathering info",
                    ),
                ],
            },
            // Duplicate entry cascade
            CascadeTemplate {
                trigger: AnomalyType::Error(ErrorType::DuplicateEntry),
                steps: vec![CascadeStepTemplate::new(
                    AnomalyType::Error(ErrorType::ReversedAmount),
                    0.70,
                    (1, 5),
                    "Reversal of duplicate",
                )],
            },
        ]
    }

    /// Starts a new cascade if a template matches.
    pub fn maybe_start_cascade<R: Rng>(
        &mut self,
        trigger: &AnomalyType,
        document_id: impl Into<String>,
        date: NaiveDate,
        rng: &mut R,
    ) -> Option<Uuid> {
        // Find matching template
        let template = self.templates.iter().find(|t| t.trigger == *trigger)?;

        let mut cascade = ErrorCascade::new(trigger.clone(), document_id, date, &self.uuid_factory);

        // Generate steps from template
        let mut step_num = 0u32;
        for step_template in &template.steps {
            if rng.random::<f64>() < step_template.probability {
                step_num += 1;

                if step_num > self.config.max_depth {
                    break;
                }

                let lag = if step_template.lag_min == step_template.lag_max {
                    step_template.lag_min
                } else {
                    rng.random_range(step_template.lag_min..=step_template.lag_max)
                };

                let step = CascadeStep::new(step_num, step_template.anomaly_type.clone(), lag)
                    .with_reason(&step_template.reason);

                cascade.add_step(step);
            }
        }

        // Only create cascade if it has steps
        if cascade.steps.is_empty() {
            return None;
        }

        let cascade_id = cascade.cascade_id;
        self.active_cascades.push(cascade);
        Some(cascade_id)
    }

    /// Gets cascades that have steps due on or before a given date.
    pub fn get_due_cascades(&mut self, date: NaiveDate) -> Vec<(Uuid, CascadeStep)> {
        let mut due = Vec::new();

        for cascade in &self.active_cascades {
            if let Some(next_date) = cascade.next_step_date() {
                if next_date <= date {
                    if let Some(step) = cascade.next_step() {
                        due.push((cascade.cascade_id, step.clone()));
                    }
                }
            }
        }

        due
    }

    /// Marks a cascade step as executed and advances.
    pub fn execute_step(
        &mut self,
        cascade_id: Uuid,
        document_id: impl Into<String>,
        anomaly_id: impl Into<String>,
    ) {
        let doc_id = document_id.into();
        let ano_id = anomaly_id.into();

        if let Some(cascade) = self
            .active_cascades
            .iter_mut()
            .find(|c| c.cascade_id == cascade_id)
        {
            if let Some(step) = cascade.next_step_mut() {
                step.mark_executed(&doc_id, &ano_id);
            }
            cascade.advance();

            // Move to completed if done
            if cascade.is_complete() {
                // Will be handled by cleanup
            }
        }
    }

    /// Cleans up completed cascades.
    pub fn cleanup(&mut self) {
        let completed: Vec<_> = self
            .active_cascades
            .drain(..)
            .filter(|c| !c.is_complete())
            .collect();

        let newly_completed: Vec<_> = self
            .active_cascades
            .iter()
            .filter(|c| c.is_complete())
            .cloned()
            .collect();

        self.completed_cascades.extend(newly_completed);
        self.active_cascades = completed;
    }

    /// Returns active cascade count.
    pub fn active_count(&self) -> usize {
        self.active_cascades.len()
    }

    /// Returns completed cascade count.
    pub fn completed_count(&self) -> usize {
        self.completed_cascades.len()
    }

    /// Adds a custom template.
    pub fn add_template(&mut self, template: CascadeTemplate) {
        self.templates.push(template);
    }
}

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

    #[test]
    fn test_cascade_step() {
        let step = CascadeStep::new(1, AnomalyType::Error(ErrorType::DuplicateEntry), 5)
            .with_reason("Test reason");

        assert_eq!(step.step, 1);
        assert_eq!(step.lag_days, 5);
        assert!(!step.executed);
    }

    #[test]
    fn test_error_cascade() {
        let uuid_factory = DeterministicUuidFactory::new(42, GeneratorType::Anomaly);
        let mut cascade = ErrorCascade::new(
            AnomalyType::Error(ErrorType::MisclassifiedAccount),
            "JE001",
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            &uuid_factory,
        );

        cascade.add_step(CascadeStep::new(
            1,
            AnomalyType::Error(ErrorType::DuplicateEntry),
            5,
        ));
        cascade.add_step(CascadeStep::new(
            2,
            AnomalyType::Error(ErrorType::ReversedAmount),
            10,
        ));

        assert_eq!(cascade.steps.len(), 2);
        assert!(!cascade.is_complete());

        // Check next step date
        let next_date = cascade.next_step_date().unwrap();
        assert_eq!(next_date, NaiveDate::from_ymd_opt(2024, 1, 20).unwrap());
    }

    #[test]
    fn test_cascade_generator() {
        let mut generator = CascadeGenerator::new(CascadeConfig::default());
        let mut rng = ChaCha8Rng::seed_from_u64(42);

        // Try to start a cascade for misclassified account
        let cascade_id = generator.maybe_start_cascade(
            &AnomalyType::Error(ErrorType::MisclassifiedAccount),
            "JE001",
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            &mut rng,
        );

        // May or may not create cascade depending on RNG
        if cascade_id.is_some() {
            assert!(generator.active_count() > 0);
        }
    }

    #[test]
    fn test_cascade_generator_no_match() {
        let mut generator = CascadeGenerator::new(CascadeConfig::default());
        let mut rng = ChaCha8Rng::seed_from_u64(42);

        // Try to start a cascade for an unregistered trigger
        let cascade_id = generator.maybe_start_cascade(
            &AnomalyType::Fraud(datasynth_core::models::FraudType::SelfApproval),
            "JE001",
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            &mut rng,
        );

        assert!(cascade_id.is_none());
    }
}