programmatic_run/
programmatic_run.rs1use evorule_cli::executor::execute;
16use evorule_cli::output::fact_to_human;
17use evorule_reactor::Fact;
18use evorule_tcb::JsonValue;
19use std::collections::BTreeMap;
20
21fn main() {
22 println!("🚀 evorule-cli 程序化调用示例\n");
23
24 let mut set_params = BTreeMap::new();
27 set_params.insert("attr".to_string(), JsonValue::string("x"));
28 set_params.insert("operation".to_string(), JsonValue::string("set"));
29 set_params.insert("value".to_string(), JsonValue::Integer(42));
30 let mut set_rule = BTreeMap::new();
31 set_rule.insert("type".to_string(), JsonValue::string("set"));
32 set_rule.insert("params".to_string(), JsonValue::object(set_params));
33 let core_eval = vec![JsonValue::object(set_rule)];
34
35 let mut p = BTreeMap::new();
37 p.insert("x".to_string(), JsonValue::Integer(0));
38 let payload = JsonValue::object(p);
39 println!("📦 初始 payload: {payload}");
40
41 let mut instr = BTreeMap::new();
43 instr.insert("type".to_string(), JsonValue::string("noop"));
44 let instruction = JsonValue::object(instr);
45
46 let facts = match execute(&core_eval, payload, instruction, 100) {
48 Ok(facts) => facts,
49 Err(e) => {
50 eprintln!("❌ 执行失败: {e:?}");
51 std::process::exit(1);
52 }
53 };
54
55 println!("\n📜 生成的 fact log ({} 条):", facts.len());
57 for fact in &facts {
58 println!(" {}", fact_to_human(fact));
59 }
60
61 if let Some(Fact::Stable { final_snapshot, .. }) = facts.last() {
63 let x = final_snapshot.get("x").and_then(|v| v.as_i64());
64 if x == Some(42) {
65 println!("\n✅ 最终 x = 42,符合预期");
66 } else {
67 eprintln!("\n❌ 最终 x = {x:?},期望 42");
68 std::process::exit(1);
69 }
70 } else {
71 eprintln!("\n❌ 没有 Stable 事实,执行异常");
72 std::process::exit(1);
73 }
74}