pub struct RuleEvaluator { /* private fields */ }Expand description
The decisions a set of rules makes, reading by reading, with no link and no outputs of its own.
A RuleEngine receives each message, decodes it, and carries out what a rule calls
for. This is its deciding half on its own, for a program that moves its own messages
and drives its own outputs: hand it a topic and a reading, and it says which rules set
or cleared and what each calls for. The engine runs on one, so the two decide alike.
§Examples
use pamoja_kit::Edge;
use pamoja_profile::{Action, Condition, Rule, RuleEvaluator, Rules};
// Open a roof vent when a greenhouse passes 30 C, and close it once it is back
// under 27 C.
let rules = Rules::new().with(
Rule::new("vent-when-hot", Condition::above("greenhouse/air", 30.0).with_hysteresis(3.0))
.then(Action::Drive { actuator: "roof-vent".into(), on: true })
.otherwise(Action::Drive { actuator: "roof-vent".into(), on: false }),
);
let mut evaluator = RuleEvaluator::new(rules)?;
assert!(evaluator.evaluate("greenhouse/air", 29.0)?.is_empty());
let fired = evaluator.evaluate("greenhouse/air", 31.5)?;
assert_eq!(fired[0].edge, Edge::Set);
assert_eq!(fired[0].actions, [Action::Drive { actuator: "roof-vent".into(), on: true }]);Implementations§
Source§impl RuleEvaluator
impl RuleEvaluator
Sourcepub fn new(rules: Rules) -> Result<Self>
pub fn new(rules: Rules) -> Result<Self>
Arms a set of rules, every condition starting cleared.
§Arguments
rules- the rules to judge readings by.
§Returns
The evaluator.
§Errors
Returns [Error::Codec] naming the first problem Rules::check finds.
Sourcepub fn evaluate(&mut self, topic: &str, reading: f32) -> Result<Vec<Fired>>
pub fn evaluate(&mut self, topic: &str, reading: f32) -> Result<Vec<Fired>>
Judges one reading from one topic against every rule that watches it.
§Arguments
topic- the topic the reading arrived on.reading- the reading, already decoded from the message.
§Returns
What fired, in the order the rules are listed: each rule whose condition set or cleared, with the actions it calls for. It is empty when no rule watches the topic or the reading changed nothing.
§Errors
Returns [Error::Codec] when a rule watches the topic and the reading is not a
finite number, such as the NaN a failed probe reports, which would otherwise
leave every condition as it was with nothing to say why.
Sourcepub fn topics(&self) -> Vec<&str>
pub fn topics(&self) -> Vec<&str>
Lists the topics the rules watch, which are the topics to subscribe to.
§Returns
Each topic once, in name order.
Sourcepub fn actuators(&self) -> Vec<&str>
pub fn actuators(&self) -> Vec<&str>
Lists the actuators the rules drive, which are the outputs a program has to have.
§Returns
Each actuator once, in name order.