|
| 1 | +//! Heartbeat Demo — Proof of life for the ladybug-rs cognitive substrate. |
| 2 | +//! |
| 3 | +//! Exercises the core subsystems in a single pass to verify the system is |
| 4 | +//! alive and functioning: |
| 5 | +//! |
| 6 | +//! 1. BindSpace — 8+8 addressing, O(1) read/write |
| 7 | +//! 2. CogRedis — DN.SET/DN.GET command execution |
| 8 | +//! 3. NARS — Truth value revision |
| 9 | +//! 4. Collapse — Gate evaluation (FLOW/HOLD/BLOCK) |
| 10 | +//! 5. SIMD — Hamming distance computation |
| 11 | +//! |
| 12 | +//! Usage: `cargo run --example heartbeat` |
| 13 | +
|
| 14 | +use std::time::Instant; |
| 15 | + |
| 16 | +use ladybug::core::Fingerprint; |
| 17 | +use ladybug::core::simd::hamming_distance; |
| 18 | +use ladybug::nars::TruthValue; |
| 19 | +use ladybug::cognitive::{get_gate_state, GateState}; |
| 20 | +use ladybug::storage::{BindSpace, Addr, FINGERPRINT_WORDS}; |
| 21 | + |
| 22 | +fn main() { |
| 23 | + println!(); |
| 24 | + println!("=========================================================="); |
| 25 | + println!(" LADYBUG-RS HEARTBEAT"); |
| 26 | + println!("=========================================================="); |
| 27 | + println!(); |
| 28 | + |
| 29 | + let t0 = Instant::now(); |
| 30 | + let mut checks_passed = 0u32; |
| 31 | + let total_checks = 5u32; |
| 32 | + |
| 33 | + // ── 1. BindSpace ──────────────────────────────────────────────────── |
| 34 | + print!(" [1/5] BindSpace 8+8 addressing ... "); |
| 35 | + { |
| 36 | + let mut bs = BindSpace::new(); |
| 37 | + let addr = Addr::new(0x80, 0x01); // Node zone |
| 38 | + let fp = [42u64; FINGERPRINT_WORDS]; |
| 39 | + bs.write_at(addr, fp); |
| 40 | + let read_back = bs.read(addr); |
| 41 | + assert!(read_back.is_some(), "BindSpace read after write failed"); |
| 42 | + let node = read_back.unwrap(); |
| 43 | + assert_eq!(node.fingerprint[0], 42, "Fingerprint data mismatch"); |
| 44 | + checks_passed += 1; |
| 45 | + println!("OK (addr={:04x})", addr.0); |
| 46 | + } |
| 47 | + |
| 48 | + // ── 2. CogRedis ──────────────────────────────────────────────────── |
| 49 | + print!(" [2/5] CogRedis DN.SET/DN.GET ... "); |
| 50 | + { |
| 51 | + use ladybug::storage::{CogRedis, RedisResult}; |
| 52 | + let mut redis = CogRedis::new(); |
| 53 | + |
| 54 | + // DN.SET returns the hex address of the new node |
| 55 | + let result = redis.execute_command("DN.SET Ada:A:heartbeat:test hello"); |
| 56 | + let addr_hex = match &result { |
| 57 | + RedisResult::String(s) => { |
| 58 | + assert!(!s.is_empty(), "DN.SET should return address"); |
| 59 | + s.clone() |
| 60 | + } |
| 61 | + _ => panic!("DN.SET failed: {:?}", result), |
| 62 | + }; |
| 63 | + |
| 64 | + // DN.GET returns an array with node info |
| 65 | + let get_result = redis.execute_command("DN.GET Ada:A:heartbeat:test"); |
| 66 | + match &get_result { |
| 67 | + RedisResult::Array(arr) => { |
| 68 | + assert!(!arr.is_empty(), "DN.GET should return node info"); |
| 69 | + } |
| 70 | + _ => panic!("DN.GET failed: {:?}", get_result), |
| 71 | + } |
| 72 | + checks_passed += 1; |
| 73 | + println!("OK (addr={})", addr_hex); |
| 74 | + } |
| 75 | + |
| 76 | + // ── 3. NARS Truth Value Revision ──────────────────────────────────── |
| 77 | + print!(" [3/5] NARS revision ............. "); |
| 78 | + { |
| 79 | + let tv1 = TruthValue::new(0.9, 0.8); |
| 80 | + let tv2 = TruthValue::new(0.85, 0.7); |
| 81 | + let revised = tv1.revision(&tv2); |
| 82 | + |
| 83 | + // Revised confidence must be higher than either input |
| 84 | + assert!( |
| 85 | + revised.confidence > tv1.confidence && revised.confidence > tv2.confidence, |
| 86 | + "Revision must increase confidence: {:.3} should be > {:.3} and {:.3}", |
| 87 | + revised.confidence, tv1.confidence, tv2.confidence |
| 88 | + ); |
| 89 | + // Frequency should be between inputs (weighted average) |
| 90 | + assert!( |
| 91 | + revised.frequency >= 0.0 && revised.frequency <= 1.0, |
| 92 | + "Frequency out of range" |
| 93 | + ); |
| 94 | + checks_passed += 1; |
| 95 | + println!("OK (f={:.3}, c={:.3})", revised.frequency, revised.confidence); |
| 96 | + } |
| 97 | + |
| 98 | + // ── 4. Collapse Gate ──────────────────────────────────────────────── |
| 99 | + print!(" [4/5] Collapse gate ............. "); |
| 100 | + { |
| 101 | + let flow = get_gate_state(0.05); |
| 102 | + let hold = get_gate_state(0.25); |
| 103 | + let block = get_gate_state(0.45); |
| 104 | + assert_eq!(flow, GateState::Flow, "SD=0.05 should be Flow"); |
| 105 | + assert_eq!(hold, GateState::Hold, "SD=0.25 should be Hold"); |
| 106 | + assert_eq!(block, GateState::Block, "SD=0.45 should be Block"); |
| 107 | + checks_passed += 1; |
| 108 | + println!("OK (Flow/Hold/Block)"); |
| 109 | + } |
| 110 | + |
| 111 | + // ── 5. SIMD Hamming Distance ──────────────────────────────────────── |
| 112 | + print!(" [5/5] SIMD hamming distance ..... "); |
| 113 | + { |
| 114 | + let a = Fingerprint::from_content("heartbeat_a"); |
| 115 | + let b = Fingerprint::from_content("heartbeat_b"); |
| 116 | + let d = hamming_distance(&a, &b); |
| 117 | + assert!(d > 0, "Different fingerprints should have distance > 0"); |
| 118 | + // Self-distance must be 0 |
| 119 | + assert_eq!(hamming_distance(&a, &a), 0, "Self-distance must be 0"); |
| 120 | + checks_passed += 1; |
| 121 | + println!("OK (d={})", d); |
| 122 | + } |
| 123 | + |
| 124 | + // ── Summary ───────────────────────────────────────────────────────── |
| 125 | + let elapsed = t0.elapsed(); |
| 126 | + println!(); |
| 127 | + println!("----------------------------------------------------------"); |
| 128 | + println!(" Result: {}/{} checks passed in {:.1}ms", |
| 129 | + checks_passed, total_checks, elapsed.as_secs_f64() * 1000.0); |
| 130 | + |
| 131 | + if checks_passed == total_checks { |
| 132 | + println!(" HEARTBEAT: ALIVE"); |
| 133 | + } else { |
| 134 | + println!(" HEARTBEAT: DEGRADED ({} failures)", |
| 135 | + total_checks - checks_passed); |
| 136 | + } |
| 137 | + println!("=========================================================="); |
| 138 | + println!(); |
| 139 | +} |
0 commit comments