Skip to main content

Policy

Trait Policy 

Source
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§

Source

type Reading

What the policy decides on: a number, or a driver’s whole measurement.

Source

type Command

What the policy issues: an on/off setting, a duty, a setpoint.

Required Methods§

Source

fn evaluate(&mut self, reading: &Self::Reading) -> Reaction<Self::Command>

Decides what one reading calls for.

§Arguments
  • reading - the latest reading, borrowed so the node can still publish it.
§Returns

The command to apply, if any, and any alert the reading raised.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<P: Policy + ?Sized> Policy for Box<P>

Source§

type Reading = <P as Policy>::Reading

Source§

type Command = <P as Policy>::Command

Source§

fn evaluate(&mut self, reading: &Self::Reading) -> Reaction<Self::Command>

Implementors§