Skip to main content

RuleEvaluator

Struct RuleEvaluator 

Source
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

Source

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.

Source

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.

Source

pub fn watches(&self, topic: &str) -> bool

Whether any rule watches a topic, so a program knows whether to decode a message before handing it over.

§Arguments
  • topic - the topic a message arrived on.
§Returns

true when some rule watches it.

Source

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.

Source

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.

Source

pub fn rules(&self) -> impl Iterator<Item = &Rule>

Walks the rules in the order they are checked.

§Returns

Each rule.

Source

pub fn is_set(&self, rule: &str) -> Option<bool>

Whether a rule’s condition currently holds.

§Arguments
  • rule - the rule’s name.
§Returns

The condition’s state, or None for a name no rule has.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.