pub fn execute(
core_eval: &[JsonValue],
initial_payload: JsonValue,
initial_instruction: JsonValue,
max_steps: usize,
) -> Result<Vec<Fact>, CliError>Expand description
执行规则,产生 Fact 序列
§参数
core_eval:transform 规则列表(由io_util::load_rules加载)initial_payload:初始 payloadinitial_instruction:初始指令(通常是{"type":"noop"}触发 transform 链)max_steps:最大执行步数上界(先检后 pop)
§返回
Vec<Fact>:包含 Command、若干 StateTransition、可选 Error、结尾 Stable
§不变量
- FIFO 队列:
VecDeque::pop_front,不能用Vec::pop - max_steps 先检后 pop:超限发 Error + break
- I/O 两阶段:IoRequest 时缓存 orig 指令到 pending_io,0.2.0 无 handler 发 Error
Examples found in repository?
examples/programmatic_run.rs (line 47)
21fn main() {
22 println!("🚀 evorule-cli 程序化调用示例\n");
23
24 // 1. 构造一个 set 规则:把 x 设为 42
25 // (set 是 primitive 业务规则,匹配任何指令)
26 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 // 2. 初始 payload: { x: 0 }
36 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 // 3. 触发指令:noop(不消耗规则,纯函数测试场景)
42 let mut instr = BTreeMap::new();
43 instr.insert("type".to_string(), JsonValue::string("noop"));
44 let instruction = JsonValue::object(instr);
45
46 // 4. 执行(最多 100 步)
47 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 // 5. 打印 fact log
56 println!("\n📜 生成的 fact log ({} 条):", facts.len());
57 for fact in &facts {
58 println!(" {}", fact_to_human(fact));
59 }
60
61 // 6. 验证最终 Stable
62 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}