1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::{cell::RefCell, cmp::Ordering, rc::Rc};

use super::{Duration, Index, Sched, Trigger};

use derive_more::Display;

#[derive(Debug, Default)]
pub struct Port<T>(T, bool);

impl<T> Port<T> {
    pub fn new(t: T) -> Self {
        Port(t, false)
    }
    pub fn get<'a>(&'a self) -> &'a T {
        &self.0
    }
    pub fn set(&mut self, t: T) {
        self.0 = t;
        self.1 = true;
    }
}

pub trait IsPresent {
    fn is_present(&self) -> bool;
    fn reset(&mut self);
}

impl<T> IsPresent for Port<T> {
    fn is_present(&self) -> bool {
        self.1
    }
    fn reset(&mut self) {
        self.1 = false;
    }
}

type OutputTriggers<S> = (Rc<RefCell<dyn IsPresent>>, Vec<Rc<Trigger<S>>>);

/// Reaction activation record to push onto the reaction queue.
#[derive(Display)]
#[display(fmt = "{:p} {} {}", "reactor", "index", "chain_id")]
pub struct Reaction<S>
where
    S: Sched,
{
    pub name: &'static str,
    /// Reaction closure
    pub reactor: Box<RefCell<dyn FnMut(&mut S) -> ()>>,
    /// Inverse priority determined by dependency analysis.
    pub index: Index,
    /// Binary encoding of the branches that this reaction has upstream in the dependency graph.
    pub chain_id: u64,
    /// Vector of tuples per Output that are sensitive to this Reaction.
    /// Each output has a list of associated triggers
    pub triggers: Vec<OutputTriggers<S>>,
    /// Indicator that this reaction has already started executing.
    pub running: bool,
    /// Local deadline relative to the time stamp for invocation of the reaction.
    /// Local deadline violation handler.
    pub local_deadline: Option<(Duration, Box<RefCell<dyn FnMut(&mut S) -> bool>>)>,
}

impl<S> Reaction<S>
where
    S: Sched,
{
    pub fn new(
        name: &'static str,
        reactor: Box<RefCell<dyn FnMut(&mut S) -> ()>>,
        index: Index,
        chain_id: u64,
        triggers: Vec<OutputTriggers<S>>,
    ) -> Self {
        Self {
            name,
            reactor,
            index,
            chain_id,
            triggers,
            running: false,
            local_deadline: None,
        }
    }
}

impl<S> core::fmt::Debug for Reaction<S>
where
    S: Sched,
{
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match *self {
            Reaction {
                name: ref __name,
                reactor: ref __reactor,
                index: ref __index,
                chain_id: ref __chain_id,
                triggers: ref __triggers,
                running: ref __running,
                local_deadline: ref __local_deadline,
                // phantom: ref __phantom,
            } => {
                let mut debug_trait_builder = f.debug_struct("Reaction");
                debug_trait_builder.field("name", &&(*__name));
                // debug_trait_builder.field("reactor", format!("{:p}", &&*__reactor));
                debug_trait_builder.field("index", &&(*__index));
                debug_trait_builder.field("chain_id", &&(*__chain_id));
                // debug_trait_builder.field("num_outputs", &&(*__num_outputs));
                // debug_trait_builder.field("triggers", &&(*__triggers));
                debug_trait_builder.field("running", &&(*__running));
                // debug_trait_builder.field("local_deadline", &&(__local_deadline.map(|deadline|
                // deadline.0)));
                debug_trait_builder.finish()
            }
        }
    }
}

impl<S> PartialEq for Reaction<S>
where
    S: Sched,
{
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index
    }
}

impl<S> Eq for Reaction<S> where S: Sched {}

impl<S> PartialOrd for Reaction<S>
where
    S: Sched,
{
    fn partial_cmp(&self, other: &Reaction<S>) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<S> Ord for Reaction<S>
where
    S: Sched,
{
    fn cmp(&self, other: &Reaction<S>) -> Ordering {
        other.index.cmp(&self.index)
    }
}