use evorule_cli::executor::execute;
use evorule_cli::output::fact_to_human;
use evorule_reactor::Fact;
use evorule_tcb::JsonValue;
use std::collections::BTreeMap;
fn main() {
println!("🚀 evorule-cli 程序化调用示例\n");
let mut set_params = BTreeMap::new();
set_params.insert("attr".to_string(), JsonValue::string("x"));
set_params.insert("operation".to_string(), JsonValue::string("set"));
set_params.insert("value".to_string(), JsonValue::Integer(42));
let mut set_rule = BTreeMap::new();
set_rule.insert("type".to_string(), JsonValue::string("set"));
set_rule.insert("params".to_string(), JsonValue::object(set_params));
let core_eval = vec![JsonValue::object(set_rule)];
let mut p = BTreeMap::new();
p.insert("x".to_string(), JsonValue::Integer(0));
let payload = JsonValue::object(p);
println!("📦 初始 payload: {payload}");
let mut instr = BTreeMap::new();
instr.insert("type".to_string(), JsonValue::string("noop"));
let instruction = JsonValue::object(instr);
let facts = match execute(&core_eval, payload, instruction, 100) {
Ok(facts) => facts,
Err(e) => {
eprintln!("❌ 执行失败: {e:?}");
std::process::exit(1);
}
};
println!("\n📜 生成的 fact log ({} 条):", facts.len());
for fact in &facts {
println!(" {}", fact_to_human(fact));
}
if let Some(Fact::Stable { final_snapshot, .. }) = facts.last() {
let x = final_snapshot.get("x").and_then(|v| v.as_i64());
if x == Some(42) {
println!("\n✅ 最终 x = 42,符合预期");
} else {
eprintln!("\n❌ 最终 x = {x:?},期望 42");
std::process::exit(1);
}
} else {
eprintln!("\n❌ 没有 Stable 事实,执行异常");
std::process::exit(1);
}
}