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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! A bin that propagates the input value to a selected output.
//!
//! The bin receives a configuration conntaining a mapping of
//! unsigned integer values to outputs. If the `select` value converted
//! to an unsigned integer (as in casted in terms of computer programming
//! languages, meaning it gets cut off, not rounded) matches the index
//! of an output, the `input` value gets propagated to this output.
//! All other outputs produce a `Nothing` value.
//!
//! ```text
//!   ┌──────[multiplex]──────┐
//!  ⇒│input        <output 0>│⇒
//!  ⇒│select       <output 1>│⇒
//!   │             <output 2>│⇒
//!   │             …         ┊⇒
//!   │             <output n>│⇒
//!   └───────────────────────┘
//! ```          

use super::{
    sink_names_input_select, BinBuildEnvironment, BinDescription,
    Calculator, FetchItem, GetCalibration, Item, Iteration, Result, Scope,
    SinkBin, SinkNames, SourceBin, SourceId, SourceNames,
    SourceSinkBinDescription, WriteDotSimple, SINK_INPUT, SINK_SELECT,
};
use crate::error;
use indexmap::{IndexMap, IndexSet};
use snafu::OptionExt;

static BIN_TYPE: &str = "multiplex";

/// A bin that propagates the input value to a selected output.
#[derive(Debug)]
pub struct Bin {
    scope: Scope,

    source_input: Box<dyn FetchItem>,
    source_select: Box<dyn FetchItem>,

    outputs: IndexMap<String, (usize, Item)>,
}

impl SinkBin for Bin {}

impl SourceBin for Bin {
    fn get_source_data(&self, source: &SourceId) -> Result<Item> {
        let (_, item) = self.outputs.get(&source.id).context(
            error::MissingSourceName {
                scope: self.scope.clone(),
                name: source.id.to_string(),
                bin_type: BIN_TYPE.to_string(),
            },
        )?;
        Ok(item.clone())
    }
}

impl Calculator for Bin {
    fn calculate(&mut self, _iteration: &Iteration) -> Result<()> {
        let input = self.source_input.fetch_item(&self.scope)?;
        let select = self.source_select.fetch_item(&self.scope)?;

        let select = select.to_usize().ok();

        for (i, v) in self.outputs.values_mut() {
            *v = match select {
                Some(select_index) if *i == select_index => input.clone(),
                Some(_) | None => Item::Nothing,
            };
        }
        Ok(())
    }
}

/// Description for the multiplex bin.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Description {
    /// The outputs with their corresponding indices.
    pub outputs: IndexMap<String, usize>,
}

impl BinDescription for Description {
    type Bin = Bin;

    fn check_validity(
        &self,
        _scope: &Scope,
        _get_calibration: &mut dyn GetCalibration,
    ) -> Result<()> {
        Ok(())
    }

    fn bin_type(&self) -> &'static str {
        BIN_TYPE
    }
}

impl SinkNames for Description {
    fn sink_names(&self) -> IndexSet<String> {
        sink_names_input_select()
    }
}

impl SourceNames for Description {
    fn source_names(&self) -> Result<IndexSet<String>> {
        Ok(self.outputs.keys().cloned().collect())
    }
}

impl SourceSinkBinDescription for Description {
    fn build_bin(
        &self,
        scope: &Scope,
        env: &mut dyn BinBuildEnvironment,
    ) -> Result<Self::Bin> {
        Ok(Bin {
            scope: scope.clone(),
            source_input: env.resolve(SINK_INPUT)?,
            source_select: env.resolve(SINK_SELECT)?,

            outputs: self
                .outputs
                .iter()
                .map(|(k, v)| (k.to_string(), (*v, Item::Nothing)))
                .collect(),
        })
    }
}

impl WriteDotSimple for Description {}

#[cfg(test)]
mod tests {
    use super::Description;
    use crate::bins::{directsource, verificationsink};
    use crate::{run_bin, Result};
    use indexmap::indexset;

    #[test]
    fn simulate() -> Result<()> {
        use crate::Item::*;

        let input = directsource::Description {
            columns: indexset!["input".to_string(), "select".to_string()],
            rows: vec![
                vec![Nothing, Nothing],
                vec![U8(49), Nothing],
                vec![Nothing, U8(1)],
                vec![U8(50), U8(0)],
                vec![U8(51), U8(1)],
                vec![U8(52), U8(2)],
                vec![U8(53), U8(3)],
                vec![U8(54), U8(4)],
            ]
            .into(),
        };
        let verification = verificationsink::Description {
            columns: indexset!["a".to_string(), "b".to_string()],
            expected: vec![
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![U8(51), Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, U8(54)],
            ]
            .into(),
        };
        let description = Description {
            outputs: vec![
                ("a".to_string(), 1usize),
                ("b".to_string(), 4usize),
            ]
            .into_iter()
            .collect(),
        };

        run_bin(&input, &description, &verification)
    }
}