datasynth-graph 2.3.0

Graph/network export for synthetic accounting data - supports PyTorch Geometric, Neo4j, and DGL formats
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
//! Node models for graph representation.

use chrono::NaiveDate;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Unique identifier for a node.
pub type NodeId = u64;

/// Type of node in the graph.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NodeType {
    /// GL Account node.
    Account,
    /// Journal Entry document node.
    JournalEntry,
    /// Vendor node.
    Vendor,
    /// Customer node.
    Customer,
    /// User/Employee node.
    User,
    /// Company/Legal Entity node.
    Company,
    /// Cost Center node.
    CostCenter,
    /// Profit Center node.
    ProfitCenter,
    /// Material node.
    Material,
    /// Fixed Asset node.
    FixedAsset,
    /// Custom node type.
    Custom(String),
}

impl NodeType {
    /// Returns the type name as a string.
    pub fn as_str(&self) -> &str {
        match self {
            NodeType::Account => "Account",
            NodeType::JournalEntry => "JournalEntry",
            NodeType::Vendor => "Vendor",
            NodeType::Customer => "Customer",
            NodeType::User => "User",
            NodeType::Company => "Company",
            NodeType::CostCenter => "CostCenter",
            NodeType::ProfitCenter => "ProfitCenter",
            NodeType::Material => "Material",
            NodeType::FixedAsset => "FixedAsset",
            NodeType::Custom(s) => s,
        }
    }
}

/// A node in the graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphNode {
    /// Unique node ID.
    pub id: NodeId,
    /// Node type.
    pub node_type: NodeType,
    /// External ID (e.g., account code, vendor ID).
    pub external_id: String,
    /// Node label for display.
    pub label: String,
    /// Numeric features for ML.
    pub features: Vec<f64>,
    /// Categorical features (will be one-hot encoded).
    pub categorical_features: HashMap<String, String>,
    /// Node properties.
    pub properties: HashMap<String, NodeProperty>,
    /// Labels for supervised learning.
    pub labels: Vec<String>,
    /// Is this node an anomaly?
    pub is_anomaly: bool,
    /// Anomaly type if anomalous.
    pub anomaly_type: Option<String>,
}

impl GraphNode {
    /// Creates a new graph node.
    pub fn new(id: NodeId, node_type: NodeType, external_id: String, label: String) -> Self {
        Self {
            id,
            node_type,
            external_id,
            label,
            features: Vec::new(),
            categorical_features: HashMap::new(),
            properties: HashMap::new(),
            labels: Vec::new(),
            is_anomaly: false,
            anomaly_type: None,
        }
    }

    /// Adds a numeric feature.
    pub fn with_feature(mut self, value: f64) -> Self {
        self.features.push(value);
        self
    }

    /// Adds multiple numeric features.
    pub fn with_features(mut self, values: Vec<f64>) -> Self {
        self.features.extend(values);
        self
    }

    /// Adds a categorical feature.
    pub fn with_categorical(mut self, name: &str, value: &str) -> Self {
        self.categorical_features
            .insert(name.to_string(), value.to_string());
        self
    }

    /// Adds a property.
    pub fn with_property(mut self, name: &str, value: NodeProperty) -> Self {
        self.properties.insert(name.to_string(), value);
        self
    }

    /// Marks the node as anomalous.
    pub fn as_anomaly(mut self, anomaly_type: &str) -> Self {
        self.is_anomaly = true;
        self.anomaly_type = Some(anomaly_type.to_string());
        self
    }

    /// Adds a label.
    pub fn with_label(mut self, label: &str) -> Self {
        self.labels.push(label.to_string());
        self
    }

    /// Returns the feature vector dimension.
    pub fn feature_dim(&self) -> usize {
        self.features.len()
    }

    /// Create a graph node from any type implementing `ToNodeProperties`.
    ///
    /// This bridges the domain model structs (with `ToNodeProperties`) to the
    /// graph export pipeline by converting all typed properties into `NodeProperty` values.
    pub fn from_entity(id: NodeId, entity: &dyn datasynth_core::models::ToNodeProperties) -> Self {
        let type_name = entity.node_type_name();
        let mut node = GraphNode::new(
            id,
            NodeType::Custom(type_name.to_string()),
            type_name.to_string(),
            type_name.to_string(),
        );
        for (key, value) in entity.to_node_properties() {
            node.properties.insert(key, NodeProperty::from(value));
        }
        node
    }
}

/// Property value for a node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NodeProperty {
    /// String value.
    String(String),
    /// Integer value.
    Int(i64),
    /// Float value.
    Float(f64),
    /// Decimal value.
    Decimal(Decimal),
    /// Boolean value.
    Bool(bool),
    /// Date value.
    Date(NaiveDate),
    /// List of strings.
    StringList(Vec<String>),
}

impl NodeProperty {
    /// Converts to string representation.
    pub fn to_string_value(&self) -> String {
        match self {
            NodeProperty::String(s) => s.clone(),
            NodeProperty::Int(i) => i.to_string(),
            NodeProperty::Float(f) => f.to_string(),
            NodeProperty::Decimal(d) => d.to_string(),
            NodeProperty::Bool(b) => b.to_string(),
            NodeProperty::Date(d) => d.to_string(),
            NodeProperty::StringList(v) => v.join(","),
        }
    }

    /// Converts to numeric value (for features).
    pub fn to_numeric(&self) -> Option<f64> {
        match self {
            NodeProperty::Int(i) => Some(*i as f64),
            NodeProperty::Float(f) => Some(*f),
            NodeProperty::Decimal(d) => (*d).try_into().ok(),
            NodeProperty::Bool(b) => Some(if *b { 1.0 } else { 0.0 }),
            _ => None,
        }
    }
}

impl From<datasynth_core::models::GraphPropertyValue> for NodeProperty {
    fn from(v: datasynth_core::models::GraphPropertyValue) -> Self {
        use datasynth_core::models::GraphPropertyValue;
        match v {
            GraphPropertyValue::String(s) => NodeProperty::String(s),
            GraphPropertyValue::Int(i) => NodeProperty::Int(i),
            GraphPropertyValue::Float(f) => NodeProperty::Float(f),
            GraphPropertyValue::Decimal(d) => NodeProperty::Decimal(d),
            GraphPropertyValue::Bool(b) => NodeProperty::Bool(b),
            GraphPropertyValue::Date(d) => NodeProperty::Date(d),
            GraphPropertyValue::StringList(v) => NodeProperty::StringList(v),
        }
    }
}

/// Account node with accounting-specific features.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountNode {
    /// Base node.
    pub node: GraphNode,
    /// Account code.
    pub account_code: String,
    /// Account name.
    pub account_name: String,
    /// Account type (Asset, Liability, etc.).
    pub account_type: String,
    /// Account category.
    pub account_category: Option<String>,
    /// Is balance sheet account.
    pub is_balance_sheet: bool,
    /// Normal balance (Debit/Credit).
    pub normal_balance: String,
    /// Company code.
    pub company_code: String,
    /// Country code (ISO 3166-1 alpha-2) of the owning company.
    pub country: Option<String>,
}

impl AccountNode {
    /// Creates a new account node.
    pub fn new(
        id: NodeId,
        account_code: String,
        account_name: String,
        account_type: String,
        company_code: String,
    ) -> Self {
        let node = GraphNode::new(
            id,
            NodeType::Account,
            account_code.clone(),
            format!("{account_code} - {account_name}"),
        );

        Self {
            node,
            account_code,
            account_name,
            account_type,
            account_category: None,
            is_balance_sheet: false,
            normal_balance: "Debit".to_string(),
            company_code,
            country: None,
        }
    }

    /// Computes features for the account node.
    pub fn compute_features(&mut self) {
        // Account type encoding
        let type_feature = match self.account_type.as_str() {
            "Asset" => 0.0,
            "Liability" => 1.0,
            "Equity" => 2.0,
            "Revenue" => 3.0,
            "Expense" => 4.0,
            _ => 5.0,
        };
        self.node.features.push(type_feature);

        // Balance sheet indicator
        self.node
            .features
            .push(if self.is_balance_sheet { 1.0 } else { 0.0 });

        // Normal balance encoding
        self.node.features.push(if self.normal_balance == "Debit" {
            1.0
        } else {
            0.0
        });

        // Account code as normalized numeric feature [0, 1]
        // Parse up to 4 leading digits and divide by 10000.
        let code_prefix: String = self
            .account_code
            .chars()
            .take(4)
            .take_while(|c| c.is_ascii_digit())
            .collect();
        if let Ok(code_num) = code_prefix.parse::<f64>() {
            self.node.features.push(code_num / 10000.0);
        } else {
            self.node.features.push(0.0);
        }

        // Add categorical features
        self.node
            .categorical_features
            .insert("account_type".to_string(), self.account_type.clone());
        self.node
            .categorical_features
            .insert("company_code".to_string(), self.company_code.clone());
        if let Some(ref country) = self.country {
            self.node
                .categorical_features
                .insert("country".to_string(), country.clone());
        }
    }
}

/// User node for approval networks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserNode {
    /// Base node.
    pub node: GraphNode,
    /// User ID.
    pub user_id: String,
    /// User name.
    pub user_name: String,
    /// Department.
    pub department: Option<String>,
    /// Role.
    pub role: Option<String>,
    /// Manager ID.
    pub manager_id: Option<String>,
    /// Approval limit.
    pub approval_limit: Option<Decimal>,
    /// Is active.
    pub is_active: bool,
}

impl UserNode {
    /// Creates a new user node.
    pub fn new(id: NodeId, user_id: String, user_name: String) -> Self {
        let node = GraphNode::new(id, NodeType::User, user_id.clone(), user_name.clone());

        Self {
            node,
            user_id,
            user_name,
            department: None,
            role: None,
            manager_id: None,
            approval_limit: None,
            is_active: true,
        }
    }

    /// Computes features for the user node.
    pub fn compute_features(&mut self) {
        // Active status
        self.node
            .features
            .push(if self.is_active { 1.0 } else { 0.0 });

        // Approval limit (log-scaled)
        if let Some(limit) = self.approval_limit {
            let limit_f64: f64 = limit.try_into().unwrap_or(0.0);
            self.node.features.push((limit_f64 + 1.0).ln());
        } else {
            self.node.features.push(0.0);
        }

        // Add categorical features
        if let Some(ref dept) = self.department {
            self.node
                .categorical_features
                .insert("department".to_string(), dept.clone());
        }
        if let Some(ref role) = self.role {
            self.node
                .categorical_features
                .insert("role".to_string(), role.clone());
        }
    }
}

/// Company/Entity node for entity relationship graphs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompanyNode {
    /// Base node.
    pub node: GraphNode,
    /// Company code.
    pub company_code: String,
    /// Company name.
    pub company_name: String,
    /// Country.
    pub country: String,
    /// Currency.
    pub currency: String,
    /// Is parent company.
    pub is_parent: bool,
    /// Parent company code.
    pub parent_code: Option<String>,
    /// Ownership percentage (if subsidiary).
    pub ownership_percent: Option<Decimal>,
}

impl CompanyNode {
    /// Creates a new company node.
    pub fn new(id: NodeId, company_code: String, company_name: String) -> Self {
        let node = GraphNode::new(
            id,
            NodeType::Company,
            company_code.clone(),
            company_name.clone(),
        );

        Self {
            node,
            company_code,
            company_name,
            country: "US".to_string(),
            currency: "USD".to_string(),
            is_parent: false,
            parent_code: None,
            ownership_percent: None,
        }
    }

    /// Computes features for the company node.
    pub fn compute_features(&mut self) {
        // Is parent
        self.node
            .features
            .push(if self.is_parent { 1.0 } else { 0.0 });

        // Ownership percentage
        if let Some(pct) = self.ownership_percent {
            let pct_f64: f64 = pct.try_into().unwrap_or(0.0);
            self.node.features.push(pct_f64 / 100.0);
        } else {
            self.node.features.push(1.0); // 100% for parent
        }

        // Add categorical features
        self.node
            .categorical_features
            .insert("country".to_string(), self.country.clone());
        self.node
            .categorical_features
            .insert("currency".to_string(), self.currency.clone());
    }
}

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

    #[test]
    fn test_graph_node_creation() {
        let node = GraphNode::new(1, NodeType::Account, "1000".to_string(), "Cash".to_string())
            .with_feature(100.0)
            .with_categorical("type", "Asset");

        assert_eq!(node.id, 1);
        assert_eq!(node.features.len(), 1);
        assert!(node.categorical_features.contains_key("type"));
    }

    #[test]
    fn test_account_node() {
        let mut account = AccountNode::new(
            1,
            "1000".to_string(),
            "Cash".to_string(),
            "Asset".to_string(),
            "1000".to_string(),
        );
        account.is_balance_sheet = true;
        account.compute_features();

        assert!(!account.node.features.is_empty());
    }

    #[test]
    fn test_from_graph_property_value() {
        use datasynth_core::models::GraphPropertyValue;

        let prop: NodeProperty = GraphPropertyValue::Bool(true).into();
        assert!(matches!(prop, NodeProperty::Bool(true)));

        let prop: NodeProperty = GraphPropertyValue::Int(42).into();
        assert!(matches!(prop, NodeProperty::Int(42)));

        let prop: NodeProperty = GraphPropertyValue::String("hello".into()).into();
        assert!(matches!(prop, NodeProperty::String(ref s) if s == "hello"));
    }

    #[test]
    fn test_from_entity() {
        use datasynth_core::models::{GraphPropertyValue, ToNodeProperties};
        use std::collections::HashMap;

        struct TestEntity;
        impl ToNodeProperties for TestEntity {
            fn node_type_name(&self) -> &'static str {
                "test_entity"
            }
            fn node_type_code(&self) -> u16 {
                999
            }
            fn to_node_properties(&self) -> HashMap<String, GraphPropertyValue> {
                let mut p = HashMap::new();
                p.insert("name".into(), GraphPropertyValue::String("Test".into()));
                p.insert("active".into(), GraphPropertyValue::Bool(true));
                p
            }
        }

        let node = GraphNode::from_entity(42, &TestEntity);
        assert_eq!(node.id, 42);
        assert_eq!(node.node_type, NodeType::Custom("test_entity".into()));
        assert!(node.properties.contains_key("name"));
        assert!(node.properties.contains_key("active"));
    }
}