programmatic_run/
programmatic_run.rs1use evorule_cli::executor::execute;
16use evorule_cli::output::fact_to_human;
17use evorule_tcb::JsonValue;
18use std::collections::BTreeMap;
19
20fn main() {
21 println!("🚀 evorule-cli 程序化调用示例\n");
22
23 let mut set_params = BTreeMap::new();
26 set_params.insert("attr".to_string(), JsonValue::string("x"));
27 set_params.insert("operation".to_string(), JsonValue::string("set"));
28 set_params.insert("value".to_string(), JsonValue::Integer(42));
29 let mut set_rule = BTreeMap::new();
30 set_rule.insert("type".to_string(), JsonValue::string("set"));
31 set_rule.insert("params".to_string(), JsonValue::object(set_params));
32 let core_eval = vec![JsonValue::object(set_rule)];
33
34 let mut p = BTreeMap::new();
36 p.insert("x".to_string(), JsonValue::Integer(0));
37 let payload = JsonValue::object(p);
38 println!("📦 初始 payload: {payload}");
39
40 let mut instr = BTreeMap::new();
42 instr.insert("type".to_string(), JsonValue::string("noop"));
43 let instruction = JsonValue::object(instr);
44
45 let (facts, final_payload) = match execute(&core_eval, payload, instruction, 100) {
49 Ok(result) => result,
50 Err(e) => {
51 eprintln!("❌ 执行失败: {e:?}");
52 std::process::exit(1);
53 }
54 };
55
56 println!("\n📜 生成的 fact log ({} 条):", facts.len());
58 for fact in &facts {
59 println!(" {}", fact_to_human(fact));
60 }
61
62 let x = final_payload.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}