Expand description
Expr — a closed, serializable expression vocabulary over session state.
Where Guard is the closed vocabulary for boolean
questions about state and marking, Expr is the closed vocabulary for
values computed from state: the language of computed (derived) state
variables authored as data. Every atom is a named, parameterized operation;
there are no closures, so an Expr round-trips through JSON, can be edited
in a UI, validated at load time (Expr::keys_read feeds the state-key
diff), and evaluated identically in the live runtime, the offline
simulator, and generated code.
use gemini_adk_rs::expr::Expr;
use gemini_adk_rs::state::State;
// risk = 0.6 * overdue_ratio + 0.4 * missed_payments / 10
let expr: Expr = serde_json::from_value(serde_json::json!({
"add": [
{"mul": [{"const": 0.6}, {"key": "overdue_ratio"}]},
{"mul": [{"const": 0.04}, {"key": "missed_payments"}]}
]
})).unwrap();
let state = State::new();
state.set("overdue_ratio", 0.5).unwrap();
state.set("missed_payments", 3).unwrap();
assert_eq!(expr.eval(&state), Some(serde_json::json!(0.42)));§Evaluation semantics
- Reads go through
State::get, so thederived:fallback applies — one computed variable can read another by its bare key. - Arithmetic and comparison atoms are strict: a missing key or
non-numeric operand makes the whole expression
None(no write). - Logic atoms (
all/any/not) are total: a missing or non-boolean operand counts asfalse, mirroringGuard::is_trueon an unset key. coalescereturns the first operand that evaluates to a value;ifselects a branch on the truthiness of its condition.
Enums§
- Expr
- A serializable expression over session state. See the module docs.