pub fn fact_to_human(fact: &Fact) -> StringExpand description
单个 Fact 的单行摘要
格式:[F{id}] {type} {detail}
- Command:
[F1] Command type=noop - StateTransition:
[F2] StateTransition cause=F1 - IoRequest:
[F3] IoRequest io_type=call_external - Stable:
[F4] Stable - Error:
[F5] Error: max_steps exceeded
§示例
use evorule_cli::output::fact_to_human;
use evorule_reactor::{Fact, FactId};
use evorule_tcb::JsonValue;
let command = Fact::Command {
id: FactId(1),
instruction: JsonValue::object_from_pairs(&[
("type", JsonValue::string("noop")),
]),
};
assert_eq!(fact_to_human(&command), "[F1] Command type=noop");
let stable = Fact::Stable {
id: FactId(4),
version: 1,
};
assert_eq!(fact_to_human(&stable), "[F4] Stable version=1");Examples found in repository?
examples/programmatic_run.rs (line 59)
20fn main() {
21 println!("🚀 evorule-cli 程序化调用示例\n");
22
23 // 1. 构造一个 set 规则:把 x 设为 42
24 // (set 是 primitive 业务规则,匹配任何指令)
25 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 // 2. 初始 payload: { x: 0 }
35 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 // 3. 触发指令:noop(不消耗规则,纯函数测试场景)
41 let mut instr = BTreeMap::new();
42 instr.insert("type".to_string(), JsonValue::string("noop"));
43 let instruction = JsonValue::object(instr);
44
45 // 4. 执行(最多 100 步)
46 // 返回 (fact 序列, 最终 payload)——最终 payload 经返回值直接交付
47 // (CR-20260901-001:Stable 不再内嵌全量快照)
48 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 // 5. 打印 fact log
57 println!("\n📜 生成的 fact log ({} 条):", facts.len());
58 for fact in &facts {
59 println!(" {}", fact_to_human(fact));
60 }
61
62 // 6. 验证最终 payload
63 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}