Skip to main content

network_kb/
network_kb.rs

1/// Example: Network Active Knowledge Base
2/// This example shows how to use the NetworkActiveKnowledgeBase to communicate
3/// with a remote target system via network sockets.
4use rust_lstar::knowledge_base::{KnowledgeBaseTrait, NetworkActiveKnowledgeBase};
5use rust_lstar::*;
6use std::sync::{Arc, Mutex};
7use std::time::Duration;
8
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    println!("=== Network Active Knowledge Base Example ===\n");
11
12    // This example expects an independent coffee server running
13    // (see examples/coffee_server.rs).
14    let kb = NetworkActiveKnowledgeBase::new("127.0.0.1".to_string(), 3000, Duration::from_secs(5));
15
16    // Define input vocabulary
17    let vocabulary = vec![
18        "REFILL_WATER".to_string(),
19        "REFILL_COFFEE".to_string(),
20        "PRESS_BUTTON_A".to_string(),
21        "PRESS_BUTTON_B".to_string(),
22        "PRESS_BUTTON_C".to_string(),
23    ];
24
25    println!("Target Host: {}", kb.target_host());
26    println!("Target Port: {}\n", kb.target_port());
27
28    let kb: Arc<Mutex<dyn KnowledgeBaseTrait>> = Arc::new(Mutex::new(kb));
29
30    // Create learner. The network KB connects per submitted word.
31    let mut learner = LSTAR::new(vocabulary, kb, 4, None, None);
32
33    match learner.learn() {
34        Ok(automata) => {
35            println!("\n=== Learned Automaton from Network Target ===\n");
36            println!("{}", automata.build_dot_code());
37            println!(
38                "\nStates: {}\nTransitions: {}",
39                automata.get_states().len(),
40                automata.transitions.len()
41            );
42        }
43        Err(e) => {
44            eprintln!("Learning error: {}", e);
45            println!("\nTo run this example with a real connection:");
46            println!("  1. Start a coffee server: cargo run --example coffee_server");
47            println!("  2. Run this example again");
48        }
49    }
50
51    Ok(())
52}