tensorlogic-adapters 0.1.0

Symbol tables, axis metadata, and domain masks for TensorLogic
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//! Integration tests with real-world schema scenarios.

use tensorlogic_adapters::*;

/// Test a complete academic domain schema.
#[test]
fn test_academic_schema() {
    let mut table = SymbolTable::new();

    // Define domains
    table
        .add_domain(DomainInfo::new("Student", 1000))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Professor", 100))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Course", 500))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Department", 20))
        .expect("unwrap");

    // Define domain hierarchy
    let mut hierarchy = DomainHierarchy::new();
    hierarchy.add_subtype("Student", "Person");
    hierarchy.add_subtype("Professor", "Person");

    // Define predicates
    table
        .add_predicate(PredicateInfo::new(
            "enrolled",
            vec!["Student".to_string(), "Course".to_string()],
        ))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new(
            "teaches",
            vec!["Professor".to_string(), "Course".to_string()],
        ))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new(
            "belongs_to",
            vec!["Course".to_string(), "Department".to_string()],
        ))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new(
            "advises",
            vec!["Professor".to_string(), "Student".to_string()],
        ))
        .expect("unwrap");

    // Validate schema
    let validator = SchemaValidator::new(&table);
    let report = validator.validate().expect("unwrap");
    assert!(report.errors.is_empty(), "Schema should be valid");

    // Test serialization round-trip
    let json = table.to_json().expect("unwrap");
    let restored = SymbolTable::from_json(&json).expect("unwrap");
    assert_eq!(table.domains.len(), restored.domains.len());
    assert_eq!(table.predicates.len(), restored.predicates.len());

    // Test compact representation
    let compact = CompactSchema::from_symbol_table(&table);
    let stats = compact.compression_stats();
    // Compression might not always reduce size for medium schemas
    assert!(
        stats.compression_ratio() > 0.0,
        "Compression ratio should be positive"
    );

    println!(
        "Academic schema - Compression ratio: {:.2}%",
        stats.compression_ratio() * 100.0
    );
    println!(
        "Academic schema - Space savings: {:.2}%",
        stats.space_savings()
    );
}

/// Test a social network schema with versioning.
#[test]
fn test_social_network_versioning() {
    // Version 1: Basic social network
    let mut v1 = SymbolTable::new();
    v1.add_domain(DomainInfo::new("User", 10000))
        .expect("unwrap");
    v1.add_domain(DomainInfo::new("Post", 50000))
        .expect("unwrap");
    v1.add_predicate(PredicateInfo::new(
        "follows",
        vec!["User".to_string(), "User".to_string()],
    ))
    .expect("unwrap");
    v1.add_predicate(PredicateInfo::new(
        "created",
        vec!["User".to_string(), "Post".to_string()],
    ))
    .expect("unwrap");

    // Version 2: Add reactions and groups
    let mut v2 = v1.clone();
    v2.add_domain(DomainInfo::new("Group", 1000))
        .expect("unwrap");
    v2.add_domain(DomainInfo::new("Reaction", 5))
        .expect("unwrap");
    v2.add_predicate(PredicateInfo::new(
        "member_of",
        vec!["User".to_string(), "Group".to_string()],
    ))
    .expect("unwrap");
    v2.add_predicate(PredicateInfo::new(
        "reacted_with",
        vec![
            "User".to_string(),
            "Post".to_string(),
            "Reaction".to_string(),
        ],
    ))
    .expect("unwrap");

    // Compare versions
    let diff = compute_diff(&v1, &v2);
    assert_eq!(diff.domains_added.len(), 2);
    assert_eq!(diff.predicates_added.len(), 2);
    assert!(diff.is_backward_compatible());

    let compat = check_compatibility(&v1, &v2);
    assert_eq!(compat, CompatibilityLevel::BackwardCompatible);

    // Generate diff report
    let report = diff.report();
    assert!(report.contains("Domains Added"));
    assert!(report.contains("Group"));
    assert!(report.contains("Reaction"));

    println!("Social network diff report:\n{}", report);
}

/// Test e-commerce schema with constraints.
#[test]
fn test_ecommerce_schema() {
    let mut table = SymbolTable::new();

    // Define domains
    table
        .add_domain(DomainInfo::new("Customer", 100000))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Product", 10000))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Order", 500000))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Category", 100))
        .expect("unwrap");

    // Define predicates with constraints
    let mut purchases = PredicateInfo::new(
        "purchases",
        vec!["Customer".to_string(), "Product".to_string()],
    );
    purchases.constraints = Some(
        PredicateConstraints::new().with_property(PredicateProperty::Functional), // Each purchase links to one product
    );
    table.add_predicate(purchases).expect("unwrap");

    let mut contains =
        PredicateInfo::new("contains", vec!["Order".to_string(), "Product".to_string()]);
    contains.description = Some("Products contained in an order".to_string());
    table.add_predicate(contains).expect("unwrap");

    let mut in_category = PredicateInfo::new(
        "in_category",
        vec!["Product".to_string(), "Category".to_string()],
    );
    in_category.constraints = Some(
        PredicateConstraints::new().with_property(PredicateProperty::Functional), // Each product has one category
    );
    table.add_predicate(in_category).expect("unwrap");

    // Validate
    let validator = SchemaValidator::new(&table);
    let report = validator.validate().expect("unwrap");
    assert!(report.errors.is_empty());

    // Test compiler export
    let export = CompilerExport::export_all(&table);
    assert_eq!(export.domains.len(), 4);
    assert_eq!(export.predicate_signatures.len(), 3);
}

/// Test schema merge and conflict resolution.
#[test]
fn test_schema_merge() {
    // Base schema
    let mut base = SymbolTable::new();
    base.add_domain(DomainInfo::new("Entity", 1000))
        .expect("unwrap");
    base.add_predicate(PredicateInfo::new("related", vec!["Entity".to_string()]))
        .expect("unwrap");

    // Update schema 1: Add attributes
    let mut update1 = SymbolTable::new();
    update1
        .add_domain(DomainInfo::new("Entity", 1000))
        .expect("unwrap"); // Need Entity for predicate
    update1
        .add_domain(DomainInfo::new("Attribute", 100))
        .expect("unwrap");
    update1
        .add_predicate(PredicateInfo::new(
            "has_attr",
            vec!["Entity".to_string(), "Attribute".to_string()],
        ))
        .expect("unwrap");

    // Update schema 2: Modify entity cardinality
    let mut update2 = SymbolTable::new();
    update2
        .add_domain(DomainInfo::new("Entity", 2000))
        .expect("unwrap"); // Increased

    // Merge base + update1
    let merged1 = merge_tables(&base, &update1);
    assert_eq!(merged1.domains.len(), 2);
    assert_eq!(merged1.predicates.len(), 2);

    // Merge merged1 + update2
    let final_merged = merge_tables(&merged1, &update2);
    assert_eq!(
        final_merged
            .get_domain("Entity")
            .expect("unwrap")
            .cardinality,
        2000
    );
}

/// Test parametric types in a real scenario.
#[test]
fn test_collection_schema() {
    let mut table = SymbolTable::new();

    // Define base domains
    table
        .add_domain(DomainInfo::new("User", 1000))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Item", 5000))
        .expect("unwrap");

    // Create parametric types
    let user_list = ParametricType::list(TypeParameter::concrete("User"));
    let item_set = ParametricType::map(
        TypeParameter::concrete("User"),
        TypeParameter::concrete("Item"),
    );

    // Add domains with parametric types
    let mut shopping_cart = DomainInfo::new("ShoppingCart", 1000);
    shopping_cart.parametric_type = Some(item_set);
    table.add_domain(shopping_cart).expect("unwrap");

    let mut friends_list = DomainInfo::new("FriendsList", 1000);
    friends_list.parametric_type = Some(user_list);
    table.add_domain(friends_list).expect("unwrap");

    // Validate
    let validator = SchemaValidator::new(&table);
    let report = validator.validate().expect("unwrap");
    assert!(report.errors.is_empty());
}

/// Test hierarchical domain schema.
#[test]
fn test_organizational_hierarchy() {
    let mut table = SymbolTable::new();
    let mut hierarchy = DomainHierarchy::new();

    // Define organization structure
    table
        .add_domain(DomainInfo::new("Employee", 1000))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Manager", 100))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Director", 20))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Executive", 5))
        .expect("unwrap");

    // Build hierarchy
    hierarchy.add_subtype("Manager", "Employee");
    hierarchy.add_subtype("Director", "Manager");
    hierarchy.add_subtype("Executive", "Director");

    // Verify transitivity
    assert!(hierarchy.is_subtype("Executive", "Employee"));
    assert!(hierarchy.is_subtype("Director", "Employee"));
    assert!(!hierarchy.is_subtype("Employee", "Executive"));

    // Define predicates using hierarchy
    table
        .add_predicate(PredicateInfo::new(
            "reports_to",
            vec!["Employee".to_string(), "Manager".to_string()],
        ))
        .expect("unwrap");

    // Validate hierarchy is acyclic
    assert!(hierarchy.validate_acyclic().is_ok());
}

/// Test metadata and provenance tracking.
#[test]
fn test_metadata_tracking() {
    let mut table = SymbolTable::new();

    // Create domain with metadata
    let mut person = DomainInfo::new("Person", 1000);

    let provenance = Provenance::new("admin", "2025-11-06T00:00:00Z")
        .with_source("schema_v1.yaml", Some(1))
        .with_notes("Initial schema setup");

    let mut documentation = Documentation::new("Represents individuals in the system")
        .with_description("Person domain for storing individual entity data");
    documentation.add_example(Example::new(
        "John Doe",
        "Person { id: 1, name: 'John Doe' }",
    ));

    let mut metadata = Metadata::new();
    metadata.provenance = Some(provenance);
    metadata.documentation = Some(documentation);

    person.metadata = Some(metadata);
    table.add_domain(person).expect("unwrap");

    // Verify metadata
    let retrieved = table.get_domain("Person").expect("unwrap");
    assert!(retrieved.metadata.is_some());
    let metadata = retrieved.metadata.as_ref().expect("unwrap");
    assert!(metadata.provenance.is_some());
    assert!(metadata.documentation.is_some());
}

/// Test large schema performance.
#[test]
fn test_large_schema() {
    let mut table = SymbolTable::new();

    // Create many domains
    for i in 0..100 {
        table
            .add_domain(DomainInfo::new(format!("Domain{}", i), 1000))
            .expect("unwrap");
    }

    // Create many predicates
    for i in 0..200 {
        let domain_idx = i % 100;
        table
            .add_predicate(PredicateInfo::new(
                format!("pred{}", i),
                vec![format!("Domain{}", domain_idx)],
            ))
            .expect("unwrap");
    }

    // Test operations on large schema
    assert_eq!(table.domains.len(), 100);
    assert_eq!(table.predicates.len(), 200);

    // Test compact representation
    let compact = CompactSchema::from_symbol_table(&table);
    let stats = compact.compression_stats();
    println!("Large schema - Unique strings: {}", stats.unique_strings);
    println!(
        "Large schema - Compression ratio: {:.2}",
        stats.compression_ratio()
    );
    println!(
        "Large schema - Space savings: {:.2}%",
        stats.space_savings()
    );

    // Verify round-trip
    let restored = compact.to_symbol_table().expect("unwrap");
    assert_eq!(table.domains.len(), restored.domains.len());
    assert_eq!(table.predicates.len(), restored.predicates.len());
}

/// Test binary serialization for efficient storage.
#[test]
fn test_binary_storage() {
    let mut table = SymbolTable::new();
    table
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");
    table
        .add_domain(DomainInfo::new("Location", 50))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new(
            "at",
            vec!["Person".to_string(), "Location".to_string()],
        ))
        .expect("unwrap");

    // Test binary serialization
    let compact = CompactSchema::from_symbol_table(&table);
    let binary = compact.to_binary().expect("unwrap");

    // Verify size is reasonable
    assert!(!binary.is_empty());
    println!("Binary size: {} bytes", binary.len());

    // Restore from binary
    let restored_compact = CompactSchema::from_binary(&binary).expect("unwrap");
    let restored_table = restored_compact.to_symbol_table().expect("unwrap");

    assert_eq!(table.domains.len(), restored_table.domains.len());
    assert_eq!(table.predicates.len(), restored_table.predicates.len());
}

/// Test validation with compiler bundle.
#[test]
fn test_compiler_bundle_validation() {
    let mut table = SymbolTable::new();
    table
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new("knows", vec!["Person".to_string()]))
        .expect("unwrap");

    // Export to compiler bundle
    let bundle = CompilerExport::export_all(&table);

    // Create another table and validate bundle against it
    let mut other_table = SymbolTable::new();
    other_table
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");

    let result = SymbolTableSync::validate_bundle(&other_table, &bundle).expect("unwrap");
    assert!(result.is_valid());

    // Test invalid bundle
    let mut invalid_bundle = CompilerExportBundle::new();
    invalid_bundle.predicate_signatures.insert(
        "unknown_pred".to_string(),
        vec!["UnknownDomain".to_string()],
    );

    let result = SymbolTableSync::validate_bundle(&table, &invalid_bundle).expect("unwrap");
    assert!(!result.is_valid());
    assert!(!result.errors.is_empty());
}

/// Test CLI validation tool with valid schema.
#[test]
fn test_cli_validate_valid_schema() {
    use std::fs;
    use std::process::Command;

    let temp_dir = std::env::temp_dir();
    let schema_path = temp_dir.join("test_schema_valid.yaml");

    // Create a valid schema
    let mut table = SymbolTable::new();
    table
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new(
            "knows",
            vec!["Person".to_string(), "Person".to_string()],
        ))
        .expect("unwrap");

    let yaml = table.to_yaml().expect("unwrap");
    fs::write(&schema_path, yaml).expect("unwrap");

    // Run validation command
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "schema_validate",
            "--",
            schema_path.to_str().expect("unwrap"),
        ])
        .output();

    // Clean up
    let _ = fs::remove_file(&schema_path);

    if let Ok(output) = output {
        assert!(output.status.success(), "Validation should succeed");
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("Schema validation passed"));
    }
}

/// Test CLI migration tool convert command.
#[test]
fn test_cli_migrate_convert() {
    use std::fs;
    use std::process::Command;

    let temp_dir = std::env::temp_dir();
    let json_path = temp_dir.join("test_schema.json");
    let yaml_path = temp_dir.join("test_schema_converted.yaml");

    // Create a schema in JSON
    let mut table = SymbolTable::new();
    table
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");
    table
        .add_predicate(PredicateInfo::new(
            "knows",
            vec!["Person".to_string(), "Person".to_string()],
        ))
        .expect("unwrap");

    let json = table.to_json().expect("unwrap");
    fs::write(&json_path, json).expect("unwrap");

    // Run convert command
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "schema_migrate",
            "--",
            "convert",
            json_path.to_str().expect("unwrap"),
            yaml_path.to_str().expect("unwrap"),
        ])
        .output();

    // Verify conversion
    if let Ok(output) = output {
        if output.status.success() {
            assert!(yaml_path.exists(), "YAML file should be created");

            // Verify content
            let yaml_content = fs::read_to_string(&yaml_path).expect("unwrap");
            let restored = SymbolTable::from_yaml(&yaml_content).expect("unwrap");
            assert_eq!(table.domains.len(), restored.domains.len());
            assert_eq!(table.predicates.len(), restored.predicates.len());
        }
    }

    // Clean up
    let _ = fs::remove_file(&json_path);
    let _ = fs::remove_file(&yaml_path);
}

/// Test CLI migration tool diff command.
#[test]
fn test_cli_migrate_diff() {
    use std::fs;
    use std::process::Command;

    let temp_dir = std::env::temp_dir();
    let old_path = temp_dir.join("test_schema_old.yaml");
    let new_path = temp_dir.join("test_schema_new.yaml");

    // Create old schema
    let mut old_table = SymbolTable::new();
    old_table
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");
    old_table
        .add_predicate(PredicateInfo::new(
            "knows",
            vec!["Person".to_string(), "Person".to_string()],
        ))
        .expect("unwrap");

    fs::write(&old_path, old_table.to_yaml().expect("unwrap")).expect("unwrap");

    // Create new schema (with additions)
    let mut new_table = old_table.clone();
    new_table
        .add_domain(DomainInfo::new("Location", 50))
        .expect("unwrap");
    new_table
        .add_predicate(PredicateInfo::new(
            "at",
            vec!["Person".to_string(), "Location".to_string()],
        ))
        .expect("unwrap");

    fs::write(&new_path, new_table.to_yaml().expect("unwrap")).expect("unwrap");

    // Run diff command
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "schema_migrate",
            "--",
            "diff",
            old_path.to_str().expect("unwrap"),
            new_path.to_str().expect("unwrap"),
        ])
        .output();

    // Verify diff output
    if let Ok(output) = output {
        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            assert!(stdout.contains("Added domains") || stdout.contains("Added predicates"));
        }
    }

    // Clean up
    let _ = fs::remove_file(&old_path);
    let _ = fs::remove_file(&new_path);
}

/// Test CLI migration tool merge command.
#[test]
fn test_cli_migrate_merge() {
    use std::fs;
    use std::process::Command;

    let temp_dir = std::env::temp_dir();
    let schema1_path = temp_dir.join("test_schema1.yaml");
    let schema2_path = temp_dir.join("test_schema2.yaml");
    let merged_path = temp_dir.join("test_schema_merged.yaml");

    // Create first schema
    let mut table1 = SymbolTable::new();
    table1
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap");
    table1
        .add_predicate(PredicateInfo::new(
            "knows",
            vec!["Person".to_string(), "Person".to_string()],
        ))
        .expect("unwrap");

    fs::write(&schema1_path, table1.to_yaml().expect("unwrap")).expect("unwrap");

    // Create second schema
    let mut table2 = SymbolTable::new();
    table2
        .add_domain(DomainInfo::new("Person", 100))
        .expect("unwrap"); // Added Person domain
    table2
        .add_domain(DomainInfo::new("Location", 50))
        .expect("unwrap");
    table2
        .add_predicate(PredicateInfo::new(
            "at",
            vec!["Person".to_string(), "Location".to_string()],
        ))
        .expect("unwrap");

    fs::write(&schema2_path, table2.to_yaml().expect("unwrap")).expect("unwrap");

    // Run merge command
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "schema_migrate",
            "--",
            "merge",
            schema1_path.to_str().expect("unwrap"),
            schema2_path.to_str().expect("unwrap"),
            merged_path.to_str().expect("unwrap"),
        ])
        .output();

    // Verify merged result
    if let Ok(output) = output {
        if output.status.success() && merged_path.exists() {
            let merged_content = fs::read_to_string(&merged_path).expect("unwrap");
            let merged_table = SymbolTable::from_yaml(&merged_content).expect("unwrap");

            // Should contain domains from both schemas
            assert!(merged_table.get_domain("Person").is_some());
            assert!(merged_table.get_domain("Location").is_some());
        }
    }

    // Clean up
    let _ = fs::remove_file(&schema1_path);
    let _ = fs::remove_file(&schema2_path);
    let _ = fs::remove_file(&merged_path);
}