ruvector/crates/ruvector-dag/examples/basic_usage.rs
Claude bc4e63d4d4
feat(dag): implement Neural Self-Learning DAG with QuDAG integration
Complete implementation of the Neural DAG Learning system combining RuVector
vector database with QuDAG quantum-resistant consensus.

Core Features:
- QueryDag structure with HashMap-based adjacency and cycle detection
- 18+ operator types (SeqScan, HnswScan, HashJoin, NestedLoop, etc.)
- Topological, DFS, and BFS traversal iterators
- JSON/binary serialization

Attention Mechanisms (7 total):
- Basic: Topological, CausalCone, CriticalPath, MinCutGated
- Advanced: HierarchicalLorentz, ParallelBranch, TemporalBTSP
- UCB bandit selector for automatic mechanism selection
- LRU attention cache with 10k entry default

SONA (Self-Optimizing Neural Architecture):
- MicroLoRA adaptation (<100μs, rank-2)
- TrajectoryBuffer with lock-free ArrayQueue (10k capacity)
- ReasoningBank with K-means++ clustering
- EWC++ for catastrophic forgetting prevention (λ=5000)

MinCut Optimization:
- O(n^0.12) subpolynomial amortized updates
- Local k-cut approximation for sublinear bottleneck detection
- Criticality-based flow computation
- Redundancy analysis and repair suggestions

Self-Healing System:
- Z-score anomaly detection with adaptive thresholds
- Index health monitoring (HNSW/IVFFlat metrics)
- Learning drift detection with ADWIN algorithm
- Repair strategies: reindex, parameter tuning, learning reset

QuDAG Integration:
- ML-KEM-768 quantum-resistant encryption
- ML-DSA-65 quantum-resistant signatures
- Differential privacy (Laplace/Gaussian mechanisms)
- rUv token staking, rewards (5% APY), governance (67% threshold)

PostgreSQL Extension:
- GUC variables for configuration
- Planner/executor hooks for query interception
- Background worker for continuous learning
- 50+ SQL functions for all features

Testing:
- 46+ integration tests across all modules
- 11 benchmark groups for performance validation
- Test fixtures and data generators
- Mock QuDAG client for isolated testing

Documentation:
- Comprehensive README with architecture overview
- 5 example programs demonstrating all features
- Implementation notes for attention mechanisms

Total: ~12,000+ lines of new Rust code
2025-12-29 22:58:43 +00:00

77 lines
2.3 KiB
Rust

//! Basic usage example for Neural DAG Learning
use ruvector_dag::dag::{QueryDag, OperatorNode, OperatorType};
fn main() {
println!("=== Neural DAG Learning - Basic Usage ===\n");
// Create a new DAG
let mut dag = QueryDag::new();
// Add nodes representing query operators
println!("Building query DAG...");
let scan = dag.add_node(OperatorNode::seq_scan(0, "users"));
println!(" Added SeqScan on 'users' (id: {})", scan);
let filter = dag.add_node(OperatorNode::filter(1, "age > 18"));
println!(" Added Filter 'age > 18' (id: {})", filter);
let sort = dag.add_node(OperatorNode::sort(2, vec!["name".to_string()]));
println!(" Added Sort by 'name' (id: {})", sort);
let limit = dag.add_node(OperatorNode::limit(3, 10));
println!(" Added Limit 10 (id: {})", limit);
let result = dag.add_node(OperatorNode::new(4, OperatorType::Result));
println!(" Added Result (id: {})", result);
// Connect nodes
dag.add_edge(scan, filter).unwrap();
dag.add_edge(filter, sort).unwrap();
dag.add_edge(sort, limit).unwrap();
dag.add_edge(limit, result).unwrap();
println!("\nDAG Statistics:");
println!(" Nodes: {}", dag.node_count());
println!(" Edges: {}", dag.edge_count());
// Compute topological order
let order = dag.topological_sort().unwrap();
println!("\nTopological Order: {:?}", order);
// Compute depths
let depths = dag.compute_depths();
println!("\nNode Depths:");
for (id, depth) in &depths {
println!(" Node {}: depth {}", id, depth);
}
// Get children
println!("\nNode Children:");
for node_id in 0..5 {
let children = dag.get_children(node_id);
println!(" Node {}: {:?}", node_id, children);
}
// Serialize to JSON
let json = dag.to_json().unwrap();
println!("\nJSON (first 200 chars):\n{}", &json[..json.len().min(200)]);
// Demonstrate iterators
println!("\nDFS Traversal:");
for (i, node_id) in dag.dfs_iter(scan).enumerate() {
if i < 10 {
println!(" Visit: {}", node_id);
}
}
println!("\nBFS Traversal:");
for (i, node_id) in dag.bfs_iter(scan).enumerate() {
if i < 10 {
println!(" Visit: {}", node_id);
}
}
println!("\n=== Example Complete ===");
}