pub trait Policy {
type Reading;
type Command;
// Required method
fn evaluate(&mut self, reading: &Self::Reading) -> Reaction<Self::Command>;
}Expand description
The decision half of a node: what one reading calls for.
A Controller is the policy behind every built-in kind, deciding an f32
reading into an on/off command. A policy of your own decides whatever a driver
reads, a whole measurement struct included, into whatever an actuator takes, and
Node::with_policy runs it in the same read, decide,
act, publish loop. Evaluation is synchronous and hardware-free, so a policy is
unit-testable with no devices and no network.
§Examples
use pamoja_profile::{Alert, Policy, Reaction};
// Frost settles when the air is cold and damp, so this reads both at once and
// switches a heater, raising its own condition when the risk is on.
struct FrostGuard {
warn_below: f32,
}
impl Policy for FrostGuard {
type Reading = (f32, f32);
type Command = bool;
fn evaluate(&mut self, &(temperature, humidity): &(f32, f32)) -> Reaction {
let risk = temperature < self.warn_below && humidity > 80.0;
Reaction {
actuator: Some(risk),
alert: risk.then_some(Alert::Custom { code: "FrostRisk", value: temperature }),
}
}
}
let mut guard = FrostGuard { warn_below: 2.0 };
let cold = guard.evaluate(&(1.0, 92.0));
assert_eq!(cold.actuator, Some(true));
assert_eq!(cold.alert.map(Alert::kind), Some("FrostRisk"));
assert_eq!(guard.evaluate(&(1.0, 40.0)).alert, None);Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".